The Battle for Wesnoth  1.15.1+dev
game.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 "server/game.hpp"
16 
17 #include "filesystem.hpp"
18 #include "game_config.hpp" // game_config::observer_team_name
19 #include "lexical_cast.hpp"
20 #include "log.hpp"
24 #include "server/server.hpp"
25 
26 #include <cstdio>
27 #include <iomanip>
28 #include <sstream>
29 
30 static lg::log_domain log_server("server");
31 #define ERR_GAME LOG_STREAM(err, log_server)
32 #define WRN_GAME LOG_STREAM(warn, log_server)
33 #define LOG_GAME LOG_STREAM(info, log_server)
34 #define DBG_GAME LOG_STREAM(debug, log_server)
35 
36 static lg::log_domain log_config("config");
37 #define WRN_CONFIG LOG_STREAM(warn, log_config)
38 
39 namespace
40 {
41 struct split_conv_impl
42 {
43  void operator()(std::vector<int>& res, const simple_wml::string_span& span)
44  {
45  if(!span.empty()) {
46  res.push_back(span.to_int());
47  }
48  }
49 };
50 
51 template<typename TResult, typename TConvert>
52 std::vector<TResult> split(const simple_wml::string_span& val, TConvert conv, const char c = ',')
53 {
54  std::vector<TResult> res;
55 
58 
59  while(i2 != val.end()) {
60  if(*i2 == c) {
61  conv(res, simple_wml::string_span(i1, i2));
62  ++i2;
63  i1 = i2;
64  } else {
65  ++i2;
66  }
67  }
68 
69  conv(res, simple_wml::string_span(i1, i2));
70  return res;
71 }
72 }
73 
74 namespace wesnothd
75 {
76 template<typename Container>
77 void send_to_players(simple_wml::document& data, const Container& players, socket_ptr exclude = socket_ptr())
78 {
79  for(const auto& player : players) {
80  if(player != exclude) {
81  send_to_player(player, data);
82  }
83  }
84 }
85 
86 int game::id_num = 1;
87 
88 void game::missing_user(socket_ptr /*socket*/, const std::string& func) const
89 {
90  WRN_GAME << func << "(): Could not find user (socket:\t<some C++ pointer>"
91  << ") in player_info_ in game:\t\"" << name_ << "\" (" << id_ << ")\n";
92 }
93 
95  const socket_ptr& host,
96  const std::string& name,
97  bool save_replays,
98  const std::string& replay_save_path)
99  : player_connections_(player_connections)
100  , id_(id_num++)
101  , name_(name)
102  , password_()
103  , owner_(host)
104  , players_()
105  , observers_()
106  , muted_observers_()
107  , sides_()
108  , side_controllers_()
109  , nsides_(0)
110  , started_(false)
111  , level_()
112  , history_()
113  , description_(nullptr)
114  , current_turn_(0)
115  , current_side_index_(0)
116  , num_turns_(0)
117  , all_observers_muted_(false)
118  , bans_()
119  , name_bans_()
120  , players_not_advanced_()
121  , termination_()
122  , save_replays_(save_replays)
123  , replay_save_path_(replay_save_path)
124  , rng_()
125  , last_choice_request_id_(-1) /* or maybe 0 ? it shouldn't matter*/
126 {
127  assert(owner_);
128  players_.push_back(owner_);
129 
130  const auto iter = player_connections_.find(owner_);
131  if(iter == player_connections_.end()) {
132  missing_user(owner_, __func__);
133  return;
134  }
135 
136  // Mark the host as unavailable in the lobby.
137  iter->info().mark_available(id_, name_);
138  iter->info().set_status(player::PLAYING);
139 }
140 
142 {
143  try {
144  save_replay();
145 
146  for(const socket_ptr& user_ptr : all_game_users()) {
147  remove_player(user_ptr, false, true);
148  }
149 
150  clear_history();
151  } catch(...) {
152  }
153 }
154 
155 /// returns const so that operator [] won't create empty keys if not existent
157 {
158  if(const simple_wml::node* multiplayer = root.child("multiplayer")) {
159  return *multiplayer;
160  } else {
161  ERR_GAME << "no [multiplayer] found. Returning root\n";
162  return root;
163  }
164 }
165 
167 {
168  return get_multiplayer(level_.root())["observer"].to_bool(true);
169 }
170 
172 {
173  return get_multiplayer(level_.root())["registered_users_only"].to_bool(true);
174 }
175 
177 {
178  return std::find(observers_.begin(), observers_.end(), player) != observers_.end();
179 }
180 
182 {
183  if(!is_observer(player)) {
184  return false;
185  }
186 
188  return true;
189  }
190 
191  return std::find(muted_observers_.begin(), muted_observers_.end(), player) != muted_observers_.end();
192 }
193 
195 {
196  return std::find(players_.begin(), players_.end(), player) != players_.end();
197 }
198 
199 std::string game::username(const socket_ptr& player) const
200 {
201  const auto iter = player_connections_.find(player);
202  if(iter != player_connections_.end()) {
203  return iter->info().name();
204  }
205 
206  return "(unknown)";
207 }
208 
209 std::string game::list_users(user_vector users, const std::string& func) const
210 {
211  std::string list;
212 
213  for(const user_vector::value_type& user : users) {
214  const auto iter = player_connections_.find(user);
215 
216  if(iter != player_connections_.end()) {
217  if(!list.empty()) {
218  list += ", ";
219  }
220 
221  list += iter->info().name();
222  } else {
223  missing_user(user, func);
224  }
225  }
226 
227  return list;
228 }
229 
231 {
233 
234  DBG_GAME << "****\n Performing controller tweaks. sides = " << std::endl;
235  DBG_GAME << debug_sides_info() << std::endl;
236  DBG_GAME << "****" << std::endl;
237 
238  update_side_data(); // Necessary to read the level_ and get sides_, etc. updated to match
239 
240  for(unsigned side_index = 0; side_index < sides.size(); ++side_index) {
241  simple_wml::node& side = *sides[side_index];
242 
243  if(side["controller"] != "null") {
244  if(sides_[side_index] == 0) {
245  sides_[side_index] = owner_;
246  std::stringstream msg;
247  msg << "Side " << side_index + 1
248  << " had no controller during controller tweaks! The host was assigned control.";
249 
250  LOG_GAME << msg.str() << " (game id: " << id_ << ")\n";
252  }
253 
254  const auto user = player_connections_.find(sides_[side_index]);
255  std::string user_name = "null (server missing user)";
256  if(user == player_connections_.end()) {
257  missing_user(user->socket(), __func__);
258  } else {
259  user_name = username(user->socket());
260  }
261 
262  // Issue change_controller command, transferring this side to its owner with proper name and controller.
263  // Ensures that what the server now thinks is true is effected on all of the clients.
264  //
265  // We use the "player_left" field as follows. Normally change_controller sends one message to the owner,
266  // and one message to everyone else. In case that a player drops, the owner is gone and should not get
267  // a message, instead the host gets a [side_drop] message.
268  //
269  // In the server controller tweaks, we want to avoid sending controller change messages to the host.
270  // Doing this has the negative consequence that all of the AI side names are given the owners name.
271  // Therefore, if the side belongs to the host, we pass player_left = true, otherwise player_left = false.
272  change_controller(side_index, sides_[side_index], user_name, sides_[side_index] == owner_);
273 
274  // next line change controller types found in level_ to be what is appropriate for an observer at game
275  // start.
276  side.set_attr("is_local", "no");
277 
278  if(sides_[side_index] == 0) {
279  std::stringstream msg;
280  msg << "Side " << side_index + 1 << " had no controller AFTER controller tweaks! Ruh Roh!";
281  LOG_GAME << msg.str() << " (game id: " << id_ << ")\n";
282  }
283  }
284  }
285 
286  // This is the last time that update_side_data will actually run, as now the game will start and
287  // started_ will be true.
289 
290  // TODO: Does it matter that the server is telling the host to change a bunch of sides?
291  // According to playturn.cpp, the host should ignore all such messages. Still might be better
292  // not to send them at all, although not if it complicates the server code.
293 }
294 
295 void game::start_game(const socket_ptr& starter)
296 {
298  DBG_GAME << "****\n Starting game. sides = " << std::endl;
299  DBG_GAME << debug_sides_info() << std::endl;
300  DBG_GAME << "****" << std::endl;
301 
302  // If the game was already started we're actually advancing.
303  const bool advance = started_;
304  started_ = true;
305  // Prevent inserting empty keys when reading.
306  const simple_wml::node& multiplayer = get_multiplayer(level_.root());
307 
308  const bool save = multiplayer["savegame"].to_bool();
309  LOG_GAME
310  << client_address(starter) << "\t" << player_connections_.find(starter)->name() << "\t"
311  << (advance ? "advanced" : "started") << (save ? " reloaded" : "") << " game:\t\"" << name_ << "\" (" << id_
312  // << ") with: " << list_users(players_, __func__) << ". Settings: map: " << s["id"]
313  << ") with: " << list_users(players_, __func__)
314  << ". Settings: map: " << multiplayer["mp_scenario"]
315  // << "\tera: " << (s.child("era") ? (*s.child("era"))["id"] : "")
316  << "\tera: " << multiplayer["mp_era"]
317  << "\tXP: " << multiplayer["experience_modifier"]
318  << "\tGPV: " << multiplayer["mp_village_gold"]
319  << "\tfog: " << multiplayer["mp_fog"]
320  << "\tshroud: " << multiplayer["mp_shroud"]
321  << "\tobservers: " << multiplayer["observer"]
322  << "\tshuffle: " << multiplayer["shuffle_sides"]
323  << "\ttimer: " << multiplayer["mp_countdown"]
324  << (multiplayer["mp_countdown"].to_bool()
325  ? "\treservoir time: " + multiplayer["mp_countdown_reservoir_time"].to_string()
326  + "\tinit time: " + multiplayer["mp_countdown_init_time"].to_string()
327  + "\taction bonus: " + multiplayer["mp_countdown_action_bonus"].to_string()
328  + "\tturn bonus: " + multiplayer["mp_countdown_turn_bonus"].to_string()
329  : "")
330  << "\n";
331 
332 
333  for(unsigned side_index = 0; side_index < sides.size(); ++side_index) {
334  simple_wml::node& side = *sides[side_index];
335 
336  if(side["controller"] != "null") {
337  if(side_index >= sides_.size()) {
338  continue;
339  }
340 
341  if(sides_[side_index] == 0) {
342  std::stringstream msg;
343  msg << "Side " << side_index + 1
344  << " has no controller but should! The host needs to assign control for the game to proceed past "
345  "that side's turn.";
346 
347  LOG_GAME << msg.str() << " (game id: " << id_ << ")\n";
349  }
350  }
351  }
352 
353  DBG_GAME << "Number of sides: " << nsides_ << "\n";
354  int turn = 1;
355  int side = 0;
356 
357  // Savegames have a snapshot that tells us which side starts.
358  if(const simple_wml::node* snapshot = level_.root().child("snapshot")) {
359  turn = lexical_cast_default<int>((*snapshot)["turn_at"], 1);
360  side = lexical_cast_default<int>((*snapshot)["playing_team"], 0);
361  LOG_GAME << "Reload from turn: " << turn << ". Current side is: " << side + 1 << ".\n";
362  }
363  current_turn_ = turn;
364  current_side_index_ = side;
365  num_turns_ = lexical_cast_default<int>((*starting_pos(level_.root()))["turns"], -1);
366 
368  clear_history();
369 
370  // Send [observer] tags for all observers that are already in the game.
372 }
373 
375 {
376  started_ = false;
377 
379  describe_slots();
380 }
381 
383 {
384  const std::size_t side_index = (*side)["side"].to_int() - 1;
385 
386  // Negative values are casted (int -> std::size_t) to very high values to this check will fail for them too.
387  if(side_index >= sides_.size()) {
388  return false;
389  }
390 
391  if(sides_[side_index] != 0) {
392  return false;
393  }
394 
395  // We expect that the host will really use our proposed side number. (He could do different...)
396  cfg.root().set_attr_dup("side", (*side)["side"]);
397 
398  // Tell the host which side the new player should take.
399  send_to_player(owner_, cfg);
400  return true;
401 }
402 
403 bool game::take_side(const socket_ptr& user)
404 {
405  DBG_GAME << "take_side...\n";
406 
407  if(started_) {
408  return false;
409  }
410 
412  cfg.root().set_attr_dup("name", player_connections_.find(user)->name().c_str());
413 
414  // FIXME: The client code (multiplayer.wait.cpp) the host code (connect_engine.cpp) and the server code
415  // (this file) has this code to figure out a fitting side for new players, this is clearly too much.
416  // Check if we can figure out a fitting side.
418 
419  for(const simple_wml::node* side : sides) {
420  if(((*side)["controller"] == "human" || (*side)["controller"] == "reserved")
421  && (*side)["current_player"] == player_connections_.find(user)->name().c_str()) {
422 
423  if(send_taken_side(cfg, side)) {
424  return true;
425  }
426  }
427  }
428 
429  // If there was no fitting side just take the first available.
430  for(const simple_wml::node* side : sides) {
431  if((*side)["controller"] == "human") {
432  if(send_taken_side(cfg, side)) {
433  return true;
434  }
435  }
436  }
437 
438  DBG_GAME << "take_side: there are no more sides available\n";
439 
440  // If we get here we couldn't find a side to take
441  return false;
442 }
443 
445 {
446  side_controllers_.clear();
447  sides_.clear();
448 
449  nsides_ = get_sides_list().size();
450 
451  side_controllers_.resize(nsides_);
452  sides_.resize(nsides_);
453 }
454 
456 {
457  // Added by iceiceice: since level_ will now reflect how an observer views the replay start
458  // position and not the current position, the sides_, side_controllers_, players_ info should
459  // not be updated from the level_ after the game has started. Controller changes are now stored
460  // in the history, so an observer that joins will get up to date that way.
461  if(started_) {
462  return;
463  }
464 
465  DBG_GAME << "update_side_data...\n";
467 
468  // Remember everyone that is in the game.
469  const user_vector users = all_game_users();
470 
471  players_.clear();
472  observers_.clear();
473 
474  reset_sides();
475 
476  const simple_wml::node::child_list& level_sides = get_sides_list();
477 
478  // For each user:
479  // * Find the username.
480  // * Find the side this username corresponds to.
481  for(const socket_ptr& user : users) {
482  auto iter = player_connections_.find(user);
483  if(iter == player_connections_.end()) {
484  missing_user(user, __func__);
485  continue;
486  }
487 
488  bool side_found = false;
489  for(unsigned side_index = 0; side_index < level_sides.size(); ++side_index) {
490  const simple_wml::node* side = level_sides[side_index];
491 
492  if(side_index >= sides_.size() || sides_[side_index] != 0) {
493  continue;
494  }
495 
496  const simple_wml::string_span& player_id = (*side)["player_id"];
497  const simple_wml::string_span& controller = (*side)["controller"];
498 
499  if(player_id == iter->info().name().c_str()) {
500  // We found invalid [side] data. Some message would be cool.
501  if(controller != "human" && controller != "ai") {
502  continue;
503  }
504 
505  side_controllers_[side_index].parse(controller);
506  sides_[side_index] = user;
507  side_found = true;
508  } else if(user == owner_ && (controller == "null")) {
509  // the *user == owner_ check has no effect,
510  // it's just an optimisation so that we only do this once.
511  side_controllers_[side_index].parse(controller);
512  }
513  }
514 
515  if(side_found) {
516  players_.push_back(user);
517  iter->info().set_status(player::PLAYING);
518  } else {
519  observers_.push_back(user);
520  iter->info().set_status(player::OBSERVING);
521  }
522  }
523 
525 }
526 
528 {
529  DBG_GAME << "transfer_side_control...\n";
530 
531  if(!is_player(sock) && sock != owner_) {
532  send_server_message("You cannot change controllers: not a player.", sock);
533  return;
534  }
535 
536  // Check the side number.
537  const unsigned int side_num = cfg["side"].to_int();
538  if(side_num < 1 || side_num > sides_.size()) {
539  std::ostringstream msg;
540  msg << "The side number has to be between 1 and " << sides_.size() << ".";
541  send_server_message(msg.str(), sock);
542  return;
543  }
544 
545  if(side_num > get_sides_list().size()) {
546  send_server_message("Invalid side number.", sock);
547  return;
548  }
549 
550  const simple_wml::string_span& newplayer_name = cfg["player"];
551  const socket_ptr old_player = sides_[side_num - 1];
552  const auto oldplayer = player_connections_.find(old_player);
553  if(oldplayer == player_connections_.end()) {
554  missing_user(old_player, __func__);
555  }
556 
557  const std::string old_player_name = username(old_player);
558 
559  // Not supported anymore.
560  if(newplayer_name.empty()) {
561  std::stringstream msg;
562  msg << "Received invalid [change_controller] with no player= attribute specified";
563  DBG_GAME << msg.str() << "\n";
564  send_server_message(msg.str(), sock);
565  return;
566  }
567 
568  // Check if the sender actually owns the side he gives away or is the host.
569  if(!(sock == old_player || sock == owner_)) {
570  std::stringstream msg;
571  msg << "You can't give away side " << side_num << ". It's controlled by '" << old_player_name << "' not you.";
572  DBG_GAME << msg.str() << "\n";
573  send_server_message(msg.str(), sock);
574  return;
575  }
576 
577  // find the player that is passed control
578  socket_ptr newplayer = find_user(newplayer_name);
579 
580  // Is he in this game?
581  if(player_connections_.find(newplayer) == player_connections_.end() || !is_member(newplayer)) {
582  send_server_message(newplayer_name.to_string() + " is not in this game", sock);
583  return;
584  }
585 
586  if(newplayer == old_player) {
587  std::stringstream msg;
588  msg << "That's already " << newplayer_name << "'s side, silly.";
589  send_server_message(msg.str(), sock);
590  return;
591  }
592 
593  sides_[side_num - 1].reset();
594 
595  // If the old player lost his last side, make him an observer.
596  if(std::find(sides_.begin(), sides_.end(), old_player) == sides_.end() && is_player(old_player)) {
597  observers_.push_back(old_player);
598 
599  player_connections_.find(old_player)->info().set_status(player::OBSERVING);
600  players_.erase(std::remove(players_.begin(), players_.end(), old_player), players_.end());
601 
602  // Tell others that the player becomes an observer.
603  send_and_record_server_message(old_player_name + " becomes an observer.");
604 
605  // Update the client side observer list for everyone except old player.
606  simple_wml::document observer_join;
607  observer_join.root().add_child("observer").set_attr_dup("name", old_player_name.c_str());
608  send_data(observer_join, old_player);
609  }
610 
611  change_controller(side_num - 1, newplayer, player_connections_.find(newplayer)->info().name(), false);
612 
613  // If we gave the new side to an observer add him to players_.
614  if(is_observer(newplayer)) {
615  players_.push_back(newplayer);
616  player_connections_.find(newplayer)->info().set_status(player::PLAYING);
617  observers_.erase(std::remove(observers_.begin(), observers_.end(), newplayer), observers_.end());
618  // Send everyone but the new player the observer_quit message.
619  send_observerquit(newplayer);
620  }
621 }
622 
624  const std::size_t side_index, const socket_ptr& sock, const std::string& player_name, const bool player_left)
625 {
626  DBG_GAME << __func__ << "...\n";
627 
628  const std::string& side = lexical_cast_default<std::string, std::size_t>(side_index + 1);
629  sides_[side_index] = sock;
630 
631  if(player_left && side_controllers_[side_index] == CONTROLLER::AI) {
632  // Automatic AI side transfer.
633  } else {
634  if(started_) {
635  send_and_record_server_message(player_name + " takes control of side " + side + ".");
636  }
637  }
638 
639  simple_wml::document response;
640  simple_wml::node& change = response.root().add_child("change_controller");
641 
642  change.set_attr_dup("side", side.c_str());
643  change.set_attr_dup("player", player_name.c_str());
644 
645  // Tell everyone but the new player that this side's controller changed.
646  change.set_attr_dup("controller", side_controllers_[side_index].to_cstring());
647  change.set_attr("is_local", "no");
648 
649  send_data(response, sock);
650 
651  if(started_) { // this is added instead of the if (started_) {...} below
652  // the purpose of these records is so that observers, replay viewers, get controller updates correctly
653  record_data(response.clone());
654  }
655 
656  // Tell the new player that he controls this side now.
657  // Just don't send it when the player left the game. (The host gets the
658  // side_drop already.)
659  if(!player_left) {
660  change.set_attr("is_local", "yes");
661  send_to_player(sock, response);
662  }
663 }
664 
666 {
667  const std::string owner_name = username(owner_);
669  cfg.root().add_child("host_transfer");
670 
671  std::string message = owner_name + " has been chosen as the new host.";
672  send_to_player(owner_, cfg);
674 }
675 
677 {
678  if(started_ || description_ == nullptr) {
679  return false;
680  }
681 
682  int available_slots = 0;
683  int num_sides = get_sides_list().size();
684  int i = 0;
685 
686  for(const simple_wml::node* side : get_sides_list()) {
687  if(((*side)["allow_player"].to_bool(true) == false) || (*side)["controller"] == "null") {
688  num_sides--;
689  } else if(sides_[i] == 0) {
690  ++available_slots;
691  }
692 
693  ++i;
694  }
695 
696  simple_wml::node* slots_cfg = description_->child("slot_data");
697  if(!slots_cfg) {
698  slots_cfg = &description_->add_child("slot_data");
699  }
700 
701  slots_cfg->set_attr_int("vacant", available_slots);
702  slots_cfg->set_attr_int("max", num_sides);
703 
704  return true;
705 }
706 
707 bool game::player_is_banned(const socket_ptr& sock, const std::string& name) const
708 {
709  auto ban = std::find(bans_.begin(), bans_.end(), client_address(sock));
710  auto name_ban = std::find(name_bans_.begin(), name_bans_.end(), name);
711 
712  return ban != bans_.end() || name_ban != name_bans_.end();
713 }
714 
716 {
719  send_and_record_server_message("All observers have been muted.");
720  } else {
721  send_and_record_server_message("Muting of all observers has been removed.");
722  }
723 }
724 
725 void game::send_muted_observers(const socket_ptr& user) const
726 {
728  send_server_message("All observers are muted.", user);
729  return;
730  }
731 
732  std::string muted_nicks = list_users(muted_observers_, __func__);
733 
734  send_server_message("Muted observers: " + muted_nicks, user);
735 }
736 
737 void game::mute_observer(const simple_wml::node& mute, const socket_ptr& muter)
738 {
739  if(muter != owner_) {
740  send_server_message("You cannot mute: not the game host.", muter);
741  return;
742  }
743 
744  const simple_wml::string_span& username = mute["username"];
745  if(username.empty()) {
746  send_muted_observers(muter);
747  return;
748  }
749 
750  const socket_ptr& user = find_user(username);
751 
752  /**
753  * @todo FIXME: Maybe rather save muted nicks as a set of strings and
754  * also allow muting of usernames not in the game.
755  */
756  if(!user || !is_observer(user)) {
757  send_server_message("Observer '" + username.to_string() + "' not found.", muter);
758  return;
759  }
760 
761  // Prevent muting ourselves.
762  if(user == muter) {
763  send_server_message("Don't mute yourself, silly.", muter);
764  return;
765  }
766 
767  if(is_muted_observer(user)) {
768  send_server_message(username.to_string() + " is already muted.", muter);
769  return;
770  }
771 
772  LOG_GAME << client_address(muter) << "\t" << game::username(muter) << " muted: " << username << " ("
773  << client_address(user) << ")\tin game:\t\"" << name_ << "\" (" << id_ << ")\n";
774 
775  muted_observers_.push_back(user);
776  send_and_record_server_message(username.to_string() + " has been muted.");
777 }
778 
779 void game::unmute_observer(const simple_wml::node& unmute, const socket_ptr& unmuter)
780 {
781  if(unmuter != owner_) {
782  send_server_message("You cannot unmute: not the game host.", unmuter);
783  return;
784  }
785 
786  const simple_wml::string_span& username = unmute["username"];
787  if(username.empty()) {
788  muted_observers_.clear();
789  send_and_record_server_message("Everyone has been unmuted.");
790  return;
791  }
792 
793  const socket_ptr& user = find_user(username);
794  if(!user || !is_observer(user)) {
795  send_server_message("Observer '" + username.to_string() + "' not found.", unmuter);
796  return;
797  }
798 
799  if(!is_muted_observer(user)) {
800  send_server_message(username.to_string() + " is not muted.", unmuter);
801  return;
802  }
803 
804  LOG_GAME << client_address(unmuter) << "\t" << game::username(unmuter) << " unmuted: " << username << " ("
805  << client_address(user) << ")\tin game:\t\"" << name_ << "\" (" << id_ << ")\n";
806 
808  send_and_record_server_message(username.to_string() + " has been unmuted.");
809 }
810 
811 void game::send_leave_game(const socket_ptr& user) const
812 {
813  static simple_wml::document leave_game("[leave_game]\n[/leave_game]\n", simple_wml::INIT_COMPRESSED);
814  send_to_player(user, leave_game);
815 }
816 
818 {
819  if(kicker != owner_) {
820  send_server_message("You cannot kick: not the game host", kicker);
821  return socket_ptr();
822  }
823 
824  const simple_wml::string_span& username = kick["username"];
825  const socket_ptr& user = find_user(username);
826 
827  if(!user || !is_member(user)) {
828  send_server_message("'" + username.to_string() + "' is not a member of this game.", kicker);
829  return socket_ptr();
830  } else if(user == kicker) {
831  send_server_message("Don't kick yourself, silly.", kicker);
832  return socket_ptr();
833  } else if(player_connections_.find(user)->info().is_moderator()) {
834  send_server_message("You're not allowed to kick a moderator.", kicker);
835  return socket_ptr();
836  }
837 
838  LOG_GAME << client_address(kicker) << "\t" << game::username(kicker) << "\tkicked: " << username << " ("
839  << client_address(user) << ")\tfrom game:\t\"" << name_ << "\" (" << id_ << ")\n";
840 
841  send_and_record_server_message(username.to_string() + " has been kicked.");
842 
843  // Tell the user to leave the game.
844  send_leave_game(user);
845  remove_player(user);
846  return user;
847 }
848 
850 {
851  if(banner != owner_) {
852  send_server_message("You cannot ban: not the game host", banner);
853  return socket_ptr();
854  }
855 
856  const simple_wml::string_span& username = ban["username"];
857  const socket_ptr& user = find_user(username);
858 
859  if(!user) {
860  send_server_message("User '" + username.to_string() + "' not found.", banner);
861  return socket_ptr();
862  } else if(user == banner) {
863  send_server_message("Don't ban yourself, silly.", banner);
864  return socket_ptr();
865  } else if(player_is_banned(user, username.to_string())) {
866  send_server_message("'" + username.to_string() + "' is already banned.", banner);
867  return socket_ptr();
868  } else if(player_connections_.find(user)->info().is_moderator()) {
869  send_server_message("You're not allowed to ban a moderator.", banner);
870  return socket_ptr();
871  }
872 
873  LOG_GAME << client_address(banner) << "\t" << game::username(banner) << "\tbanned: " << username << " ("
874  << client_address(user) << ")\tfrom game:\t\"" << name_ << "\" (" << id_ << ")\n";
875 
876  bans_.push_back(client_address(user));
877  name_bans_.push_back(username.to_string());
878  send_and_record_server_message(username.to_string() + " has been banned.");
879 
880  if(is_member(user)) {
881  // tell the user to leave the game.
882  send_leave_game(user);
883  remove_player(user);
884  return user;
885  }
886 
887  // Don't return the user if he wasn't in this game.
888  return socket_ptr();
889 }
890 
891 void game::unban_user(const simple_wml::node& unban, const socket_ptr& unbanner)
892 {
893  if(unbanner != owner_) {
894  send_server_message("You cannot unban: not the game host.", unbanner);
895  return;
896  }
897 
898  const simple_wml::string_span& username = unban["username"];
899  const socket_ptr& user = find_user(username);
900 
901  if(!user) {
902  send_server_message("User '" + username.to_string() + "' not found.", unbanner);
903  return;
904  }
905 
906  if(!player_is_banned(user, username.to_string())) {
907  send_server_message("'" + username.to_string() + "' is not banned.", unbanner);
908  return;
909  }
910 
911  LOG_GAME
912  << client_address(unbanner) << "\t" << player_connections_.find(unbanner)->info().name()
913  << "\tunbanned: " << username << " (" << client_address(user) << ")\tfrom game:\t\"" << name_ << "\" ("
914  << id_ << ")\n";
915 
916  bans_.erase(std::remove(bans_.begin(), bans_.end(), client_address(user)), bans_.end());
917  name_bans_.erase(std::remove(name_bans_.begin(), name_bans_.end(), username.to_string()), name_bans_.end());
918  send_and_record_server_message(username.to_string() + " has been unbanned.");
919 }
920 
922 {
923  if(!owner_) {
924  ERR_GAME << "No owner in game::process_message" << std::endl;
925  }
926 
927  simple_wml::node* const message = data.root().child("message");
928  assert(message);
929  message->set_attr_dup("sender", player_connections_.find(user)->info().name().c_str());
930 
931  const simple_wml::string_span& msg = (*message)["message"];
932  chat_message::truncate_message(msg, *message);
933 
934  send_data(data, user, "game message");
935 }
936 
937 bool game::is_legal_command(const simple_wml::node& command, const socket_ptr& user)
938 {
939  const bool is_player = this->is_player(user);
940  const bool is_host = user == owner_;
941  const bool is_current = is_current_player(user);
942 
943  if(command.has_attr("from_side")) {
944  const std::size_t from_side_index = command["from_side"].to_int() - 1;
945 
946  // Someone pretends to be the server...
947  if(command["from_side"] == "server") {
948  return false;
949  }
950 
951  if(from_side_index >= sides_.size() || sides_[from_side_index] != user) {
952  return false;
953  }
954  }
955 
956  if(is_current) {
957  return true;
958  }
959 
960  // Only single commands allowed.
961  // NOTE: some non-dependent commands like move,attack.. might contain a [checkup] tag after their first data.
962  // But those packages are only sent by the currently active player which we check above.
963  if(!command.one_child()) {
964  return false;
965  }
966 
967  // Chatting is never an illegal command.
968  if(command.child("speak")) {
969  return true;
970  }
971  if(command.child("surrender")) {
972  const simple_wml::string_span& sn = command.child("surrender")->attr("side_number");
973  if(sn.is_null()) {
974  return false;
975  }
976 
977  std::size_t side_number = sn.to_int();
978  if(side_number >= sides_.size() || sides_[side_number] != user) {
979  return false;
980  } else {
981  return true;
982  }
983  }
984 
985  // AKA it's generated by get_user_input for example [global_variable]
986  if(is_player && command.has_attr("dependent") && command.has_attr("from_side")) {
987  return true;
988  }
989 
990  if((is_player || is_host) && (
991  command.child("label") ||
992  command.child("clear_labels") ||
993  command.child("rename") ||
994  command.child("countdown_update")
995  )) {
996  return true;
997  }
998 
999  return false;
1000 }
1001 
1003 {
1004  // DBG_GAME << "processing commands: '" << cfg << "'\n";
1005  if(!started_) {
1006  return false;
1007  }
1008 
1009  simple_wml::node* const turn = data.root().child("turn");
1010  bool turn_ended = false;
1011 
1012  // Any private 'speak' commands must be repackaged separate
1013  // to other commands, and re-sent, since they should only go
1014  // to some clients.
1015  bool repackage = false;
1016  int index = 0;
1017  std::vector<int> marked;
1018 
1019  const simple_wml::node::child_list& commands = turn->children("command");
1020 
1021  for(simple_wml::node* command : commands) {
1022  DBG_GAME << "game " << id_ << " received [" << (*command).first_child() << "] from player '" << username(user)
1023  << "'(" << user << ") during turn " << current_side_index_ + 1 << "," << current_turn_ << "\n";
1024  if(!is_legal_command(*command, user)) {
1025  LOG_GAME << "ILLEGAL COMMAND in game: " << id_ << " (((" << simple_wml::node_to_string(*command)
1026  << ")))\n";
1027 
1028  std::stringstream msg;
1029  msg << "Removing illegal command '" << (*command).first_child().to_string() << "' from: " << username(user)
1030  << ". Current player is: " << username(current_player()) << " (" << current_side_index_ + 1 << "/" << nsides_
1031  << ").";
1032  LOG_GAME << msg.str() << " (socket: " << current_player() << ") (game id: " << id_ << ")\n";
1033  send_and_record_server_message(msg.str());
1034 
1035  marked.push_back(index - marked.size());
1036  } else if((*command).child("speak")) {
1037  simple_wml::node& speak = *(*command).child("speak");
1038  if(!speak["to_sides"].empty() || is_muted_observer(user)) {
1039  DBG_GAME << "repackaging..." << std::endl;
1040  repackage = true;
1041  }
1042 
1043  const simple_wml::string_span& msg = speak["message"];
1044  chat_message::truncate_message(msg, speak);
1045 
1046  // Force the description to be correct,
1047  // to prevent spoofing of messages.
1048  speak.set_attr_dup("id", player_connections_.find(user)->info().name().c_str());
1049 
1050  // Also check the side for players.
1051  if(is_player(user)) {
1052  const std::size_t side_index = speak["side"].to_int() - 1;
1053 
1054  if(side_index >= sides_.size() || sides_[side_index] != user) {
1055  if(user == current_player()) {
1056  speak.set_attr_dup("side", lexical_cast_default<std::string>(current_side() + 1).c_str());
1057  } else {
1058  const auto s = std::find(sides_.begin(), sides_.end(), user);
1059  speak.set_attr_dup("side", lexical_cast_default<std::string>(s - sides_.begin() + 1).c_str());
1060  }
1061  }
1062  }
1063  } else if (command->child("surrender")) {
1064  std::size_t side_index = 0;
1065 
1066  for(auto s : sides_) {
1067  if(s == user) {
1068  break;
1069  }
1070  ++side_index;
1071  }
1072 
1073  if(side_index < sides_.size()) {
1075  std::string playername;
1076  cfg.root().set_attr_dup("side", std::to_string(side_index + 1).c_str());
1077 
1078  // figure out who gets the surrendered side
1079  if(owner_ == user) {
1080  playername = username(sides_[(side_index + 1) % sides_.size()]);
1081  } else {
1082  playername = username(owner_);
1083  }
1084 
1085  cfg.root().set_attr_dup("player", playername.c_str());
1086  transfer_side_control(user, cfg.root());
1087  }
1088  send_and_record_server_message(username(user) + " has surrendered.");
1089  } else if(is_current_player(user) && (*command).child("end_turn")) {
1090  simple_wml::node& endturn = *(*command).child("end_turn");
1091  turn_ended = end_turn(endturn["next_player_number"].to_int());
1092  }
1093 
1094  ++index;
1095  }
1096 
1097  for(const int j : marked) {
1098  turn->remove_child("command", j);
1099  }
1100 
1101  if(turn->no_children()) {
1102  return false;
1103  }
1104 
1105  if(!repackage) {
1106  record_data(data.clone());
1107  send_data(data, user, "game replay");
1108  return turn_ended;
1109  }
1110 
1111  for(simple_wml::node* command : commands) {
1112  simple_wml::node* const speak = (*command).child("speak");
1113  if(speak == nullptr) {
1115  simple_wml::node& mturn = mdata->root().add_child("turn");
1116  (*command).copy_into(mturn.add_child("command"));
1117  send_data(*mdata, user, "game replay");
1118  record_data(mdata);
1119  continue;
1120  }
1121 
1122  const simple_wml::string_span& to_sides = (*speak)["to_sides"];
1123 
1124  // Anyone can send to the observer team.
1125  if(is_muted_observer(user) && to_sides != game_config::observer_team_name.c_str()) {
1126  send_server_message("You have been muted, others can't see your message!", user);
1127  continue;
1128  }
1129 
1130  auto message = std::make_unique<simple_wml::document>();
1131  simple_wml::node& message_turn = message->root().add_child("turn");
1132  simple_wml::node& message_turn_command = message_turn.add_child("command");
1133  message_turn_command.set_attr("undo", "no");
1134  speak->copy_into(message_turn_command.add_child("speak"));
1135 
1136  if(to_sides.empty()) {
1137  send_data(*message, user, "game message");
1138  record_data(message.release());
1139  } else if(to_sides == game_config::observer_team_name) {
1140  send_to_players(*message, observers_, user);
1141  record_data(message.release());
1142  } else {
1143  send_data_sides(*message, to_sides, user, "game message");
1144  }
1145  }
1146 
1147  return turn_ended;
1148 }
1149 
1151 {
1152  uint32_t seed = rng_.get_next_random();
1153 
1154  std::stringstream stream;
1155  stream << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex << seed;
1156 
1158  simple_wml::node& turn = mdata->root().add_child("turn");
1159  simple_wml::node& command = turn.add_child("command");
1160  simple_wml::node& random_seed = command.add_child("random_seed");
1161 
1162  random_seed.set_attr_dup("new_seed", stream.str().c_str());
1163 
1164  command.set_attr("from_side", "server");
1165  command.set_attr("dependent", "yes");
1166 
1167  send_data(*mdata, socket_ptr(), "game replay");
1168  record_data(mdata);
1169 }
1170 
1172 {
1173  const std::size_t side_index = req["side"].to_int() - 1;
1174  CONTROLLER new_controller;
1175  CONTROLLER old_controller;
1176 
1177  if(!new_controller.parse(req["new_controller"])) {
1179  "Could not handle [request_choice] [change_controller] with invalid controller '" + req["new_controller"].to_string() + "'");
1180  return;
1181  }
1182 
1183  if(!old_controller.parse(req["old_controller"])) {
1185  "Could not handle [request_choice] [change_controller] with invalid controller '" + req["old_controller"].to_string() + "'");
1186  return;
1187  }
1188 
1189  if(old_controller != this->side_controllers_[side_index]) {
1191  "Found unexpected old_controller= '" + old_controller.to_string() + "' in [request_choice] [change_controller]");
1192  }
1193 
1194  if(side_index >= sides_.size()) {
1196  "Could not handle [request_choice] [change_controller] with invalid side '" + req["side"].to_string() + "'");
1197  return;
1198  }
1199 
1200  const bool was_null = this->side_controllers_[side_index] == CONTROLLER::EMPTY;
1201  const bool becomes_null = new_controller == CONTROLLER::EMPTY;
1202 
1203  if(was_null) {
1204  assert(sides_[side_index] == 0);
1205  sides_[side_index] = current_player();
1206  }
1207 
1208  if(becomes_null) {
1209  sides_[side_index].reset();
1210  }
1211 
1212  side_controllers_[side_index] = new_controller;
1213 
1215  simple_wml::node& turn = mdata->root().add_child("turn");
1216  simple_wml::node& command = turn.add_child("command");
1217  simple_wml::node& change_controller_wml = command.add_child("change_controller_wml");
1218 
1219  change_controller_wml.set_attr_dup("controller", new_controller.to_cstring());
1220  change_controller_wml.set_attr("is_local", "yes");
1221 
1222  command.set_attr("from_side", "server");
1223  command.set_attr("dependent", "yes");
1224 
1225  // Calling send_to_one to 0 connect causes the package to be sent to all clients.
1226  if(sides_[side_index] != 0) {
1227  send_to_player(sides_[side_index], *mdata);
1228  }
1229 
1230  change_controller_wml.set_attr("is_local", "no");
1231 
1232  send_data(*mdata, sides_[side_index], "game replay");
1233  record_data(mdata);
1234 }
1235 
1236 void game::handle_choice(const simple_wml::node& data, const socket_ptr& user)
1237 {
1238 
1239  if(!started_) {
1240  return;
1241  }
1242 
1243  // note, that during end turn events, it's side=1 for the server but side= side_count() on the clients.
1244 
1245  // Otherwise we allow observers to cause OOS for the playing clients by sending
1246  // server choice requests based on incompatible local changes. To solve this we block
1247  // server choice requests from observers.
1248  if(user != owner_ && !is_player(user)) {
1249  return;
1250  }
1251 
1252  // since we reset the last_choice_request_id_ when a new scenario is loaded,
1253  // the code would otherwise wrongly accept these requests from client in old
1254  // scenarios. which would result on oos.
1255  if(players_not_advanced_.find(user) != players_not_advanced_.end()) {
1256  return;
1257  }
1258 
1259  int request_id = lexical_cast_default<int>(data["request_id"], -10);
1260  if(request_id <= last_choice_request_id_) {
1261  // We gave already an anwer to this request.
1262  return;
1263  }
1264 
1265  DBG_GAME << "answering seed request " << request_id << " by player "
1266  << player_connections_.find(user)->info().name() << "(" << user << ")" << std::endl;
1267  last_choice_request_id_ = request_id;
1268 
1269  if(const simple_wml::node* rand = data.child("random_seed")) {
1270  handle_random_choice(*rand);
1271  } else if(const simple_wml::node* ccw = data.child("change_controller_wml")) {
1273  } else {
1274  send_and_record_server_message("Found unknown server choice request: [" + data.first_child().to_string() + "]");
1275  }
1276 }
1277 
1279 {
1280  if(!started_ || !is_player(user)) {
1281  return;
1282  }
1283 
1284  const simple_wml::node& wb_node = *data.child("whiteboard");
1285 
1286  // Ensure "side" attribute match with user
1287  const simple_wml::string_span& to_sides = wb_node["to_sides"];
1288  std::size_t const side_index = wb_node["side"].to_int() - 1;
1289 
1290  if(side_index >= sides_.size() || sides_[side_index] != user) {
1291  std::ostringstream msg;
1292  msg << "Ignoring illegal whiteboard data, sent from user '" << player_connections_.find(user)->info().name()
1293  << "' which had an invalid side '" << side_index + 1 << "' specified" << std::endl;
1294 
1295  const std::string& msg_str = msg.str();
1296 
1297  LOG_GAME << msg_str << std::endl;
1299  return;
1300  }
1301 
1302  send_data_sides(data, to_sides, user, "whiteboard");
1303 }
1304 
1306 {
1307  if(!started_ || !is_player(user)) {
1308  return;
1309  }
1310 
1311  const simple_wml::node& ctw_node = *data.child("change_turns_wml");
1312  const int current_turn = ctw_node["current"].to_int();
1313  const int num_turns = ctw_node["max"].to_int();
1314  if(num_turns > 10000 || current_turn > 10000) {
1315  // ignore this to prevent errors related to integer overflow.
1316  return;
1317  }
1318 
1319  set_current_turn(current_turn);
1320  num_turns_ = num_turns;
1321 
1322  assert(static_cast<int>(this->current_turn()) == current_turn);
1323 
1324  simple_wml::node* turns_cfg = description_->child("turn_data");
1325  if(!turns_cfg) {
1326  turns_cfg = &description_->add_child("turn_data");
1327  }
1328 
1329  ctw_node.copy_into(*turns_cfg);
1330 
1331  // Don't send or store this change, all players should have gotten it by wml.
1332 }
1333 
1334 bool game::end_turn(int new_side)
1335 {
1336  if(new_side > 0) {
1337  current_side_index_ = new_side - 1;
1338  }
1339  else {
1341  }
1342 
1343  // Skip over empty sides.
1344  for(int i = 0; i < nsides_ && side_controllers_[current_side()] == CONTROLLER::EMPTY; ++i) {
1346  }
1347 
1348  auto res = std::div(current_side_index_, nsides_ > 0 ? nsides_ : 1);
1349 
1350  if(res.quot == 0) {
1351  return false;
1352  }
1353  current_side_index_ = res.rem;
1354  current_turn_ += res.quot;
1355 
1356  if(description_ == nullptr) {
1357  // TODO: why do we need this?
1358  return false;
1359  }
1360 
1361  update_turn_data();
1362 
1363  return true;
1364 }
1365 
1367 {
1368  if(description_ == nullptr) {
1369  return;
1370  }
1371 
1372  simple_wml::node* turns_cfg = description_->child("turn_data");
1373  if(!turns_cfg) {
1374  turns_cfg = &description_->add_child("turn_data");
1375  }
1376 
1377  turns_cfg->set_attr_int("current", current_turn());
1378  turns_cfg->set_attr_int("max", num_turns_);
1379 
1380 }
1381 
1382 ///@todo differentiate between "observers not allowed" and "player already in the game" errors.
1383 // maybe return a string with an error message.
1385 {
1386  if(is_member(player)) {
1387  ERR_GAME << "ERROR: Player is already in this game. (socket: " << player << ")\n";
1388  return false;
1389  }
1390 
1391  socket_ptr user = player;
1392 
1394 
1395  bool became_observer = false;
1396  if(!started_ && !observer && take_side(user)) {
1397  DBG_GAME << "adding player...\n";
1398  players_.push_back(player);
1399 
1400  player_connections_.find(user)->info().set_status(player::PLAYING);
1401 
1402  send_and_record_server_message(player_connections_.find(user)->info().name() + " has joined the game.", player);
1403  } else if(!allow_observers() && !player_connections_.find(user)->info().is_moderator()) {
1404  return false;
1405  } else {
1406  if(!observer) {
1407  became_observer = true;
1408  observer = true;
1409  }
1410 
1411  DBG_GAME << "adding observer...\n";
1412  observers_.push_back(player);
1413  if(!allow_observers()) {
1415  player_connections_.find(user)->info().name() + " is now observing the game.", player);
1416  }
1417 
1418  simple_wml::document observer_join;
1419  observer_join.root()
1420  .add_child("observer")
1421  .set_attr_dup("name", player_connections_.find(user)->info().name().c_str());
1422 
1423  // Send observer join to everyone except the new observer.
1424  send_data(observer_join, player);
1425  }
1426 
1427  LOG_GAME
1428  << client_address(player) << "\t" << player_connections_.find(user)->info().name() << "\tjoined game:\t\""
1429  << name_ << "\" (" << id_ << ")" << (observer ? " as an observer" : "") << ". (socket: " << player
1430  << ")\n";
1431 
1432  player_connections_.find(user)->info().mark_available(id_, name_);
1433  player_connections_.find(user)->info().set_status((observer) ? player::OBSERVING : player::PLAYING);
1435 
1436  // Send the user the game data.
1437  send_to_player(player, level_);
1438 
1439  if(started_) {
1440  // Tell this player that the game has started
1441  static simple_wml::document start_game_doc("[start_game]\n[/start_game]\n", simple_wml::INIT_COMPRESSED);
1442  send_to_player(player, start_game_doc);
1443 
1444  // Send observer join of all the observers in the game to the new player
1445  // only once the game started. The client forgets about it anyway otherwise.
1446  send_observerjoins(player);
1447 
1448  // Send the player the history of the game to-date.
1449  send_history(player);
1450  } else {
1451  send_user_list();
1452  }
1453 
1454  const std::string clones = has_same_ip(player);
1455  if(!clones.empty()) {
1457  player_connections_.find(user)->info().name() + " has the same IP as: " + clones);
1458  }
1459 
1460  if(became_observer) {
1461  // in case someone took the last slot right before this player
1462  send_server_message("You are an observer.", player);
1463  }
1464 
1465  return true;
1466 }
1467 
1468 bool game::remove_player(const socket_ptr& player, const bool disconnect, const bool destruct)
1469 {
1470  if(!is_member(player)) {
1471  ERR_GAME << "ERROR: User is not in this game. (socket: " << player << ")\n";
1472  return false;
1473  }
1474 
1476  DBG_GAME << "removing player...\n";
1477 
1478  const bool host = (player == owner_);
1479  const bool observer = is_observer(player);
1480 
1481  players_.erase(std::remove(players_.begin(), players_.end(), player), players_.end());
1482  observers_.erase(std::remove(observers_.begin(), observers_.end(), player), observers_.end());
1483  players_not_advanced_.erase(player);
1484 
1485  const bool game_ended = players_.empty() || (host && !started_);
1486 
1487  socket_ptr user = player;
1488 
1489  LOG_GAME
1490  << client_address(user)
1491  << "\t" << player_connections_.find(user)->info().name()
1492  << ((game_ended && !(observer && destruct)) ? (started_ ? "\tended" : "\taborted") : "\thas left")
1493  << " game:\t\"" << name_ << "\" (" << id_ << ")"
1494  << (game_ended && started_ && !(observer && destruct)
1495  ? " at turn: " + lexical_cast_default<std::string, std::size_t>(current_turn())
1496  + " with reason: '" + termination_reason() + "'"
1497  : "")
1498  << (observer ? " as an observer" : "") << (disconnect ? " and disconnected" : "") << ". (socket: " << user
1499  << ")\n";
1500 
1501  if(game_ended && started_ && !(observer && destruct)) {
1502  send_server_message_to_all(player_connections_.find(user)->info().name() + " ended the game.", player);
1503  }
1504 
1505  if(game_ended || destruct) {
1506  return game_ended;
1507  }
1508 
1509  // Don't mark_available() since the player got already removed from the
1510  // games_and_users_list_.
1511  if(!disconnect) {
1512  player_connections_.find(user)->info().mark_available();
1513  }
1514 
1515  if(observer) {
1516  send_observerquit(user);
1517  } else {
1518  send_and_record_server_message(player_connections_.find(user)->info().name()
1519  + (disconnect ? " has disconnected." : " has left the game."), player);
1520  }
1521 
1522  // If the player was host choose a new one.
1523  if(host) {
1524  owner_ = players_.front();
1525  notify_new_host();
1526  }
1527 
1528  bool ai_transfer = false;
1529 
1530  // Look for all sides the player controlled and drop them.
1531  // (Give them to the host.
1532  for(unsigned side_index = 0; side_index < sides_.size(); ++side_index) {
1533  const socket_ptr& side = sides_[side_index];
1534 
1535  if(side != player) {
1536  continue;
1537  }
1538 
1539  if(side_controllers_[side_index] == CONTROLLER::AI) {
1540  ai_transfer = true;
1541  }
1542 
1543  change_controller(side_index, owner_, username(owner_));
1544 
1545  // Check whether the host is actually a player and make him one if not.
1546  if(!is_player(owner_)) {
1547  DBG_GAME << "making the owner a player...\n";
1548  auto owner_iter = player_connections_.find(owner_);
1549  if(owner_iter == player_connections_.end())
1550  ERR_GAME << "game owner " << client_address(owner_) << "is not in player_connections_\n";
1551  else {
1552  owner_iter->info().set_status(player::PLAYING);
1553  observers_.erase(std::remove(observers_.begin(), observers_.end(), owner_), observers_.end());
1554  players_.push_back(owner_);
1556  }
1557  }
1558 
1559  // send the host a notification of removal of this side
1560  const std::string side_drop = lexical_cast_default<std::string, std::size_t>(side_index + 1);
1561 
1562  simple_wml::document drop;
1563  auto& node_side_drop = drop.root().add_child("side_drop");
1564 
1565  node_side_drop.set_attr_dup("side_num", side_drop.c_str());
1566  node_side_drop.set_attr_dup("controller", side_controllers_[side_index].to_cstring());
1567 
1568  DBG_GAME << "*** sending side drop: \n" << drop.output() << std::endl;
1569 
1570  send_to_player(owner_, drop);
1571  }
1572 
1573  if(ai_transfer) {
1574  send_and_record_server_message("AI sides transferred to host.");
1575  }
1576 
1578 
1579  send_user_list(player);
1580  return false;
1581 }
1582 
1583 void game::send_user_list(const socket_ptr& exclude) const
1584 {
1585  // If the game hasn't started yet, then send all players a list of the users in the game.
1586  if(started_ /*|| description_ == nullptr*/) {
1587  return;
1588  }
1589 
1591  simple_wml::node& list = cfg.root();
1592 
1593  for(const socket_ptr& user_ptr : all_game_users()) {
1594  const auto pl = player_connections_.find(user_ptr);
1595 
1596  if(pl != player_connections_.end()) {
1597  simple_wml::node& user = list.add_child("user");
1598 
1599  // Don't need to duplicate pl->info().name().c_str() because the
1600  // document will be destroyed by the end of the function
1601  user.set_attr_dup("name", pl->info().name().c_str());
1602  user.set_attr("host", is_owner(user_ptr) ? "yes" : "no");
1603  user.set_attr("observer", is_observer(user_ptr) ? "yes" : "no");
1604  }
1605  }
1606 
1607  send_data(cfg, exclude);
1608 }
1609 
1610 void game::new_scenario(const socket_ptr& sender)
1611 {
1612  assert(sender == owner_);
1613  players_not_advanced_.clear();
1614  for(const socket_ptr& user_ptr : all_game_users()) {
1615  if(user_ptr != sender) {
1616  players_not_advanced_.insert(user_ptr);
1617  }
1618  }
1619 }
1620 
1622 {
1623  send_server_message_to_all(player_connections_.find(user)->info().name() + " advances to the next scenario", user);
1624 
1625  simple_wml::document cfg_scenario;
1626  simple_wml::node& next_scen = cfg_scenario.root().add_child("next_scenario");
1627  level_.root().copy_into(next_scen);
1628  next_scen.set_attr("started", started_ ? "yes" : "no");
1629 
1630  DBG_GAME << "****\n loading next scenario for a client. sides info = " << std::endl;
1631  DBG_GAME << debug_sides_info() << std::endl;
1632  DBG_GAME << "****" << std::endl;
1633 
1634  //
1635  // Change the controller to match that client.
1636  //
1637  // FIXME: This breaks scenario transitions with mp connect screen shown.
1638  //
1639  // FIXME: This causes bugs, esp if controller have changed since the
1640  // beginning of the next scenario
1641  //
1642  // There are currently 2 possible ideas to fix this issue:
1643  //
1644  // 1) When the scenario starts, we store the controllers at that
1645  // point and use that data when a client loads the the next
1646  // scenario (here)
1647  //
1648  // 2) When a client loads the next scenario we send it the
1649  // observers' starting point (meaning we don't change sides
1650  // here), and then we send that side an automatic controller
1651  // change later.
1652  //
1653  simple_wml::document doc_controllers;
1654  simple_wml::node& cfg_controllers = doc_controllers.root().add_child("controllers");
1655 
1656  for(const auto& side_user : sides_) {
1657  simple_wml::node& cfg_controller = cfg_controllers.add_child("controller");
1658  cfg_controller.set_attr("is_local", side_user == user ? "yes" : "no");
1659  }
1660 
1661  send_to_player(user, cfg_scenario);
1662  send_to_player(user, doc_controllers);
1663 
1664  players_not_advanced_.erase(user);
1665 
1666  // Send the player the history of the game to-date.
1667  send_history(user);
1668 
1669  // Send observer join of all the observers in the game to the user.
1670  send_observerjoins(user);
1671 }
1672 
1673 void game::send_data(simple_wml::document& data, const socket_ptr& exclude, std::string /*packet_type*/) const
1674 {
1675  send_to_players(data, all_game_users(), exclude);
1676 }
1677 
1678 namespace
1679 {
1680 struct controls_side_helper
1681 {
1683  const std::vector<int>& sides_;
1684 
1685  controls_side_helper(const wesnothd::game& game, const std::vector<int>& sides)
1686  : game_(game)
1687  , sides_(sides)
1688  {
1689  }
1690 
1691  bool operator()(socket_ptr user) const
1692  {
1693  return game_.controls_side(sides_, user);
1694  }
1695 };
1696 }
1697 
1699  const simple_wml::string_span& sides,
1700  const socket_ptr& exclude,
1701  std::string /*packet_type*/) const
1702 {
1703  std::vector<int> sides_vec = ::split<int>(sides, ::split_conv_impl());
1704 
1705  DBG_GAME << __func__ << "...\n";
1706 
1707  decltype(players_) filtered_players;
1708 
1709  std::copy_if(players_.begin(), players_.end(), std::back_inserter(filtered_players),
1710  controls_side_helper(*this, sides_vec));
1711 
1712  send_to_players(data, filtered_players, exclude);
1713 }
1714 
1715 bool game::controls_side(const std::vector<int>& sides, const socket_ptr& player) const
1716 {
1717  for(int side : sides) {
1718  std::size_t side_index = side - 1;
1719 
1720  if(side_index < sides_.size() && sides_[side_index] == player) {
1721  return true;
1722  }
1723  }
1724 
1725  return false;
1726 }
1727 
1728 std::string game::has_same_ip(const socket_ptr& user) const
1729 {
1730  const user_vector users = all_game_users();
1731  const std::string ip = client_address(user);
1732 
1733  std::string clones;
1734  for(const socket_ptr& u : users) {
1735  if(ip == client_address(u) && user != u) {
1736  const auto pl = player_connections_.find(u);
1737 
1738  if(pl != player_connections_.end()) {
1739  clones += (clones.empty() ? "" : ", ") + pl->info().name();
1740  }
1741  }
1742  }
1743 
1744  return clones;
1745 }
1746 
1747 void game::send_observerjoins(const socket_ptr& sock) const
1748 {
1749  for(const socket_ptr& ob : observers_) {
1750  if(ob == sock) {
1751  continue;
1752  }
1753 
1755  cfg.root().add_child("observer").set_attr_dup("name", player_connections_.find(ob)->info().name().c_str());
1756 
1757  if(sock == socket_ptr()) {
1758  // Send to everyone except the observer in question.
1759  send_data(cfg, ob);
1760  } else {
1761  // Send to the (new) user.
1762  send_to_player(sock, cfg);
1763  }
1764  }
1765 }
1766 
1768 {
1769  simple_wml::document observer_quit;
1770 
1771  // Don't need to dup the attribute because this document is short-lived.
1772  observer_quit.root()
1773  .add_child("observer_quit")
1774  .set_attr_dup("name", player_connections_.find(observer)->info().name().c_str());
1775 
1776  send_data(observer_quit, observer);
1777 }
1778 
1779 void game::send_history(const socket_ptr& socket) const
1780 {
1781  if(history_.empty()) {
1782  return;
1783  }
1784 
1785  // we make a new document based on converting to plain text and
1786  // concatenating the buffers.
1787  // TODO: Work out how to concentate buffers without decompressing.
1788  std::string buf;
1789  for(auto& h : history_) {
1790  buf += h.output();
1791  }
1792 
1793  try {
1795  doc->compress();
1796 
1797  send_to_player(socket, *doc);
1798 
1799  history_.clear();
1800  history_.push_back(doc);
1801  } catch(const simple_wml::error& e) {
1802  WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
1803  }
1804 }
1805 
1806 static bool is_invalid_filename_char(char c)
1807 {
1808  return !(isalnum(c) ||
1809  (c == '_') ||
1810  (c == '-') ||
1811  (c == '.') ||
1812  (c == '(') ||
1813  (c == ')') ||
1814  (c == '#') ||
1815  (c == ',') ||
1816  (c == '!') ||
1817  (c == '^') ||
1818  (c == '+') ||
1819  (c == '=') ||
1820  (c == '@') ||
1821  (c == '%') ||
1822  (c == '\'')
1823  );
1824 }
1825 
1827 {
1828  std::stringstream name;
1829  name << (*starting_pos(level_.root()))["name"] << " Turn " << current_turn() << " (" << id_ << ").bz2";
1830  std::string filename(name.str());
1831  std::replace(filename.begin(), filename.end(), ' ', '_');
1832  filename.erase(std::remove_if(filename.begin(), filename.end(), is_invalid_filename_char), filename.end());
1833  return filename;
1834 }
1835 
1837 {
1838  if(!save_replays_ || !started_ || history_.empty()) {
1839  return;
1840  }
1841 
1842  std::string replay_commands;
1843  for(const auto& h : history_) {
1844  const simple_wml::node::child_list& turn_list = h.root().children("turn");
1845 
1846  for(const simple_wml::node* turn : turn_list) {
1847  replay_commands += simple_wml::node_to_string(*turn);
1848  }
1849  }
1850 
1851  history_.clear();
1852 
1853  std::stringstream replay_data;
1854  try {
1855  // level_.set_attr_dup("label", name.str().c_str());
1856 
1857  // Used by replays.wesnoth.org as of December 2017. No client usecases.
1858  level_.set_attr_dup("mp_game_title", name_.c_str());
1859 
1860  const bool has_old_replay = level_.child("replay") != nullptr;
1861 
1862  // If there is already a replay in the level_, which means this is a reloaded game,
1863  // then we don't need to add the [start] in the replay.
1864  replay_data
1865  << level_.output()
1866  // This can result in having 2 [replay] at toplevel since level_ can contain one already. But the
1867  // client can handle this (simply merges them).
1868  << "[replay]\n"
1869  // The [start] is generated at the clients and not sent over the network so we add it here.
1870  // It usually contains some checkup data that is used to check whether the calculated results
1871  // match the ones calculated in the replay. But that's not necessary
1872  << (has_old_replay ? "" : "\t[command]\n\t\t[start]\n\t\t[/start]\n\t[/command]\n")
1873  << replay_commands << "[/replay]\n";
1874 
1875  std::string replay_data_str = replay_data.str();
1876  simple_wml::document replay(replay_data_str.c_str(), simple_wml::INIT_STATIC);
1877 
1878  std::string filename = get_replay_filename();
1879  DBG_GAME << "saving replay: " << filename << std::endl;
1880 
1882  (*os) << replay.output_compressed(true);
1883 
1884  if(!os->good()) {
1885  ERR_GAME << "Could not save replay! (" << filename << ")" << std::endl;
1886  }
1887  } catch(const simple_wml::error& e) {
1888  WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
1889  }
1890 }
1891 
1893 {
1894  data->compress();
1895  history_.push_back(data);
1896 }
1897 
1899 {
1900  history_.clear();
1901 }
1902 
1904 {
1905  description_ = desc;
1906  if(!password_.empty()) {
1907  description_->set_attr("password", "yes");
1908  }
1909 }
1910 
1911 void game::set_termination_reason(const std::string& reason)
1912 {
1913  /* if (reason == "out of sync") {
1914  simple_wml::string_span era;
1915  if (level_.child("era")) {
1916  era = level_.child("era")->attr("id");
1917  }
1918  termination_ = "out of sync - " + era.to_string();
1919  }*/
1920  if(termination_.empty()) {
1921  termination_ = reason;
1922  }
1923 }
1924 
1926 {
1927  user_vector res;
1928 
1929  res.insert(res.end(), players_.begin(), players_.end());
1930  res.insert(res.end(), observers_.begin(), observers_.end());
1931 
1932  return res;
1933 }
1934 
1935 std::string game::debug_player_info() const
1936 {
1937  std::stringstream result;
1938  result << "game id: " << id_ << "\n";
1939 
1940  // result << "players_.size: " << players_.size() << "\n";
1941  for(const socket_ptr& p : players_) {
1942  const auto user = player_connections_.find(p);
1943 
1944  if(user != player_connections_.end()) {
1945  result << "player: " << user->info().name().c_str() << "\n";
1946  } else {
1947  result << "player: '" << p << "' not found\n";
1948  }
1949  }
1950 
1951  // result << "observers_.size: " << observers_.size() << "\n";
1952  for(const socket_ptr& o : observers_) {
1953  const auto user = player_connections_.find(o);
1954 
1955  if(user != player_connections_.end()) {
1956  result << "observer: " << user->info().name().c_str() << "\n";
1957  } else {
1958  result << "observer: '" << o << "' not found\n";
1959  }
1960  }
1961  /* result << "player_info_: begin\n";
1962  for (player_map::const_iterator info = player_info_->begin(); info != player_info_->end(); info++){
1963  result << info->second.name().c_str() << "\n";
1964  }
1965  result << "player_info_: end\n";*/
1966  return result.str();
1967 }
1968 
1969 std::string game::debug_sides_info() const
1970 {
1971  std::stringstream result;
1972  result << "game id: " << id_ << "\n";
1974 
1975  result << "\t\t level, server\n";
1976 
1977  for(const simple_wml::node* s : sides) {
1978  result
1979  << "side " << (*s)["side"].to_int()
1980  << " :\t" << (*s)["controller"].to_string()
1981  << "\t, " << side_controllers_[(*s)["side"].to_int() - 1].to_cstring()
1982  << "\t( " << sides_[(*s)["side"].to_int() - 1]
1983  << ",\t" << (*s)["current_player"].to_string() << " )\n";
1984  }
1985 
1986  return result.str();
1987 }
1988 
1990 {
1991  const auto iter = player_connections_.get<name_t>().find(name.to_string());
1992  if(iter != player_connections_.get<name_t>().end()) {
1993  return iter->socket();
1994  } else {
1995  return socket_ptr();
1996  }
1997 }
1998 
1999 void game::send_and_record_server_message(const char* message, const socket_ptr& exclude)
2000 {
2002  send_server_message(message, socket_ptr(), doc);
2003  send_data(*doc, exclude, "message");
2004 
2005  if(started_) {
2006  record_data(doc);
2007  } else {
2008  delete doc;
2009  }
2010 }
2011 
2012 void game::send_server_message_to_all(const char* message, const socket_ptr& exclude) const
2013 {
2015  send_server_message(message, socket_ptr(), &doc);
2016  send_data(doc, exclude, "message");
2017 }
2018 
2019 void game::send_server_message(const char* message, const socket_ptr& sock, simple_wml::document* docptr) const
2020 {
2021  simple_wml::document docbuf;
2022  if(docptr == nullptr) {
2023  docptr = &docbuf;
2024  }
2025 
2026  simple_wml::document& doc = *docptr;
2027 
2028  if(started_) {
2029  simple_wml::node& cmd = doc.root().add_child("turn");
2030  simple_wml::node& cfg = cmd.add_child("command");
2031  cfg.set_attr("undo", "no");
2032  simple_wml::node& msg = cfg.add_child("speak");
2033 
2034  msg.set_attr("id", "server");
2035  msg.set_attr_dup("message", message);
2036  } else {
2037  simple_wml::node& msg = doc.root().add_child("message");
2038 
2039  msg.set_attr("sender", "server");
2040  msg.set_attr_dup("message", message);
2041  }
2042 
2043  if(sock) {
2044  send_to_player(sock, doc);
2045  }
2046 }
2047 } // namespace wesnothd
node & add_child(const char *name)
Definition: simple_wml.cpp:464
bool remove_player(const socket_ptr &player, const bool disconnect=false, const bool destruct=false)
Removes a user from the game.
Definition: game.cpp:1468
void send_data(simple_wml::document &data, const socket_ptr &exclude=socket_ptr(), std::string packet_type="") const
Definition: game.cpp:1673
const char * const_iterator
Definition: simple_wml.hpp:45
bool save_replays_
Definition: game.hpp:520
void remove()
Removes a tip.
Definition: tooltip.cpp:189
const string_span & attr(const char *key) const
Definition: simple_wml.hpp:127
void set_current_turn(int turn)
Definition: game.hpp:135
void missing_user(socket_ptr socket, const std::string &func) const
Function to log when we don&#39;t find a connection in player_info_.
Definition: game.cpp:88
void update_game()
Definition: game.cpp:374
std::string to_string() const
Definition: simple_wml.cpp:181
const socket_ptr & host
Definition: game.hpp:45
void new_scenario(const socket_ptr &player)
when the host sends the new scenario of a mp campaign
Definition: game.cpp:1610
const std::string & name() const
Definition: game.hpp:57
void save_replay()
Definition: game.cpp:1836
player_connections & player_connections_
Definition: game.hpp:452
bool has_attr(const char *key) const
Definition: simple_wml.cpp:403
void handle_random_choice(const simple_wml::node &data)
Definition: game.cpp:1150
void send_history(const socket_ptr &sock) const
Definition: game.cpp:1779
void change_controller(const std::size_t side_num, const socket_ptr &sock, const std::string &player_name, const bool player_left=true)
Send [change_controller] message to tell all clients the new controller&#39;s name or controller type (hu...
Definition: game.cpp:623
const simple_wml::node::child_list & get_sides_list() const
Definition: game.hpp:110
bool player_is_banned(const socket_ptr &player, const std::string &name) const
Checks whether the connection&#39;s ip address or username is banned.
Definition: game.cpp:707
New lexcical_cast header.
user_vector muted_observers_
Definition: game.hpp:469
void update_side_data()
Resets the side configuration according to the scenario data.
Definition: game.cpp:455
void send_and_record_server_message(const char *message, const socket_ptr &exclude=socket_ptr())
Send data to all players in this game except &#39;exclude&#39;.
Definition: game.cpp:1999
const char * end() const
Definition: simple_wml.hpp:90
node & set_attr_int(const char *key, int value)
Definition: simple_wml.cpp:439
void process_whiteboard(simple_wml::document &data, const socket_ptr &user)
Handles incoming [whiteboard] data.
Definition: game.cpp:1278
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:62
node & set_attr(const char *key, const char *value)
Definition: simple_wml.cpp:411
std::size_t current_side() const
Definition: game.hpp:328
int nsides_
Number of sides in the current scenario.
Definition: game.hpp:477
user_vector observers_
A vector of observers (members not owning a side).
Definition: game.hpp:468
void send_server_message(const char *message, const socket_ptr &sock=socket_ptr(), simple_wml::document *doc=nullptr) const
Definition: game.cpp:2019
socket_ptr find_user(const simple_wml::string_span &name)
Shortcut to a convenience function for finding a user by name.
Definition: game.cpp:1989
bool all_observers_muted_
Definition: game.hpp:508
#define h
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
bool is_observer(const socket_ptr &player) const
Definition: game.cpp:176
std::string termination_
Definition: game.hpp:518
void send_leave_game(const socket_ptr &user) const
Definition: game.cpp:811
randomness::mt_rng rng_
A wrapper for mersenne twister rng which generates randomness for this game.
Definition: game.hpp:524
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.hpp:276
const child_list & children(const char *name) const
Definition: simple_wml.cpp:633
bool no_children() const
Definition: simple_wml.hpp:169
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
void send_server_message_to_all(const char *message, const socket_ptr &exclude=socket_ptr()) const
Definition: game.cpp:2012
history history_
Definition: game.hpp:500
const socket_ptr current_player() const
Definition: game.hpp:333
std::string username(const socket_ptr &pl) const
Returns the name of the user or "(unfound)".
Definition: game.cpp:199
std::vector< std::string > bans_
Definition: game.hpp:511
const wesnothd::game & game_
Definition: game.cpp:1682
node * child(const char *name)
Definition: simple_wml.hpp:260
const char * output()
node * child(const char *name)
Definition: simple_wml.cpp:606
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
bool process_turn(simple_wml::document &data, const socket_ptr &user)
Handles [end_turn], repackages [commands] with private [speak]s in them and sends the data...
Definition: game.cpp:1002
void process_message(simple_wml::document &data, const socket_ptr &user)
Definition: game.cpp:921
const char * begin() const
Definition: simple_wml.hpp:89
void process_change_turns_wml(simple_wml::document &data, const socket_ptr &user)
Handles incoming [change_turns_wml] data.
Definition: game.cpp:1305
int last_choice_request_id_
Definition: game.hpp:525
void truncate_message(const simple_wml::string_span &str, simple_wml::node &message)
Function to ensure a text message is within the allowed length.
#define LOG_GAME
Definition: game.cpp:33
void start_game(const socket_ptr &starter)
Definition: game.cpp:295
bool end_turn(int new_side)
Function which should be called every time a player ends their turn (i.e.
Definition: game.cpp:1334
void reset_sides()
calculates the initial value for sides_, side_controllerds_, nsides_
Definition: game.cpp:444
std::vector< CONTROLLER > side_controllers_
Definition: game.hpp:474
int num_turns_
Definition: game.hpp:507
void update_turn_data()
Definition: game.cpp:1366
bool allow_observers() const
Definition: game.cpp:166
void handle_controller_choice(const simple_wml::node &data)
Definition: game.cpp:1171
bool describe_slots()
Set the description to the number of available slots.
Definition: game.cpp:676
static bool is_invalid_filename_char(char c)
Definition: game.cpp:1806
void load_next_scenario(const socket_ptr &user)
A user (player only?) asks for the next scenario to advance to.
Definition: game.cpp:1621
std::string get_replay_filename()
Definition: game.cpp:1826
void send_observerjoins(const socket_ptr &sock=socket_ptr()) const
Send [observer] tags of all the observers in the game to the user or everyone if none given...
Definition: game.cpp:1747
#define WRN_CONFIG
Definition: game.cpp:37
const user_vector all_game_users() const
Adds players and observers into one vector and returns that.
Definition: game.cpp:1925
std::string has_same_ip(const socket_ptr &user) const
Checks whether a user has the same IP as any other members of this game.
Definition: game.cpp:1728
void perform_controller_tweaks()
Definition: game.cpp:230
bool add_player(const socket_ptr &player, bool observer=false)
Add a user to the game.
Definition: game.cpp:1384
bool is_player(const socket_ptr &player) const
Definition: game.cpp:194
std::string list_users(user_vector users, const std::string &func) const
Returns a comma separated list of user names.
Definition: game.cpp:209
void send_to_player(socket_ptr socket, simple_wml::document &doc)
Definition: server.cpp:1811
std::unique_ptr< std::ostream > scoped_ostream
Definition: filesystem.hpp:40
void send_observerquit(const socket_ptr &observer) const
Definition: game.cpp:1767
socket_ptr kick_member(const simple_wml::node &kick, const socket_ptr &kicker)
Kick a member by name.
Definition: game.cpp:817
socket_ptr owner_
The game host or later owner (if the host left).
Definition: game.hpp:462
static simple_wml::node * starting_pos(simple_wml::node &data)
Definition: game.hpp:88
const std::vector< int > & sides_
Definition: game.cpp:1683
void send_user_list(const socket_ptr &exclude=socket_ptr()) const
Function to send a list of users to all clients.
Definition: game.cpp:1583
std::size_t i
Definition: function.cpp:933
std::vector< std::string > name_bans_
Definition: game.hpp:512
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
void send_muted_observers(const socket_ptr &user) const
Definition: game.cpp:725
mock_party p
static map_location::DIRECTION s
void remove_child(const char *name, std::size_t index)
Definition: simple_wml.cpp:601
std::vector< node * > child_list
Definition: simple_wml.hpp:124
void unban_user(const simple_wml::node &unban, const socket_ptr &unbanner)
Definition: game.cpp:891
bool take_side(const socket_ptr &user)
Figures out which side to take and tells that side to the game owner.
Definition: game.cpp:403
std::string client_address(const socket_ptr socket)
bool is_current_player(const socket_ptr &player) const
Definition: game.hpp:338
std::size_t current_turn() const
Definition: game.hpp:130
#define ERR_GAME
Definition: game.cpp:31
bool is_legal_command(const simple_wml::node &command, const socket_ptr &user)
Definition: game.cpp:937
Declarations for File-IO.
void mute_observer(const simple_wml::node &mute, const socket_ptr &muter)
Mute an observer or give a message of all currently muted observers if no name is given...
Definition: game.cpp:737
int current_side_index_
Definition: game.hpp:506
void record_data(simple_wml::document *data)
Definition: game.cpp:1892
side_vector sides_
A vector of side owners.
Definition: game.hpp:472
void notify_new_host()
In case of a host transfer, notify the new host about its status.
Definition: game.cpp:665
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
void handle_choice(const simple_wml::node &data, const socket_ptr &user)
Definition: game.cpp:1236
static void save(LexState *ls, int c)
Definition: llex.cpp:57
void set_description(simple_wml::node *desc)
Functions to set/get the address of the game&#39;s summary description as sent to players in the lobby...
Definition: game.cpp:1903
static const simple_wml::node & get_multiplayer(const simple_wml::node &root)
returns const so that operator [] won&#39;t create empty keys if not existent
Definition: game.cpp:156
std::string observer
simple_wml::node * description_
Pointer to the game&#39;s description in the games_and_users_list_.
Definition: game.hpp:503
std::string name_
The name of the game.
Definition: game.hpp:458
static lg::log_domain log_config("config")
std::string debug_sides_info() const
Helps debugging controller tweaks.
Definition: game.cpp:1969
static lg::log_domain log_server("server")
bool is_owner(const socket_ptr &player) const
Definition: game.hpp:62
void unmute_observer(const simple_wml::node &unmute, const socket_ptr &unmuter)
Definition: game.cpp:779
bool one_child() const
Definition: simple_wml.hpp:170
bool find(E event, F functor)
Tests whether an event handler is available.
void send_data_sides(simple_wml::document &data, const simple_wml::string_span &sides, const socket_ptr &exclude=socket_ptr(), std::string packet_type="") const
Definition: game.cpp:1698
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const socket_ptr, &player_record::socket > >, bmi::hashed_unique< bmi::tag< name_t >, bmi::const_mem_fun< player_record, const std::string &, &player_record::name > >, bmi::ordered_non_unique< bmi::tag< game_t >, bmi::const_mem_fun< player_record, int, &player_record::game_id > > > > player_connections
Definition: ban.cpp:28
const std::string & termination_reason() const
Definition: game.hpp:297
std::string replay_save_path_
Definition: game.hpp:521
int current_turn_
Definition: game.hpp:505
Standard logging facilities (interface).
user_vector players_
A vector of players (members owning a side).
Definition: game.hpp:465
#define DBG_GAME
Definition: game.cpp:34
std::vector< socket_ptr > user_vector
Definition: game.hpp:32
bool is_muted_observer(const socket_ptr &player) const
Definition: game.cpp:181
void transfer_side_control(const socket_ptr &sock, const simple_wml::node &cfg)
Let&#39;s a player owning a side give it to another player or observer.
Definition: game.cpp:527
std::string message
Definition: exceptions.hpp:31
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:30
std::string password_
Definition: game.hpp:459
#define e
int side_number
Definition: game_info.hpp:39
bool started_
Definition: game.hpp:478
bool send_taken_side(simple_wml::document &cfg, const simple_wml::node *side) const
Definition: game.cpp:382
void copy_into(node &n) const
Definition: simple_wml.cpp:805
std::set< socket_ptr > players_not_advanced_
in multiplayer campaigns it can happen that some players are still in the previousl scenario keep tra...
Definition: game.hpp:516
mock_char c
std::string debug_player_info() const
Helps debugging player and observer lists.
Definition: game.cpp:1935
bool is_member(const socket_ptr &player) const
Definition: game.hpp:67
bool controls_side(const std::vector< int > &sides, const socket_ptr &player) const
Function which returns true iff &#39;player&#39; controls any of the sides spcified in &#39;sides&#39;.
Definition: game.cpp:1715
simple_wml::document level_
The current scenario data.
Definition: game.hpp:496
void clear_history()
Definition: game.cpp:1898
void mute_all_observers()
Definition: game.cpp:715
void send_to_players(simple_wml::document &data, const Container &players, socket_ptr exclude=socket_ptr())
Definition: game.cpp:77
void set_termination_reason(const std::string &reason)
Definition: game.cpp:1911
socket_ptr ban_user(const simple_wml::node &ban, const socket_ptr &banner)
Ban and kick a user by name.
Definition: game.cpp:849
std::string node_to_string(const node &n)
Definition: simple_wml.cpp:793
#define WRN_GAME
Definition: game.cpp:32
bool registered_users_only() const
Definition: game.cpp:171
const std::string observer_team_name
observer team name used for observer team chat
Definition: game_config.cpp:90
filesystem::scoped_ostream ostream_file(const std::string &fname, bool create_directory)
Definition: filesystem.cpp:958
bool save_replays()
Definition: game.cpp:787