diff options
-rw-r--r-- | common/message.h | 2 | ||||
-rw-r--r-- | server/connection.cpp | 2 | ||||
-rw-r--r-- | server/connection.h | 1 | ||||
-rw-r--r-- | server/game.cpp | 41 | ||||
-rw-r--r-- | server/game.h | 25 | ||||
-rw-r--r-- | server/lobby.cpp | 2 | ||||
-rw-r--r-- | server/player.cpp | 20 | ||||
-rw-r--r-- | server/player.h | 12 |
8 files changed, 86 insertions, 19 deletions
diff --git a/common/message.h b/common/message.h index bd68efe..6b2744d 100644 --- a/common/message.h +++ b/common/message.h @@ -107,7 +107,7 @@ namespace Message { class GameStart : public BoostBase { public: - typedef boost::shared_ptr<LoginResponse> p; + typedef boost::shared_ptr<GameStart> p; GameStart(); diff --git a/server/connection.cpp b/server/connection.cpp index b64ae11..fd2d257 100644 --- a/server/connection.cpp +++ b/server/connection.cpp @@ -8,7 +8,7 @@ Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) void Connection::handle_read(uint8_t* data, std::size_t bytes, const boost::system::error_code& error) { if(error) { - recv_callback = NULL; + recv_callback.clear(); return; } diff --git a/server/connection.h b/server/connection.h index 3428b88..e6680c9 100644 --- a/server/connection.h +++ b/server/connection.h @@ -10,6 +10,7 @@ class Connection : public ConnectionBase, public boost::enable_shared_from_this<Connection> { private: friend class TCPServer; + friend class Player; boost::asio::ip::tcp::socket socket; diff --git a/server/game.cpp b/server/game.cpp index 13f2a18..e5a21b6 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -1,16 +1,41 @@ #include "game.h" +#include <boost/bind.hpp> + #include <iostream> +Game::p Game::create(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { + Game::p p(new Game(player_1, player_2, player_3, player_4)); + p->start(); + return p; +} + Game::Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { + players.push_back(player_1); + players.push_back(player_2); + players.push_back(player_3); + players.push_back(player_4); +} + +void Game::handle_ready() { + if(--waiting_players) { + return; + } + + std::cout << "All ready!" << std::endl; +} + +void Game::start() { std::cout << "Started a game with " - << player_1->nick() << ", " - << player_2->nick() << ", " - << player_3->nick() << " and " - << player_4->nick() << "." << std::endl; + << players[0]->nick() << ", " + << players[1]->nick() << ", " + << players[2]->nick() << " and " + << players[3]->nick() << "." << std::endl; + + waiting_players = 4; - player_1->game_start(); - player_2->game_start(); - player_3->game_start(); - player_4->game_start(); + players[0]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); + players[1]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); + players[2]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); + players[3]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players); } diff --git a/server/game.h b/server/game.h index f2f64ac..00da0bc 100644 --- a/server/game.h +++ b/server/game.h @@ -1,13 +1,32 @@ #ifndef GAME_H #define GAME_H +#include <boost/shared_ptr.hpp> +#include <boost/enable_shared_from_this.hpp> + +#include <vector> + #include "player.h" -class Game { - private: - +class Game : public boost::enable_shared_from_this<Game> { public: + typedef boost::shared_ptr<Game> p; + + static p create(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4); + + private: + std::vector<Player::p> players; + + int waiting_players; + Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4); + + //! Handle Ready message from player. + void handle_ready(); + + //! Start the game. + void start(); + }; #endif diff --git a/server/lobby.cpp b/server/lobby.cpp index b31183b..748d70f 100644 --- a/server/lobby.cpp +++ b/server/lobby.cpp @@ -18,7 +18,7 @@ void Lobby::handle_action(Player::p player) { std::cout << "Player " << player->nick() << " entered the lobby." << std::endl; if(waiting.size() >= 3) { - new Game(waiting[0], waiting[1], waiting[2], player); + Game::create(waiting[0], waiting[1], waiting[2], player); waiting.clear(); } else { waiting.push_back(player); diff --git a/server/player.cpp b/server/player.cpp index b2cdd83..1901da7 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -8,7 +8,7 @@ Player::p Player::create(Connection::p c, boost::function<void (Player::p)> f) { return p; } -Player::Player(Connection::p c, boost::function<void (Player::p)> f) : connection(c), lobby_callback(f) { +Player::Player(Connection::p c, boost::function<void (Player::p)> f) : connection(c), timer(c->socket.get_io_service()), lobby_callback(f) { } @@ -41,10 +41,24 @@ void Player::handle_login(Message::p msg) { lobby_callback(shared_from_this()); } +void Player::handle_ready(Message::p msg) { + if(msg->type != Message::Types::Ready) { + return; + } + + ready_callback(); +} + std::string Player::nick() { return nick_; } -void Player::game_start() { - connection->send(make_shared<Message::GameStart>()); +void Player::game_start(boost::function<void ()> callback, std::vector<Player::p> players) { + Message::GameStart::p msg = make_shared<Message::GameStart>(); + + for(std::vector<Player::p>::iterator i = players.begin(); i != players.end(); i++) { + msg->players.push_back((*i)->nick()); + } + + connection->send(msg); } diff --git a/server/player.h b/server/player.h index 17e8989..667d6ea 100644 --- a/server/player.h +++ b/server/player.h @@ -5,6 +5,7 @@ #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/function.hpp> +#include <boost/asio.hpp> #include "connection.h" @@ -17,7 +18,11 @@ class Player : public boost::enable_shared_from_this<Player> { private: Connection::p connection; + boost::asio::deadline_timer timer; + boost::function<void (Player::p)> lobby_callback; + boost::function<void ()> ready_callback; + std::string nick_; @@ -26,15 +31,18 @@ class Player : public boost::enable_shared_from_this<Player> { //! Start communicating. void start(); - //! Handle login. + //! Handle Login-message. void handle_login(Message::p msg); + //! Handle Ready-message. + void handle_ready(Message::p msg); + public: //! Return player's nick. std::string nick(); //! Notify client of a game start. - void game_start(); + void game_start(boost::function<void ()> callback, std::vector<Player::p> players); }; #endif |