#include "player.h" #include Player::p Player::create(Connection::p c, boost::function f) { Player::p p(new Player(c)); p->start(f); return p; } Player::Player(Connection::p c) : connection(c), timer(c->socket.get_io_service()) { } void Player::start(boost::function f) { // Send Hello. connection->send(make_shared("aotenjoud git")); // Wait for Login. connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, f)); } void Player::handle_login(Message::p msg, boost::function lobby_callback) { if(msg->type != Message::Types::Login) { return; } Message::Login::p login_msg = dynamic_pointer_cast(msg); // Check if nick is invalid. if(login_msg->nick.size() == 0) { connection->send(make_shared(false)); connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, lobby_callback)); return; } connection->send(make_shared(true)); nick_ = login_msg->nick; lobby_callback(shared_from_this()); } void Player::handle_ready(Message::p msg, boost::function ready_callback) { if(msg->type != Message::Types::Ready) { return; } ready_callback(); } void Player::handle_action(Message::p msg, boost::function action_callback) { if(msg->type != Message::Types::RoundAction) { return; } Message::RoundAction::p action_msg = dynamic_pointer_cast(msg); //action_msg->action.player = id; if(action_callback != 0) { action_callback(action_msg->action); } } std::string Player::nick() { return nick_; } void Player::game_start(boost::function callback, std::vector players) { Message::GameStart::p msg = make_shared(); for(std::vector::iterator i = players.begin(); i != players.end(); i++) { msg->players.push_back((*i)->nick()); } msg->player_id = id; connection->send(msg); connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1, callback)); } void Player::round_start() { connection->send(make_shared()); } void Player::round_state(State state) { connection->send(make_shared(state)); } void Player::round_end() { connection->send(make_shared()); } void Player::get_action(boost::function callback) { connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback)); } void Player::kill_action() { }