The Battle for Wesnoth  1.15.12+dev
game_history.cpp
Go to the documentation of this file.
1 /*
2  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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 #ifdef HAVE_MYSQLPP
15 
18 #include "log.hpp"
19 
20 static lg::log_domain log_sql_handler("sql_executor");
21 #define ERR_SQL LOG_STREAM(err, log_sql_handler)
22 
23 void game_history::read(mariadb::result_set_ref rslt)
24 {
25  while(rslt->next())
26  {
27  result r;
28  r.game_name = rslt->get_string("GAME_NAME");
29  r.reload = rslt->get_boolean("RELOAD");
30  r.game_start = rslt->get_date_time("START_TIME").str();
31  r.scenario_name = rslt->get_string("SCENARIO_NAME");
32  r.scenario_id = rslt->get_string("SCENARIO_ID");
33  r.era_name = rslt->get_string("ERA_NAME");
34  r.era_id = rslt->get_string("ERA_ID");
35  for(const auto& player_info : utils::split(rslt->get_string("PLAYERS")))
36  {
37  std::vector<std::string> info = utils::split(player_info, ':');
38  if(info.size() == 2)
39  {
40  r.players.emplace_back(player{ info[0], info[1] });
41  }
42  else
43  {
44  ERR_SQL << "Expected player information to split into two fields, instead found the value `" << player_info << "`." << std::endl;
45  }
46  }
47  r.modification_names = utils::split(rslt->get_string("MODIFICATION_NAMES"));
48  r.modification_ids = utils::split(rslt->get_string("MODIFICATION_IDS"));
49  r.replay_url = rslt->get_string("REPLAY_URL");
50  results.push_back(std::move(r));
51  }
52 }
53 
54 std::unique_ptr<simple_wml::document> game_history::to_doc()
55 {
56  auto doc = std::make_unique<simple_wml::document>();
57  for(const auto& result : results)
58  {
59  simple_wml::node& ghr = doc->root().add_child("game_history_result");
60  ghr.set_attr_dup("game_name", result.game_name.c_str());
61  ghr.set_attr_int("reload", result.reload);
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("scenario_id", result.scenario_id.c_str());
65  ghr.set_attr_dup("era_name", result.era_name.c_str());
66  ghr.set_attr_dup("era_id", result.era_id.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  for(const auto& mod : result.modification_ids)
79  {
80  simple_wml::node& m = ghr.add_child("modification");
81  m.set_attr_dup("id", mod.c_str());
82  }
83  }
84  return doc;
85 }
86 
87 #endif //HAVE_MYSQLPP
node & add_child(const char *name)
Definition: simple_wml.cpp:464
void read(mariadb::result_set_ref rslt)
logger & info()
Definition: log.cpp:88
node & set_attr_int(const char *key, int value)
Definition: simple_wml.cpp:439
std::vector< result > results
node & set_attr_dup(const char *key, const char *value)
Definition: simple_wml.cpp:427
mock_party p
std::vector< std::string > split(const config_attribute_value &val)
std::unique_ptr< simple_wml::document > to_doc()