The Battle for Wesnoth  1.19.0-dev
match_history.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2024
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 
16 
17 #include "formula/string_utils.hpp"
18 #include "gettext.hpp"
19 #include "filesystem.hpp"
21 #include "gui/dialogs/message.hpp"
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/listbox.hpp"
26 #include "gui/widgets/text_box.hpp"
27 #include "gui/widgets/window.hpp"
30 #include "wesnothd_connection.hpp"
31 
32 using namespace std::chrono_literals;
33 
34 static lg::log_domain log_network("network");
35 #define DBG_NW LOG_STREAM(debug, log_network)
36 #define ERR_NW LOG_STREAM(err, log_network)
37 
38 namespace gui2::dialogs
39 {
40 REGISTER_DIALOG(mp_match_history)
41 
42 mp_match_history::mp_match_history(const std::string& player_name, wesnothd_connection& connection, bool wait_for_response)
43  : modal_dialog(window_id())
44  , player_name_(player_name)
45  , connection_(connection)
46  , offset_(0)
47  , wait_for_response_(wait_for_response)
48 {
49  register_label("title", true, VGETTEXT("Match History — $player", {{"player", player_name_}}));
50 }
51 
52 void mp_match_history::pre_show(window& win)
53 {
54  win.set_enter_disabled(true);
55 
56  button& newer_history = find_widget<button>(&win, "newer_history", false);
57  button& older_history = find_widget<button>(&win, "older_history", false);
58  connect_signal_mouse_left_click(newer_history, std::bind(&mp_match_history::newer_history_offset, this));
59  connect_signal_mouse_left_click(older_history, std::bind(&mp_match_history::older_history_offset, this));
60 
61  button& search = find_widget<button>(&win, "search", false);
62  connect_signal_mouse_left_click(search, std::bind(&mp_match_history::new_search, this));
63 
64  text_box& search_player = find_widget<text_box>(&win, "search_player", false);
65  search_player.set_value(player_name_);
66 
67  std::vector<config> content_types;
68  content_types.emplace_back("label", _("Scenario"));
69  content_types.emplace_back("label", _("Era"));
70  content_types.emplace_back("label", _("Modification"));
71 
72  find_widget<menu_button>(&win, "search_content_type", false).set_values(content_types);
73 
74  update_display();
75 }
76 
77 void mp_match_history::new_search()
78 {
79  int old_offset = offset_;
80  std::string old_player_name = player_name_;
81  text_box& search_player = find_widget<text_box>(get_window(), "search_player", false);
82  player_name_ = search_player.get_value();
83 
84  // display update failed, set the offset back to what it was before
85  if(!update_display()) {
86  offset_ = old_offset;
87  player_name_ = old_player_name;
88  } else {
89  label& title = find_widget<label>(get_window(), "title", false);
90  title.set_label(VGETTEXT("Match History — $player", {{"player", player_name_}}));
91  }
92 }
93 
94 void mp_match_history::newer_history_offset()
95 {
96  offset_ -= 10;
97  // display update failed, set the offset back to what it was before
98  if(!update_display()) {
99  offset_ += 10;
100  }
101 }
102 
103 void mp_match_history::older_history_offset()
104 {
105  offset_ += 10;
106  // display update failed, set the offset back to what it was before
107  if(!update_display()) {
108  offset_ -= 10;
109  }
110 }
111 
112 namespace
113 {
114 std::string key_with_fallback(const config::attribute_value& val)
115 {
116  // Note that using the fallback arg of str() doesn't work since the value is set,
117  // it's just set as an empty string, and the fallback is only returned if the value
118  // is actually unset.
119  if(std::string s = val.str(); !s.empty()) {
120  return s;
121  } else {
122  return font::unicode_em_dash;
123  }
124 }
125 };
126 
127 bool mp_match_history::update_display()
128 {
129  const config history = request_history();
130 
131  // request failed, nothing to do
132  if(history.child_count("game_history_results") == 0) {
133  return false;
134  }
135 
136  listbox* history_box = find_widget<listbox>(get_window(), "history", false, true);
137  history_box->clear();
138 
139  listbox* tab_bar = find_widget<listbox>(get_window(), "tab_bar", false, true);
140  connect_signal_notify_modified(*tab_bar, std::bind(&mp_match_history::tab_switch_callback, this));
141 
142  int i = 0;
143  for(const config& game : history.mandatory_child("game_history_results").child_range("game_history_result")) {
144  widget_data row;
145  grid& history_grid = history_box->add_row(row);
146 
147  dynamic_cast<label*>(history_grid.find("game_name", false))->set_label(key_with_fallback(game["game_name"]));
148  dynamic_cast<label*>(history_grid.find("scenario_name", false))->set_label(key_with_fallback(game["scenario_name"]));
149  dynamic_cast<label*>(history_grid.find("era_name", false))->set_label("<span color='#baac7d'>" + _("Era: ") + "</span>" + key_with_fallback(game["era_name"]));
150  dynamic_cast<label*>(history_grid.find("game_start", false))->set_label(key_with_fallback(game["game_start"]) + _(" UTC+0"));
151  dynamic_cast<label*>(history_grid.find("version", false))->set_label(key_with_fallback(game["version"]));
152 
153  button* replay_download = dynamic_cast<button*>(history_grid.find("replay_download", false));
154  std::string replay_url = game["replay_url"].str();
155  if(!replay_url.empty()) {
156  std::string filename = utils::split(replay_url, '/').back();
157  std::string local_save = filesystem::get_saves_dir()+"/"+filename;
158 
159  connect_signal_mouse_left_click(*replay_download, std::bind(&network::download, replay_url, local_save));
160  } else {
161  replay_download->set_active(false);
162  }
163 
164  std::vector<std::string> player_list;
165  std::vector<std::string> player_faction_list;
166  for(const config& player : game.child_range("player")) {
167  player_list.emplace_back(font::unicode_bullet + " " + player["name"].str() + ":");
168  player_faction_list.emplace_back(player["faction"].str());
169  }
170 
171  label* players = dynamic_cast<label*>(history_grid.find("players", false));
172  players->set_label(utils::join(player_list, "\n"));
173 
174  label* factions = dynamic_cast<label*>(history_grid.find("player_factions", false));
175  factions->set_label(utils::join(player_faction_list, "\n"));
176 
177  history_grid.find("player_grid", false)->set_visible(gui2::widget::visibility::invisible);
178 
179  label* modifications = dynamic_cast<label*>(history_grid.find("modifications", false));
180  const auto& children = game.child_range("modification");
181  if(!children.empty()) {
182  std::vector<std::string> modifications_list;
183 
184  for(const config& modification : game.child_range("modification")) {
185  modifications_list.emplace_back(font::unicode_bullet + " " + modification["name"].str());
186  }
187 
188  modifications->set_label(utils::join(modifications_list, "\n"));
189  }
191 
192  i++;
193  if(i == 10) {
194  break;
195  }
196  }
197 
198  // this is already the most recent history, can't get anything newer
199  if(offset_ == 0) {
200  button* newer_history = find_widget<button>(get_window(), "newer_history", false, true);
201  newer_history->set_active(false);
202  } else {
203  button* newer_history = find_widget<button>(get_window(), "newer_history", false, true);
204  newer_history->set_active(true);
205  }
206 
207  // the server returns up to 11 and the client displays at most 10
208  // if fewer than 11 rows are returned, then there are no older rows left to get next
209  if(history.child_count("game_history_result") < 11) {
210  button* older_history = find_widget<button>(get_window(), "older_history", false, true);
211  older_history->set_active(false);
212  } else {
213  button* older_history = find_widget<button>(get_window(), "older_history", false, true);
214  older_history->set_active(true);
215  }
216 
217  return true;
218 }
219 
220 const config mp_match_history::request_history()
221 {
222  config request;
223  config& child = request.add_child("game_history_request");
224  child["offset"] = offset_;
225  child["search_player"] = player_name_;
226  child["search_game_name"] = find_widget<text_box>(get_window(), "search_game_name", false).get_value();
227  child["search_content_type"] = find_widget<menu_button>(get_window(), "search_content_type", false).get_value();
228  child["search_content"] = find_widget<text_box>(get_window(), "search_content", false).get_value();
229  DBG_NW << request.debug();
230  connection_.send_data(request);
231 
232  int times_waited = 0;
233  while(true) {
234  config response;
235 
236  // I'm not really sure why this works to be honest
237  // I would've expected that there would be a risk of regular lobby responses showing up here since it's a reference to the lobby's network connection
238  // however testing has resulted in showing that this is not the case
239  // lobby responses are received in the lobby's network_handler() method when this method is not running
240  // lobby responses are not received while this method is running, and are handled in the lobby after it completes
241  // history results are never received in the lobby
242  if(connection_.receive_data(response)) {
243  if(response.child_count("game_history_results") == 0) {
244  DBG_NW << "Received non-history data: " << response.debug();
245  if(!response["error"].str().empty()) {
246  ERR_NW << "Received error from server: " << response["error"].str();
247  gui2::show_error_message(_("The server responded with an error:")+" "+response["error"].str());
248  return {};
249  }
250  } else if(response.mandatory_child("game_history_results").child_count("game_history_result") == 0) {
251  DBG_NW << "Player has no game history data.";
252  gui2::show_error_message(_("No game history found."));
253  return {};
254  } else {
255  DBG_NW << "Received history data: " << response.debug();
256  return response;
257  }
258  } else {
259  DBG_NW << "Received no data";
260  }
261 
262  if(times_waited > 20 || !wait_for_response_) {
263  ERR_NW << "Timed out waiting for history data, returning nothing";
264  if(wait_for_response_) {
265  gui2::show_error_message(_("Request timed out."));
266  }
267  return {};
268  }
269 
270  times_waited++;
271  std::this_thread::sleep_for(250ms);
272  }
273 
274  DBG_NW << "Something else happened while waiting for history data, returning nothing";
275  gui2::show_error_message(_("Request encountered an unexpected error, please check the logs."));
276  return {};
277 }
278 
279 void mp_match_history::tab_switch_callback()
280 {
281  listbox* history_box = find_widget<listbox>(get_window(), "history", false, true);
282  listbox* tab_bar = find_widget<listbox>(get_window(), "tab_bar", false, true);
283  int tab = tab_bar->get_selected_row();
284 
285  for(unsigned i = 0; i < history_box->get_item_count(); i++) {
286  grid* history_grid = history_box->get_row_grid(i);
287  if(tab == 0) {
288  history_grid->find("player_grid", false)->set_visible(gui2::widget::visibility::invisible);
289  history_grid->find("modifications", false)->set_visible(gui2::widget::visibility::invisible);
290  } else if(tab == 1) {
291  history_grid->find("player_grid", false)->set_visible(gui2::widget::visibility::visible);
292  history_grid->find("modifications", false)->set_visible(gui2::widget::visibility::invisible);
293  } else if(tab == 2) {
294  history_grid->find("player_grid", false)->set_visible(gui2::widget::visibility::invisible);
295  history_grid->find("modifications", false)->set_visible(gui2::widget::visibility::visible);
296  }
297  }
298 }
299 
300 } // namespace dialogs
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:367
std::size_t child_count(config_key_type key) const
Definition: config.cpp:297
child_itors child_range(config_key_type key)
Definition: config.cpp:273
std::string debug() const
Definition: config.cpp:1244
config & add_child(config_key_type key)
Definition: config.cpp:441
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
Abstract base class for all modal dialogs.
Base container class.
Definition: grid.hpp:32
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
The listbox class.
Definition: listbox.hpp:43
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:59
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:230
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:118
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:268
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
virtual void set_label(const t_string &text)
std::string get_value() const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
Class for a single line text area.
Definition: text_box.hpp:142
void set_visible(const visibility visible)
Definition: widget.cpp:470
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:327
A class that represents a TCP/IP connection to the wesnothd server.
Declarations for File-IO.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::size_t i
Definition: function.cpp:968
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
#define ERR_NW
static lg::log_domain log_network("network")
#define DBG_NW
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::string get_saves_dir()
const std::string unicode_em_dash
Definition: constants.cpp:44
const std::string unicode_bullet
Definition: constants.cpp:47
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:203
void download(const std::string &url, const std::string &local_path)
Initiates a standalone download of a single file from an HTTPS URL.
const std::vector< std::string > & modifications(bool mp)
Definition: game.cpp:708
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
SDL_Window * get_window()
Definition: video.cpp:655
static map_location::DIRECTION s