Changeset 197


Ignore:
Timestamp:
02/05/09 17:37:36 (4 years ago)
Author:
guyru
Message:

Save score distribution

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/statistics.cpp

    r193 r197  
    2121#include <string> 
    2222#include <cstdlib> 
     23#include <boost/algorithm/string/split.hpp> 
     24#include <boost/algorithm/string/classification.hpp> 
    2325#include "statistics.h" 
    2426#include "utility.h" 
     
    2628using namespace std; 
    2729using namespace statistics; 
     30using namespace boost; 
    2831 
    2932Statistics::Statistics(configuration::Configuration *backend) 
    3033{ 
    3134        string tmp; 
     35        vector<string> tmp_vec; 
    3236        this->backend = backend; 
    3337         
     
    3741        tmp = backend->get("statistics_games_finished"); 
    3842        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        } 
    3955         
    4056} 
     
    4864void Statistics::game_finished(int score) 
    4965{        
     66        int score_slot; 
    5067        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]++; 
    5172        save(); 
    5273} 
    5374 
    5475void Statistics::save() { 
     76        string tmp; 
    5577        backend->set("statistics_games_started", stringify(games_started)); 
    5678        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); 
    5788 
    5889        backend->save(); 
     
    6091 
    6192void Statistics::reset() { 
    62         backend->set("statistics_games_started", 0); 
    63         backend->set("statistics_games_finished", 0); 
     93        games_started = 0; 
     94        games_finished = 0; 
    6495 
    65         backend->save(); 
     96        score_distribution = vector<int>(score_distributions_slots, 0); 
     97         
     98        save(); 
    6699} 
  • trunk/src/statistics.h

    r193 r197  
    2323 
    2424#include "configuration.h" 
     25#include <vector>  
    2526namespace statistics { 
    2627 
     
    5253        int games_started; 
    5354        int games_finished; 
     55        std::vector<int> score_distribution; 
    5456        configuration::Configuration *backend; 
    5557}; 
     58 
     59const int score_distribution_granuality = 50; 
     60// anything above the following score will be in the same slot 
     61const int score_distribution_max = 500; 
     62const int score_distributions_slots = score_distribution_max/score_distribution_granuality+1; 
    5663 
    5764} //namespace 
Note: See TracChangeset for help on using the changeset viewer.