diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-15 08:43:27 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-15 08:43:27 +0100 |
commit | 7e599c9e515556bf2122bd33f971ef37bbaffd07 (patch) | |
tree | 92bcf07118fa739c1cc2120ec912acdcda50c9ba | |
parent | c6a83f89ea55ba2071611513ac85b64134ac8f7b (diff) |
Added Game-class and GameStart message.
-rw-r--r-- | common/connectionbase.cpp | 4 | ||||
-rw-r--r-- | common/message.cpp | 12 | ||||
-rw-r--r-- | common/message.h | 10 | ||||
-rw-r--r-- | server/game.cpp | 16 | ||||
-rw-r--r-- | server/game.h | 20 | ||||
-rw-r--r-- | server/lobby.cpp | 9 | ||||
-rw-r--r-- | server/lobby.h | 4 | ||||
-rw-r--r-- | server/player.cpp | 4 | ||||
-rw-r--r-- | server/player.h | 3 |
9 files changed, 69 insertions, 13 deletions
diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index c269349..ce23752 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -50,6 +50,10 @@ void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { m = make_shared<Message::LoginResponse>(); break; + case Message::Types::GameStart: + m = make_shared<Message::GameStart>(); + break; + default: throw std::runtime_error("Deserialization attempted on unknown message type."); } diff --git a/common/message.cpp b/common/message.cpp index 8b3dc74..2dd9691 100644 --- a/common/message.cpp +++ b/common/message.cpp @@ -63,3 +63,15 @@ std::pair<uint8_t*, std::size_t> Message::LoginResponse::serialize() { void Message::LoginResponse::deserialize(uint8_t* data, std::size_t bytes) { login_ok = bool(data[0]); } + +Message::GameStart::GameStart() : Base(Types::GameStart) { + +} + +std::pair<uint8_t*, std::size_t> Message::GameStart::serialize() { + return std::pair<uint8_t*, std::size_t>(0, 0); +} + +void Message::GameStart::deserialize(uint8_t* data, std::size_t bytes) { + +} diff --git a/common/message.h b/common/message.h index 56096ed..3587229 100644 --- a/common/message.h +++ b/common/message.h @@ -80,6 +80,16 @@ namespace Message { virtual void deserialize(uint8_t* data, std::size_t bytes); }; + class GameStart : public Base { + public: + typedef boost::shared_ptr<LoginResponse> p; + + GameStart(); + + virtual std::pair<uint8_t*, std::size_t> serialize(); + virtual void deserialize(uint8_t* data, std::size_t bytes); + }; + typedef boost::shared_ptr<Base> p; }; diff --git a/server/game.cpp b/server/game.cpp index 36fd33c..13f2a18 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -1,4 +1,16 @@ #include "game.h" -Game::Game(){} -Game::~Game(){} +#include <iostream> + +Game::Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { + std::cout << "Started a game with " + << player_1->nick() << ", " + << player_2->nick() << ", " + << player_3->nick() << " and " + << player_4->nick() << "." << std::endl; + + player_1->game_start(); + player_2->game_start(); + player_3->game_start(); + player_4->game_start(); +} diff --git a/server/game.h b/server/game.h index 1cb03c1..f2f64ac 100644 --- a/server/game.h +++ b/server/game.h @@ -1,15 +1,13 @@ -#ifndef GAME_H
-#define GAME_H
+#ifndef GAME_H +#define GAME_H -#include <list> #include "player.h" -
-class Game{ + +class Game { private: - std::list<Player> players; + public: - Game(); - ~Game(); -};
-
-#endif // GAME_H
+ Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4); +}; + +#endif diff --git a/server/lobby.cpp b/server/lobby.cpp index a3b50d7..b31183b 100644 --- a/server/lobby.cpp +++ b/server/lobby.cpp @@ -4,6 +4,8 @@ #include <iostream> +#include "game.h" + void Lobby::handle_connect(Connection::p connection) { // Create player. Player::create(connection, boost::bind(&Lobby::handle_action, this, _1)); @@ -14,6 +16,13 @@ void Lobby::handle_connect(Connection::p connection) { 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); + waiting.clear(); + } else { + waiting.push_back(player); + } } Lobby::Lobby() : server(io_service) { diff --git a/server/lobby.h b/server/lobby.h index d266a99..6f5572e 100644 --- a/server/lobby.h +++ b/server/lobby.h @@ -3,6 +3,8 @@ #include <boost/asio.hpp> +#include <vector> + #include "tcpserver.h" #include "player.h" @@ -11,6 +13,8 @@ class Lobby { boost::asio::io_service io_service; TCPServer server; + std::vector<Player::p> waiting; + //! Handle new connection. void handle_connect(Connection::p connection); diff --git a/server/player.cpp b/server/player.cpp index 7243d81..b2cdd83 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -44,3 +44,7 @@ void Player::handle_login(Message::p msg) { std::string Player::nick() { return nick_; } + +void Player::game_start() { + connection->send(make_shared<Message::GameStart>()); +} diff --git a/server/player.h b/server/player.h index 8b75885..17e8989 100644 --- a/server/player.h +++ b/server/player.h @@ -32,6 +32,9 @@ class Player : public boost::enable_shared_from_this<Player> { public: //! Return player's nick. std::string nick(); + + //! Notify client of a game start. + void game_start(); }; #endif |