-
Notifications
You must be signed in to change notification settings - Fork 0
/
Score.cpp
98 lines (84 loc) · 1.64 KB
/
Score.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Score.h"
Score::Score()
{
this->score = 0;
}
void Score::readFileToScores(const char* content)
{
auto fileSize = strlen(content);
if (fileSize == 0)
{
return;
}
size_t pos = 0;
char nameStore[256]{};
nameStore[0] = '\0';
char numberStore[256]{};
numberStore[0] = '\0';
size_t tempCur = 0;
while (pos <= fileSize)
{
auto c = *(content + pos);
if (c >= 'A' && c <= 'Z')
{
if (tempCur < sizeof(nameStore))
{
nameStore[tempCur++] = c;
}
nameStore[tempCur] = '\0';
}
else if (c >= '0' && c <= '9')
{
if (tempCur < sizeof(numberStore))
{
numberStore[tempCur++] = c;
}
numberStore[tempCur] = '\0';
}
else
{
if (strlen(numberStore) > 0 && strlen(nameStore) > 0)
{
this->scores.insert(std::pair<std::string, size_t>(std::string(nameStore), atoi(numberStore)));
nameStore[0] = '\0';
numberStore[0] = '\0';
}
tempCur = 0;
}
pos++;
}
}
void Score::setScore(Uint32 score)
{
this->score = score;
}
Uint32 Score::getScore() const
{
return this->score;
}
void Score::addNewName(std::string name)
{
this->scores.insert(std::pair<std::string, size_t>(std::string(name), this->getScore()));
while (this->scores.size() > 5)
{
this->scores.erase(--this->scores.cend());
}
}
void Score::clearScores()
{
this->scores.clear();
}
std::set<std::pair<std::string, size_t>, classcomp> Score::getScores() const
{
return this->scores;
}
std::string Score::getScoresContent() const
{
std::string content = "";
for (auto it = this->scores.cbegin(); it != this->scores.cend(); ++it)
{
content += it->first + " ";
content += std::to_string(it->second) + '\n';
}
return content;
}