// Copyright © 2001-2004 Rob Kaper , // 2001 Erik Bourget // 2010-2015 Sylvain Rochet // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // version 2 as published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; see the file COPYING. If not, see // . #include "config.h" #include #include #include #include #include #include "socket.h" #include "card.h" #include "display.h" #include "estate.h" #include "io.h" #include "game.h" #include "player.h" #include "trade.h" // move trade commands to game/engine Player::Player(Socket *socket) : GameObject(-1, GameObject::GPlayer) { m_socket = socket; m_game = 0; m_estate = 0; m_destination = 0; m_tokenLocation = 0; m_identified = false; } Player::~Player() { while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); } while (!m_display.empty()) { delete *m_display.begin(); m_display.erase(m_display.begin()); } } void Player::reset(bool removeProperties) { while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); } while (!m_display.empty()) { delete *m_display.begin(); m_display.erase(m_display.begin()); } m_estate = m_destination = m_tokenLocation = 0; if (removeProperties) { removeProperty("money"); removeProperty("doublecount"); removeProperty("jailcount"); removeProperty("jailed"); removeProperty("bankrupt"); removeProperty("hasturn"); removeProperty("spectator"); removeProperty("can_roll"); removeProperty("canrollagain"); removeProperty("can_buyestate"); removeProperty("canauction"); removeProperty("location"); removeProperty("directmove"); removeProperty("turnorder"); } } void Player::identify(int id) { m_id = id; setProperty("cookie", std::string(itoa(m_id)) + "/" + itoa(rand()), this); m_identified = true; } void Player::ioWrite(const char *fmt, ...) { int n, size = 256; char *buf = new char[size]; static std::string ioStr; va_list arg; buf[0] = 0; while (1) { va_start(arg, fmt); n = vsnprintf(buf, size, fmt, arg); va_end(arg); if (n > -1 && n < size) { ioStr = buf; delete[] buf; ioWrite(ioStr); return; } if (n > -1) size = n+1; else size *= 2; delete[] buf; buf = new char[size]; } } void Player::ioWrite(std::string data) { if (m_socket) m_socket->ioWrite(data); } void Player::ioInfo(const char *data, ...) { va_list arg; char buf[2048]; va_start(arg, data); vsnprintf(buf, sizeof(buf)-1, data, arg); va_end(arg); buf[sizeof(buf)-1] = 0; ioWrite("\n", buf); } void Player::ioInfo(const std::string data) { ioWrite("\n"); } void Player::ioError(const char *data, ...) { va_list arg; char buf[2048]; va_start(arg, data); vsnprintf(buf, sizeof(buf)-1, data, arg); va_end(arg); buf[sizeof(buf)-1] = 0; ioWrite("\n", buf); } void Player::ioError(const std::string data) { ioWrite("\n"); } void Player::ioNoSuchCmd(const std::string data) { if (data.size()) ioError("Command is unavailable at current game status: " + data); else ioError("No such command."); } void Player::sendDisplay(Display *display) { std::string text, estate; if (display->text().size()) { text = "text=\"" + display->text() + "\" "; } if (display->clearEstate()) { estate = "estateid=\"-1\" "; } else if (display->estate()) { estate = "estateid=\"" + itoa(display->estate()->id()) + "\" "; } ioWrite("clearText(), display->clearButtons()); std::vector buttons = display->buttons(); if (buttons.size() > 0) { ioWrite(">"); DisplayButton *button; for (std::vector::iterator it = buttons.begin() ; it != buttons.end() && (button = *it) ; ++it) ioWrite("