diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-12-25 12:54:59 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-12-25 12:54:59 +0100 |
commit | fae209a9e93400c3a2072befda9c820634cf9278 (patch) | |
tree | 2d69e2c75fff0e08468c168f773abbc939a2ff03 /src/lobby.cpp | |
parent | 94a1189d757f0269ac081ad2d750152e30564986 (diff) |
Diffstat (limited to 'src/lobby.cpp')
-rw-r--r-- | src/lobby.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/lobby.cpp b/src/lobby.cpp new file mode 100644 index 0000000..9fb769e --- /dev/null +++ b/src/lobby.cpp @@ -0,0 +1,81 @@ +#include "lobby.h" + +#include <boost/bind.hpp> + +#include <iostream> + +#include "game.h" + +void Lobby::handle_connect(Connection::p connection) { + // Send Hello. + connection->send(make_shared<Message::Hello>("aotenjoud git")); + + // Wait for Login. + connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); + + // Get another connection. + server.get_connection(boost::bind(&Lobby::handle_connect, this, _1)); +} + +void Lobby::handle_login(Connection::p connection, Message::p msg) { + if(msg->type != Message::Types::Login) { + return; + } + + Message::Login::p login_msg = dynamic_pointer_cast<Message::Login>(msg); + + Client::p client; + + // Check if this is a reconnection attempt and whether a reconnection is possible. + if(login_msg->cookie && (client = Client::exists(login_msg))) { + client->reconnect(connection); + return; + } + + // Validate nick. + if(login_msg->nick.size() == 0) { + connection->send(make_shared<Message::LoginResponse>(Message::LoginResponse::Invalid)); + connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); + return; + } + + // Create a new client. + client = Client::create(connection, login_msg->nick); + + std::vector<std::string> game_modes; + + game_modes.push_back("1p test mode"); + game_modes.push_back("4p test mode"); + + client->lobby_status(game_modes, boost::bind(&Lobby::handle_action, this, client, _1)); +} + +void Lobby::handle_action(Client::p client, int game_mode) { + std::cout << "Client " << client->nick() << " joined a game." << std::endl; + + switch(game_mode) { + case 0: { + Game::create(client, make_shared<ClientDumb>(), make_shared<ClientDumb>(), make_shared<ClientDumb>()); + } break; + + case 1: { + if(waiting.size() >= 3) { + Game::create(waiting[0], waiting[1], waiting[2], client); + waiting.clear(); + } else { + waiting.push_back(client); + } + } break; + } +} + +Lobby::Lobby() : server(io_service) { + +} + +void Lobby::run() { + // Get a connection. + server.get_connection(boost::bind(&Lobby::handle_connect, this, _1)); + + io_service.run(); +} |