diff options
-rw-r--r-- | common/action.cpp | 10 | ||||
-rw-r--r-- | common/action.h | 13 | ||||
-rw-r--r-- | server/game.cpp | 10 | ||||
-rw-r--r-- | server/player.cpp | 12 | ||||
-rw-r--r-- | server/player.h | 4 |
5 files changed, 36 insertions, 13 deletions
diff --git a/common/action.cpp b/common/action.cpp new file mode 100644 index 0000000..467df4e --- /dev/null +++ b/common/action.cpp @@ -0,0 +1,10 @@ +#include "action.h" +#include <algorithm> + +bool Action::operator==(Action other) { + return type == other.type && target == other.target; +} + +bool Actions::contains(Action action) { + return std::find(begin(), end(), action) != end(); +} diff --git a/common/action.h b/common/action.h index 5099e03..bae22cf 100644 --- a/common/action.h +++ b/common/action.h @@ -2,6 +2,7 @@ #define ACTION_H #include <vector> +#include <boost/serialization/base_object.hpp> class Action { public: @@ -35,6 +36,16 @@ class Action { } }; -typedef std::vector<Action> Actions; +//! List of actions. +class Actions : public std::vector<Action> { + public: + //! Check if specified action is present in list. + bool contains(Action action); + + template<class Archive> + void serialize(Archive & ar, const unsigned int version) { + ar & boost::serialization::base_object<std::vector<Action> >(*this); + } +}; #endif 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<void ()> ready_callbac ready_callback(); } -void Player::handle_action(Message::p msg, boost::function<void (Action)> action_callback) { +void Player::handle_action(Message::p msg, boost::function<void (Action)> action_callback, Actions possible_actions) { if(msg->type != Message::Types::RoundAction) { return; } Message::RoundAction::p action_msg = dynamic_pointer_cast<Message::RoundAction>(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<Message::RoundEnd>()); } -void Player::get_action(boost::function<void (Action)> callback) { - connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback)); +void Player::get_action(boost::function<void (Action)> 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<Player> { void handle_ready(Message::p msg, boost::function<void ()> ready_callback); //! Handle Action-message. - void handle_action(Message::p msg, boost::function<void (Action)> action_callback); + void handle_action(Message::p msg, boost::function<void (Action)> action_callback, Actions expected_actions); public: //! The ID of the player @@ -56,7 +56,7 @@ class Player : public boost::enable_shared_from_this<Player> { void round_end(); //! Get action. - void get_action(boost::function<void (Action)> callback); + void get_action(boost::function<void (Action)> callback, Actions expected_actions); void kill_action(); }; |