The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
room.cpp
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 "config.hpp"
16 #include "game.hpp"
17 #include "player_network.hpp"
18 #include "room.hpp"
19 #include "log.hpp"
21 #include "util.hpp"
22 
23 static lg::log_domain log_server("server");
24 #define ERR_ROOM LOG_STREAM(err, log_server)
25 #define LOG_ROOM LOG_STREAM(info, log_server)
26 #define DBG_ROOM LOG_STREAM(debug, log_server)
27 
28 namespace wesnothd {
29 
31  : name_(name)
32  , members_()
33  , persistent_(false)
34  , topic_()
35  , logged_(false)
36 {
37 }
38 
39 room::room(const config& wml)
40  : name_(wml["name"])
41  , members_()
42  , persistent_(wml["persistent"].to_bool())
43  , topic_(wml["topic"])
44  , logged_(wml["logged"].to_bool())
45 {
46 }
47 
48 void room::write(config& cfg) const
49 {
50  cfg["name"] = name_;
51  cfg["persistent"] = persistent_;
52  cfg["topic"] = topic_;
53  cfg["logged"] = logged_;
54 }
55 
56 const std::string& room::name() const
57 {
58  return name_;
59 }
60 
61 bool room::persistent() const
62 {
63  return persistent_;
64 }
65 
67 {
68  persistent_ = v;
69 }
70 
71 const std::string& room::topic() const
72 {
73  return topic_;
74 }
75 
77 {
78  topic_ = v;
79 }
80 
81 bool room::logged() const
82 {
83  return logged_;
84 }
85 
86 void room::set_logged(bool v)
87 {
88  logged_ = v;
89 }
90 
92 {
93  if (is_member(player)) {
94  ERR_ROOM << "ERROR: Player is already in this room. (socket: "
95  << player << ")\n";
96  return false;
97  }
98  members_.push_back(player);
99  return true;
100 }
101 
103 {
105  std::find(members_.begin(), members_.end(), player);
106  if (itor != members_.end()) {
107  members_.erase(itor);
108  } else {
109  ERR_ROOM << "ERROR: Player is not in this room. (socket: "
110  << player << ")\n";
111  }
112 }
113 
114 
116  const network::connection exclude,
117  std::string packet_type) const
118 {
119  wesnothd::send_to_many(data, members(), exclude, packet_type);
120 }
121 
123 {
125  send_server_message(message, 0, &doc);
126  send_data(doc, exclude, "message");
127 }
128 
130  simple_wml::document* docptr) const
131 {
132  simple_wml::document docbuf;
133  if(docptr == nullptr) {
134  docptr = &docbuf;
135  }
136  simple_wml::document& doc = *docptr;
137 
138  simple_wml::node& msg = doc.root().add_child("message");
139  msg.set_attr("sender", "server");
140  msg.set_attr_dup("message", message);
141 
142  if(sock) {
143  send_to_one(doc, sock, "message");
144  }
145 }
146 
148  const player_map::iterator /*user*/)
149 {
150 }
151 
152 
153 
154 } //end namespace wesnothd
node & add_child(const char *name)
Definition: simple_wml.cpp:465
static lg::log_domain log_server("server")
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
node & set_attr(const char *key, const char *value)
Definition: simple_wml.cpp:411
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
bool persistent_
Definition: room.hpp:172
Definitions for the interface to Wesnoth Markup Language (WML).
void remove_player(network::connection player)
Leaving the room.
Definition: room.cpp:102
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
const GLdouble * v
Definition: glew.h:1359
connection_vector members_
Definition: room.hpp:171
#define ERR_ROOM
Definition: room.cpp:24
Templates and utility-routines for strings and numbers.
void set_persistent(bool v)
Set the persistent flag for this room.
Definition: room.cpp:66
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
std::string name_
Definition: room.hpp:170
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
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
-file actions.hpp
bool logged_
Definition: room.hpp:174
bool find(E event, F functor)
Tests whether an event handler is available.
Definition: ban.cpp:28
Standard logging facilities (interface).
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
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