summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-11-19 17:52:44 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-11-19 17:52:44 +0100
commitcad0ae2d88e74cc48bf62f872128d737013bbac2 (patch)
treecafe222d173658235f3f56e4f43faaa65f886682
parenta5fbbe1c51968d330f1621efb5bf5d3b3c4ddf4d (diff)
Add game state and action functions to Player class.
-rw-r--r--server/player.cpp28
-rw-r--r--server/player.h14
2 files changed, 41 insertions, 1 deletions
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<Message::RoundAction>(msg);
+
+ action_callback(action_msg->action);
+}
+
std::string Player::nick() {
return nick_;
}
void Player::game_start(boost::function<void ()> callback, std::vector<Player::p> players) {
+ ready_callback = callback;
+
Message::GameStart::p msg = make_shared<Message::GameStart>();
for(std::vector<Player::p>::iterator i = players.begin(); i != players.end(); i++) {
@@ -61,4 +73,20 @@ void Player::game_start(boost::function<void ()> callback, std::vector<Player::p
}
connection->send(msg);
+
+ connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1));
+}
+
+void Player::round_start() {
+ connection->send(make_shared<Message::RoundStart>());
+}
+
+void Player::round_state(State::p state) {
+ connection->send(make_shared<Message::RoundState>(state));
+}
+
+void Player::get_action(boost::function<void (Action)> 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<Player> {
boost::function<void (Player::p)> lobby_callback;
boost::function<void ()> ready_callback;
-
+ boost::function<void (Action)> action_callback;
std::string nick_;
@@ -37,12 +37,24 @@ class Player : public boost::enable_shared_from_this<Player> {
//! 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<void ()> callback, std::vector<Player::p> 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<void (Action)> callback);
};
#endif