The Battle for Wesnoth  1.15.2+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 << "Side " << side_num << " is already controlled by " << newplayer_name << ".";
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  ++nsides_;
1174  side_controllers_.push_back(CONTROLLER::EMPTY);
1175  sides_.emplace_back();
1176 }
1177 
1179 {
1180  const std::size_t side_index = req["side"].to_int() - 1;
1181  CONTROLLER new_controller;
1182  CONTROLLER old_controller;
1183 
1184  if(!new_controller.parse(req["new_controller"])) {
1186  "Could not handle [request_choice] [change_controller] with invalid controller '" + req["new_controller"].to_string() + "'");
1187  return;
1188  }
1189 
1190  if(!old_controller.parse(req["old_controller"])) {
1192  "Could not handle [request_choice] [change_controller] with invalid controller '" + req["old_controller"].to_string() + "'");
1193  return;
1194  }
1195 
1196  if(old_controller != this->side_controllers_[side_index]) {
1198  "Found unexpected old_controller= '" + old_controller.to_string() + "' in [request_choice] [change_controller]");
1199  }
1200 
1201  if(side_index >= sides_.size()) {
1203  "Could not handle [request_choice] [change_controller] with invalid side '" + req["side"].to_string() + "'");
1204  return;
1205  }
1206 
1207  const bool was_null = this->side_controllers_[side_index] == CONTROLLER::EMPTY;
1208  const bool becomes_null = new_controller == CONTROLLER::EMPTY;
1209 
1210  if(was_null) {
1211  assert(sides_[side_index] == 0);
1212  sides_[side_index] = current_player();
1213  }
1214 
1215  if(becomes_null) {
1216  sides_[side_index].reset();
1217  }
1218 
1219  side_controllers_[side_index] = new_controller;
1220 
1222  simple_wml::node& turn = mdata->root().add_child("turn");
1223  simple_wml::node& command = turn.add_child("command");
1224  simple_wml::node& change_controller_wml = command.add_child("change_controller_wml");
1225 
1226  change_controller_wml.set_attr_dup("controller", new_controller.to_cstring());
1227  change_controller_wml.set_attr("is_local", "yes");
1228 
1229  command.set_attr("from_side", "server");
1230  command.set_attr("dependent", "yes");
1231 
1232  // Calling send_to_one to 0 connect causes the package to be sent to all clients.
1233  if(sides_[side_index] != 0) {
1234  send_to_player(sides_[side_index], *mdata);
1235  }
1236 
1237  change_controller_wml.set_attr("is_local", "no");
1238 
1239  send_data(*mdata, sides_[side_index], "game replay");
1240  record_data(mdata);
1241 }
1242 
1243 void game::handle_choice(const simple_wml::node& data, const socket_ptr& user)
1244 {
1245 
1246  if(!started_) {
1247  return;
1248  }
1249 
1250  // note, that during end turn events, it's side=1 for the server but side= side_count() on the clients.
1251 
1252  // Otherwise we allow observers to cause OOS for the playing clients by sending
1253  // server choice requests based on incompatible local changes. To solve this we block
1254  // server choice requests from observers.
1255  if(user != owner_ && !is_player(user)) {
1256  return;
1257  }
1258 
1259  // since we reset the last_choice_request_id_ when a new scenario is loaded,
1260  // the code would otherwise wrongly accept these requests from client in old
1261  // scenarios. which would result on oos.
1262  if(players_not_advanced_.find(user) != players_not_advanced_.end()) {
1263  return;
1264  }
1265 
1266  int request_id = lexical_cast_default<int>(data["request_id"], -10);
1267  if(request_id <= last_choice_request_id_) {
1268  // We gave already an anwer to this request.
1269  return;
1270  }
1271 
1272  DBG_GAME << "answering seed request " << request_id << " by player "
1273  << player_connections_.find(user)->info().name() << "(" << user << ")" << std::endl;
1274  last_choice_request_id_ = request_id;
1275 
1276  if(const simple_wml::node* rand = data.child("random_seed")) {
1277  handle_random_choice(*rand);
1278  } else if(const simple_wml::node* ccw = data.child("change_controller_wml")) {
1280  } else if(const simple_wml::node* asw = data.child("add_side_wml")) {
1281  handle_add_side_wml(*asw);
1282  } else {
1283  send_and_record_server_message("Found unknown server choice request: [" + data.first_child().to_string() + "]");
1284  }
1285 }
1286 
1288 {
1289  if(!started_ || !is_player(user)) {
1290  return;
1291  }
1292 
1293  const simple_wml::node& wb_node = *data.child("whiteboard");
1294 
1295  // Ensure "side" attribute match with user
1296  const simple_wml::string_span& to_sides = wb_node["to_sides"];
1297  std::size_t const side_index = wb_node["side"].to_int() - 1;
1298 
1299  if(side_index >= sides_.size() || sides_[side_index] != user) {
1300  std::ostringstream msg;
1301  msg << "Ignoring illegal whiteboard data, sent from user '" << player_connections_.find(user)->info().name()
1302  << "' which had an invalid side '" << side_index + 1 << "' specified" << std::endl;
1303 
1304  const std::string& msg_str = msg.str();
1305 
1306  LOG_GAME << msg_str << std::endl;
1308  return;
1309  }
1310 
1311  send_data_sides(data, to_sides, user, "whiteboard");
1312 }
1313 
1315 {
1316  if(!started_ || !is_player(user)) {
1317  return;
1318  }
1319 
1320  const simple_wml::node& ctw_node = *data.child("change_turns_wml");
1321  const int current_turn = ctw_node["current"].to_int();
1322  const int num_turns = ctw_node["max"].to_int();
1323  if(num_turns > 10000 || current_turn > 10000) {
1324  // ignore this to prevent errors related to integer overflow.
1325  return;
1326  }
1327 
1328  set_current_turn(current_turn);
1329  num_turns_ = num_turns;
1330 
1331  assert(static_cast<int>(this->current_turn()) == current_turn);
1332 
1333  simple_wml::node* turns_cfg = description_->child("turn_data");
1334  if(!turns_cfg) {
1335  turns_cfg = &description_->add_child("turn_data");
1336  }
1337 
1338  ctw_node.copy_into(*turns_cfg);
1339 
1340  // Don't send or store this change, all players should have gotten it by wml.
1341 }
1342 
1343 bool game::end_turn(int new_side)
1344 {
1345  if(new_side > 0) {
1346  current_side_index_ = new_side - 1;
1347  }
1348  else {
1350  }
1351 
1352  // Skip over empty sides.
1353  for(int i = 0; i < nsides_ && side_controllers_[current_side()] == CONTROLLER::EMPTY; ++i) {
1355  }
1356 
1357  auto res = std::div(current_side_index_, nsides_ > 0 ? nsides_ : 1);
1358 
1359  if(res.quot == 0) {
1360  return false;
1361  }
1362  current_side_index_ = res.rem;
1363  current_turn_ += res.quot;
1364 
1365  if(description_ == nullptr) {
1366  // TODO: why do we need this?
1367  return false;
1368  }
1369 
1370  update_turn_data();
1371 
1372  return true;
1373 }
1374 
1376 {
1377  if(description_ == nullptr) {
1378  return;
1379  }
1380 
1381  simple_wml::node* turns_cfg = description_->child("turn_data");
1382  if(!turns_cfg) {
1383  turns_cfg = &description_->add_child("turn_data");
1384  }
1385 
1386  turns_cfg->set_attr_int("current", current_turn());
1387  turns_cfg->set_attr_int("max", num_turns_);
1388 
1389 }
1390 
1391 ///@todo differentiate between "observers not allowed" and "player already in the game" errors.
1392 // maybe return a string with an error message.
1394 {
1395  if(is_member(player)) {
1396  ERR_GAME << "ERROR: Player is already in this game. (socket: " << player << ")\n";
1397  return false;
1398  }
1399 
1400  socket_ptr user = player;
1401 
1403 
1404  bool became_observer = false;
1405  if(!started_ && !observer && take_side(user)) {
1406  DBG_GAME << "adding player...\n";
1407  players_.push_back(player);
1408 
1409  player_connections_.find(user)->info().set_status(player::PLAYING);
1410 
1411  send_and_record_server_message(player_connections_.find(user)->info().name() + " has joined the game.", player);
1412  } else if(!allow_observers() && !player_connections_.find(user)->info().is_moderator()) {
1413  return false;
1414  } else {
1415  if(!observer) {
1416  became_observer = true;
1417  observer = true;
1418  }
1419 
1420  DBG_GAME << "adding observer...\n";
1421  observers_.push_back(player);
1422  if(!allow_observers()) {
1424  player_connections_.find(user)->info().name() + " is now observing the game.", player);
1425  }
1426 
1427  simple_wml::document observer_join;
1428  observer_join.root()
1429  .add_child("observer")
1430  .set_attr_dup("name", player_connections_.find(user)->info().name().c_str());
1431 
1432  // Send observer join to everyone except the new observer.
1433  send_data(observer_join, player);
1434  }
1435 
1436  LOG_GAME
1437  << client_address(player) << "\t" << player_connections_.find(user)->info().name() << "\tjoined game:\t\""
1438  << name_ << "\" (" << id_ << ")" << (observer ? " as an observer" : "") << ". (socket: " << player
1439  << ")\n";
1440 
1441  player_connections_.find(user)->info().mark_available(id_, name_);
1442  player_connections_.find(user)->info().set_status((observer) ? player::OBSERVING : player::PLAYING);
1444 
1445  // Send the user the game data.
1446  send_to_player(player, level_);
1447 
1448  if(started_) {
1449  // Tell this player that the game has started
1450  static simple_wml::document start_game_doc("[start_game]\n[/start_game]\n", simple_wml::INIT_COMPRESSED);
1451  send_to_player(player, start_game_doc);
1452 
1453  // Send observer join of all the observers in the game to the new player
1454  // only once the game started. The client forgets about it anyway otherwise.
1455  send_observerjoins(player);
1456 
1457  // Send the player the history of the game to-date.
1458  send_history(player);
1459  } else {
1460  send_user_list();
1461  }
1462 
1463  const std::string clones = has_same_ip(player);
1464  if(!clones.empty()) {
1466  player_connections_.find(user)->info().name() + " has the same IP as: " + clones);
1467  }
1468 
1469  if(became_observer) {
1470  // in case someone took the last slot right before this player
1471  send_server_message("You are an observer.", player);
1472  }
1473 
1474  return true;
1475 }
1476 
1477 bool game::remove_player(const socket_ptr& player, const bool disconnect, const bool destruct)
1478 {
1479  if(!is_member(player)) {
1480  ERR_GAME << "ERROR: User is not in this game. (socket: " << player << ")\n";
1481  return false;
1482  }
1483 
1485  DBG_GAME << "removing player...\n";
1486 
1487  const bool host = (player == owner_);
1488  const bool observer = is_observer(player);
1489 
1490  players_.erase(std::remove(players_.begin(), players_.end(), player), players_.end());
1491  observers_.erase(std::remove(observers_.begin(), observers_.end(), player), observers_.end());
1492  players_not_advanced_.erase(player);
1493 
1494  const bool game_ended = players_.empty() || (host && !started_);
1495 
1496  socket_ptr user = player;
1497 
1498  LOG_GAME
1499  << client_address(user)
1500  << "\t" << player_connections_.find(user)->info().name()
1501  << ((game_ended && !(observer && destruct)) ? (started_ ? "\tended" : "\taborted") : "\thas left")
1502  << " game:\t\"" << name_ << "\" (" << id_ << ")"
1503  << (game_ended && started_ && !(observer && destruct)
1504  ? " at turn: " + lexical_cast_default<std::string, std::size_t>(current_turn())
1505  + " with reason: '" + termination_reason() + "'"
1506  : "")
1507  << (observer ? " as an observer" : "") << (disconnect ? " and disconnected" : "") << ". (socket: " << user
1508  << ")\n";
1509 
1510  if(game_ended && started_ && !(observer && destruct)) {
1511  send_server_message_to_all(player_connections_.find(user)->info().name() + " ended the game.", player);
1512  }
1513 
1514  if(game_ended || destruct) {
1515  return game_ended;
1516  }
1517 
1518  // Don't mark_available() since the player got already removed from the
1519  // games_and_users_list_.
1520  if(!disconnect) {
1521  player_connections_.find(user)->info().mark_available();
1522  }
1523 
1524  if(observer) {
1525  send_observerquit(user);
1526  } else {
1527  send_and_record_server_message(player_connections_.find(user)->info().name()
1528  + (disconnect ? " has disconnected." : " has left the game."), player);
1529  }
1530 
1531  // If the player was host choose a new one.
1532  if(host) {
1533  owner_ = players_.front();
1534  notify_new_host();
1535  }
1536 
1537  bool ai_transfer = false;
1538 
1539  // Look for all sides the player controlled and drop them.
1540  // (Give them to the host.
1541  for(unsigned side_index = 0; side_index < sides_.size(); ++side_index) {
1542  const socket_ptr& side = sides_[side_index];
1543 
1544  if(side != player) {
1545  continue;
1546  }
1547 
1548  if(side_controllers_[side_index] == CONTROLLER::AI) {
1549  ai_transfer = true;
1550  }
1551 
1552  change_controller(side_index, owner_, username(owner_));
1553 
1554  // Check whether the host is actually a player and make him one if not.
1555  if(!is_player(owner_)) {
1556  DBG_GAME << "making the owner a player...\n";
1557  auto owner_iter = player_connections_.find(owner_);
1558  if(owner_iter == player_connections_.end())
1559  ERR_GAME << "game owner " << client_address(owner_) << "is not in player_connections_\n";
1560  else {
1561  owner_iter->info().set_status(player::PLAYING);
1562  observers_.erase(std::remove(observers_.begin(), observers_.end(), owner_), observers_.end());
1563  players_.push_back(owner_);
1565  }
1566  }
1567 
1568  // send the host a notification of removal of this side
1569  const std::string side_drop = lexical_cast_default<std::string, std::size_t>(side_index + 1);
1570 
1571  simple_wml::document drop;
1572  auto& node_side_drop = drop.root().add_child("side_drop");
1573 
1574  node_side_drop.set_attr_dup("side_num", side_drop.c_str());
1575  node_side_drop.set_attr_dup("controller", side_controllers_[side_index].to_cstring());
1576 
1577  DBG_GAME << "*** sending side drop: \n" << drop.output() << std::endl;
1578 
1579  send_to_player(owner_, drop);
1580  }
1581 
1582  if(ai_transfer) {
1583  send_and_record_server_message("AI sides transferred to host.");
1584  }
1585 
1587 
1588  send_user_list(player);
1589  return false;
1590 }
1591 
1592 void game::send_user_list(const socket_ptr& exclude) const
1593 {
1594  // If the game hasn't started yet, then send all players a list of the users in the game.
1595  if(started_ /*|| description_ == nullptr*/) {
1596  return;
1597  }
1598 
1600  simple_wml::node& list = cfg.root();
1601 
1602  for(const socket_ptr& user_ptr : all_game_users()) {
1603  const auto pl = player_connections_.find(user_ptr);
1604 
1605  if(pl != player_connections_.end()) {
1606  simple_wml::node& user = list.add_child("user");
1607 
1608  // Don't need to duplicate pl->info().name().c_str() because the
1609  // document will be destroyed by the end of the function
1610  user.set_attr_dup("name", pl->info().name().c_str());
1611  user.set_attr("host", is_owner(user_ptr) ? "yes" : "no");
1612  user.set_attr("observer", is_observer(user_ptr) ? "yes" : "no");
1613  }
1614  }
1615 
1616  send_data(cfg, exclude);
1617 }
1618 
1619 void game::new_scenario(const socket_ptr& sender)
1620 {
1621  assert(sender == owner_);
1622  players_not_advanced_.clear();
1623  for(const socket_ptr& user_ptr : all_game_users()) {
1624  if(user_ptr != sender) {
1625  players_not_advanced_.insert(user_ptr);
1626  }
1627  }
1628  started_ = false;
1629 }
1630 
1632 {
1633  send_server_message_to_all(player_connections_.find(user)->info().name() + " advances to the next scenario", user);
1634 
1635  simple_wml::document cfg_scenario;
1636  simple_wml::node& next_scen = cfg_scenario.root().add_child("next_scenario");
1637  level_.root().copy_into(next_scen);
1638  next_scen.set_attr("started", started_ ? "yes" : "no");
1639 
1640  DBG_GAME << "****\n loading next scenario for a client. sides info = " << std::endl;
1641  DBG_GAME << debug_sides_info() << std::endl;
1642  DBG_GAME << "****" << std::endl;
1643 
1644  //
1645  // Change the controller to match that client.
1646  //
1647  // FIXME: This breaks scenario transitions with mp connect screen shown.
1648  //
1649  // FIXME: This causes bugs, esp if controller have changed since the
1650  // beginning of the next scenario
1651  //
1652  // There are currently 2 possible ideas to fix this issue:
1653  //
1654  // 1) When the scenario starts, we store the controllers at that
1655  // point and use that data when a client loads the the next
1656  // scenario (here)
1657  //
1658  // 2) When a client loads the next scenario we send it the
1659  // observers' starting point (meaning we don't change sides
1660  // here), and then we send that side an automatic controller
1661  // change later.
1662  //
1663  simple_wml::document doc_controllers;
1664  simple_wml::node& cfg_controllers = doc_controllers.root().add_child("controllers");
1665 
1666  for(const auto& side_user : sides_) {
1667  simple_wml::node& cfg_controller = cfg_controllers.add_child("controller");
1668  cfg_controller.set_attr("is_local", side_user == user ? "yes" : "no");
1669  }
1670 
1671  send_to_player(user, cfg_scenario);
1672  send_to_player(user, doc_controllers);
1673 
1674  players_not_advanced_.erase(user);
1675 
1676  // Send the player the history of the game to-date.
1677  send_history(user);
1678 
1679  // Send observer join of all the observers in the game to the user.
1680  send_observerjoins(user);
1681 }
1682 
1683 void game::send_data(simple_wml::document& data, const socket_ptr& exclude, std::string /*packet_type*/) const
1684 {
1685  send_to_players(data, all_game_users(), exclude);
1686 }
1687 
1688 namespace
1689 {
1690 struct controls_side_helper
1691 {
1693  const std::vector<int>& sides_;
1694 
1695  controls_side_helper(const wesnothd::game& game, const std::vector<int>& sides)
1696  : game_(game)
1697  , sides_(sides)
1698  {
1699  }
1700 
1701  bool operator()(socket_ptr user) const
1702  {
1703  return game_.controls_side(sides_, user);
1704  }
1705 };
1706 }
1707 
1709  const simple_wml::string_span& sides,
1710  const socket_ptr& exclude,
1711  std::string /*packet_type*/) const
1712 {
1713  std::vector<int> sides_vec = ::split<int>(sides, ::split_conv_impl());
1714 
1715  DBG_GAME << __func__ << "...\n";
1716 
1717  decltype(players_) filtered_players;
1718 
1719  std::copy_if(players_.begin(), players_.end(), std::back_inserter(filtered_players),
1720  controls_side_helper(*this, sides_vec));
1721 
1722  send_to_players(data, filtered_players, exclude);
1723 }
1724 
1725 bool game::controls_side(const std::vector<int>& sides, const socket_ptr& player) const
1726 {
1727  for(int side : sides) {
1728  std::size_t side_index = side - 1;
1729 
1730  if(side_index < sides_.size() && sides_[side_index] == player) {
1731  return true;
1732  }
1733  }
1734 
1735  return false;
1736 }
1737 
1738 std::string game::has_same_ip(const socket_ptr& user) const
1739 {
1740  const user_vector users = all_game_users();
1741  const std::string ip = client_address(user);
1742 
1743  std::string clones;
1744  for(const socket_ptr& u : users) {
1745  if(ip == client_address(u) && user != u) {
1746  const auto pl = player_connections_.find(u);
1747 
1748  if(pl != player_connections_.end()) {
1749  clones += (clones.empty() ? "" : ", ") + pl->info().name();
1750  }
1751  }
1752  }
1753 
1754  return clones;
1755 }
1756 
1757 void game::send_observerjoins(const socket_ptr& sock) const
1758 {
1759  for(const socket_ptr& ob : observers_) {
1760  if(ob == sock) {
1761  continue;
1762  }
1763 
1765  cfg.root().add_child("observer").set_attr_dup("name", player_connections_.find(ob)->info().name().c_str());
1766 
1767  if(sock == socket_ptr()) {
1768  // Send to everyone except the observer in question.
1769  send_data(cfg, ob);
1770  } else {
1771  // Send to the (new) user.
1772  send_to_player(sock, cfg);
1773  }
1774  }
1775 }
1776 
1778 {
1779  simple_wml::document observer_quit;
1780 
1781  // Don't need to dup the attribute because this document is short-lived.
1782  observer_quit.root()
1783  .add_child("observer_quit")
1784  .set_attr_dup("name", player_connections_.find(observer)->info().name().c_str());
1785 
1786  send_data(observer_quit, observer);
1787 }
1788 
1789 void game::send_history(const socket_ptr& socket) const
1790 {
1791  if(history_.empty()) {
1792  return;
1793  }
1794 
1795  // we make a new document based on converting to plain text and
1796  // concatenating the buffers.
1797  // TODO: Work out how to concentate buffers without decompressing.
1798  std::string buf;
1799  for(auto& h : history_) {
1800  buf += h.output();
1801  }
1802 
1803  try {
1805  doc->compress();
1806 
1807  send_to_player(socket, *doc);
1808 
1809  history_.clear();
1810  history_.push_back(doc);
1811  } catch(const simple_wml::error& e) {
1812  WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
1813  }
1814 }
1815 
1816 static bool is_invalid_filename_char(char c)
1817 {
1818  return !(isalnum(c) ||
1819  (c == '_') ||
1820  (c == '-') ||
1821  (c == '.') ||
1822  (c == '(') ||
1823  (c == ')') ||
1824  (c == '#') ||
1825  (c == ',') ||
1826  (c == '!') ||
1827  (c == '^') ||
1828  (c == '+') ||
1829  (c == '=') ||
1830  (c == '@') ||
1831  (c == '%') ||
1832  (c == '\'')
1833  );
1834 }
1835 
1837 {
1838  std::stringstream name;
1839  name << (*starting_pos(level_.root()))["name"] << " Turn " << current_turn() << " (" << id_ << ").bz2";
1840  std::string filename(name.str());
1841  std::replace(filename.begin(), filename.end(), ' ', '_');
1842  filename.erase(std::remove_if(filename.begin(), filename.end(), is_invalid_filename_char), filename.end());
1843  return filename;
1844 }
1845 
1847 {
1848  if(!save_replays_ || !started_ || history_.empty()) {
1849  return;
1850  }
1851 
1852  std::string replay_commands;
1853  for(const auto& h : history_) {
1854  const simple_wml::node::child_list& turn_list = h.root().children("turn");
1855 
1856  for(const simple_wml::node* turn : turn_list) {
1857  replay_commands += simple_wml::node_to_string(*turn);
1858  }
1859  }
1860 
1861  history_.clear();
1862 
1863  std::stringstream replay_data;
1864  try {
1865  // level_.set_attr_dup("label", name.str().c_str());
1866 
1867  // Used by replays.wesnoth.org as of December 2017. No client usecases.
1868  level_.set_attr_dup("mp_game_title", name_.c_str());
1869 
1870  const bool has_old_replay = level_.child("replay") != nullptr;
1871 
1872  // If there is already a replay in the level_, which means this is a reloaded game,
1873  // then we don't need to add the [start] in the replay.
1874  replay_data
1875  << level_.output()
1876  // This can result in having 2 [replay] at toplevel since level_ can contain one already. But the
1877  // client can handle this (simply merges them).
1878  << "[replay]\n"
1879  // The [start] is generated at the clients and not sent over the network so we add it here.
1880  // It usually contains some checkup data that is used to check whether the calculated results
1881  // match the ones calculated in the replay. But that's not necessary
1882  << (has_old_replay ? "" : "\t[command]\n\t\t[start]\n\t\t[/start]\n\t[/command]\n")
1883  << replay_commands << "[/replay]\n";
1884 
1885  std::string replay_data_str = replay_data.str();
1886  simple_wml::document replay(replay_data_str.c_str(), simple_wml::INIT_STATIC);
1887 
1888  std::string filename = get_replay_filename();
1889  DBG_GAME << "saving replay: " << filename << std::endl;
1890 
1892  (*os) << replay.output_compressed(true);
1893 
1894  if(!os->good()) {
1895  ERR_GAME << "Could not save replay! (" << filename << ")" << std::endl;
1896  }
1897  } catch(const simple_wml::error& e) {
1898  WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
1899  }
1900 }
1901 
1903 {
1904  data->compress();
1905  history_.push_back(data);
1906 }
1907 
1909 {
1910  history_.clear();
1911 }
1912 
1914 {
1915  description_ = desc;
1916  if(!password_.empty()) {
1917  description_->set_attr("password", "yes");
1918  }
1919 }
1920 
1921 void game::set_termination_reason(const std::string& reason)
1922 {
1923  /* if (reason == "out of sync") {
1924  simple_wml::string_span era;
1925  if (level_.child("era")) {
1926  era = level_.child("era")->attr("id");
1927  }
1928  termination_ = "out of sync - " + era.to_string();
1929  }*/
1930  if(termination_.empty()) {
1931  termination_ = reason;
1932  }
1933 }
1934 
1936 {
1937  user_vector res;
1938 
1939  res.insert(res.end(), players_.begin(), players_.end());
1940  res.insert(res.end(), observers_.begin(), observers_.end());
1941 
1942  return res;
1943 }
1944 
1945 std::string game::debug_player_info() const
1946 {
1947  std::stringstream result;
1948  result << "game id: " << id_ << "\n";
1949 
1950  // result << "players_.size: " << players_.size() << "\n";
1951  for(const socket_ptr& p : players_) {
1952  const auto user = player_connections_.find(p);
1953 
1954  if(user != player_connections_.end()) {
1955  result << "player: " << user->info().name().c_str() << "\n";
1956  } else {
1957  result << "player: '" << p << "' not found\n";
1958  }
1959  }
1960 
1961  // result << "observers_.size: " << observers_.size() << "\n";
1962  for(const socket_ptr& o : observers_) {
1963  const auto user = player_connections_.find(o);
1964 
1965  if(user != player_connections_.end()) {
1966  result << "observer: " << user->info().name().c_str() << "\n";
1967  } else {
1968  result << "observer: '" << o << "' not found\n";
1969  }
1970  }
1971  /* result << "player_info_: begin\n";
1972  for (player_map::const_iterator info = player_info_->begin(); info != player_info_->end(); info++){
1973  result << info->second.name().c_str() << "\n";
1974  }
1975  result << "player_info_: end\n";*/
1976  return result.str();
1977 }
1978 
1979 std::string game::debug_sides_info() const
1980 {
1981  std::stringstream result;
1982  result << "game id: " << id_ << "\n";
1984 
1985  result << "\t\t level, server\n";
1986 
1987  for(const simple_wml::node* s : sides) {
1988  result
1989  << "side " << (*s)["side"].to_int()
1990  << " :\t" << (*s)["controller"].to_string()
1991  << "\t, " << side_controllers_[(*s)["side"].to_int() - 1].to_cstring()
1992  << "\t( " << sides_[(*s)["side"].to_int() - 1]
1993  << ",\t" << (*s)["current_player"].to_string() << " )\n";
1994  }
1995 
1996  return result.str();
1997 }
1998 
2000 {
2001  const auto iter = player_connections_.get<name_t>().find(name.to_string());
2002  if(iter != player_connections_.get<name_t>().end()) {
2003  return iter->socket();
2004  } else {
2005  return socket_ptr();
2006  }
2007 }
2008 
2009 void game::send_and_record_server_message(const char* message, const socket_ptr& exclude)
2010 {
2012  send_server_message(message, socket_ptr(), doc);
2013  send_data(*doc, exclude, "message");
2014 
2015  if(started_) {
2016  record_data(doc);
2017  } else {
2018  delete doc;
2019  }
2020 }
2021 
2022 void game::send_server_message_to_all(const char* message, const socket_ptr& exclude) const
2023 {
2025  send_server_message(message, socket_ptr(), &doc);
2026  send_data(doc, exclude, "message");
2027 }
2028 
2029 void game::send_server_message(const char* message, const socket_ptr& sock, simple_wml::document* docptr) const
2030 {
2031  simple_wml::document docbuf;
2032  if(docptr == nullptr) {
2033  docptr = &docbuf;
2034  }
2035 
2036  simple_wml::document& doc = *docptr;
2037 
2038  if(started_) {
2039  simple_wml::node& cmd = doc.root().add_child("turn");
2040  simple_wml::node& cfg = cmd.add_child("command");
2041  cfg.set_attr("undo", "no");
2042  simple_wml::node& msg = cfg.add_child("speak");
2043 
2044  msg.set_attr("id", "server");
2045  msg.set_attr_dup("message", message);
2046  } else {
2047  simple_wml::node& msg = doc.root().add_child("message");
2048 
2049  msg.set_attr("sender", "server");
2050  msg.set_attr_dup("message", message);
2051  }
2052 
2053  if(sock) {
2054  send_to_player(sock, doc);
2055  }
2056 }
2057 
2058 bool game::is_reload() const
2059 {
2060  const simple_wml::node& multiplayer = get_multiplayer(level_.root());
2061  return multiplayer.has_attr("savegame") && multiplayer["savegame"].to_bool();
2062 }
2063 
2064 } // 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:1477
void send_data(simple_wml::document &data, const socket_ptr &exclude=socket_ptr(), std::string packet_type="") const
Definition: game.cpp:1683
const char * const_iterator
Definition: simple_wml.hpp:45
bool save_replays_
Definition: game.hpp:529
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:1619
const std::string & name() const
Definition: game.hpp:57
void save_replay()
Definition: game.cpp:1846
player_connections & player_connections_
Definition: game.hpp:461
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:1789
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:478
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:2009
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:1287
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:337
int nsides_
Number of sides in the current scenario.
Definition: game.hpp:486
user_vector observers_
A vector of observers (members not owning a side).
Definition: game.hpp:477
void send_server_message(const char *message, const socket_ptr &sock=socket_ptr(), simple_wml::document *doc=nullptr) const
Definition: game.cpp:2029
socket_ptr find_user(const simple_wml::string_span &name)
Shortcut to a convenience function for finding a user by name.
Definition: game.cpp:1999
bool all_observers_muted_
Definition: game.hpp:517
#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:527
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:533
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:2022
history history_
Definition: game.hpp:509
const socket_ptr current_player() const
Definition: game.hpp:342
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:520
const wesnothd::game & game_
Definition: game.cpp:1692
node * child(const char *name)
Definition: simple_wml.hpp:260
const char * output()
node * child(const char *name)
Definition: simple_wml.cpp:606
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:1314
int last_choice_request_id_
Definition: game.hpp:534
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:1343
void reset_sides()
calculates the initial value for sides_, side_controllerds_, nsides_
Definition: game.cpp:444
std::vector< CONTROLLER > side_controllers_
Definition: game.hpp:483
int num_turns_
Definition: game.hpp:516
void update_turn_data()
Definition: game.cpp:1375
bool allow_observers() const
Definition: game.cpp:166
void handle_controller_choice(const simple_wml::node &data)
Definition: game.cpp:1178
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:1816
void load_next_scenario(const socket_ptr &user)
A user (player only?) asks for the next scenario to advance to.
Definition: game.cpp:1631
std::string get_replay_filename()
Definition: game.cpp:1836
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:1757
#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:1935
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:1738
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:1393
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:1827
std::unique_ptr< std::ostream > scoped_ostream
Definition: filesystem.hpp:40
void send_observerquit(const socket_ptr &observer) const
Definition: game.cpp:1777
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:471
static simple_wml::node * starting_pos(simple_wml::node &data)
Definition: game.hpp:88
const std::vector< int > & sides_
Definition: game.cpp:1693
void send_user_list(const socket_ptr &exclude=socket_ptr()) const
Function to send a list of users to all clients.
Definition: game.cpp:1592
std::size_t i
Definition: function.cpp:933
std::vector< std::string > name_bans_
Definition: game.hpp:521
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:347
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:515
void record_data(simple_wml::document *data)
Definition: game.cpp:1902
side_vector sides_
A vector of side owners.
Definition: game.hpp:481
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:1243
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:1913
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
void handle_add_side_wml(const simple_wml::node &req)
Definition: game.cpp:1171
simple_wml::node * description_
Pointer to the game&#39;s description in the games_and_users_list_.
Definition: game.hpp:512
std::string name_
The name of the game.
Definition: game.hpp:467
static lg::log_domain log_config("config")
std::string debug_sides_info() const
Helps debugging controller tweaks.
Definition: game.cpp:1979
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:1708
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
bool is_reload() const
Definition: game.cpp:2058
Definition: ban.cpp:28
const std::string & termination_reason() const
Definition: game.hpp:302
std::string replay_save_path_
Definition: game.hpp:530
int current_turn_
Definition: game.hpp:514
Standard logging facilities (interface).
user_vector players_
A vector of players (members owning a side).
Definition: game.hpp:474
#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:468
#define e
int side_number
Definition: game_info.hpp:39
bool started_
Definition: game.hpp:487
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:525
mock_char c
std::string debug_player_info() const
Helps debugging player and observer lists.
Definition: game.cpp:1945
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:1725
simple_wml::document level_
The current scenario data.
Definition: game.hpp:505
void clear_history()
Definition: game.cpp:1908
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:1921
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)
bool save_replays()
Definition: game.cpp:798