-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMorseStat.cpp
80 lines (66 loc) · 2.23 KB
/
MorseStat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "MorseStat.h"
#include <QtCore/QSettings>
#include "qdebug.h"
MorseStat::MorseStat(QObject *parent) :
QObject(parent), maxTimeCount(20), m_tryCount(0), m_goodCount(0), m_timeList()
{
}
int MorseStat::getTryCount() {
return m_tryCount;
}
int MorseStat::getGoodCount() {
return m_goodCount;
}
int MorseStat::getGoodPercentage(int minStartingGood) {
if (m_tryCount == 0)
return 0;
if (minStartingGood != 0 && m_goodCount < minStartingGood) {
qDebug() << "not good enough: " << m_goodCount << "/" << minStartingGood;
return 100 * m_goodCount / minStartingGood;
}
return int(100.0 * float(m_goodCount)/float(m_tryCount));
}
float MorseStat::getAverageTime() {
QList<float>::iterator iter;
QList<float>::iterator end = m_timeList.end();
float total = 0.0;
if (m_tryCount == 0)
return -1.0;
for(iter = m_timeList.begin(); iter != end; iter++) {
total = total + *iter;
}
return total/m_timeList.count();
}
void MorseStat::addTime(float newtime) {
m_timeList.append(newtime);
if (m_tryCount >= maxTimeCount)
m_timeList.pop_front();
m_tryCount++;
}
void MorseStat::addStat(float newtime, bool successful) {
addTime(newtime);
if (successful)
m_goodCount++;
}
void MorseStat::saveStats(QSettings &settings, const QString &statSetName) {
settings.setValue(statSetName + "/stats/tryCount", m_tryCount);
settings.setValue(statSetName + "/stats/goodCount", m_goodCount);
settings.beginWriteArray(statSetName + "/stats/times");
int maxListNum = m_timeList.count();
for(int i = 0; i < maxListNum; i++) {
settings.setArrayIndex(i);
settings.setValue("time", m_timeList[i]);
}
settings.endArray();
}
void MorseStat::loadStats(QSettings &settings, const QString &statSetName) {
m_tryCount = settings.value(statSetName + "/stats/tryCount", 0).toInt();
m_goodCount = settings.value(statSetName + "/stats/goodCount", 0).toInt();
m_timeList.clear();
int maxListNum = settings.beginReadArray(statSetName + "/stats/times");
for(int i = 0; i < maxListNum; i++) {
settings.setArrayIndex(i);
m_timeList.push_back(settings.value("time").toFloat());
}
settings.endArray();
}