The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
room.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 #ifndef SERVER_ROOM_HPP_INCLUDED
16 #define SERVER_ROOM_HPP_INCLUDED
17 
18 #include "network.hpp"
19 #include "player.hpp"
20 #include "simple_wml.hpp"
21 
22 namespace wesnothd {
23 
24 typedef std::vector<network::connection> connection_vector;
25 typedef std::map<network::connection,player> player_map;
26 class game;
27 
28 /**
29  * A room is a group of players that can communicate via messages.
30  */
31 class room {
32 public:
33  /**
34  * Construct a room with just a name and default settings
35  */
36  room(const std::string& name);
37 
38  /**
39  * Construct a room from WML
40  */
41  room(const config& cfg);
42 
43  /**
44  * Write room info to a config
45  */
46  void write(config& cfg) const;
47 
48  /**
49  * The name of this room
50  */
51  const std::string& name() const;
52 
53  /**
54  * Whether this room should be 'persistent', i.e. not deleted when there
55  * are no players within and stored on disk if needed.
56  */
57  bool persistent() const;
58 
59  /**
60  * Set the persistent flag for this room
61  */
62  void set_persistent(bool v);
63 
64  /**
65  * Whether the room is logged (and might end up in e.g. the lobby bot
66  */
67  bool logged() const;
68 
69  /**
70  * Set the room's logged flag
71  */
72  void set_logged(bool v);
73 
74  /**
75  * This room's topic/motd, sent to all joining players
76  */
77  const std::string& topic() const;
78 
79  /**
80  * Set the topic for this room
81  */
82  void set_topic(const std::string& v);
83 
84  /**
85  * Return the number of players in this room
86  */
87  size_t size() const {
88  return members_.size();
89  }
90 
91  /**
92  * Return true iif the room is empty
93  */
94  bool empty() const {
95  return members_.empty();
96  }
97 
98  /**
99  * Return the members of this room
100  */
101  const std::vector<network::connection>& members() const {
102  return members_;
103  }
104 
105  /**
106  * Membership checker.
107  * @return true iif player is a member of this room
108  */
110  return std::find(members_.begin(), members_.end(), player) != members_.end();
111  }
112 
113  /**
114  * Joining the room
115  * @return true if the player was successfully added
116  */
118 
119  /**
120  * Leaving the room
121  */
123 
124  /**
125  * Chat message processing
126  */
128 
129  /**
130  * Convenience function for sending a wml document to all (or all except
131  * one) members.
132  * @see send_to_many
133  * @param data the document to send
134  * @param exclude if nonzero, do not send to this player
135  * @param packet_type the packet type, if empty the root node name is used
136  */
137  void send_data(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
138 
139  /**
140  * Send a text message to all members
141  * @param message the message text
142  * @param exclude if nonzero, do not send to this player
143  */
144  void send_server_message_to_all(const char* message, network::connection exclude=0) const;
146  {
147  send_server_message_to_all(message.c_str(), exclude);
148  }
149 
150  /**
151  * Prepare a text message and/or send it to a player. If a nonzero sock
152  * is passed, the message is sent to this player. If a non-null pointer
153  * to a simple_wml::document is passed, the message is stored in that
154  * document.
155  * @param message the message text
156  * @param sock the socket to send the message to, if nonzero
157  * @param docptr the wml document to store the message in, if nonnull
158  */
159  void send_server_message(const char* message, network::connection sock,
160  simple_wml::document* docptr = nullptr) const;
161 
163  simple_wml::document* docptr = nullptr) const
164  {
165  send_server_message(message.c_str(), sock, docptr);
166  }
167 
168 
169 private:
171  connection_vector members_;
174  bool logged_;
175 };
176 
177 } //end namespace wesnothd
178 
179 #endif
void set_logged(bool v)
Set the room's logged flag.
Definition: room.cpp:86
const std::string & topic() const
This room's topic/motd, sent to all joining players.
Definition: room.cpp:71
bool is_member(network::connection player) const
Membership checker.
Definition: room.hpp:109
const std::string & name() const
The name of this room.
Definition: room.cpp:56
std::string topic_
Definition: room.hpp:173
void send_server_message(const char *message, network::connection sock, simple_wml::document *docptr=nullptr) const
Prepare a text message and/or send it to a player.
Definition: room.cpp:129
size_t size() const
Return the number of players in this room.
Definition: room.hpp:87
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
bool persistent_
Definition: room.hpp:172
std::map< network::connection, player > player_map
Definition: room.hpp:25
void remove_player(network::connection player)
Leaving the room.
Definition: room.cpp:102
A room is a group of players that can communicate via messages.
Definition: room.hpp:31
void send_data(simple_wml::document &data, const network::connection exclude=0, std::string packet_type="") const
Convenience function for sending a wml document to all (or all except one) members.
Definition: room.cpp:115
bool add_player(network::connection player)
Joining the room.
Definition: room.cpp:91
void send_server_message(const std::string &message, network::connection sock, simple_wml::document *docptr=nullptr) const
Definition: room.hpp:162
const GLdouble * v
Definition: glew.h:1359
void send_server_message_to_all(const std::string &message, network::connection exclude=0) const
Definition: room.hpp:145
connection_vector members_
Definition: room.hpp:171
std::vector< network::connection > connection_vector
Definition: room.hpp:24
void set_persistent(bool v)
Set the persistent flag for this room.
Definition: room.cpp:66
std::string name_
Definition: room.hpp:170
void write(config &cfg) const
Write room info to a config.
Definition: room.cpp:48
void process_message(simple_wml::document &data, const player_map::iterator user)
Chat message processing.
Definition: room.cpp:147
GLuint const GLchar * name
Definition: glew.h:1782
bool logged_
Definition: room.hpp:174
bool find(E event, F functor)
Tests whether an event handler is available.
Definition: ban.cpp:28
bool persistent() const
Whether this room should be 'persistent', i.e.
Definition: room.cpp:61
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
int connection
Definition: network.hpp:74
room(const std::string &name)
Construct a room with just a name and default settings.
Definition: room.cpp:30
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 logged() const
Whether the room is logged (and might end up in e.g.
Definition: room.cpp:81
bool empty() const
Return true iif the room is empty.
Definition: room.hpp:94
void send_server_message_to_all(const char *message, network::connection exclude=0) const
Send a text message to all members.
Definition: room.cpp:122
void set_topic(const std::string &v)
Set the topic for this room.
Definition: room.cpp:76
GLsizei const GLcharARB ** string
Definition: glew.h:4503
const std::vector< network::connection > & members() const
Return the members of this room.
Definition: room.hpp:101