From b28b3c5b8ddb6d73c1e7745d15ea328326a544f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atle=20Hellvik=20Havs=C3=B8?= Date: Tue, 14 Dec 2010 23:28:19 +0100 Subject: Made server send GameEnd. --- server/client.cpp | 14 +++++++++++++- server/client.h | 6 ++++++ server/game.cpp | 44 +++++++++++++++++++++++++++++++++----------- server/game.h | 3 +++ 4 files changed, 55 insertions(+), 12 deletions(-) (limited to 'server') 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 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 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 callback) { - callback(); + if(callback) callback(); } void ClientDumb::get_action(boost::function callback, Actions expected_actions) { callback(expected_actions.back()); } + +void ClientDumb::game_end(Message::GameEnd::p msg, boost::function 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 callback, Actions expected_actions) = 0; + virtual void game_end(Message::GameEnd::p msg, boost::function callback) = 0; + }; //! Class implementing ClientBase for real clients. @@ -102,6 +104,8 @@ class Client : public ClientBase, public boost::enable_shared_from_this //! Reconnect a player. virtual void reconnect(Connection::p c); + + virtual void game_end(Message::GameEnd::p msg, boost::function callback); }; typedef std::vector Clients; @@ -121,6 +125,8 @@ class ClientDumb : public ClientBase { virtual void round_end(Message::RoundEnd::p msg, boost::function callback); virtual void get_action(boost::function callback, Actions expected_actions); + + virtual void game_end(Message::GameEnd::p msg, boost::function 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(); @@ -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(); + 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 { //! End the round. void round_end(Endcondition end); + + //! End the game + void game_end(); }; #endif -- cgit v1.2.3