server/room_manager.hpp

Go to the documentation of this file.
00001 /* $Id: room_manager.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 #include "room.hpp"
00017 
00018 #include <boost/utility.hpp>
00019 
00020 class config;
00021 
00022 #ifndef SERVER_ROOM_MANAGER_HPP_INCLUDED
00023 #define SERVER_ROOM_MANAGER_HPP_INCLUDED
00024 
00025 namespace wesnothd {
00026 
00027 /**
00028  * The room manager manages the lobby and other rooms in the server, and related
00029  * client message processing.
00030  * The lobby represents players that are on the server, but not in any game.
00031  */
00032 class room_manager : private boost::noncopyable
00033 {
00034 public:
00035     /**
00036      * Room manager constructor
00037      */
00038     room_manager(player_map& all_players);
00039 
00040     /**
00041      * Room manager destructor
00042      */
00043     ~room_manager();
00044 
00045     enum PRIVILEGE_POLICY {
00046         PP_EVERYONE,
00047         PP_REGISTERED,
00048         PP_ADMINS,
00049         PP_NOBODY,
00050         PP_COUNT
00051     };
00052 
00053     static PRIVILEGE_POLICY pp_from_string(const std::string& str);
00054 
00055     /**
00056      * Load settings from the main config file
00057      */
00058     void load_config(const config& cfg);
00059 
00060     /**
00061      * Reads stored rooms from a file on disk, or returns immediately
00062      * if load_config was not called before or the storage filename is empty
00063      */
00064     void read_rooms();
00065 
00066     /**
00067      * Writes rooms to the storage file or returns immediately if load_config
00068      * was not called beforethe storage filename is empty
00069      */
00070     void write_rooms();
00071 
00072     /**
00073      * Dirty flag for rooms -- true if there were changes that should be written
00074      * to disk
00075      */
00076     bool dirty() const { return dirty_; }
00077 
00078     /**
00079      * Get a room by name, or NULL if it does not exist
00080      */
00081     room* get_room(const std::string& name);
00082 
00083     /**
00084      * @param  name the room name to check
00085      * @return true iif the room existst
00086      */
00087     bool room_exists(const std::string& name) const;
00088 
00089     /**
00090      * Create room named "name" if it does not exist already.
00091      */
00092     room* create_room(const std::string& name);
00093 
00094     /**
00095      * Get a room by name or create that room if it does not exist and
00096      * creating rooms is allowed.
00097      * @return a valid pointer to a room or NULL if the room did not exist and
00098      *         could not be created.
00099      */
00100     room* get_create_room(const std::string& name, network::connection player);
00101 
00102     /**
00103      * @return true iif the player is in the lobby
00104      */
00105     bool in_lobby(network::connection player) const;
00106 
00107     /**
00108      * Player-enters-lobby action. Will autorejoin "stored" rooms (the ones
00109      * the player was before enetering a game, for instance)
00110      */
00111     void enter_lobby(network::connection player);
00112 
00113     /**
00114      * All players from a game re-enter the lobby
00115      */
00116     void enter_lobby(const game& game);
00117 
00118     /**
00119      * Player exits lobby.
00120      */
00121     void exit_lobby(network::connection player);
00122 
00123     /**
00124      * Remove info abut given player from all rooms
00125      */
00126     void remove_player(network::connection player);
00127 
00128     /**
00129      * Check if the room exists, log failures.
00130      * @return non-NULL iff the room exists and the player is a member
00131      */
00132     room* require_room(const std::string& room_name,
00133         const player_map::iterator user, const char* log_string = "use");
00134 
00135     /**
00136      * Check if the room exists and if the player is a member, log failures.
00137      * @return non-NULL iff the room exists and the player is a member
00138      */
00139     room* require_member(const std::string& room_name,
00140         const player_map::iterator user, const char* log_string = "use");
00141 
00142     /**
00143      * Process a message (chat message) sent to a room. Check conditions
00144      * and resend to other players in the room.
00145      */
00146     void process_message(simple_wml::document& data, const player_map::iterator user);
00147 
00148     /**
00149      * Process a player's request to join a room
00150      */
00151     void process_room_join(simple_wml::document& data, const player_map::iterator user);
00152 
00153     /**
00154      * Process a player's request to leave a room
00155      */
00156     void process_room_part(simple_wml::document& data, const player_map::iterator user);
00157 
00158     /**
00159      * Process a general room query
00160      */
00161     void process_room_query(simple_wml::document& data, const player_map::iterator user);
00162 
00163     /**
00164      * Lobby convenience accesor
00165      */
00166     const room& lobby() const { return *lobby_; }
00167 
00168 private:
00169     void do_room_join(network::connection player, const std::string& room_name);
00170 
00171     /**
00172      * Adds a player to a room, maintaining internal consistency
00173      * Will send appropriate error messages to the player.
00174      * @return true iif the operation was successful, false otherwise
00175      */
00176     bool player_enters_room(network::connection player, room* room);
00177 
00178     /**
00179      * Removes a player from a room, maintaining internal consistency
00180      */
00181     void player_exits_room(network::connection player, room* room);
00182 
00183     /**
00184      * Stores the room names (other than lobby) of the given player for future
00185      * use (rejoin)
00186      */
00187     void store_player_rooms(network::connection player);
00188 
00189     /**
00190      * Unstores (rejoins) player's rooms that were previously stored.
00191      * No action if not stored earlier or no rooms.
00192      */
00193     void unstore_player_rooms(const player_map::iterator user);
00194 
00195     /**
00196      * Helper function that calls the player_map::iterator version
00197      * of unstore_player_rooms
00198      */
00199     void unstore_player_rooms(network::connection player);
00200 
00201     /**
00202      * Fill a wml node (message) with members of a room
00203      */
00204     void fill_member_list(const room* room, simple_wml::node& root);
00205 
00206     /**
00207      * Fill a wml node (message) with a room list
00208      */
00209     void fill_room_list(simple_wml::node& root);
00210 
00211     /** Reference to the all players map */
00212     player_map& all_players_;
00213 
00214     /** The lobby-room, treated separetely */
00215     room* lobby_;
00216 
00217     /** Rooms by name */
00218     typedef std::map<std::string, room*> t_rooms_by_name_;
00219     t_rooms_by_name_ rooms_by_name_;
00220 
00221     /** Rooms by player */
00222     typedef std::map<network::connection, std::set<room*> > t_rooms_by_player_;
00223     t_rooms_by_player_ rooms_by_player_;
00224 
00225     /** Room names stored for players that have entered a game */
00226     typedef std::map<network::connection, std::set<std::string> > t_player_stored_rooms_;
00227     t_player_stored_rooms_ player_stored_rooms_;
00228 
00229     /**
00230      * Persistent room storage filename.  If empty, rooms are not stored on disk.
00231      */
00232     std::string filename_;
00233 
00234     /**
00235      * Flag controlling whether to compress the stored rooms or not
00236      */
00237     bool compress_stored_rooms_;
00238 
00239     /**
00240      * Policy regarding who can create new rooms
00241      */
00242     PRIVILEGE_POLICY new_room_policy_;
00243 
00244     /**
00245      * 'Dirty' flag with regards to room info that will be stored on disk
00246      */
00247     bool dirty_;
00248 
00249     /**
00250      * The main (lobby) room name
00251      */
00252     static const char* const lobby_name_;
00253 };
00254 
00255 } //namespace wesnothd
00256 
00257 
00258 #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