summaryrefslogtreecommitdiff
path: root/server/lobby.cpp
blob: f74a4f28d492832c006e141bde14d7134e43a042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "lobby.h"

#include <boost/bind.hpp>

#include <iostream>

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);
	
	std::cout << "Player " << login_msg->nick << " entered the lobby." << std::endl;
	
	// Check if nick is invalid.
	if(login_msg->nick.size() == 0) {
		connection->send(make_shared<Message::LoginResponse>(false));
		connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1));
		return;
	}
	
	connection->send(make_shared<Message::LoginResponse>(true));
	
	// Do something with the connection to keep it around.
}

Lobby::Lobby() : server(io_service) {
	
}

void Lobby::run() {
	// Get a connection.
	server.get_connection(boost::bind(&Lobby::handle_connect, this, _1));
	
	io_service.run();
}