-
Notifications
You must be signed in to change notification settings - Fork 2
/
highscore.cpp
70 lines (62 loc) · 2 KB
/
highscore.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
#include "highscore.h"
bool Highscore::Hero::operator<(const Hero &other) const {
return getPoints() > other.getPoints();
}
Highscore::Highscore(): settings("SolderinSkaters","TiltNRoll") {
if (!settings.contains("highscore/names")) {
qDebug("[HS] highscore settings not found, creating some");
reset();
save();
} else {
qDebug("[HS] highscore settings found, loading them");
load();
}
}
bool Highscore::isHighscore(int points) {
int rank = 1;
Hero h;
foreach(h,heroes) if(h.points>points) rank++;
return rank<10;
}
void Highscore::save() {
qDebug("[HS]");
QList<QVariant> names, points, levels;
Hero h;
foreach(h, heroes) {
names.append(h.name);
points.append(h.points);
levels.append(h.level);
}
settings.setValue("highscore/names", names);
settings.setValue("highscore/points", points);
settings.setValue("highscore/levels", levels);
}
void Highscore::load() {
QList<QVariant> names = settings.value("highscore/names").toList();
QList<QVariant> points = settings.value("highscore/points").toList();
QList<QVariant> levels = settings.value("highscore/levels").toList();
heroes.clear();
for (int i=0; i<names.size(); i++) {
heroes.append(Hero(names[i].toString(), points[i].toInt(), levels[i].toInt()));
}
qSort(heroes.begin(), heroes.end());
}
bool Highscore::addToHighscore(Hero hero) {
if (!isHighscore(hero.points)) return false;
heroes.append(hero);
qSort(heroes.begin(), heroes.end());
while (heroes.size() > 10) heroes.pop_back();
return true;
}
void Highscore::reset() {
heroes.clear();
heroes.append(Hero("Keywan",9,1));
heroes.append(Hero("Jan",8,1));
heroes.append(Hero("ACE aka Alexander",7,1));
heroes.append(Hero("Flo",6,1));
heroes.append(Hero("Nick",5,1));
heroes.append(Hero("Linse",4,1));
heroes.append(Hero("Sebastian",3,1));
heroes.append(Hero("Erik",2,1));
heroes.append(Hero("Lennart",1,1));
}