summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/game.cpp16
-rw-r--r--server/game.h20
-rw-r--r--server/lobby.cpp9
-rw-r--r--server/lobby.h4
-rw-r--r--server/player.cpp4
-rw-r--r--server/player.h3
6 files changed, 43 insertions, 13 deletions
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