server/room.hpp

Go to the documentation of this file.
00001 /* $Id: room.hpp 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 #ifndef SERVER_ROOM_HPP_INCLUDED
00017 #define SERVER_ROOM_HPP_INCLUDED
00018 
00019 #include "../network.hpp"
00020 #include "player.hpp"
00021 #include "simple_wml.hpp"
00022 
00023 namespace wesnothd {
00024 
00025 typedef std::vector<network::connection> connection_vector;
00026 typedef std::map<network::connection,player> player_map;
00027 class game;
00028 
00029 /**
00030  * A room is a group of players that can communicate via messages.
00031  */
00032 class room {
00033 public:
00034     /**
00035      * Construct a room with just a name and default settings
00036      */
00037     room(const std::string& name);
00038 
00039     /**
00040      * Construct a room from WML
00041      */
00042     room(const config& cfg);
00043 
00044     /**
00045      * Write room info to a config
00046      */
00047     void write(config& cfg) const;
00048 
00049     /**
00050      * The name of this room
00051      */
00052     const std::string& name() const;
00053 
00054     /**
00055      * Whether this room should be 'persistent', i.e. not deleted when there
00056      * are no players within and stored on disk if needed.
00057      */
00058     bool persistent() const;
00059 
00060     /**
00061      * Set the persistent flag for this room
00062      */
00063     void set_persistent(bool v);
00064 
00065     /**
00066      * Whether the room is logged (and might end up in e.g. the lobby bot
00067      */
00068     bool logged() const;
00069 
00070     /**
00071     * Set the room's logged flag
00072     */
00073     void set_logged(bool v);
00074 
00075     /**
00076      * This room's topic/motd, sent to all joining players
00077      */
00078     const std::string& topic() const;
00079 
00080     /**
00081      * Set the topic for this room
00082      */
00083     void set_topic(const std::string& v);
00084 
00085     /**
00086      * Return the number of players in this room
00087      */
00088     size_t size() const {
00089         return members_.size();
00090     }
00091 
00092     /**
00093      * Return true iif the room is empty
00094      */
00095     bool empty() const {
00096         return members_.empty();
00097     }
00098 
00099     /**
00100      * Return the members of this room
00101      */
00102     const std::vector<network::connection>& members() const {
00103         return members_;
00104     }
00105 
00106     /**
00107      * Membership checker.
00108      * @return true iif player is a member of this room
00109      */
00110     bool is_member(network::connection player) const {
00111         return std::find(members_.begin(), members_.end(), player) != members_.end();
00112     }
00113 
00114     /**
00115      * Joining the room
00116      * @return true if the player was successfully added
00117      */
00118     bool add_player(network::connection player);
00119 
00120     /**
00121      * Leaving the room
00122      */
00123     void remove_player(network::connection player);
00124 
00125     /**
00126      * Chat message processing
00127      */
00128     void process_message(simple_wml::document& data, const player_map::iterator user);
00129 
00130     /**
00131      * Convenience function for sending a wml document to all (or all except
00132      * one) members.
00133      * @see send_to_many
00134      * @param data        the document to send
00135      * @param exclude     if nonzero, do not send to this player
00136      * @param packet_type the packet type, if empty the root node name is used
00137      */
00138     void send_data(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
00139 
00140     /**
00141      * Send a text message to all members
00142      * @param message the message text
00143      * @param exclude if nonzero, do not send to this player
00144      */
00145     void send_server_message_to_all(const char* message, network::connection exclude=0) const;
00146     void send_server_message_to_all(const std::string& message, network::connection exclude=0) const
00147     {
00148         send_server_message_to_all(message.c_str(), exclude);
00149     }
00150 
00151     /**
00152      * Prepare a text message and/or send it to a player. If a nonzero sock
00153      * is passed, the message is sent to this player. If a non-null pointer
00154      * to a simple_wml::document is passed, the message is stored in that
00155      * document.
00156      * @param message the message text
00157      * @param sock    the socket to send the message to, if nonzero
00158      * @param docptr  the wml document to store the message in, if nonnull
00159      */
00160     void send_server_message(const char* message, network::connection sock,
00161         simple_wml::document* docptr = NULL) const;
00162 
00163     void send_server_message(const std::string& message, network::connection sock,
00164         simple_wml::document* docptr = NULL) const
00165     {
00166         send_server_message(message.c_str(), sock, docptr);
00167     }
00168 
00169 
00170 private:
00171     std::string name_;
00172     connection_vector members_;
00173     bool persistent_;
00174     std::string topic_;
00175     bool logged_;
00176 };
00177 
00178 } //end namespace wesnothd
00179 
00180 #endif
 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