game_server.hpp 4.18 KB
Newer Older
louiz’'s avatar
louiz’ committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/** @addtogroup Network
 *  @{
 */

/**
 * The server running one game. It keeps one world representation, receives
 * command from various clients and sends back commands to execute, in the
 * futur, to concerned clients, and execute them later using the TurnHandler
 * class.
 * @class GameServer
 */

#ifndef __GAME_SERVER_HPP__
# define __GAME_SERVER_HPP__

louiz’'s avatar
louiz’ committed
16
17
18
#include <vector>

#include <game/game.hpp>
louiz’'s avatar
louiz’ committed
19
20
21
22
#include <game/remote_game_client.hpp>
#include <game/occupants_handler.hpp>
#include <network/server.hpp>
#include <world/world.hpp>
louiz’'s avatar
louiz’ committed
23
24
#include <game/replay.hpp>
#include <game/turn_handler.hpp>
louiz’'s avatar
louiz’ committed
25

louiz’'s avatar
louiz’ committed
26
class GameServer: public Game, public Server<RemoteGameClient>
louiz’'s avatar
louiz’ committed
27
28
{
public:
louiz’'s avatar
louiz’ committed
29
  GameServer(short port);
louiz’'s avatar
louiz’ committed
30
  ~GameServer();
louiz’'s avatar
louiz’ committed
31
  // World* get_world();
louiz’'s avatar
louiz’ committed
32
33
34
35
36
37
38
39
40

  virtual void on_new_client(RemoteGameClient*) override final;
  virtual void on_client_left(RemoteGameClient*) override final;
  /**
   * Send the complete content of the replay contained in the World
   * to the client.
   */
  void send_replay(RemoteGameClient*);
  /**
louiz’'s avatar
louiz’ committed
41
   * Send a start message to the client telling it to start
louiz’'s avatar
louiz’ committed
42
43
   * confirming turns and actions from there.
   */
louiz’'s avatar
louiz’ committed
44
  void send_start_message(RemoteGameClient*);
louiz’'s avatar
louiz’ committed
45
46
47
48
49
  /**
   * Take all futur actions in the turn_handler, if they are not
   * completely validated yet, change their validations_needed value
   * to the current number of occupants, and send them to the client.
   */
louiz’'s avatar
louiz’ committed
50
  void send_future_actions(RemoteGameClient*);
louiz’'s avatar
louiz’ committed
51
  /**
louiz’'s avatar
louiz’ committed
52
53
   * Send a message to signal all clients that the next turn is now ready to
   * be executed.
louiz’'s avatar
louiz’ committed
54
   */
louiz’'s avatar
louiz’ committed
55
  void send_turn();
louiz’'s avatar
louiz’ committed
56
57

  void tick();
louiz’'s avatar
louiz’ committed
58
59
  // void pause_game();
  // void unpause_game();
louiz’'s avatar
louiz’ committed
60
  void start_game();
louiz’'s avatar
louiz’ committed
61
62
63
64
65
66
67
  void send_message_to_all(const char* name, const std::string& data);
  void send_order_to_all(const char* name, const google::protobuf::Message& srl);
  /**
   * Called when a turn is done. Validate our next turn and send a TURN
   * command to all clients.
   */
  void on_next_turn(const TurnNb nb);
louiz’'s avatar
louiz’ committed
68

louiz’'s avatar
louiz’ committed
69
  void send_new_entity_order(const EntityType type, const Position& pos, const uint16_t team);
louiz’'s avatar
louiz’ committed
70
  void send_move_order(const std::vector<EntityId> ids, const Position& pos, const bool queue);
louiz’'s avatar
louiz’ committed
71
72
73
74
75
76
77
78
79
80
  /**
   * Send an Order to each client, to make each of these entities cast the
   * given ability.
   */
  void send_cast_order(const std::vector<EntityId>& ids, const Position& pos,
                       const uint32_t type, const bool queue);
  void send_cast_order(const std::vector<EntityId>& ids, const EntityId pos,
                       const uint32_t type, const bool queue);
  void send_cast_order(const std::vector<EntityId>& ids,
                       const uint32_t type, const bool queue);
louiz’'s avatar
louiz’ committed
81
82
83
84
85
86
  /**
   * This will create the initial game
   * state.
   * The iniatial state is generated by associating some actions with the
   * turn number 1.
   * When a new occupant joins (even if the game is not started) it will
louiz’'s avatar
louiz’ committed
87
   * receive these messages as part of the "replay". If the game is not yet
louiz’'s avatar
louiz’ committed
88
89
90
91
92
93
94
   * started, the replay is only one turn long.
   * If a player joins later (the game is started), it will still receive
   * the actions associated with the first turn, plus the subsequent game
   * actions.
   */
  void init();
  /**
louiz’'s avatar
louiz’ committed
95
   * Called whenever we receive a MOVE message from one client.
louiz’'s avatar
louiz’ committed
96
97
98
   * We try to generate a path, if the move is valid, and then
   * send that path action to all clients.
   */
louiz’'s avatar
louiz’ committed
99
  void on_move_request(Message*);
louiz’'s avatar
louiz’ committed
100
101
102
103
  /**
   * Called whenever we receive a CAST message from one client.
   */
  void on_cast_request(Message*);
louiz’'s avatar
louiz’ committed
104
  /**
louiz’'s avatar
louiz’ committed
105
   * Called whenever we receive a BUILD message from one client.
louiz’'s avatar
louiz’ committed
106
107
   * Check if the position is valid and the building can be built (money, the unit actually has this ability, etc).
   */
louiz’'s avatar
louiz’ committed
108
  void build_callback(Message*);
louiz’'s avatar
louiz’ committed
109
  /**
louiz’'s avatar
louiz’ committed
110
   * Called whenever we receive a SPAWN message from one client.  Should
louiz’'s avatar
louiz’ committed
111
112
113
   * check if that unit can actually do that (money, capacity to do it,
   * etc), before returning a DoSpawnEvent.
   */
louiz’'s avatar
louiz’ committed
114
  void spawn_callback(Message*);
louiz’'s avatar
louiz’ committed
115
116
117
118
119

private:
  GameServer(const GameServer&);
  GameServer& operator=(const GameServer&);
  /**
louiz’'s avatar
louiz’ committed
120
121
   * Store the already executed Actions, to pass them to save them in a
   * file, and send them to the new occupants when they join a running game.
louiz’'s avatar
louiz’ committed
122
   */
louiz’'s avatar
louiz’ committed
123
  Replay replay;
louiz’'s avatar
louiz’ committed
124
125
126
127
};

#endif // __GAME_SERVER_HPP__
/**#@}*/