From a265fcaf73543d028b44a10fbbca2b18979f3845 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Nov 2010 01:33:42 +0100 Subject: Check for valid action. --- server/game.cpp | 10 +++++----- server/player.cpp | 12 +++++++----- server/player.h | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'server') diff --git a/server/game.cpp b/server/game.cpp index d13235b..4d102a7 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -67,26 +67,26 @@ void Game::round_update() { // Only implemented discard so far, so only current player that needs to be able to do a action. int smart = 0; for(Actions::iterator it = gamestate.possible_actions.begin(); it != gamestate.possible_actions.end(); ++it) { - smart = smart | (1 << it->player); + //smart = smart | (1 << it->player); } int num_player_actions = 0; if(smart & 1) { num_player_actions++; - players[0]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); + players[0]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 2) { num_player_actions++; - players[1]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); + players[1]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 4) { num_player_actions++; - players[2]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); + players[2]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(smart & 8) { num_player_actions++; - players[3]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1)); + players[3]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1), gamestate.possible_actions); } if(num_player_actions == 0) { diff --git a/server/player.cpp b/server/player.cpp index b1d8467..d8baaf8 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -49,16 +49,18 @@ void Player::handle_ready(Message::p msg, boost::function ready_callbac ready_callback(); } -void Player::handle_action(Message::p msg, boost::function action_callback) { +void Player::handle_action(Message::p msg, boost::function action_callback, Actions possible_actions) { 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) { + // Check if the action is valid. + if(possible_actions.contains(action_msg->action)) { action_callback(action_msg->action); + } else { + action_callback(possible_actions[0]); } } @@ -91,8 +93,8 @@ 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::get_action(boost::function callback, Actions expected_actions) { + connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback, expected_actions)); } void Player::kill_action() { diff --git a/server/player.h b/server/player.h index d0dc73b..25eddd3 100644 --- a/server/player.h +++ b/server/player.h @@ -34,7 +34,7 @@ class Player : public boost::enable_shared_from_this { void handle_ready(Message::p msg, boost::function ready_callback); //! Handle Action-message. - void handle_action(Message::p msg, boost::function action_callback); + void handle_action(Message::p msg, boost::function action_callback, Actions expected_actions); public: //! The ID of the player @@ -56,7 +56,7 @@ class Player : public boost::enable_shared_from_this { void round_end(); //! Get action. - void get_action(boost::function callback); + void get_action(boost::function callback, Actions expected_actions); void kill_action(); }; -- cgit v1.2.3