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