-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreboardManager.cpp
70 lines (55 loc) · 1.26 KB
/
ScoreboardManager.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 "ScoreboardManager.h"
#include <fstream>
#include <iostream>
static bool loadedScores = false;
static int activeManagers = 0;
std::multimap<int, std::string, std::greater<>> ScoreboardManager::scores;
struct ScoreData
{
int score = 0;
std::string name;
};
ScoreboardManager::ScoreboardManager()
{
activeManagers++;
if (!loadedScores)
LoadScores();
}
ScoreboardManager::~ScoreboardManager()
{
activeManagers--;
if (!activeManagers)
SaveScores();
}
void ScoreboardManager::AddScore(int score, const char* name)
{
scores.insert(std::pair<int, std::string>(score, name));
}
void ScoreboardManager::LoadScores()
{
std::ifstream file;
file.open("scores.bin", std::ios::binary);
if (!file)
return;
while (!file.eof())
{
ScoreData temp;
file >> temp.score >> temp.name;
if (!temp.name.empty() && temp.score != 0)
scores.insert(std::pair<int, std::string>(temp.score, temp.name));
}
file.close();
loadedScores = true;
}
void ScoreboardManager::SaveScores()
{
std::ofstream file("scores.bin", std::ios::binary);
for (const auto& score : scores)
{
ScoreData temp;
temp.score = score.first;
temp.name = score.second;
file << score.first << " " << score.second << std::endl;
}
file.close();
}