source: trunk/src/statistics.cpp @ 197

Last change on this file since 197 was 197, checked in by guyru, 5 years ago

Save score distribution

File size: 3.1 KB
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 <cstdlib>
23#include <boost/algorithm/string/split.hpp>
24#include <boost/algorithm/string/classification.hpp>
25#include "statistics.h"
26#include "utility.h"
27
28using namespace std;
29using namespace statistics;
30using namespace boost;
31
32Statistics::Statistics(configuration::Configuration *backend)
33{
34        string tmp;
35        vector<string> tmp_vec;
36        this->backend = backend;
37       
38        tmp = backend->get("statistics_games_started");
39        games_started = atoi(tmp.c_str());
40
41        tmp = backend->get("statistics_games_finished");
42        games_finished = atoi(tmp.c_str());
43
44        tmp = backend->get("statistics_score_distribution");
45        split(tmp_vec, tmp, is_any_of(","));
46        if (tmp_vec.size() != score_distributions_slots) {
47                reset();
48                return;
49        }
50
51        for (vector<string>::iterator iter = tmp_vec.begin();
52                iter != tmp_vec.end(); iter++) {
53                score_distribution.push_back(atoi(iter->c_str()));
54        }
55       
56}
57
58void Statistics::game_started()
59{       
60        games_started++;
61        save();
62}
63
64void Statistics::game_finished(int score)
65{       
66        int score_slot;
67        games_finished++;
68
69        score_slot = score/score_distribution_granuality;
70        score_slot = score_slot<score_distributions_slots ? score_slot : score_slot;
71        score_distribution[score_slot]++;
72        save();
73}
74
75void Statistics::save() {
76        string tmp;
77        backend->set("statistics_games_started", stringify(games_started));
78        backend->set("statistics_games_finished", stringify(games_finished));
79
80        tmp = "";
81        for (vector<int>::iterator iter = score_distribution.begin();
82                iter != score_distribution.end(); iter++) {
83                tmp += stringify(*iter) + ",";
84        }
85        //delete trailing comma
86        tmp = tmp.substr(0, tmp.size()-1);
87        backend->set("statistics_score_distribution", tmp);
88
89        backend->save();
90}
91
92void Statistics::reset() {
93        games_started = 0;
94        games_finished = 0;
95
96        score_distribution = vector<int>(score_distributions_slots, 0);
97       
98        save();
99}
Note: See TracBrowser for help on using the repository browser.