00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef GAME_HPP_INCLUDED
00017 #define GAME_HPP_INCLUDED
00018
00019 #include "../network.hpp"
00020 #include "player.hpp"
00021
00022 #include "simple_wml.hpp"
00023
00024 #include <map>
00025 #include <vector>
00026
00027
00028
00029 namespace wesnothd {
00030
00031 typedef std::map<network::connection,player> player_map;
00032 typedef std::vector<network::connection> user_vector;
00033 typedef std::vector<network::connection> side_vector;
00034
00035 class game
00036 {
00037 public:
00038 game(player_map& players, const network::connection host=0,
00039 const std::string& name="", bool save_replays=false,
00040 const std::string& replay_save_path="");
00041 ~game();
00042
00043 int id() const { return id_; }
00044 const std::string& name() const { return name_; }
00045
00046 bool is_owner(const network::connection player) const { return (player == owner_); }
00047 bool is_member(const network::connection player) const
00048 { return is_player(player) || is_observer(player); }
00049 bool allow_observers() const;
00050 bool is_observer(const network::connection player) const;
00051 bool is_player(const network::connection player) const;
00052
00053
00054 bool player_is_banned(const network::connection player) const;
00055 bool level_init() const { return level_.child("side") != NULL; }
00056 bool started() const { return started_; }
00057
00058 size_t nplayers() const { return players_.size(); }
00059 size_t nobservers() const { return observers_.size(); }
00060 size_t current_turn() const { return (nsides_ ? end_turn_ / nsides_ + 1 : 0); }
00061
00062 void mute_all_observers();
00063
00064
00065
00066
00067
00068 void mute_observer(const simple_wml::node& mute, const player_map::const_iterator muter);
00069
00070 void unmute_observer(const simple_wml::node& unmute, const player_map::const_iterator unmuter);
00071
00072
00073
00074
00075
00076
00077
00078 network::connection kick_member(const simple_wml::node& kick, const player_map::const_iterator kicker);
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 network::connection ban_user(const simple_wml::node& ban, const player_map::const_iterator banner);
00089
00090 void unban_user(const simple_wml::node& unban, const player_map::const_iterator unbanner);
00091
00092
00093
00094
00095
00096
00097 bool add_player(const network::connection player, bool observer = false);
00098
00099
00100
00101
00102
00103
00104
00105
00106 bool remove_player(const network::connection player, const bool disconnect=false, const bool destruct=false);
00107
00108
00109 const user_vector all_game_users() const;
00110
00111 void start_game(const player_map::const_iterator starter);
00112
00113
00114 void load_next_scenario(const player_map::const_iterator user) const;
00115
00116
00117 void update_side_data();
00118
00119
00120 void transfer_side_control(const network::connection sock, const simple_wml::node& cfg);
00121
00122 void process_message(simple_wml::document& data, const player_map::iterator user);
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 bool process_turn(simple_wml::document& data, const player_map::const_iterator user);
00134
00135
00136 void process_whiteboard(simple_wml::document& data, const player_map::const_iterator user);
00137
00138
00139
00140
00141
00142
00143 bool describe_slots();
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 void send_server_message(const char* message, network::connection sock=0, simple_wml::document* doc=NULL) const;
00152 void send_server_message(const std::string& message, network::connection sock=0, simple_wml::document* doc=NULL) const
00153 {
00154 send_server_message(message.c_str(), sock, doc);
00155 }
00156
00157
00158 void send_and_record_server_message(const char* message, const network::connection exclude=0);
00159 void send_and_record_server_message(const std::string& message, const network::connection exclude=0)
00160 {
00161 send_and_record_server_message(message.c_str(), exclude);
00162 }
00163
00164 void send_data(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
00165
00166 void clear_history();
00167 void record_data(simple_wml::document* data);
00168 void save_replay();
00169
00170
00171 simple_wml::document& level() { return level_; }
00172
00173
00174
00175
00176
00177 void set_description(simple_wml::node* desc);
00178 simple_wml::node* description() const { return description_; }
00179
00180 void set_password(const std::string& passwd) { password_ = passwd; }
00181 bool password_matches(const std::string& passwd) const {
00182 return password_.empty() || passwd == password_;
00183 }
00184
00185 const std::string& termination_reason() const {
00186 static const std::string aborted = "aborted";
00187 static const std::string not_started = "not started";
00188 return started_ ? (termination_.empty() ? aborted : termination_) : not_started;
00189 }
00190
00191 void set_termination_reason(const std::string& reason);
00192
00193 void allow_global(const simple_wml::document &data);
00194
00195 private:
00196
00197 game(const game&);
00198 void operator=(const game&);
00199
00200 size_t current_side() const { return (nsides_ ? end_turn_ % nsides_ : 0); }
00201 network::connection current_player() const
00202 { return (nsides_ ? sides_[current_side()] : 0); }
00203 bool is_current_player(const network::connection player) const
00204 { return (current_player() == player); }
00205 bool is_muted_observer(const network::connection player) const;
00206 bool all_observers_muted() const { return all_observers_muted_; }
00207 void send_muted_observers(const player_map::const_iterator user) const;
00208
00209 bool send_taken_side(simple_wml::document& cfg, const simple_wml::node::child_list::const_iterator side) const;
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 bool take_side(const player_map::const_iterator user);
00220
00221
00222
00223
00224
00225 void change_controller(const size_t side_num,
00226 const network::connection sock,
00227 const std::string& player_name,
00228 const bool player_left = true,
00229 const std::string& controller = "");
00230 void transfer_ai_sides(const network::connection player);
00231 void send_leave_game(network::connection user) const;
00232 void send_data_team(simple_wml::document& data, const simple_wml::string_span& team,
00233 const network::connection exclude=0, std::string packet_type = "") const;
00234 void send_data_observers(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
00235
00236
00237
00238
00239
00240 void send_observerjoins(const network::connection sock=0) const;
00241 void send_observerquit(const player_map::const_iterator observer) const;
00242 void send_history(const network::connection sock) const;
00243
00244
00245 void notify_new_host();
00246
00247
00248 player_map::iterator find_user(const simple_wml::string_span& name);
00249
00250 bool observers_can_label() const { return false; }
00251 bool observers_can_chat() const { return true; }
00252 bool is_legal_command(const simple_wml::node& command, bool is_player);
00253
00254
00255
00256
00257
00258 bool is_on_team(const simple_wml::string_span& team, const network::connection player) const;
00259
00260
00261
00262
00263
00264 std::string has_same_ip(const network::connection& user, bool observer) const;
00265
00266
00267
00268
00269
00270
00271
00272 bool end_turn();
00273
00274
00275
00276
00277
00278
00279 void send_user_list(const network::connection exclude=0) const;
00280
00281
00282 std::string username(const player_map::const_iterator pl) const;
00283
00284
00285 std::string list_users(user_vector users, const std::string& func) const;
00286
00287
00288 void missing_user(network::connection socket, const std::string& func) const;
00289
00290
00291 std::string debug_player_info() const;
00292
00293 player_map* player_info_;
00294
00295 static int id_num;
00296 int id_;
00297
00298
00299 std::string name_;
00300 std::string password_;
00301
00302
00303 network::connection owner_;
00304
00305
00306 user_vector players_;
00307
00308
00309 user_vector observers_;
00310 user_vector muted_observers_;
00311
00312
00313 side_vector sides_;
00314
00315
00316
00317
00318
00319
00320
00321 std::vector<std::string> side_controllers_;
00322
00323
00324 int nsides_;
00325 bool started_;
00326
00327
00328 simple_wml::document level_;
00329
00330
00331 mutable std::vector<simple_wml::document*> history_;
00332
00333
00334 simple_wml::node* description_;
00335
00336 int end_turn_;
00337
00338 bool all_observers_muted_;
00339
00340 std::vector<std::string> bans_;
00341
00342 std::string termination_;
00343
00344 bool save_replays_;
00345 std::string replay_save_path_;
00346
00347
00348 int global_wait_side_;
00349 };
00350
00351 struct game_is_member {
00352 game_is_member(network::connection sock) : sock_(sock) {};
00353 bool operator()(const game* g) const { return g->is_owner(sock_) || g->is_member(sock_); }
00354
00355 private:
00356 network::connection sock_;
00357 };
00358
00359 struct game_id_matches {
00360 game_id_matches(int id) : id_(id) {};
00361 bool operator()(const game* g) const { return g->id() == id_; }
00362
00363 private:
00364 int id_;
00365 };
00366 }
00367 #endif
00368