-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata.cpp
More file actions
148 lines (123 loc) · 4.19 KB
/
Copy pathuserdata.cpp
File metadata and controls
148 lines (123 loc) · 4.19 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
148
#include "userdata.h"
#include <sstream>
#include <cctype>
UserManager::UserManager() {
currentUser = "";
}
void UserManager::setCurrentUser(const std::string& username) {
currentUser = username;
}
std::string UserManager::getCurrentUser() const {
return currentUser;
}
void UserManager::saveScore(const std::string& songName, int score, int pureCount, int farCount, int lostCount, int maxCombo, double accuracy) {
if (currentUser.empty()) return;
UserScore newScore;
newScore.username = currentUser;
newScore.songName = songName;
newScore.score = score;
newScore.pureCount = pureCount;
newScore.farCount = farCount;
newScore.lostCount = lostCount;
newScore.maxCombo = maxCombo;
newScore.accuracy = accuracy;
userScores[currentUser].push_back(newScore);
saveToFile();
}
std::vector<UserScore> UserManager::getUserScores(const std::string& username) const {
auto it = userScores.find(username);
if (it != userScores.end()) {
return it->second;
}
return {};
}
std::vector<UserScore> UserManager::getSongRanking(const std::string& songName) const {
std::vector<UserScore> ranking;
for (const auto& userEntry : userScores) {
for (const auto& score : userEntry.second) {
if (score.songName == songName) {
ranking.push_back(score);
}
}
}
std::sort(ranking.begin(), ranking.end(),
[](const UserScore& a, const UserScore& b) {
return a.score > b.score;
});
return ranking;
}
bool UserManager::hasScoreForSong(const std::string& username, const std::string& songName) const {
for (const auto& score : getUserScores(username)) {
if (score.songName == songName) {
return true;
}
}
return false;
}
std::vector<std::string> UserManager::getAllUsers() const {
std::vector<std::string> users;
for (const auto& entry : userScores) {
users.push_back(entry.first);
}
return users;
}
void UserManager::saveToFile() {
std::ofstream file(dataFile);
if (!file.is_open()) {
std::cerr << "Failed to open file for writing: " << dataFile << std::endl;
return;
}
for (const auto& userEntry : userScores) {
for (const auto& score : userEntry.second) {
file << score.username << ","
<< score.songName << ","
<< score.score << ","
<< score.pureCount << ","
<< score.farCount << ","
<< score.lostCount << ","
<< score.maxCombo << ","
<< score.accuracy << "\n";
}
}
file.close();
std::cout << "User data saved to " << dataFile << std::endl;
}
void UserManager::loadFromFile() {
std::ifstream file(dataFile);
if (!file.is_open()) {
std::cerr << "No existing user data file found: " << dataFile << std::endl;
return;
}
userScores.clear();
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string token;
std::vector<std::string> tokens;
while (std::getline(iss, token, ',')) {
tokens.push_back(token);
}
if (tokens.size() != 8) {
continue;
}
// Skip legacy level-number format from older saves
bool legacyLevel = !tokens[1].empty() &&
std::all_of(tokens[1].begin(), tokens[1].end(),
[](unsigned char c) { return std::isdigit(c); });
if (legacyLevel) {
continue;
}
UserScore score;
score.username = tokens[0];
score.songName = tokens[1];
score.score = std::stoi(tokens[2]);
score.pureCount = std::stoi(tokens[3]);
score.farCount = std::stoi(tokens[4]);
score.lostCount = std::stoi(tokens[5]);
score.maxCombo = std::stoi(tokens[6]);
score.accuracy = std::stod(tokens[7]);
userScores[score.username].push_back(score);
}
file.close();
std::cout << "User data loaded from " << dataFile << std::endl;
}