server/room.cpp

Go to the documentation of this file.
00001 /* $Id: room.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Tomasz Sniatowski <kailoran@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #include "config.hpp"
00017 #include "game.hpp"
00018 #include "player_network.hpp"
00019 #include "room.hpp"
00020 #include "../log.hpp"
00021 #include "serialization/string_utils.hpp"
00022 #include "util.hpp"
00023 
00024 static lg::log_domain log_server("server");
00025 #define ERR_ROOM LOG_STREAM(err, log_server)
00026 #define LOG_ROOM LOG_STREAM(info, log_server)
00027 #define DBG_ROOM LOG_STREAM(debug, log_server)
00028 
00029 namespace wesnothd {
00030 
00031 room::room(const std::string& name)
00032     : name_(name)
00033     , members_()
00034     , persistent_(false)
00035     , topic_()
00036     , logged_(false)
00037 {
00038 }
00039 
00040 room::room(const config& wml)
00041     : name_(wml["name"])
00042     , members_()
00043     , persistent_(wml["persistent"].to_bool())
00044     , topic_(wml["topic"])
00045     , logged_(wml["logged"].to_bool())
00046 {
00047 }
00048 
00049 void room::write(config& cfg) const
00050 {
00051     cfg["name"] = name_;
00052     cfg["persistent"] = persistent_;
00053     cfg["topic"] = topic_;
00054     cfg["logged"] = logged_;
00055 }
00056 
00057 const std::string& room::name() const
00058 {
00059     return name_;
00060 }
00061 
00062 bool room::persistent() const
00063 {
00064     return persistent_;
00065 }
00066 
00067 void room::set_persistent(bool v)
00068 {
00069     persistent_ = v;
00070 }
00071 
00072 const std::string& room::topic() const
00073 {
00074     return topic_;
00075 }
00076 
00077 void room::set_topic(const std::string& v)
00078 {
00079     topic_ = v;
00080 }
00081 
00082 bool room::logged() const
00083 {
00084     return logged_;
00085 }
00086 
00087 void room::set_logged(bool v)
00088 {
00089     logged_ = v;
00090 }
00091 
00092 bool room::add_player(network::connection player)
00093 {
00094     if (is_member(player)) {
00095         ERR_ROOM << "ERROR: Player is already in this room. (socket: "
00096             << player << ")\n";
00097         return false;
00098     }
00099     members_.push_back(player);
00100     return true;
00101 }
00102 
00103 void room::remove_player(network::connection player)
00104 {
00105     const user_vector::iterator itor =
00106         std::find(members_.begin(), members_.end(), player);
00107     if (itor != members_.end()) {
00108         members_.erase(itor);
00109     } else {
00110         ERR_ROOM << "ERROR: Player is not in this room. (socket: "
00111             << player << ")\n";
00112     }
00113 }
00114 
00115 
00116 void room::send_data(simple_wml::document& data,
00117                      const network::connection exclude,
00118                      std::string packet_type) const
00119 {
00120     wesnothd::send_to_many(data, members(), exclude, packet_type);
00121 }
00122 
00123 void room::send_server_message_to_all(const char* message, network::connection exclude) const
00124 {
00125     simple_wml::document doc;
00126     send_server_message(message, 0, &doc);
00127     send_data(doc, exclude, "message");
00128 }
00129 
00130 void room::send_server_message(const char* message, network::connection sock,
00131                            simple_wml::document* docptr) const
00132 {
00133     simple_wml::document docbuf;
00134     if(docptr == NULL) {
00135         docptr = &docbuf;
00136     }
00137     simple_wml::document& doc = *docptr;
00138 
00139     simple_wml::node& msg = doc.root().add_child("message");
00140     msg.set_attr("sender", "server");
00141     msg.set_attr_dup("message", message);
00142 
00143     if(sock) {
00144         send_to_one(doc, sock, "message");
00145     }
00146 }
00147 
00148 void room::process_message(simple_wml::document& /*data*/,
00149                            const player_map::iterator /*user*/)
00150 {
00151 }
00152 
00153 
00154 
00155 } //end namespace wesnothd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:10 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs