-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaderboard.cpp
177 lines (142 loc) · 5.88 KB
/
Leaderboard.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Leaderboard.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Leaderboard::insert_new_entry(LeaderboardEntry* new_entry) {
if (head_leaderboard_entry == nullptr || head_leaderboard_entry->score < new_entry->score) {
new_entry->next_leaderboard_entry = head_leaderboard_entry;
head_leaderboard_entry = new_entry;
}
else {
LeaderboardEntry* current = head_leaderboard_entry;
int counter = 0;
while (current->next_leaderboard_entry != nullptr && current->next_leaderboard_entry->score >= new_entry->score && counter < 9) {
current = current->next_leaderboard_entry;
counter++;
}
new_entry->next_leaderboard_entry = current->next_leaderboard_entry;
current->next_leaderboard_entry = new_entry;
}
// Check if there are more than 10 entries and remove the 11th entry if needed
LeaderboardEntry* current = head_leaderboard_entry;
int count = 0;
while (current->next_leaderboard_entry != nullptr && count < 9) {
current = current->next_leaderboard_entry;
count++;
}
if (current->next_leaderboard_entry != nullptr) {
LeaderboardEntry* temp = current->next_leaderboard_entry;
current->next_leaderboard_entry = nullptr;
delete temp;
}
}
void Leaderboard::write_to_file(const string& filename) {
std::ofstream leaderboardfile(filename, std::ios::out);
if (!leaderboardfile.is_open()) {
std::cerr << "Error while opening file for writing leaderboard data...\n";
return;
}
LeaderboardEntry* currentEntry = head_leaderboard_entry;
while (currentEntry != nullptr) {
leaderboardfile << currentEntry->score << " ";
leaderboardfile << currentEntry->last_played << " ";
leaderboardfile << currentEntry->player_name << std::endl;
currentEntry = currentEntry->next_leaderboard_entry;
}
leaderboardfile.close();
}
void Leaderboard::read_from_file(const string& filename) {
std::fstream leaderboardfile(filename, std::ios::in | std::ios::out | std::ios::app);
if (!leaderboardfile.is_open()) {
std::cerr << "Error while opening Leaderboard data file...\n";
return;
}
leaderboardfile.clear();
leaderboardfile.seekg(0, std::ios::beg);
if (leaderboardfile.peek() == std::ifstream::traits_type::eof()) {
// File is empty, initialize the leaderboard
head_leaderboard_entry = nullptr;
leaderboardfile.close();
return;
}
unsigned long score;
time_t lastPlayed;
std::string playerName;
head_leaderboard_entry = nullptr; // Clear the current entries
// Read the file and construct the linked list
while (leaderboardfile >> score >> lastPlayed >> playerName) {
LeaderboardEntry* newEntry = new LeaderboardEntry(score, lastPlayed, playerName);
newEntry->next_leaderboard_entry = head_leaderboard_entry;
head_leaderboard_entry = newEntry;
}
leaderboardfile.close();
// Sort the leaderboard entries based on scores in descending order
sort_leaderboard();
// Update the 'size' variable if necessary
update_size();
}
void Leaderboard::sort_leaderboard() {
bool swapped;
LeaderboardEntry* temp;
do {
swapped = false;
LeaderboardEntry* current = head_leaderboard_entry;
while (current->next_leaderboard_entry != nullptr) {
if (current->score < current->next_leaderboard_entry->score ||
(current->score == current->next_leaderboard_entry->score &&
current->last_played > current->next_leaderboard_entry->last_played)) {
swap(current, current->next_leaderboard_entry);
swapped = true;
}
current = current->next_leaderboard_entry;
}
temp = current;
} while (swapped);
}
void Leaderboard::swap(LeaderboardEntry* entry1, LeaderboardEntry* entry2) {
unsigned long tempScore = entry1->score;
time_t tempLastPlayed = entry1->last_played;
std::string tempPlayerName = entry1->player_name;
entry1->score = entry2->score;
entry1->last_played = entry2->last_played;
entry1->player_name = entry2->player_name;
entry2->score = tempScore;
entry2->last_played = tempLastPlayed;
entry2->player_name = tempPlayerName;
}
void Leaderboard::update_size() {
size = 0;
LeaderboardEntry* current = head_leaderboard_entry;
while (current != nullptr) {
size++;
current = current->next_leaderboard_entry;
}
}
void Leaderboard::print_leaderboard() {
LeaderboardEntry* currentEntry = head_leaderboard_entry;
int number = 1;
while (currentEntry != nullptr) {
std::cout << number << ". ";
std::cout << currentEntry->player_name<< " ";
std::cout <<currentEntry->score<< " " ;
std::tm* localTime = std::localtime(¤tEntry->last_played);
char timeBuffer[20];
std::strftime(timeBuffer, sizeof(timeBuffer), "%H:%M:%S/%d.%m.%Y", localTime);
std::cout << timeBuffer<< std::endl;
currentEntry = currentEntry->next_leaderboard_entry;
number++;
}
// TODO: Print the current leaderboard status to the standard output in the format specified in the PA instructions
}
Leaderboard::~Leaderboard() {
LeaderboardEntry* current = head_leaderboard_entry;
while (current != nullptr) {
LeaderboardEntry* temp = current;
current = current->next_leaderboard_entry;
if (temp!=nullptr){delete temp;};
}
head_leaderboard_entry = nullptr; //
// TODO: Free dynamically allocated memory used for storing leaderboard entries
}