The Battle for Wesnoth  1.17.0-dev
game.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "mt_rng.hpp"
22 #include "utils/make_enum.hpp"
23 
24 #include <map>
25 #include <optional>
26 #include <vector>
27 
28 // class player;
29 
30 namespace wesnothd
31 {
32 typedef std::vector<player_iterator> user_vector;
33 typedef std::vector<std::optional<player_iterator>> side_vector;
34 class server;
35 
36 class game
37 {
38 public:
39  MAKE_ENUM(CONTROLLER,
40  (HUMAN, "human")
41  (AI, "ai")
42  (EMPTY, "null")
43  );
44 
46  player_iterator host,
47  const std::string& name = "",
48  bool save_replays = false,
49  const std::string& replay_save_path = "");
50 
51  ~game();
52 
53  /**
54  * This ID is reused between scenarios of MP campaigns.
55  * This ID resets when wesnothd is restarted.
56  * This is generally used when needing to find a particular running game.
57  * @return an ID that uniquely identifies the game within the currently running wesnothd instance.
58  */
59  int id() const
60  {
61  return id_;
62  }
63 
64  /**
65  * This ID is not reused between scenarios of MP campaigns.
66  * This ID resets when wesnothd is restarted.
67  * This is generally used during database queries.
68  *
69  * @return an ID that uniquely identifies the game within the currently running wesnothd instance.
70  */
71  int db_id() const
72  {
73  return db_id_;
74  }
75 
76  /**
77  * Increments the ID used when running database queries.
78  */
79  void next_db_id()
80  {
81  db_id_ = db_id_num++;
82  }
83 
84  /**
85  * @return The game's name.
86  */
87  const std::string& name() const
88  {
89  return name_;
90  }
91 
92  /**
93  * @param player The player being checked.
94  * @return Whether the provided player is the game's owner(host).
95  */
97  {
98  return (player == owner_);
99  }
100 
101  /**
102  * @param player The player being checked.
103  * @return Whether the provided player has joined the game.
104  */
106  {
107  return is_player(player) || is_observer(player);
108  }
109 
110  /**
111  * @return Whether observers are allowed to join.
112  */
113  bool allow_observers() const;
114 
115  /**
116  * @param player The player being checked.
117  * @return Whether the provided player is an observer of this game.
118  */
119  bool is_observer(player_iterator player) const;
120 
121  /**
122  * @param player The player being checked.
123  * @return Whether the provided player is playing this game (aka owns one or more sides).
124  */
125  bool is_player(player_iterator player) const;
126 
127  /**
128  * @param player The player being checked (by iterator).
129  * @param name The player being checked (by username).
130  * @return Whether the connection's ip address or username is banned from this game.
131  */
132  bool player_is_banned(player_iterator player, const std::string& name) const;
133 
134  /**
135  * When the host sends the new scenario of a mp campaign
136  *
137  * @param sender The player sending the scenario data.
138  */
139  void new_scenario(player_iterator sender);
140 
141  /**
142  * @return Whether this game contains scenario data and thus has been initialized.
143  */
144  bool level_init() const
145  {
146  return level_.child("snapshot") || level_.child("scenario");
147  }
148 
149  /**
150  * The non-const version.
151  *
152  * @param data The data describing the level for a game.
153  * @return The [scenario] child node if it exists, else the [snapshot] child if it exists, else @a data.
154  */
156  {
157  if(simple_wml::node* scenario = data.child("scenario")) {
158  return scenario;
159  } else if(simple_wml::node* snapshot = data.child("snapshot")) {
160  return snapshot;
161  }
162 
163  return &data;
164  }
165 
166  /**
167  * The const version.
168  *
169  * @param data The data describing the level for a game.
170  * @return The [scenario] child node if it exists, else the [snapshot] child if it exists, else @a data.
171  */
172  static const simple_wml::node* starting_pos(const simple_wml::node& data)
173  {
174  if(const simple_wml::node* scenario = data.child("scenario")) {
175  return scenario;
176  } else if(const simple_wml::node* snapshot = data.child("snapshot")) {
177  return snapshot;
178  }
179 
180  return &data;
181  }
182 
183  /**
184  * @return The nodes containing the sides in this game.
185  */
187  {
188  return starting_pos(level_.root())->children("side");
189  }
190 
191  /**
192  * @return Whether this game has started yet.
193  */
194  bool started() const
195  {
196  return started_;
197  }
198 
199  /**
200  * @return The number of players. One player can have multiple sides.
201  */
202  std::size_t nplayers() const
203  {
204  return players_.size();
205  }
206 
207  /**
208  * @return The number of observers in this game.
209  */
210  std::size_t nobservers() const
211  {
212  return observers_.size();
213  }
214 
215  /**
216  * @return This game's current turn.
217  */
218  std::size_t current_turn() const
219  {
220  return current_turn_;
221  }
222 
223  /**
224  * @return The name of the replay for this game.
225  */
226  std::string get_replay_filename();
227 
228  /** Toggles whether all observers are muted or not. */
229  void mute_all_observers();
230 
231  /**
232  * Mute an observer or give a message of all currently muted observers if no name is given.
233  *
234  * @param mute The observer to mute. Empty if sending a message to muted observers.
235  * @param muter The player doing the muting.
236  */
237  void mute_observer(const simple_wml::node& mute, player_iterator muter);
238 
239  /**
240  * Unmute an observer or unmute all currently muted observers if no name is given.
241  *
242  * @param unmute The observer to unmute. Empty if unmuting all observers.
243  * @param unmuter The player doing the unmuting.
244  */
245  void unmute_observer(const simple_wml::node& unmute, player_iterator unmuter);
246 
247  /**
248  * Kick a user from this game by name.
249  *
250  * @param kick The user to kick.
251  * @param kicker The player doing the kicking.
252  * @return The iterator to the removed member if successful, empty optional otherwise.
253  */
254  std::optional<player_iterator> kick_member(const simple_wml::node& kick, player_iterator kicker);
255 
256  /**
257  * Ban a user by name.
258  *
259  * The user does not need to be in this game but logged in.
260  *
261  * @param ban The user to ban.
262  * @param banner The player doing the banning.
263  * @return The iterator to the banned player if he was in this game, empty optional otherwise.
264  */
265  std::optional<player_iterator> ban_user(const simple_wml::node& ban, player_iterator banner);
266 
267  /**
268  * Unban a user by name.
269  *
270  * The user does not need to be in this game but logged in.
271  *
272  * @param unban The user to unban.
273  * @param unbanner The player doing the unbanning.
274  */
275  void unban_user(const simple_wml::node& unban, player_iterator unbanner);
276 
277  /**
278  * Add a user to the game.
279  *
280  * @todo differentiate between "observers not allowed" and "player already in the game" errors.
281  * maybe return a string with an error message.
282  *
283  * @param player The player to add.
284  * @param observer Whether to add the player as an observer.
285  * @return True if the user successfully joined the game, false otherwise.
286  */
287  bool add_player(player_iterator player, bool observer = false);
288 
289  /**
290  * Removes a user from the game.
291  *
292  * @param player The player to remove.
293  * @param disconnect If the player disconnected from the server entirely.
294  * @param destruct If the game is ending as well.
295  * @return True if the player's removal ends the game. That is, if there are no more players or the host left on a not yet started game.
296  */
297  bool remove_player(player_iterator player, const bool disconnect = false, const bool destruct = false);
298 
299  /**
300  * @return A vector containing all players and observers currently in this game.
301  */
302  const user_vector all_game_users() const;
303 
304  /**
305  * Starts the game (if a new game) or starts the next scenario of an MP campaign.
306  * @param starter The game's host.
307  */
308  void start_game(player_iterator starter);
309 
310  /**
311  * This is performed just before starting and before the [start_game] signal.
312  * Sends [scenario_diff]s specific to each client so that they locally control their human sides.
313  */
315 
316  /**
317  * A user asks for the next scenario to advance to.
318  *
319  * @param user The user asking for the next scenario.
320  */
322 
323  /** Resets the side configuration according to the scenario data. */
324  void update_side_data();
325 
326  /**
327  * Lets a player owning a side give it to another player or observer.
328  *
329  * @param player The player owning the side.
330  * @param cfg The node containing the transfer information.
331  */
332  void transfer_side_control(player_iterator player, const simple_wml::node& cfg);
333 
334  /**
335  * Sends an ingame message to all other players.
336  *
337  * @param data The message to send.
338  * @param user The user sending the message.
339  */
341 
342  /**
343  * Handles [end_turn], repackages [commands] with private [speak]s in them
344  * and sends the data.
345  * Also filters commands from all but the current player.
346  * Currently removes all commands but [speak] for observers and all but
347  * [speak], [label], and [rename] for players.
348  *
349  * @param data The turn commands.
350  * @param user The user who sent a command to be processed during the turn. This may not be the player whose turn it currently is.
351  * @returns True if the turn ended.
352  */
354 
355  /**
356  * Handles incoming [whiteboard] data.
357  *
358  * @param data The whiteboard data.
359  * @param user The user sending the whiteboard data.
360  */
362 
363  /**
364  * Handles incoming [change_turns_wml] data.
365  *
366  * @param data The [change_turns_wml] data.
367  * @param user The player changing turns.
368  */
370 
371  /**
372  * Set the description to the number of available slots.
373  *
374  * @returns True if the number of slots has changed.
375  */
376  bool describe_slots();
377 
378  /**
379  * Sends a message to all players in this game that aren't excluded.
380  *
381  * @param message The message to send.
382  * @param exclude The players to not send the message to.
383  */
384  void send_server_message_to_all(const char* message, std::optional<player_iterator> exclude = {});
385  /**
386  * @ref send_server_message_to_all
387  */
388  void send_server_message_to_all(const std::string& message, std::optional<player_iterator> exclude = {})
389  {
390  send_server_message_to_all(message.c_str(), exclude);
391  }
392 
393  /**
394  * Send a server message to the specified player.
395  *
396  * @param message The message to send.
397  * @param player The player to send the message to. If empty then the message is not sent.
398  * @param doc The document to create the message in. If nullptr then a new document is created.
399  */
400  void send_server_message(
401  const char* message, std::optional<player_iterator> player = {}, simple_wml::document* doc = nullptr) const;
402  /**
403  * @ref send_server_message
404  */
406  const std::string& message, std::optional<player_iterator> player = {}, simple_wml::document* doc = nullptr) const
407  {
408  send_server_message(message.c_str(), player, doc);
409  }
410 
411  /**
412  * Send data to all players in this game except 'exclude'.
413  * Also record this data for the replay.
414  *
415  * @param message The message to send.
416  * @param exclude The players to not send the message to.
417  */
418  void send_and_record_server_message(const char* message, std::optional<player_iterator> exclude = {});
419  /**
420  * @ref send_and_record_server_message
421  */
422  void send_and_record_server_message(const std::string& message, std::optional<player_iterator> exclude = {})
423  {
424  send_and_record_server_message(message.c_str(), exclude);
425  }
426 
427  /**
428  * Send data to all players except those excluded.
429  * For example, to send a message to all players except the player who typed the original message.
430  *
431  * @param data The data to send.
432  * @param players The players to send the data to.
433  * @param exclude The player from @a players to not send the data to.
434  */
435  template<typename Container>
436  void send_to_players(simple_wml::document& data, const Container& players, std::optional<player_iterator> exclude = {});
437 
438  /**
439  * Send data to all players and observers except those excluded.
440  *
441  * @param data The data to send.
442  * @param exclude The players/observers to not send the data to.
443  */
444  void send_data(simple_wml::document& data, std::optional<player_iterator> exclude = {});
445 
446  /**
447  * Clears the history of recorded WML documents.
448  */
449  void clear_history();
450 
451  /**
452  * Records a WML document in the game's history.
453  *
454  * @param data The WML document to record.
455  */
456  void record_data(std::unique_ptr<simple_wml::document> data);
457 
458  /**
459  * Move the level information and recorded history into a replay file and save it.
460  */
461  void save_replay();
462 
463  /**
464  * @return The full scenario data.
465  */
467  {
468  return level_;
469  }
470 
471  /**
472  * Set the game's description.
473  * Also set the game as requiring a password if a password is set.
474  *
475  * @param desc The node containing the game's description.
476  */
477  void set_description(simple_wml::node* desc);
478 
479  /**
480  * @return The node containing the game's current description.
481  */
483  {
484  return description_;
485  }
486 
487  /**
488  * Sets the password required to access the game.
489  *
490  * @param passwd The password to set.
491  */
492  void set_password(const std::string& passwd)
493  {
494  password_ = passwd;
495  }
496 
497  /**
498  * Set a list of usernames that should all be banned from joining the game.
499  *
500  * @param name_bans The list of usernames.
501  */
502  void set_name_bans(const std::vector<std::string> name_bans)
503  {
504  name_bans_ = name_bans;
505  }
506 
507  /**
508  * @param passwd The password to join with.
509  * @return True if the game's password is empty or if the provided password matches, false otherwise.
510  */
511  bool password_matches(const std::string& passwd) const
512  {
513  return password_.empty() || passwd == password_;
514  }
515 
516  /**
517  * @return Whether the game has a password set.
518  */
519  bool has_password() const
520  {
521  return !password_.empty();
522  }
523 
524  /**
525  * Provides the reason the game was ended.
526  *
527  * @return Either that the game was aborted (after starting), not started, or has some other reason set.
528  */
529  const std::string& termination_reason() const
530  {
531  static const std::string aborted = "aborted";
532  static const std::string not_started = "not started";
533 
534  return started_ ? (termination_.empty() ? aborted : termination_) : not_started;
535  }
536 
537  /**
538  * Sets the termination reason for this game.
539  *
540  * @param reason The termination reason.
541  */
542  void set_termination_reason(const std::string& reason);
543 
544  /**
545  * Handle a choice requested by a client, such as changing a side's controller, if initiated by WML/lua.
546  *
547  * @param data The data needed to process the choice.
548  * @param user The player making the request.
549  */
550  void handle_choice(const simple_wml::node& data, player_iterator user);
551 
552  /**
553  * Send a randomly generated number to the requestor.
554  */
555  void handle_random_choice();
556 
557  /**
558  * Handle a request to change a side's controller.
559  * Note that this does not change who owns a side.
560  *
561  * @param data Contains the information about which side to change the controller of.
562  */
563  void handle_controller_choice(const simple_wml::node& data);
564 
565  /**
566  * Adds a new, empty side owned by no one.
567  */
568  void handle_add_side_wml();
569 
570  /**
571  * Reset the internal counter for choice requests made by clients to the server.
572  */
574  {
576  }
577 
578  /**
579  * Function which returns true if 'player' controls any of the sides specified in 'sides'.
580  *
581  * @param sides The list of sides in this game.
582  * @param player The player being checked for whether they own any sides.
583  */
584  bool controls_side(const std::vector<int>& sides, player_iterator player) const;
585 
586  /**
587  * @return Whether the loaded WML has the attribute indicating that this is a reloaded savegame rather than a brand new game.
588  */
589  bool is_reload() const;
590 
591 private:
592  // forbidden operations
593  game(const game&) = delete;
594  game& operator=(const game&) = delete;
595 
596  /**
597  * @return 0 if there are no sides, or the current side index otherwise.
598  */
599  std::size_t current_side() const
600  {
601  return nsides_ != 0 ? (current_side_index_ % nsides_) : 0;
602  }
603 
604  /**
605  * @return The player who owns the current side.
606  */
607  std::optional<player_iterator> current_player() const
608  {
609  return sides_[current_side()];
610  }
611 
612  /**
613  * @param player The player being checked.
614  * @return Whether the player being checked is the current player taking their turn.
615  */
617  {
618  return (current_player() == player);
619  }
620 
621  /**
622  * @param player The observer being checked.
623  * @return True if the observer is muted or if all observers are muted, false otherwise.
624  */
625  bool is_muted_observer(player_iterator player) const;
626 
627  /**
628  * @return True if all observers have been muted via that command (not if each individual observer happens to have been manually muted).
629  */
630  bool all_observers_muted() const
631  {
632  return all_observers_muted_;
633  }
634 
635  /**
636  * Sends a message either stating that all observers are muted or listing the observers that are muted.
637  *
638  * @param user The player to send the message to.
639  */
640  void send_muted_observers(player_iterator user) const;
641 
642  /**
643  * Tell the host who owns a side.
644  *
645  * @param cfg The document to send to the host.
646  * @param side The side information to send.
647  * @return True if the document was sent, false otherwise.
648  */
649  bool send_taken_side(simple_wml::document& cfg, const simple_wml::node* side) const;
650 
651  /**
652  * Figures out which side to take and tells that side to the game owner.
653  *
654  * The owner then should send a [scenario_diff] that implements the side
655  * change and a subsequent update_side_data() call makes it actually
656  * happen.
657  * First we look for a side where save_id= or current_player= matches the
658  * new user's name then we search for the first controller="network" side.
659  *
660  * @param user The player taking a side.
661  * @return True if the side was taken, false otherwise.
662  */
663  bool take_side(player_iterator user);
664 
665  /**
666  * Send [change_controller] message to tell all clients the new controller's name or controller type (human or ai).
667  *
668  * @param side_index The index of the side whose controller is changing.
669  * @param player The player who is taking control of the side.
670  * @param player_name The name of the player who is taking control of the side.
671  * @param player_left We use the "player_left" field as follows. Normally change_controller sends one message to the owner, and one message to everyone else.
672  * In case that a player drops, the owner is gone and should not get a message, instead the host gets a [side_drop] message.
673  */
674  void change_controller(const std::size_t side_index,
675  player_iterator player,
676  const std::string& player_name,
677  const bool player_left = true);
678 
679  /**
680  * Tell everyone else but the source player that the controller type changed.
681  *
682  * @param side_index The index of the side whose controller type is changing.
683  * @param player The player who owns the side whose controller type is changing.
684  * @param player_name The name of the player who owns the side whose controller type is changing.
685  * @return The document that was sent to all other players.
686  */
687  std::unique_ptr<simple_wml::document> change_controller_type(const std::size_t side_index,
688  player_iterator player,
689  const std::string& player_name);
690 
691  /**
692  * Tells a player to leave the game.
693  *
694  * @param user The player leaving the game.
695  */
696  void send_leave_game(player_iterator user) const;
697 
698  /**
699  * Sends a document to the provided list of sides.
700  *
701  * @param data The data to be sent to the provided sides.
702  * @param sides A comma sperated list of side numbers to which the document should be sent.
703  * @param exclude Players to not send the data to.
704  */
706  const simple_wml::string_span& sides,
707  std::optional<player_iterator> exclude = {});
708 
709  /**
710  * Send a document per observer in the game.
711  * If @a player is blank, send these documents to everyone, else send them to just the observer who joined.
712  *
713  * @param player The observer who joined.
714  */
715  void send_observerjoins(std::optional<player_iterator> player = {});
717  void send_history(player_iterator sock) const;
718 
719  /** In case of a host transfer, notify the new host about its status. */
720  void notify_new_host();
721 
722  /**
723  * Shortcut to a convenience function for finding a user by name.
724  *
725  * @param name The name of the user to find.
726  * @return The player if found, else empty.
727  */
728  std::optional<player_iterator> find_user(const simple_wml::string_span& name);
729 
730  bool is_legal_command(const simple_wml::node& command, player_iterator user);
731 
732  /**
733  * Checks whether a user has the same IP as any other members of this game.
734  * @return A comma separated string of members with matching IPs.
735  */
736  std::string has_same_ip(player_iterator user) const;
737 
738  /**
739  * Function which should be called every time a player ends their turn
740  * (i.e. [end_turn] received). This will update the 'turn' attribute for
741  * the game's description when appropriate.
742  *
743  * @param new_side The side number whose turn to move it has become.
744  * @return True if the current side and-or current turn values have been updated, false otherwise.
745  */
746  bool end_turn(int new_side);
747 
748  /**
749  * Set or update the current and max turn values in the game's description.
750  */
751  void update_turn_data();
752 
753  /**
754  * Function to send a list of users to all clients.
755  * Only sends data if the game is initialized but not yet started.
756  *
757  * @param exclude The players to not send the list of users to.
758  */
759  void send_user_list(std::optional<player_iterator> exclude = {});
760 
761  /**
762  * @param pl The player.
763  * @return The player's username.
764  */
765  std::string username(player_iterator pl) const;
766 
767  /**
768  * @param users The users to create a comma separated list from.
769  * @return A comma separated list of user names.
770  */
771  std::string list_users(user_vector users) const;
772 
773  /** calculates the initial value for sides_, side_controllerds_, nsides_*/
774  void reset_sides();
775 
776  /**
777  * Helps debugging player and observer lists.
778  *
779  * @return A string listing the game IDs, players, and observers.
780  */
781  std::string debug_player_info() const;
782 
783  /**
784  * Helps debugging controller tweaks.
785  *
786  * @return A string listing the game IDs and side information.
787  */
788  std::string debug_sides_info() const;
789 
790  /** The wesnothd server instance this game exists on. */
792  player_connections& player_connections_;
793 
794  /**
795  * Incremented to retrieve a unique ID for game instances within wesnothd.
796  */
797  static int id_num;
798  /** This game's ID within wesnothd */
799  int id_;
800 
801  /**
802  * Incremented to retrieve a unique ID per wesnothd instance for game instances within the database.
803  */
804  static int db_id_num;
805  /**
806  * Used for unique identification of games played in the database.
807  * Necessary since for MP campaigns multiple scenarios can be played within the same game instance
808  * and we need a unique ID per scenario played, not per game instance.
809  */
810  int db_id_;
811 
812  /** The name of the game. */
813  std::string name_;
814  /** The password needed to join the game. */
815  std::string password_;
816 
817  /** The game host or later owner (if the host left). */
819 
820  /** A vector of players (members owning a side). */
821  user_vector players_;
822 
823  /** A vector of observers (members not owning a side). */
824  user_vector observers_;
825  /** A vector of muted observers. */
826  user_vector muted_observers_;
827 
828  /** A vector of side owners. */
829  side_vector sides_;
830 
831  /** A vector containiner the controller type for each side. */
832  std::vector<CONTROLLER> side_controllers_;
833 
834  /** Number of sides in the current scenario. */
835  int nsides_;
836  /** Whether the game has been started or not. */
837  bool started_;
838 
839  /**
840  The current scenario data.
841 
842  WRONG! This contains the initial state or the state from which
843  the game was loaded from.
844  Using this to make assumptions about the current gamestate is
845  extremely dangerous and should especially not be done for anything
846  that can be nodified by wml (especially by [modify_side]),
847  like team_name, controller ... in [side].
848 
849  FIXME: move every code here that uses this object to query those
850  information to the clients. But note that there are some checks
851  (like controller == null) that are definitely needed by the server and
852  in this case we should try to modify the client to inform the server if
853  a change of those properties occur. Ofc we shouldn't update level_
854  then, but rather store that information in a separate object
855  (like in side_controllers_).
856  */
858 
859  /** Replay data. */
860  mutable std::vector<std::unique_ptr<simple_wml::document>> history_;
861 
862  /** Pointer to the game's description in the games_and_users_list_. */
864 
865  /** The game's current turn. */
867  /** The index of the current side. The side number is current_side_index_+1. */
869  /** The maximum number of turns before the game ends. */
871  /** Whether all observers should be treated as muted. */
873 
874  /** List of banned IPs */
875  std::vector<std::string> bans_;
876  /** List of banned usernames */
877  std::vector<std::string> name_bans_;
878 
879  /**
880  * in multiplayer campaigns it can happen that some players are still in the previous scenario
881  * keep track of those players because processing certain
882  * input from those side wil lead to error (oos)
883  */
884  std::set<const player_record*> players_not_advanced_;
885 
886  /** The reason the game ended. */
887  std::string termination_;
888 
889  /** Whether to save a replay of this game. */
891  /** Where to save the replay of this game. */
892  std::string replay_save_path_;
893 
894  /** A wrapper for mersenne twister rng which generates randomness for this game */
896  /**
897  * The ID of the last request received from a client.
898  * New requests should never have a lower value than this.
899  */
901 };
902 
903 } // namespace wesnothd
bool save_replays_
Whether to save a replay of this game.
Definition: game.hpp:890
bool started() const
Definition: game.hpp:194
bool is_muted_observer(player_iterator player) const
Definition: game.cpp:155
void handle_choice(const simple_wml::node &data, player_iterator user)
Handle a choice requested by a client, such as changing a side&#39;s controller, if initiated by WML/lua...
Definition: game.cpp:1186
std::set< const player_record * > players_not_advanced_
in multiplayer campaigns it can happen that some players are still in the previous scenario keep trac...
Definition: game.hpp:884
int db_id_
Used for unique identification of games played in the database.
Definition: game.hpp:810
void send_and_record_server_message(const char *message, std::optional< player_iterator > exclude={})
Send data to all players in this game except &#39;exclude&#39;.
Definition: game.cpp:1905
void send_observerquit(player_iterator observer)
Definition: game.cpp:1692
const std::string & name() const
Definition: game.hpp:87
void send_server_message_to_all(const std::string &message, std::optional< player_iterator > exclude={})
send_server_message_to_all
Definition: game.hpp:388
void save_replay()
Move the level information and recorded history into a replay file and save it.
Definition: game.cpp:1761
player_connections & player_connections_
Definition: game.hpp:792
void mute_observer(const simple_wml::node &mute, player_iterator muter)
Mute an observer or give a message of all currently muted observers if no name is given...
Definition: game.cpp:686
std::optional< player_iterator > ban_user(const simple_wml::node &ban, player_iterator banner)
Ban a user by name.
Definition: game.cpp:797
void send_to_players(simple_wml::document &data, const Container &players, std::optional< player_iterator > exclude={})
Send data to all players except those excluded.
Definition: game.cpp:1614
void process_change_turns_wml(simple_wml::document &data, player_iterator user)
Handles incoming [change_turns_wml] data.
Definition: game.cpp:1257
bool is_player(player_iterator player) const
Definition: game.cpp:168
std::vector< std::unique_ptr< simple_wml::document > > history_
Replay data.
Definition: game.hpp:860
const simple_wml::node::child_list & get_sides_list() const
Definition: game.hpp:186
void process_message(simple_wml::document &data, player_iterator user)
Sends an ingame message to all other players.
Definition: game.cpp:869
bool is_current_player(player_iterator player) const
Definition: game.hpp:616
user_vector muted_observers_
A vector of muted observers.
Definition: game.hpp:826
void update_side_data()
Resets the side configuration according to the scenario data.
Definition: game.cpp:398
bool player_is_banned(player_iterator player, const std::string &name) const
Definition: game.cpp:656
std::string username(player_iterator pl) const
Definition: game.cpp:173
bool level_init() const
Definition: game.hpp:144
std::optional< player_iterator > find_user(const simple_wml::string_span &name)
Shortcut to a convenience function for finding a user by name.
Definition: game.cpp:1895
std::size_t current_side() const
Definition: game.hpp:599
int nsides_
Number of sides in the current scenario.
Definition: game.hpp:835
user_vector observers_
A vector of observers (members not owning a side).
Definition: game.hpp:824
void send_leave_game(player_iterator user) const
Tells a player to leave the game.
Definition: game.cpp:759
bool is_observer(player_iterator player) const
Definition: game.cpp:150
bool all_observers_muted_
Whether all observers should be treated as muted.
Definition: game.hpp:872
bool is_member(player_iterator player) const
Definition: game.hpp:105
std::string termination_
The reason the game ended.
Definition: game.hpp:887
game & operator=(const game &)=delete
randomness::mt_rng rng_
A wrapper for mersenne twister rng which generates randomness for this game.
Definition: game.hpp:895
int db_id() const
This ID is not reused between scenarios of MP campaigns.
Definition: game.hpp:71
player_iterator owner_
The game host or later owner (if the host left).
Definition: game.hpp:818
bool process_turn(simple_wml::document &data, player_iterator user)
Handles [end_turn], repackages [commands] with private [speak]s in them and sends the data...
Definition: game.cpp:946
player_connections::const_iterator player_iterator
void handle_add_side_wml()
Adds a new, empty side owned by no one.
Definition: game.cpp:1115
void send_and_record_server_message(const std::string &message, std::optional< player_iterator > exclude={})
send_and_record_server_message
Definition: game.hpp:422
std::vector< std::string > bans_
List of banned IPs.
Definition: game.hpp:875
void next_db_id()
Increments the ID used when running database queries.
Definition: game.hpp:79
wesnothd::server & server
The wesnothd server instance this game exists on.
Definition: game.hpp:791
bool is_legal_command(const simple_wml::node &command, player_iterator user)
Definition: game.cpp:881
node * child(const char *name)
Definition: simple_wml.hpp:262
node * child(const char *name)
Definition: simple_wml.cpp:607
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const any_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
int last_choice_request_id_
The ID of the last request received from a client.
Definition: game.hpp:900
bool password_matches(const std::string &passwd) const
Definition: game.hpp:511
bool end_turn(int new_side)
Function which should be called every time a player ends their turn (i.e.
Definition: game.cpp:1286
void reset_sides()
calculates the initial value for sides_, side_controllerds_, nsides_
Definition: game.cpp:387
std::vector< CONTROLLER > side_controllers_
A vector containiner the controller type for each side.
Definition: game.hpp:832
bool has_password() const
Definition: game.hpp:519
void process_whiteboard(simple_wml::document &data, player_iterator user)
Handles incoming [whiteboard] data.
Definition: game.cpp:1230
int num_turns_
The maximum number of turns before the game ends.
Definition: game.hpp:870
void send_history(player_iterator sock) const
Definition: game.cpp:1704
void update_turn_data()
Set or update the current and max turn values in the game&#39;s description.
Definition: game.cpp:1318
bool allow_observers() const
Definition: game.cpp:145
void handle_controller_choice(const simple_wml::node &data)
Handle a request to change a side&#39;s controller.
Definition: game.cpp:1122
bool describe_slots()
Set the description to the number of available slots.
Definition: game.cpp:625
void transfer_side_control(player_iterator player, const simple_wml::node &cfg)
Lets a player owning a side give it to another player or observer.
Definition: game.cpp:465
void handle_random_choice()
Send a randomly generated number to the requestor.
Definition: game.cpp:1094
static int db_id_num
Incremented to retrieve a unique ID per wesnothd instance for game instances within the database...
Definition: game.hpp:804
std::string get_replay_filename()
Definition: game.cpp:1751
std::optional< player_iterator > current_player() const
Definition: game.hpp:607
std::size_t nobservers() const
Definition: game.hpp:210
void new_scenario(player_iterator sender)
When the host sends the new scenario of a mp campaign.
Definition: game.cpp:1549
void start_game(player_iterator starter)
Starts the game (if a new game) or starts the next scenario of an MP campaign.
Definition: game.cpp:248
const user_vector all_game_users() const
Definition: game.cpp:1850
bool controls_side(const std::vector< int > &sides, player_iterator player) const
Function which returns true if &#39;player&#39; controls any of the sides specified in &#39;sides&#39;.
Definition: game.cpp:1644
void perform_controller_tweaks()
This is performed just before starting and before the [start_game] signal.
Definition: game.cpp:193
bool remove_player(player_iterator player, const bool disconnect=false, const bool destruct=false)
Removes a user from the game.
Definition: game.cpp:1416
int id_
This game&#39;s ID within wesnothd.
Definition: game.hpp:799
std::unique_ptr< simple_wml::document > change_controller_type(const std::size_t side_index, player_iterator player, const std::string &player_name)
Tell everyone else but the source player that the controller type changed.
Definition: game.cpp:598
void send_data(simple_wml::document &data, std::optional< player_iterator > exclude={})
Send data to all players and observers except those excluded.
Definition: game.cpp:1623
bool all_observers_muted() const
Definition: game.hpp:630
std::optional< player_iterator > kick_member(const simple_wml::node &kick, player_iterator kicker)
Kick a user from this game by name.
Definition: game.cpp:765
void load_next_scenario(player_iterator user)
A user asks for the next scenario to advance to.
Definition: game.cpp:1561
bool is_owner(player_iterator player) const
Definition: game.hpp:96
static simple_wml::node * starting_pos(simple_wml::node &data)
The non-const version.
Definition: game.hpp:155
void send_server_message(const char *message, std::optional< player_iterator > player={}, simple_wml::document *doc=nullptr) const
Send a server message to the specified player.
Definition: game.cpp:1923
static const simple_wml::node * starting_pos(const simple_wml::node &data)
The const version.
Definition: game.hpp:172
std::vector< std::string > name_bans_
List of banned usernames.
Definition: game.hpp:877
void record_data(std::unique_ptr< simple_wml::document > data)
Records a WML document in the game&#39;s history.
Definition: game.cpp:1817
void send_observerjoins(std::optional< player_iterator > player={})
Send a document per observer in the game.
Definition: game.cpp:1672
std::vector< std::optional< player_iterator > > side_vector
Definition: game.hpp:33
void change_controller(const std::size_t side_index, player_iterator player, 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:566
std::vector< node * > child_list
Definition: simple_wml.hpp:126
bool add_player(player_iterator player, bool observer=false)
Add a user to the game.
Definition: game.cpp:1333
std::size_t current_turn() const
Definition: game.hpp:218
void reset_last_synced_context_id()
Reset the internal counter for choice requests made by clients to the server.
Definition: game.hpp:573
int current_side_index_
The index of the current side.
Definition: game.hpp:868
side_vector sides_
A vector of side owners.
Definition: game.hpp:829
void notify_new_host()
In case of a host transfer, notify the new host about its status.
Definition: game.cpp:614
void set_description(simple_wml::node *desc)
Set the game&#39;s description.
Definition: game.cpp:1828
std::string has_same_ip(player_iterator user) const
Checks whether a user has the same IP as any other members of this game.
Definition: game.cpp:1657
void set_password(const std::string &passwd)
Sets the password required to access the game.
Definition: game.hpp:492
void send_data_sides(simple_wml::document &data, const simple_wml::string_span &sides, std::optional< player_iterator > exclude={})
Sends a document to the provided list of sides.
Definition: game.cpp:1628
std::string observer
std::size_t nplayers() const
Definition: game.hpp:202
std::string list_users(user_vector users) const
Definition: game.cpp:178
simple_wml::node * description_
Pointer to the game&#39;s description in the games_and_users_list_.
Definition: game.hpp:863
std::string name_
The name of the game.
Definition: game.hpp:813
void unban_user(const simple_wml::node &unban, player_iterator unbanner)
Unban a user by name.
Definition: game.cpp:839
std::string debug_sides_info() const
Helps debugging controller tweaks.
Definition: game.cpp:1876
static int id_num
Incremented to retrieve a unique ID for game instances within wesnothd.
Definition: game.hpp:797
MAKE_ENUM(CONTROLLER,(HUMAN, "human")(AI, "ai")(EMPTY, "null"))
std::vector< player_iterator > user_vector
Definition: game.hpp:32
bool is_reload() const
Definition: game.cpp:1952
void send_user_list(std::optional< player_iterator > exclude={})
Function to send a list of users to all clients.
Definition: game.cpp:1526
const std::string & termination_reason() const
Provides the reason the game was ended.
Definition: game.hpp:529
std::string replay_save_path_
Where to save the replay of this game.
Definition: game.hpp:892
int current_turn_
The game&#39;s current turn.
Definition: game.hpp:866
user_vector players_
A vector of players (members owning a side).
Definition: game.hpp:821
void send_server_message_to_all(const char *message, std::optional< player_iterator > exclude={})
Sends a message to all players in this game that aren&#39;t excluded.
Definition: game.cpp:1916
std::string password_
The password needed to join the game.
Definition: game.hpp:815
void send_muted_observers(player_iterator user) const
Sends a message either stating that all observers are muted or listing the observers that are muted...
Definition: game.cpp:674
bool started_
Whether the game has been started or not.
Definition: game.hpp:837
bool send_taken_side(simple_wml::document &cfg, const simple_wml::node *side) const
Tell the host who owns a side.
Definition: game.cpp:325
std::string debug_player_info() const
Helps debugging player and observer lists.
Definition: game.cpp:1860
void set_name_bans(const std::vector< std::string > name_bans)
Set a list of usernames that should all be banned from joining the game.
Definition: game.hpp:502
void send_server_message(const std::string &message, std::optional< player_iterator > player={}, simple_wml::document *doc=nullptr) const
send_server_message
Definition: game.hpp:405
void unmute_observer(const simple_wml::node &unmute, player_iterator unmuter)
Unmute an observer or unmute all currently muted observers if no name is given.
Definition: game.cpp:727
Defines the MAKE_ENUM macro.
simple_wml::document level_
The current scenario data.
Definition: game.hpp:857
void clear_history()
Clears the history of recorded WML documents.
Definition: game.cpp:1823
void mute_all_observers()
Toggles whether all observers are muted or not.
Definition: game.cpp:664
bool take_side(player_iterator user)
Figures out which side to take and tells that side to the game owner.
Definition: game.cpp:346
void set_termination_reason(const std::string &reason)
Sets the termination reason for this game.
Definition: game.cpp:1836
game(wesnothd::server &server, player_connections &player_connections, player_iterator host, const std::string &name="", bool save_replays=false, const std::string &replay_save_path="")
Definition: game.cpp:78
simple_wml::document & level()
Definition: game.hpp:466
simple_wml::node * description() const
Definition: game.hpp:482
bool save_replays()
Definition: game.cpp:770
int id() const
This ID is reused between scenarios of MP campaigns.
Definition: game.hpp:59