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