From cad0ae2d88e74cc48bf62f872128d737013bbac2 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Fri, 19 Nov 2010 17:52:44 +0100 Subject: Add game state and action functions to Player class. --- server/player.cpp | 28 ++++++++++++++++++++++++++++ server/player.h | 14 +++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/server/player.cpp b/server/player.cpp index 1901da7..cb84c9a 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -49,11 +49,23 @@ void Player::handle_ready(Message::p msg) { ready_callback(); } +void Player::handle_action(Message::p msg) { + if(msg->type != Message::Types::RoundAction) { + return; + } + + Message::RoundAction::p action_msg = dynamic_pointer_cast(msg); + + action_callback(action_msg->action); +} + std::string Player::nick() { return 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++) { @@ -61,4 +73,20 @@ void Player::game_start(boost::function callback, std::vectorsend(msg); + + connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1)); +} + +void Player::round_start() { + connection->send(make_shared()); +} + +void Player::round_state(State::p state) { + connection->send(make_shared(state)); +} + +void Player::get_action(boost::function callback) { + action_callback = callback; + + connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1)); } diff --git a/server/player.h b/server/player.h index 667d6ea..ce37d0a 100644 --- a/server/player.h +++ b/server/player.h @@ -22,7 +22,7 @@ class Player : public boost::enable_shared_from_this { boost::function lobby_callback; boost::function ready_callback; - + boost::function action_callback; std::string nick_; @@ -37,12 +37,24 @@ class Player : public boost::enable_shared_from_this { //! Handle Ready-message. void handle_ready(Message::p msg); + //! Handle Action-message. + void handle_action(Message::p msg); + public: //! Return player's nick. std::string nick(); //! Notify client of a game start. void game_start(boost::function callback, std::vector players); + + //! Notify client of a round start. + void round_start(); + + //! Send round state. + void round_state(State::p state); + + //! Get action. + void get_action(boost::function callback); }; #endif -- cgit v1.2.3