Get image paths from tracker
[impuzzle] / src / statistics.cpp
index eb5043e..b81af23 100644 (file)
@@ -1,3 +1,21 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include "statistics.h"
 #include "defines.h"
 
@@ -6,6 +24,8 @@
 #include <QTextStream>
 #include <QStringList>
 
+#include <QDebug>
+
 Statistics *Statistics::instance_ = 0;
 
 Statistics::Statistics(QObject *parent) :
@@ -47,7 +67,7 @@ qreal Statistics::averageMoves(Difficulty difficulty) const
 {
     qreal count = 0.0;
     if(games_.at(difficulty) > 0) {
-        count = moves_.at(difficulty) / static_cast<qreal>(games_.at(difficulty));
+        count = (moves_.at(difficulty) / games_.at(difficulty));
     }
 
     return count;
@@ -66,7 +86,7 @@ int Statistics::maxMoves(Difficulty difficulty) const
 void Statistics::addNewScore(int moves, Difficulty difficulty)
 {
     if(moves_.count() >= difficulty) {
-        moves_[difficulty] += (moves_[difficulty] + moves);
+        moves_[difficulty] += moves;
     }
 
     if(maxMoves_.count() >= difficulty) {
@@ -99,6 +119,7 @@ void Statistics::readFile()
                .arg(STATS_FILE));
 
     if(!file.exists()) {
+        qDebug() << __PRETTY_FUNCTION__ << "No settings file";
         return;
     }
 
@@ -126,6 +147,15 @@ void Statistics::readFile()
 
 void Statistics::saveFile()
 {
+    QDir dir(QString("%1/%2")
+             .arg(QDir::homePath())
+             .arg(HOME_DIRECTORY));
+    if(!dir.exists()) {
+        dir.mkpath(QString("%1/%2")
+                   .arg(QDir::homePath())
+                   .arg(HOME_DIRECTORY));
+    }
+
     QFile file(QString("%1/%2/%3")
                .arg(QDir::homePath())
                .arg(HOME_DIRECTORY)
@@ -148,3 +178,18 @@ void Statistics::saveFile()
 
     file.close();
 }
+
+void Statistics::resetStatistics()
+{
+    moves_.clear();
+    minMoves_.clear();
+    maxMoves_.clear();
+    games_.clear();
+
+    moves_ << 0 << 0;
+    minMoves_ << 0 << 0;
+    maxMoves_ << 0 << 0;
+    games_ << 0 << 0;
+
+    saveFile();
+}