summaryrefslogtreecommitdiff
path: root/server/lobby.cpp
blob: 620b2fd6e511d5f9646ccca08727a66fc558e101 (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
49
50
51
52
53
54
55
56
#include "lobby.h"

#include <boost/bind.hpp>

#include <iostream>

#include "game.h"

void Lobby::handle_connect(Connection::p connection) {
	// Create player.
	Client::create(connection, boost::bind(&Lobby::handle_login, this, _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;
	
	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();
}