source: trunk/src/statistics.cpp @ 206

Revision 206, 3.4 KB checked in by guyru, 2 years ago (diff)

initial working code of simple pie plot

Line 
1/***************************************************************************
2 *   Copyright (C) 2006-2009 by Guy Rutenberg   *
3 *   guyrutenberg@gmail.com   *
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/foreach.hpp>
25#include <boost/algorithm/string/split.hpp>
26#include <boost/algorithm/string/classification.hpp>
27#include "statistics.h"
28#include "utility.h"
29
30using namespace std;
31using namespace statistics;
32using namespace boost;
33
34Statistics::Statistics(configuration::Configuration *backend)
35{
36        this->backend = backend;
37        try {
38                load_data();
39        } catch (std::exception &e) {
40                reset();
41        }
42}
43
44void Statistics::load_data()
45{
46        string tmp;
47        vector<string> tmp_vec;
48       
49        tmp = backend->get("statistics_games_started");
50        _games_started = atoi(tmp.c_str());
51
52        tmp = backend->get("statistics_games_finished");
53        _games_finished = atoi(tmp.c_str());
54
55        tmp = backend->get("statistics_score_distribution");
56        split(tmp_vec, tmp, is_any_of(","));
57        if (tmp_vec.size() != score_distributions_slots)
58                throw BadConfiguration();
59
60        BOOST_FOREACH(string i, tmp_vec) {
61                _score_distribution.push_back(atoi(i.c_str()));
62        }
63
64        istringstream i( backend->get("statistics_last_reset"));
65        i >> _last_reset;
66        if (!_last_reset)
67                throw BadConfiguration();
68               
69}
70
71void Statistics::game_started()
72{       
73        _games_started++;
74        save();
75}
76
77void Statistics::game_finished(int score)
78{       
79        int score_slot;
80        _games_finished++;
81
82        score_slot = score/score_distribution_granuality;
83        score_slot = score_slot<score_distributions_slots ? score_slot : score_slot;
84        _score_distribution[score_slot]++;
85        save();
86}
87
88void Statistics::save() {
89        string tmp;
90        backend->set("statistics_games_started", stringify(_games_started));
91        backend->set("statistics_games_finished", stringify(_games_finished));
92        backend->set("statistics_last_reset", stringify(_last_reset));
93
94        tmp = "";
95        BOOST_FOREACH(int i, _score_distribution) {
96                tmp += stringify(i) + ",";
97        }
98        //delete trailing comma
99        tmp = tmp.substr(0, tmp.size()-1);
100        backend->set("statistics_score_distribution", tmp);
101
102        backend->save();
103}
104
105void Statistics::reset() {
106        _games_started = 0;
107        _games_finished = 0;
108        _last_reset = time(NULL);
109
110        _score_distribution = vector<int>(score_distributions_slots, 0);
111       
112        save();
113}
Note: See TracBrowser for help on using the repository browser.