From 9a2d4a69e13b344d4342af3c81373ac14eeb0368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atle=20Hellvik=20Havs=C3=B8?= Date: Sun, 21 Nov 2010 21:35:50 +0100 Subject: Made the server send a empty RoundEnd message when the wall is empty (Contains only 14 tiles). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Atle Hellvik Havsø --- server/game.cpp | 36 +++++++++++++++++++++++++++++++++++- server/player.cpp | 12 +++++++++++- server/player.h | 5 +++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/server/game.cpp b/server/game.cpp index 3548e13..49cfb0b 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -4,6 +4,7 @@ #include #include +#include bool MyDataSortPredicate(const Tile& d1, const Tile& d2) { @@ -63,6 +64,13 @@ void Game::round_start() { game_state.players[player_num].hand.push_back(wall.take_one()); } + std::sort(game_state.players[0].hand.begin(),game_state.players[0].hand.end(), MyDataSortPredicate); + std::sort(game_state.players[1].hand.begin(),game_state.players[1].hand.end(), MyDataSortPredicate); + std::sort(game_state.players[2].hand.begin(),game_state.players[2].hand.end(), MyDataSortPredicate); + std::sort(game_state.players[3].hand.begin(),game_state.players[3].hand.end(), MyDataSortPredicate); + + + current_player = 3; num_player_actions = 0; draw_phase = true; @@ -74,12 +82,24 @@ void Game::round_update() { // We're in the draw_phase whenever a player draws a tile from the wall if(draw_phase) { + // If the wall is empty (Contains only 14 tiles) when we enter draw phase the round is over. + if(wall.is_done()) { + round_end(); + } + // Since we've entered the draw-phase it's the next players turn if (current_player == 3) { current_player = 0; } else { current_player++; } + + #ifdef DEBUG + + time_t current_time = std::time(0); + std::cout << std::ctime(¤t_time) << " - Waiting for action from player: " << current_player << std::endl; + + #endif // Let's take a tile Tile from_wall = wall.take_one(); @@ -126,9 +146,12 @@ void Game::round_update() { void Game::handle_action(Action action) { + players[action.player]->kill_action(); + #ifdef DEBUG - std::cout << "Player: " << action.player << " Did action: " << action.type << " On target: " << action.target << std::endl; + time_t current_time = std::time(0); + std::cout << std::ctime(¤t_time) << " Player: " << action.player << " Did action: " << action.type << " On target: " << action.target << std::endl; #endif @@ -234,6 +257,17 @@ void Game::handle_action(Action action) { void Game::round_end() { // Flere runder? round_start() // Ferdig? game_end() + players[0]->round_end(); + players[1]->round_end(); + players[2]->round_end(); + players[3]->round_end(); + + #ifdef DEBUG + + time_t current_time = std::time(0); + std::cout << std::ctime(¤t_time) << " Round is over..." << std::endl; + + #endif } void Game::start() { diff --git a/server/player.cpp b/server/player.cpp index 6e1a7d3..ce6a116 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -57,7 +57,9 @@ void Player::handle_action(Message::p msg) { Message::RoundAction::p action_msg = dynamic_pointer_cast(msg); action_msg->action.player = id; - action_callback(action_msg->action); + if(action_callback != 0) { + action_callback(action_msg->action); + } } std::string Player::nick() { @@ -87,8 +89,16 @@ void Player::round_state(State state) { connection->send(make_shared(state)); } +void Player::round_end() { + connection->send(make_shared()); +} + void Player::get_action(boost::function callback) { action_callback = callback; connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1)); } + +void Player::kill_action() { + action_callback = 0; +} diff --git a/server/player.h b/server/player.h index af27721..f558711 100644 --- a/server/player.h +++ b/server/player.h @@ -56,8 +56,13 @@ class Player : public boost::enable_shared_from_this { //! Send round state. void round_state(State state); + //! Send round end. + void round_end(); + //! Get action. void get_action(boost::function callback); + + void kill_action(); }; #endif -- cgit v1.2.3