The Battle for Wesnoth  1.17.0-dev
game_history.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2021
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.reload = rslt->get_boolean("RELOAD");
31  r.game_start = rslt->get_date_time("START_TIME").str();
32  r.scenario_name = rslt->get_string("SCENARIO_NAME");
33  r.scenario_id = rslt->get_string("SCENARIO_ID");
34  r.era_name = rslt->get_string("ERA_NAME");
35  r.era_id = rslt->get_string("ERA_ID");
36  for(const auto& player_info : utils::split(rslt->get_string("PLAYERS")))
37  {
38  std::vector<std::string> info = utils::split(player_info, ':');
39  if(info.size() == 2)
40  {
41  r.players.emplace_back(player{ info[0], info[1] });
42  }
43  else
44  {
45  ERR_SQL << "Expected player information to split into two fields, instead found the value `" << player_info << "`." << std::endl;
46  }
47  }
48  r.modification_names = utils::split(rslt->get_string("MODIFICATION_NAMES"));
49  r.modification_ids = utils::split(rslt->get_string("MODIFICATION_IDS"));
50  r.replay_url = rslt->get_string("REPLAY_URL");
51  results.push_back(std::move(r));
52  }
53 }
54 
55 std::unique_ptr<simple_wml::document> game_history::to_doc()
56 {
57  auto doc = std::make_unique<simple_wml::document>();
58  for(const auto& result : results)
59  {
60  simple_wml::node& ghr = doc->root().add_child("game_history_result");
61  ghr.set_attr_dup("game_name", result.game_name.c_str());
62  ghr.set_attr_int("reload", result.reload);
63  ghr.set_attr_dup("game_start", result.game_start.c_str());
64  ghr.set_attr_dup("scenario_name", result.scenario_name.c_str());
65  ghr.set_attr_dup("scenario_id", result.scenario_id.c_str());
66  ghr.set_attr_dup("era_name", result.era_name.c_str());
67  ghr.set_attr_dup("era_id", result.era_id.c_str());
68  for(const auto& player : result.players)
69  {
70  simple_wml::node& p = ghr.add_child("player");
71  p.set_attr_dup("name", player.name.c_str());
72  p.set_attr_dup("faction", player.faction.c_str());
73  }
74  for(const auto& mod : result.modification_names)
75  {
76  simple_wml::node& m = ghr.add_child("modification");
77  m.set_attr_dup("name", mod.c_str());
78  }
79  for(const auto& mod : result.modification_ids)
80  {
81  simple_wml::node& m = ghr.add_child("modification");
82  m.set_attr_dup("id", mod.c_str());
83  }
84  }
85  return doc;
86 }
87 
88 #endif //HAVE_MYSQLPP
node & add_child(const char *name)
Definition: simple_wml.cpp:465
void read(mariadb::result_set_ref rslt)
logger & info()
Definition: log.cpp:89
node & set_attr_int(const char *key, int value)
Definition: simple_wml.cpp:440
std::vector< result > results
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:428
mock_party p
std::vector< std::string > split(const config_attribute_value &val)
std::unique_ptr< simple_wml::document > to_doc()