The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
room_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Tomasz Sniatowski <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "room.hpp"
16 
17 #include <boost/utility.hpp>
18 
19 class config;
20 
21 #ifndef SERVER_ROOM_MANAGER_HPP_INCLUDED
22 #define SERVER_ROOM_MANAGER_HPP_INCLUDED
23 
24 namespace wesnothd {
25 
26 /**
27  * The room manager manages the lobby and other rooms in the server, and related
28  * client message processing.
29  * The lobby represents players that are on the server, but not in any game.
30  */
31 class room_manager : private boost::noncopyable
32 {
33 public:
34  /**
35  * Room manager constructor
36  */
37  room_manager(player_map& all_players);
38 
39  /**
40  * Room manager destructor
41  */
42  ~room_manager();
43 
50  };
51 
52  static PRIVILEGE_POLICY pp_from_string(const std::string& str);
53 
54  /**
55  * Load settings from the main config file
56  */
57  void load_config(const config& cfg);
58 
59  /**
60  * Reads stored rooms from a file on disk, or returns immediately
61  * if load_config was not called before or the storage filename is empty
62  */
63  void read_rooms();
64 
65  /**
66  * Writes rooms to the storage file or returns immediately if load_config
67  * was not called beforethe storage filename is empty
68  */
69  void write_rooms();
70 
71  /**
72  * Dirty flag for rooms -- true if there were changes that should be written
73  * to disk
74  */
75  bool dirty() const { return dirty_; }
76 
77  /**
78  * Get a room by name, or nullptr if it does not exist
79  */
80  room* get_room(const std::string& name);
81 
82  /**
83  * @param name the room name to check
84  * @return true iif the room existst
85  */
86  bool room_exists(const std::string& name) const;
87 
88  /**
89  * Create room named "name" if it does not exist already.
90  */
92 
93  /**
94  * Get a room by name or create that room if it does not exist and
95  * creating rooms is allowed.
96  * @return a valid pointer to a room or nullptr if the room did not exist and
97  * could not be created.
98  */
100 
101  /**
102  * @return true iif the player is in the lobby
103  */
104  bool in_lobby(network::connection player) const;
105 
106  /**
107  * Player-enters-lobby action. Will autorejoin "stored" rooms (the ones
108  * the player was before enetering a game, for instance)
109  */
111 
112  /**
113  * All players from a game re-enter the lobby
114  */
115  void enter_lobby(const game& game);
116 
117  /**
118  * Player exits lobby.
119  */
121 
122  /**
123  * Remove info abut given player from all rooms
124  */
126 
127  /**
128  * Check if the room exists, log failures.
129  * @return non-nullptr iff the room exists and the player is a member
130  */
131  room* require_room(const std::string& room_name,
132  const player_map::iterator user, const char* log_string = "use");
133 
134  /**
135  * Check if the room exists and if the player is a member, log failures.
136  * @return non-nullptr iff the room exists and the player is a member
137  */
138  room* require_member(const std::string& room_name,
139  const player_map::iterator user, const char* log_string = "use");
140 
141  /**
142  * Process a message (chat message) sent to a room. Check conditions
143  * and resend to other players in the room.
144  */
146 
147  /**
148  * Process a player's request to join a room
149  */
151 
152  /**
153  * Process a player's request to leave a room
154  */
156 
157  /**
158  * Process a general room query
159  */
161 
162  /**
163  * Lobby convenience accesor
164  */
165  const room& lobby() const { return *lobby_; }
166 
167 private:
168  void do_room_join(network::connection player, const std::string& room_name);
169 
170  /**
171  * Adds a player to a room, maintaining internal consistency
172  * Will send appropriate error messages to the player.
173  * @return true iif the operation was successful, false otherwise
174  */
176 
177  /**
178  * Removes a player from a room, maintaining internal consistency
179  */
181 
182  /**
183  * Stores the room names (other than lobby) of the given player for future
184  * use (rejoin)
185  */
187 
188  /**
189  * Unstores (rejoins) player's rooms that were previously stored.
190  * No action if not stored earlier or no rooms.
191  */
193 
194  /**
195  * Helper function that calls the player_map::iterator version
196  * of unstore_player_rooms
197  */
199 
200  /**
201  * Fill a wml node (message) with members of a room
202  */
203  void fill_member_list(const room* room, simple_wml::node& root);
204 
205  /**
206  * Fill a wml node (message) with a room list
207  */
208  void fill_room_list(simple_wml::node& root);
209 
210  /** Reference to the all players map */
212 
213  /** The lobby-room, treated separetely */
215 
216  /** Rooms by name */
217  typedef std::map<std::string, room*> t_rooms_by_name_;
218  t_rooms_by_name_ rooms_by_name_;
219 
220  /** Rooms by player */
221  typedef std::map<network::connection, std::set<room*> > t_rooms_by_player_;
222  t_rooms_by_player_ rooms_by_player_;
223 
224  /** Room names stored for players that have entered a game */
225  typedef std::map<network::connection, std::set<std::string> > t_player_stored_rooms_;
226  t_player_stored_rooms_ player_stored_rooms_;
227 
228  /**
229  * Persistent room storage filename. If empty, rooms are not stored on disk.
230  */
232 
233  /**
234  * Flag controlling whether to compress the stored rooms or not
235  */
237 
238  /**
239  * Policy regarding who can create new rooms
240  */
242 
243  /**
244  * 'Dirty' flag with regards to room info that will be stored on disk
245  */
246  bool dirty_;
247 
248  /**
249  * The main (lobby) room name
250  */
251  static const char* const lobby_name_;
252 };
253 
254 } //namespace wesnothd
255 
256 
257 #endif
void process_room_part(simple_wml::document &data, const player_map::iterator user)
Process a player's request to leave a room.
void process_message(simple_wml::document &data, const player_map::iterator user)
Process a message (chat message) sent to a room.
std::map< std::string, room * > t_rooms_by_name_
Rooms by name.
t_rooms_by_name_ rooms_by_name_
static PRIVILEGE_POLICY pp_from_string(const std::string &str)
room * lobby_
The lobby-room, treated separetely.
void fill_member_list(const room *room, simple_wml::node &root)
Fill a wml node (message) with members of a room.
void unstore_player_rooms(const player_map::iterator user)
Unstores (rejoins) player's rooms that were previously stored.
void player_exits_room(network::connection player, room *room)
Removes a player from a room, maintaining internal consistency.
std::string filename_
Persistent room storage filename.
void store_player_rooms(network::connection player)
Stores the room names (other than lobby) of the given player for future use (rejoin) ...
void do_room_join(network::connection player, const std::string &room_name)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
void fill_room_list(simple_wml::node &root)
Fill a wml node (message) with a room list.
room_manager(player_map &all_players)
Room manager constructor.
The room manager manages the lobby and other rooms in the server, and related client message processi...
bool in_lobby(network::connection player) const
std::map< network::connection, player > player_map
Definition: room.hpp:25
~room_manager()
Room manager destructor.
room * get_room(const std::string &name)
Get a room by name, or nullptr if it does not exist.
room * require_member(const std::string &room_name, const player_map::iterator user, const char *log_string="use")
Check if the room exists and if the player is a member, log failures.
t_rooms_by_player_ rooms_by_player_
A room is a group of players that can communicate via messages.
Definition: room.hpp:31
void read_rooms()
Reads stored rooms from a file on disk, or returns immediately if load_config was not called before o...
void process_room_join(simple_wml::document &data, const player_map::iterator user)
Process a player's request to join a room.
bool compress_stored_rooms_
Flag controlling whether to compress the stored rooms or not.
room * get_create_room(const std::string &name, network::connection player)
Get a room by name or create that room if it does not exist and creating rooms is allowed...
std::map< network::connection, std::set< room * > > t_rooms_by_player_
Rooms by player.
PRIVILEGE_POLICY new_room_policy_
Policy regarding who can create new rooms.
void exit_lobby(network::connection player)
Player exits lobby.
std::map< network::connection, std::set< std::string > > t_player_stored_rooms_
Room names stored for players that have entered a game.
room * require_room(const std::string &room_name, const player_map::iterator user, const char *log_string="use")
Check if the room exists, log failures.
void load_config(const config &cfg)
Load settings from the main config file.
void write_rooms()
Writes rooms to the storage file or returns immediately if load_config was not called beforethe stora...
void process_room_query(simple_wml::document &data, const player_map::iterator user)
Process a general room query.
player_map & all_players_
Reference to the all players map.
GLuint const GLchar * name
Definition: glew.h:1782
bool dirty_
'Dirty' flag with regards to room info that will be stored on disk
void remove_player(network::connection player)
Remove info abut given player from all rooms.
static const char *const lobby_name_
The main (lobby) room name.
bool player_enters_room(network::connection player, room *room)
Adds a player to a room, maintaining internal consistency Will send appropriate error messages to the...
Definition: ban.cpp:28
void enter_lobby(network::connection player)
Player-enters-lobby action.
int connection
Definition: network.hpp:74
bool room_exists(const std::string &name) const
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool dirty() const
Dirty flag for rooms – true if there were changes that should be written to disk.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
t_player_stored_rooms_ player_stored_rooms_
const room & lobby() const
Lobby convenience accesor.
room * create_room(const std::string &name)
Create room named "name" if it does not exist already.