summaryrefslogtreecommitdiff
path: root/server/lobby.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'server/lobby.cpp')
-rw-r--r--server/lobby.cpp48
1 files changed, 34 insertions, 14 deletions
diff --git a/server/lobby.cpp b/server/lobby.cpp
index 7939977..9fb769e 100644
--- a/server/lobby.cpp
+++ b/server/lobby.cpp
@@ -7,27 +7,47 @@
#include "game.h"
void Lobby::handle_connect(Connection::p connection) {
- // Create player.
- Client::create(connection, boost::bind(&Lobby::handle_login, this, _1));
+ // 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(Client::p client) {
- std::cout << "Client " << client->nick() << " entered the lobby." << std::endl;
+void Lobby::handle_login(Connection::p connection, Message::p msg) {
+ if(msg->type != Message::Types::Login) {
+ return;
+ }
- if(0) {//client->id != 0
- //We check if player is in an ongoing game?
-
- } else {
- 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));
+ 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) {