From 66940596378b502cf76ad96a5b5a1afb88378454 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Wed, 24 Nov 2010 23:22:10 +0100 Subject: Moved player-callbacks from class members to bound variables. --- server/player.cpp | 30 +++++++++++++----------------- server/player.h | 14 +++++--------- 2 files changed, 18 insertions(+), 26 deletions(-) (limited to 'server') diff --git a/server/player.cpp b/server/player.cpp index ce6a116..b1d8467 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -3,24 +3,24 @@ #include Player::p Player::create(Connection::p c, boost::function f) { - Player::p p(new Player(c, f)); - p->start(); + Player::p p(new Player(c)); + p->start(f); return p; } -Player::Player(Connection::p c, boost::function f) : connection(c), timer(c->socket.get_io_service()), lobby_callback(f) { +Player::Player(Connection::p c) : connection(c), timer(c->socket.get_io_service()) { } -void Player::start() { +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)); + connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, f)); } -void Player::handle_login(Message::p msg) { +void Player::handle_login(Message::p msg, boost::function lobby_callback) { if(msg->type != Message::Types::Login) { return; } @@ -30,7 +30,7 @@ void Player::handle_login(Message::p 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)); + connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, lobby_callback)); return; } @@ -41,7 +41,7 @@ void Player::handle_login(Message::p msg) { lobby_callback(shared_from_this()); } -void Player::handle_ready(Message::p msg) { +void Player::handle_ready(Message::p msg, boost::function ready_callback) { if(msg->type != Message::Types::Ready) { return; } @@ -49,14 +49,14 @@ void Player::handle_ready(Message::p msg) { ready_callback(); } -void Player::handle_action(Message::p msg) { +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; + //action_msg->action.player = id; if(action_callback != 0) { action_callback(action_msg->action); } @@ -67,8 +67,6 @@ std::string Player::nick() { } void Player::game_start(boost::function callback, std::vector players) { - ready_callback = callback; - Message::GameStart::p msg = make_shared(); for(std::vector::iterator i = players.begin(); i != players.end(); i++) { @@ -78,7 +76,7 @@ void Player::game_start(boost::function callback, std::vectorsend(msg); - connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1)); + connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1, callback)); } void Player::round_start() { @@ -94,11 +92,9 @@ void Player::round_end() { } void Player::get_action(boost::function callback) { - action_callback = callback; - - connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1)); + connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback)); } void Player::kill_action() { - action_callback = 0; + } diff --git a/server/player.h b/server/player.h index af9c59e..d0dc73b 100644 --- a/server/player.h +++ b/server/player.h @@ -20,25 +20,21 @@ class Player : public boost::enable_shared_from_this { boost::asio::deadline_timer timer; - boost::function lobby_callback; - boost::function ready_callback; - boost::function action_callback; - std::string nick_; - Player(Connection::p c, boost::function f); + Player(Connection::p c); //! Start communicating. - void start(); + void start(boost::function f); //! Handle Login-message. - void handle_login(Message::p msg); + void handle_login(Message::p msg, boost::function lobby_callback); //! Handle Ready-message. - void handle_ready(Message::p msg); + void handle_ready(Message::p msg, boost::function ready_callback); //! Handle Action-message. - void handle_action(Message::p msg); + void handle_action(Message::p msg, boost::function action_callback); public: //! The ID of the player -- cgit v1.2.3