-
Notifications
You must be signed in to change notification settings - Fork 7
/
Score.cpp
83 lines (70 loc) · 2.95 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
/******************************************************************************
* @file Score.cpp
* @author Andrés Gavín Murillo, 716358
* @author Rubén Rodríguez Esteban, 737215
* @date Mayo 2020
* @coms Videojuegos - OutRun
******************************************************************************/
#include "Score.hpp"
#include <fstream>
#include <sstream>
using namespace std;
Score::Score(unsigned long score, const string &name, int minutes, int secs, int centsSecond) : score(score),
name(name),
minutes(minutes),
secs(secs),
cents_second(
centsSecond) {}
vector<Score> getGlobalScores() {
vector<Score> globalScores;
ifstream fin("resources/scores.info");
if (fin.is_open()) {
string line;
for (int i = 0; getline(fin, line) && i < 7; i++) {
unsigned long score;
string name;
int minutes;
int secs;
int cents_second;
istringstream iss(line);
iss >> score >> name >> minutes >> secs >> cents_second;
globalScores.emplace_back(score, name, minutes, secs, cents_second);
}
fin.close();
}
return globalScores;
}
int isNewRecord(const vector<Score> &globalScores, unsigned long score) {
int i = 0;
for (; i < globalScores.size() && i < 7; i++) {
if (globalScores[i].score < score)
return i;
}
if (globalScores.size() < 7)
return i;
return -1;
}
bool saveNewRecord(const vector<Score> &globalScores, const Score &newRecord) {
bool saved = false;
ofstream fout("resources/scores.info", ofstream::trunc);
if (fout.is_open()) {
bool end = false;
int i = 0;
for (int j = 0; !end && j < 7; j++) {
if (!saved && ((i < globalScores.size() && globalScores[i].score < newRecord.score) ||
(i >= globalScores.size()))) {
saved = true;
fout << newRecord.score << " " << newRecord.name << " " << newRecord.minutes << " " << newRecord.secs
<< " " << newRecord.cents_second << endl;
} else {
fout << globalScores[i].score << " " << globalScores[i].name << " " << globalScores[i].minutes << " "
<< globalScores[i].secs << " " << globalScores[i].cents_second << endl;
i++;
}
if (i >= globalScores.size() && saved)
end = true;
}
fout.close();
}
return saved;
}