From 7e599c9e515556bf2122bd33f971ef37bbaffd07 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 15 Nov 2010 08:43:27 +0100 Subject: Added Game-class and GameStart message. --- server/game.cpp | 16 ++++++++++++++-- server/game.h | 20 +++++++++----------- server/lobby.cpp | 9 +++++++++ server/lobby.h | 4 ++++ server/player.cpp | 4 ++++ server/player.h | 3 +++ 6 files changed, 43 insertions(+), 13 deletions(-) (limited to 'server') 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 + +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 #include "player.h" - -class Game{ + +class Game { private: - std::list 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 +#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 +#include + #include "tcpserver.h" #include "player.h" @@ -11,6 +13,8 @@ class Lobby { boost::asio::io_service io_service; TCPServer server; + std::vector 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()); +} 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 { public: //! Return player's nick. std::string nick(); + + //! Notify client of a game start. + void game_start(); }; #endif -- cgit v1.2.3