Debian Codegraph @

src/statistics.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Guy Rutenberg *
3 * [email protected] *
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 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include <string>
22 #include <sstream>
23 #include <cstdlib>
24 #include <boost/algorithm/string/split.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 #include "statistics.h"
27 #include "utility.h"
28
29 using namespace std;
30 using namespace statistics;
31 using namespace boost;
32
33 Statistics::Statistics(configuration::Configuration *backend)
34 {
35 this->backend = backend;
36 try {
37 load_data();
38 } catch (std::exception &e) {
39 reset();
40 }
41 }
42
43 void Statistics::load_data()
44 {
45 string tmp;
46 vector<string> tmp_vec;
47
48 tmp = backend->get("statistics_games_started");
49 _games_started = atoi(tmp.c_str());
50
51 tmp = backend->get("statistics_games_finished");
52 _games_finished = atoi(tmp.c_str());
53
54 tmp = backend->get("statistics_score_distribution");
55 split(tmp_vec, tmp, is_any_of(","));
56 if (tmp_vec.size() != score_distributions_slots)
57 throw BadConfiguration();
58
59 for (string &i : tmp_vec) {
60 _score_distribution.push_back(atoi(i.c_str()));
61 }
62
63 istringstream i( backend->get("statistics_last_reset"));
64 i >> _last_reset;
65 if (!_last_reset)
66 throw BadConfiguration();
67
68 }
69
70 void Statistics::game_started()
71 {
72 _games_started++;
73 save();
74 }
75
76 void Statistics::game_finished(int score)
77 {
78 int score_slot;
79 _games_finished++;
80
81 score_slot = score/score_distribution_granuality;
82 score_slot = score_slot<score_distributions_slots ? score_slot : score_slot;
83 _score_distribution[score_slot]++;
84 save();
85 }
86
87 void Statistics::save() {
88 string tmp;
89 backend->set("statistics_games_started", stringify(_games_started));
90 backend->set("statistics_games_finished", stringify(_games_finished));
91 backend->set("statistics_last_reset", stringify(_last_reset));
92
93 tmp = "";
94 for (int &i : _score_distribution) {
95 tmp += stringify(i) + ",";
96 }
97 //delete trailing comma
98 tmp = tmp.substr(0, tmp.size()-1);
99 backend->set("statistics_score_distribution", tmp);
100
101 backend->save();
102 }
103
104 void Statistics::reset() {
105 _games_started = 0;
106 _games_finished = 0;
107 _last_reset = time(NULL);
108
109 _score_distribution = vector<int>(score_distributions_slots, 0);
110
111 save();
112 }