The Battle for Wesnoth  1.15.2+dev
synced_user_choice.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2018 by the Battle for Wesnoth Project
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "gettext.hpp"
17 #include "config.hpp"
18 #include "events.hpp"
19 #include "generic_event.hpp"
20 
21 #include <map>
22 #include <set>
23 
24 namespace mp_sync
25 {
26 
27 /**
28  * Interface for querying local choices.
29  * It has to support querying the user and making a random choice
30  */
32 {
33  virtual ~user_choice() {}
34  virtual config query_user(int side) const = 0;
35  virtual config random_choice(int side) const = 0;
36  ///whether the choice is visible for the user like an advancement choice
37  ///a non-visible choice is for example get_global_variable
38  virtual bool is_visible() const { return true; }
39  // TRANSLATORS: In networked games, this text is shown for other clients,
40  // while they wait for an action from another player.
41  // This text will be embedded into a sentence.
42  virtual std::string description() const { return _("waiting for^input"); }
43 };
44 
45 /**
46  * Performs a choice for WML events.
47  *
48  * The choice is synchronized across all the multiplayer clients and
49  * stored into the replay. The function object is called if the local
50  * client is responsible for making the choice.
51  * otherwise this function waits for a remote choice and returns it when it is received.
52  * information about the choice made is saved in replay with dependent=true
53  *
54  * @param name Tag used for storing the choice into the replay.
55  * @param side The number of the side responsible for making the choice.
56  * If zero, it defaults to the currently active side.
57  *
58  * @note In order to prevent issues with sync, crash, or infinite loop, a
59  * number of precautions must be taken when getting a choice from a
60  * specific side.
61  * - The server must recognize @name replay commands as legal from
62  * non-active players. Preferably the server should be notified
63  * about which player the data is expected from, and discard data
64  * from unexpected players.
65  */
66 config get_user_choice(const std::string &name, const user_choice &uch,
67  int side = 0);
68 /**
69  * Performs a choice for multiple sides for WML events.
70  * uch is called on all sides specified in sides, this in done simultaneously on all those sides (or one after another if one client controls multiple sides)
71  * and after all calls are executed the results are returned.
72  */
73 std::map<int, config> get_user_choice_multiple_sides(const std::string &name, const user_choice &uch,
74  std::set<int> sides);
75 
76 }
77 
79 {
80  // The sides which should execute this local choice
81  std::set<int> required_;
82  // The results
83  std::map<int, config> res_;
84  // The side for which we should do a choice locally (0 if no such side exists)
85  // Note that even if there is currently no locally choice to do it is still possible that we need to do a local choice later because we took control over a side
87  // the message displayed for sides which currently don't have to do a choice.
88  std::string wait_message_;
89  // If we failed to read the remote choices this flag is when which indicated that we should do all choices locally
90  bool oos_;
91 
93  const std::string& tagname_;
94  const int current_side_;
95  // private constructor, this object is only constructed by user_choice_manager::get_user_choice_internal
96  user_choice_manager(const std::string &name, const mp_sync::user_choice &uch, const std::set<int>& sides);
98  void search_in_replay();
99 public:
100  void pull();
101  bool finished() const
102  { return required_.size() == res_.size(); }
103  bool has_local_choice() const
104  { return local_choice_ != 0; }
105  /// Note: currently finished() does not imply !waiting() so you may need to check both.
106  bool waiting() const
107  { return local_choice_ == 0 && !oos_; }
108  void update_local_choice();
109  void ask_local_choice();
110  void fix_oos();
111  const std::string& wait_message() const { return wait_message_; }
112  /// @param name the tagname for this user choice in the replay
113  /// @param sides an array of team numbers (beginning with 1). the specified sides may not have an empty controller.
114  static std::map<int, config> get_user_choice_internal(const std::string &name, const mp_sync::user_choice &uch, const std::set<int>& sides);
115  /// Inherited from events::pump_monitor
116  void process(events::pump_info&);
118 };
config get_user_choice(const std::string &name, const user_choice &uch, int side=0)
std::set< int > required_
const std::string & tagname_
virtual std::string description() const
bool waiting() const
Note: currently finished() does not imply !waiting() so you may need to check both.
Definitions for the interface to Wesnoth Markup Language (WML).
std::map< int, config > get_user_choice_multiple_sides(const std::string &name, const user_choice &uch, std::set< int > sides)
Performs a choice for multiple sides for WML events.
virtual config random_choice(int side) const =0
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
void process(int mousex, int mousey)
Definition: tooltips.cpp:193
Interface for querying local choices.
events::generic_event changed_event_
const mp_sync::user_choice & uch_
virtual config query_user(int side) const =0
virtual bool is_visible() const
whether the choice is visible for the user like an advancement choice a non-visible choice is for exa...
std::map< int, config > res_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
const std::string & wait_message() const