-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreController.cpp
82 lines (71 loc) · 2.15 KB
/
ScoreController.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
81
82
#include <fstream>
#include "ScoreController.h"
ScoreController::ScoreController() : scores(), isRead(false), oldRecord(0) { }
ScoreController::~ScoreController() {}
void ScoreController::loadFromFile(bool isEndurance)
{
scores.clear();
std::ifstream file;
if (!isEndurance) { filepath = "highscores_class.dat"; }
else { filepath = "highscores_endur.dat"; }
file.open(filepath);
std::string nickname = "";
int points = 0, it = 1;
scores.push_back(std::make_unique<Score>(nickname, points, true));
while (file.good())
{
file >> nickname;
file >> points;
scores.push_back(std::make_unique<Score>(nickname, points, false));
getScore(it).refreshPosition(it);
it++;
}
file.close();
oldRecord = getScore(1).getPoints();
}
void ScoreController::update(uint pts)
{
scores[0]->addPoints(pts);
if (isBestBeaten()) { scores[1]->showPoints(scores[0]->getPoints()); }
}
bool ScoreController::isBestBeaten()
{
if (scores[0]->getPoints() >= scores[1]->getPoints()) { return true; }
return false;
}
bool ScoreController::checkSaving()
{
if (scores[0]->getPoints() > scores[5]->getPoints()) { return true; }
return false;
}
void ScoreController::changeOrder(sf::String enteredNickname) {
if (enteredNickname == "") { enteredNickname = "(nameless)"; }
uint it = 6;
bool isChanged;
do
{
isChanged = false;
if (scores[0]->getPoints() > scores[it - 1]->getPoints()) { --it; isChanged = true; }
} while ((it != 0) && isChanged);
if (it < 6)
{
scores.pop_back();
scores.insert(scores.begin() + it, std::make_unique<Score>(enteredNickname, scores[0]->getPoints(), false));
getScore(0).reset();
}
saveToFile();
}
void ScoreController::saveToFile() {
std::ofstream file(filepath, std::ios::out);
for (int it = 1; it < 6; it++)
{
file << scores[it]->getNickname() << " " << scores[it]->getPoints() << std::endl;
}
file.close();
}
Score& ScoreController::getScore(uint it) const { return *scores[it]; }
void ScoreController::changeScoreMode()
{
getScore(0).changeScoreMode(); getScore(0).refreshPosition(0);
getScore(1).changeScoreMode(); getScore(1).refreshPosition(1);
}