diff options
-rw-r--r-- | common/connectionbase.cpp | 5 | ||||
-rw-r--r-- | server/client.cpp | 14 | ||||
-rw-r--r-- | server/client.h | 6 | ||||
-rw-r--r-- | server/game.cpp | 44 | ||||
-rw-r--r-- | server/game.h | 3 |
5 files changed, 60 insertions, 12 deletions
diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index e2384e2..a0c5ab8 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -193,6 +193,11 @@ void ConnectionBase::send(const Message::p& msg) { oa & m; } break; + case Message::Types::GameEnd: { + Message::GameEnd::p m = dynamic_pointer_cast<Message::GameEnd>(msg); + oa & m; + } break; + default: throw std::runtime_error("Serialization attempted on unknown message type."); } diff --git a/server/client.cpp b/server/client.cpp index 345e3a3..3573128 100644 --- a/server/client.cpp +++ b/server/client.cpp @@ -152,6 +152,14 @@ void Client::get_action(boost::function<void (Action)> callback, Actions expecte connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); } +void Client::game_end(Message::GameEnd::p msg, boost::function<void ()> callback) { + connection->send(msg); + + if(callback) { + connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + } +} + unsigned int ClientDumb::id() { return 0; } @@ -173,9 +181,13 @@ void ClientDumb::round_state(const PlayerState& pl_d, const PlayerState& pl_r, c } void ClientDumb::round_end(Message::RoundEnd::p msg, boost::function<void ()> callback) { - callback(); + if(callback) callback(); } void ClientDumb::get_action(boost::function<void (Action)> callback, Actions expected_actions) { callback(expected_actions.back()); } + +void ClientDumb::game_end(Message::GameEnd::p msg, boost::function<void ()> callback) { + callback(); +} diff --git a/server/client.h b/server/client.h index 752a6a4..e6cfcc7 100644 --- a/server/client.h +++ b/server/client.h @@ -35,6 +35,8 @@ class ClientBase { //! Get action. Upon connection error, last element of expected_actions will be provided. virtual void get_action(boost::function<void (Action)> callback, Actions expected_actions) = 0; + virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback) = 0; + }; //! Class implementing ClientBase for real clients. @@ -102,6 +104,8 @@ class Client : public ClientBase, public boost::enable_shared_from_this<Client> //! Reconnect a player. virtual void reconnect(Connection::p c); + + virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback); }; typedef std::vector<Client> Clients; @@ -121,6 +125,8 @@ class ClientDumb : public ClientBase { virtual void round_end(Message::RoundEnd::p msg, boost::function<void ()> callback); virtual void get_action(boost::function<void (Action)> callback, Actions expected_actions); + + virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback); }; #endif diff --git a/server/game.cpp b/server/game.cpp index e9b1369..4816490 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -326,12 +326,6 @@ void Game::handle_action_discard(Action action, int player) { } void Game::round_end(Endcondition end) { - - round_num++; - - if(!round_num) { - round_wind++; - } Message::RoundEnd::p msg[4] = make_shared<Message::RoundEnd>(); @@ -409,12 +403,40 @@ void Game::round_end(Endcondition end) { msg[count_player]->game_end = false; if(count_player == 3) break; } - awaiting_players = 4; - players[0].client->round_end(msg[0], boost::bind(&Game::handle_ready, shared_from_this())); - players[1].client->round_end(msg[1], boost::bind(&Game::handle_ready, shared_from_this())); - players[2].client->round_end(msg[2], boost::bind(&Game::handle_ready, shared_from_this())); - players[3].client->round_end(msg[3], boost::bind(&Game::handle_ready, shared_from_this())); + round_num++; + + if(!round_num) { + round_wind++; + //Do a 4-round game for now + for(int i = 0; i < 4; ++i) { + msg[i]->game_end = true; + players[i].client->round_end(msg[i], 0); + } + game_end(); + } else { + awaiting_players = 4; + players[0].client->round_end(msg[0], boost::bind(&Game::handle_ready, shared_from_this())); + players[1].client->round_end(msg[1], boost::bind(&Game::handle_ready, shared_from_this())); + players[2].client->round_end(msg[2], boost::bind(&Game::handle_ready, shared_from_this())); + players[3].client->round_end(msg[3], boost::bind(&Game::handle_ready, shared_from_this())); + } // Ferdig? game_end() } + +void Game::game_end() { + Message::GameEnd::p msg = make_shared<Message::GameEnd>(); + for(int i = 0; i < 4; i++) { + msg->scores[i].score = players[i].score; + msg->scores[i].won = players[i].score - 25000; + } + + awaiting_players = 4; + players[0].client->game_end(msg, boost::bind(&Game::handle_ready, shared_from_this())); + players[1].client->game_end(msg, boost::bind(&Game::handle_ready, shared_from_this())); + players[2].client->game_end(msg, boost::bind(&Game::handle_ready, shared_from_this())); + players[3].client->game_end(msg, boost::bind(&Game::handle_ready, shared_from_this())); + + +} diff --git a/server/game.h b/server/game.h index 9d795a4..874a525 100644 --- a/server/game.h +++ b/server/game.h @@ -67,6 +67,9 @@ class Game : public boost::enable_shared_from_this<Game> { //! End the round. void round_end(Endcondition end); + + //! End the game + void game_end(); }; #endif |