diff options
-rw-r--r-- | server/game.cpp | 36 | ||||
-rw-r--r-- | server/player.cpp | 12 | ||||
-rw-r--r-- | 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 <iostream> #include <algorithm> +#include <ctime> 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<Message::RoundAction>(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<Message::RoundState>(state)); } +void Player::round_end() { + connection->send(make_shared<Message::RoundEnd>()); +} + void Player::get_action(boost::function<void (Action)> 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<Player> { //! Send round state. void round_state(State state); + //! Send round end. + void round_end(); + //! Get action. void get_action(boost::function<void (Action)> callback); + + void kill_action(); }; #endif |