The Battle for Wesnoth  1.19.7+dev
game_history.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 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 
15 #ifdef HAVE_MYSQLPP
16 
19 #include "log.hpp"
20 
21 static lg::log_domain log_sql_handler("sql_executor");
22 #define ERR_SQL LOG_STREAM(err, log_sql_handler)
23 
24 void game_history::read(mariadb::result_set_ref rslt)
25 {
26  while(rslt->next())
27  {
28  result r;
29  r.game_name = rslt->get_string("GAME_NAME");
30  r.game_start = rslt->get_date_time("START_TIME").str();
31  r.scenario_name = rslt->get_string("SCENARIO_NAME");
32  r.era_name = rslt->get_string("ERA_NAME");
33  for(const auto& player_info : utils::split(rslt->get_string("PLAYERS")))
34  {
35  std::vector<std::string> info = utils::split(player_info, ':');
36  if(info.size() == 2)
37  {
38  r.players.emplace_back(player{ info[0], info[1] });
39  }
40  else
41  {
42  ERR_SQL << "Expected player information to split into two fields, instead found the value `" << player_info << "`.";
43  }
44  }
45  r.modification_names = utils::split(rslt->get_string("MODIFICATION_NAMES"));
46  r.replay_url = rslt->get_string("REPLAY_URL");
47  r.version = rslt->get_string("VERSION");
48  results.push_back(std::move(r));
49  }
50 }
51 
52 std::unique_ptr<simple_wml::document> game_history::to_doc()
53 {
54  auto doc = std::make_unique<simple_wml::document>();
55 
56  simple_wml::node& results_wml = doc->root().add_child("game_history_results");
57 
58  for(const auto& result : results)
59  {
60  simple_wml::node& ghr = results_wml.add_child("game_history_result");
61  ghr.set_attr_dup("game_name", result.game_name.c_str());
62  ghr.set_attr_dup("game_start", result.game_start.c_str());
63  ghr.set_attr_dup("scenario_name", result.scenario_name.c_str());
64  ghr.set_attr_dup("era_name", result.era_name.c_str());
65  ghr.set_attr_dup("replay_url", result.replay_url.c_str());
66  ghr.set_attr_dup("version", result.version.c_str());
67  for(const auto& player : result.players)
68  {
69  simple_wml::node& p = ghr.add_child("player");
70  p.set_attr_dup("name", player.name.c_str());
71  p.set_attr_dup("faction", player.faction.c_str());
72  }
73  for(const auto& mod : result.modification_names)
74  {
75  simple_wml::node& m = ghr.add_child("modification");
76  m.set_attr_dup("name", mod.c_str());
77  }
78  }
79  return doc;
80 }
81 
82 #endif //HAVE_MYSQLPP
void read(mariadb::result_set_ref rslt)
std::unique_ptr< simple_wml::document > to_doc()
std::vector< result > results
node & add_child(const char *name)
Definition: simple_wml.cpp:466
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:429
logger & info()
Definition: log.cpp:319
std::vector< std::string > split(const config_attribute_value &val)
mock_party p