Collecting basic statistics from won games
[impuzzle] / src / statistics.cpp
1 #include "statistics.h"
2 #include "defines.h"
3
4 #include <QDir>
5 #include <QFile>
6 #include <QTextStream>
7 #include <QStringList>
8
9 Statistics *Statistics::instance_ = 0;
10
11 Statistics::Statistics(QObject *parent) :
12         QObject(parent)
13 {
14     moves_ << 0 << 0;
15     minMoves_ << 0 << 0;
16     maxMoves_ << 0 << 0;
17     games_ << 0 << 0;
18
19     readFile();
20 }
21
22 Statistics *Statistics::instance()
23 {
24     if(!instance_) {
25         instance_ = new Statistics;
26     }
27
28     return instance_;
29 }
30
31 int Statistics::gameCount(Difficulty difficulty) const
32 {
33     return games_.at(difficulty);
34 }
35
36 int Statistics::totalGameCount() const
37 {
38     int count = 0;
39     for(int i = 0; i < games_.count(); ++i) {
40         count += games_.at(i);
41     }
42
43     return count;
44 }
45
46 qreal Statistics::averageMoves(Difficulty difficulty) const
47 {
48     qreal count = 0.0;
49     if(games_.at(difficulty) > 0) {
50         count = moves_.at(difficulty) / static_cast<qreal>(games_.at(difficulty));
51     }
52
53     return count;
54 }
55
56 int Statistics::minMoves(Difficulty difficulty) const
57 {
58     return minMoves_.at(difficulty);
59 }
60
61 int Statistics::maxMoves(Difficulty difficulty) const
62 {
63     return maxMoves_.at(difficulty);
64 }
65
66 void Statistics::addNewScore(int moves, Difficulty difficulty)
67 {
68     if(moves_.count() >= difficulty) {
69         moves_[difficulty] += (moves_[difficulty] + moves);
70     }
71
72     if(maxMoves_.count() >= difficulty) {
73         if(maxMoves_.at(difficulty) < moves || maxMoves_.at(difficulty) == 0) {
74             maxMoves_[difficulty] = moves;
75         }
76     }
77
78     if(minMoves_.count() >= difficulty) {
79         if(minMoves_.at(difficulty) > moves || minMoves_.at(difficulty) == 0) {
80             minMoves_[difficulty] = moves;
81         }
82     }
83
84     saveFile();
85 }
86
87 void Statistics::increaseGameCount(Difficulty difficulty)
88 {
89     if(games_.count() >= difficulty) {
90         games_[difficulty]++;
91     }
92 }
93
94 void Statistics::readFile()
95 {
96     QFile file(QString("%1/%2/%3")
97                .arg(QDir::homePath())
98                .arg(HOME_DIRECTORY)
99                .arg(STATS_FILE));
100
101     if(!file.exists()) {
102         return;
103     }
104
105     if(!file.open(QIODevice::ReadOnly)) {
106         return;
107     }
108
109     QTextStream in(&file);
110
111     while(!in.atEnd()) {
112         QString str = in.readLine();
113
114         QStringList list = str.split(" ");
115
116         if(list.count() == 5) {
117             moves_[list.at(0).toInt()] = list.at(1).toInt();
118             minMoves_[list.at(0).toInt()] = list.at(2).toInt();
119             maxMoves_[list.at(0).toInt()] = list.at(3).toInt();
120             games_[list.at(0).toInt()] = list.at(4).toInt();
121         }
122     }
123
124     file.close();
125 }
126
127 void Statistics::saveFile()
128 {
129     QFile file(QString("%1/%2/%3")
130                .arg(QDir::homePath())
131                .arg(HOME_DIRECTORY)
132                .arg(STATS_FILE));
133
134     if(!file.open(QIODevice::WriteOnly)) {
135         return;
136     }
137
138     QTextStream out(&file);
139
140     for(int i = 0; i < games_.count(); i++) {
141         out << QString("%1 %2 %3 %4 %5\n")
142                 .arg(i)
143                 .arg(moves_.at(i))
144                 .arg(minMoves_.at(i))
145                 .arg(maxMoves_.at(i))
146                 .arg(games_.at(i));
147     }
148
149     file.close();
150 }