summaryrefslogtreecommitdiff
path: root/server/client.h
blob: e6cfcc799a51dfbee6a6ba5317241ba47e2bc53f (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef CLIENT_H
#define CLIENT_H

#include <string>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>

#include "connection.h"

//! Abstract client base class.
class ClientBase {
	public:
		typedef boost::shared_ptr<ClientBase> p;
		
		virtual ~ClientBase() {}
		
		//! Return client's nick.
		virtual std::string nick() = 0;
		
		//! Notify client of a game start.
		virtual void game_start(boost::function<void ()> callback, std::vector<std::string> players) = 0;
		
		//! Notify client of a round start.
		virtual void round_start() = 0;
		
		//! Send round state.
		virtual void round_state(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a) = 0;
		
		//! Send round end.
		virtual void round_end(Message::RoundEnd::p msg, boost::function<void ()> callback) = 0;
		
		//! Get action. Upon connection error, last element of expected_actions will be provided.
		virtual void get_action(boost::function<void (Action)> callback, Actions expected_actions) = 0;
		
		virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback) = 0;
		
};

//! Class implementing ClientBase for real clients.
class Client : public ClientBase, public boost::enable_shared_from_this<Client> {
	public:
		typedef boost::shared_ptr<Client> p;
		
		//! Construct and return a new Client instance.
		static p create(Connection::p c, std::string n);
		
		//! Check if a Client instance of the user already exists and return it.
		static p exists(Message::Login::p login_msg);
		
		~Client();
	
	private:
		uint64_t cookie;
		
		static std::map<uint64_t, boost::weak_ptr<Client> > client_list;
		
		Connection::p connection;
		
		boost::asio::deadline_timer timer;
		
		std::string nick_;
		
		Client(Connection::p c, std::string n);
		
		//! Start communicating.
		void start();
		
		//! Handle Login-message.
		void handle_login(Message::p msg, boost::function<void (Client::p)> login_callback);
		
		//! Handle LobbyAction-message.
		void handle_lobby(Message::p msg, boost::function<void (int)> lobby_callback);
		
		//! Handle Ready-message.
		void handle_ready(Message::p msg, boost::function<void ()> ready_callback);
		
		//! Handle Action-message.
		void handle_action(Message::p msg, boost::function<void (Action)> action_callback, Actions expected_actions);
		
	public:
		//! Inform client of lobby status (available game modes).
		void lobby_status(const std::vector<std::string>& game_modes, boost::function<void (int)> callback);
		
		//! Return client's nick.
		virtual std::string nick();
		
		//! Notify client of a game start.
		virtual void game_start(boost::function<void ()> callback, std::vector<std::string> players);
		
		//! Notify client of a round start.
		virtual void round_start();
		
		//! Send round state.
		virtual void round_state(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a);
		
		//! Send round end.
		virtual void round_end(Message::RoundEnd::p msg, boost::function<void ()> callback);
		
		//! Get action. Upon connection error, last element of expected_actions will be provided.
		virtual void get_action(boost::function<void (Action)> callback, Actions expected_actions);
		
		//! Reconnect a player.
		virtual void reconnect(Connection::p c);
		
		virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback);
};

typedef std::vector<Client> Clients;

class ClientDumb : public ClientBase {
	public:
		virtual unsigned int id();
		
		virtual std::string nick();
		
		virtual void game_start(boost::function<void ()> callback, std::vector<std::string> players);
		
		virtual void round_start();
		
		virtual void round_state(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a);
		
		virtual void round_end(Message::RoundEnd::p msg, boost::function<void ()> callback);
		
		virtual void get_action(boost::function<void (Action)> callback, Actions expected_actions);
		
		virtual void game_end(Message::GameEnd::p msg, boost::function<void ()> callback);
};

#endif