-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoreboard.cpp
More file actions
147 lines (121 loc) · 3.28 KB
/
scoreboard.cpp
File metadata and controls
147 lines (121 loc) · 3.28 KB
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
#include "scoreboard.h"
#include <QDebug>
#include <QDir>
#include <QDateTime>
#include <algorithm>
Scoreboard::Scoreboard(QObject *parent)
: QObject(parent),
m_isVisible(false)
{
// Set the scoreboard file path to the application directory
m_scoreboardFile = QDir::currentPath() + "/Scoreboard.txt";
// Load existing scores
loadScores();
}
bool Scoreboard::saveScore(const QString &playerName, int score)
{
// Use default name if empty
QString name = playerName.trimmed().isEmpty() ? "No Name" : playerName.trimmed();
// Create a new score entry
PlayerScore newScore;
newScore.name = name;
newScore.score = score;
// Add new score to the list
m_scores.append(newScore);
// Sort scores
sortScores();
// Trim to max scores if needed
while (m_scores.size() > MAX_SCORES)
{
m_scores.removeLast();
}
// Save to file
return saveAllScores();
}
QStringList Scoreboard::loadTopScores()
{
// Make sure scores are loaded and sorted
loadScores();
QStringList topScores;
// Convert the scores to a string list
for (int i = 0; i < m_scores.size() && i < MAX_SCORES; ++i)
{
QString scoreEntry = QString("%1. %2 : %3")
.arg(i + 1, 2)
.arg(m_scores[i].name)
.arg(m_scores[i].score);
topScores.append(scoreEntry);
}
return topScores;
}
bool Scoreboard::isHighScore(int score)
{
// Load existing scores
loadScores();
// If we don't have the maximum number of scores yet, any score qualifies
if (m_scores.size() < MAX_SCORES)
{
return true;
}
// Check if the score is higher than the lowest score in the list
return score > m_scores.last().score;
}
bool Scoreboard::loadScores()
{
// Clear current scores
m_scores.clear();
// Check if file exists
QFile file(m_scoreboardFile);
if (!file.exists())
{
qDebug() << "Scoreboard file does not exist: " << m_scoreboardFile;
return false;
}
if (!file.open(QFile::ReadOnly))
{
qDebug() << "Failed to open scoreboard file for reading:" << m_scoreboardFile;
return false;
}
// Read scores
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
QStringList parts = line.split("|");
if (parts.size() >= 2)
{
PlayerScore score;
score.name = parts[0];
score.score = parts[1].toInt();
m_scores.append(score);
}
}
file.close();
// Sort scores
sortScores();
return true;
}
void Scoreboard::sortScores()
{
std::sort(m_scores.begin(), m_scores.end(),
[](const PlayerScore &a, const PlayerScore &b)
{
return a.score > b.score;
});
}
bool Scoreboard::saveAllScores()
{
QFile file(m_scoreboardFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open scoreboard file for writing: " << m_scoreboardFile;
return false;
}
QTextStream out(&file);
for (const auto &score : m_scores)
{
out << score.name << "|" << score.score << "\n";
}
file.close();
return true;
}