-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStats.cpp
More file actions
179 lines (155 loc) · 4.46 KB
/
Copy pathGameStats.cpp
File metadata and controls
179 lines (155 loc) · 4.46 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
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
178
179
#include "GameStats.h"
#include <fstream>
#include <string>
#include <msclr/marshal_cppstd.h>
#include <sstream>
#include <memory>
namespace szablon {
GameStats::GameStats(): gameuser(nullptr) {}
void GameStats::loadUser(std::string split) {
std::vector<std::string> lines;
std::istringstream stream(split);
std::string segment;
while (std::getline(stream, segment, ';')) {
lines.push_back(segment);
}
if (lines.empty()) {
return;
}
std::string userName = lines[0];
GameUser* gameuser = new GameUser(userName, 0, 0, "-", true);
int pointSum = 0;
for (size_t i = 1; i < lines.size(); ++i) {
try {
int points = std::stoi(lines[i]);
pointSum += points;
if (!highScore || highScore < points) {
highScore = points;
this->gameuser = gameuser;
}
}
catch (const std::exception& e) {
continue;
}
}
if (!highScoreSum || highScoreSum < pointSum) {
highScoreSum = pointSum;
}
gameuser->setTotal(pointSum);
this->vectorGameUsers.push_back(gameuser);
}
GameUser* GameStats::getGameUser(std::string name)
{
for (GameUser* user : vectorGameUsers) {
if (user->getName() == name) {
return user;
}
}
return nullptr;
}
void GameStats::saveUser(GameUser* gu)
{
if (!gu) {
System::Windows::Forms::MessageBox::Show("Invalid user!",
"Error",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Error);
return;
}
std::ifstream inputFile(filePathNative);
if (!inputFile.is_open()) {
System::Windows::Forms::MessageBox::Show("Could not open file for saving user data!",
"Error",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Error);
return;
}
std::vector<std::string> lines;
std::string line;
bool updated = false;
while (std::getline(inputFile, line)) {
std::vector<std::string> parts;
std::istringstream stream(line);
std::string part;
while (std::getline(stream, part, ';')) {
parts.push_back(part);
}
if (!parts.empty() && parts[0] == gu->getName()) {
line += ";" + std::to_string(gu->getPoints());
updated = true;
}
lines.push_back(line);
}
inputFile.close();
if (!updated) {
lines.push_back(gu->getName() + ";" + std::to_string(gu->getPoints()));
}
gu->setTotal(gu->getTotalPoints());
gu->setFromBase(true);
std::ofstream outputFile(filePathNative);
if (!outputFile.is_open()) {
System::Windows::Forms::MessageBox::Show("Could not write to file!",
"Error",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Error);
return;
}
for (const auto& updatedLine : lines) {
outputFile << updatedLine << std::endl;
}
outputFile.close();
System::Windows::Forms::MessageBox::Show("User data saved successfully!",
"Info",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Information);
}
bool GameStats::openDialogFile() {
System::Windows::Forms::OpenFileDialog^ openFileDialog = gcnew System::Windows::Forms::OpenFileDialog();
openFileDialog->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog->Title = "Select a Statistics File";
if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
filePathNative = msclr::interop::marshal_as<std::string>(openFileDialog->FileName);
std::ifstream inputFile(filePathNative);
if (!inputFile.is_open()) {
System::Windows::Forms::MessageBox::Show("Failed to load the file!",
"Error",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Error);
return false;
}
std::string line;
while (std::getline(inputFile, line)) {
loadUser(line);
}
inputFile.close();
System::Windows::Forms::MessageBox::Show("Statistics successfully loaded!",
"Info",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Information);
return true;
}
System::Windows::Forms::MessageBox::Show("No file selected!",
"Info",
System::Windows::Forms::MessageBoxButtons::OK,
System::Windows::Forms::MessageBoxIcon::Information);
return false;
}
GameUser* GameStats::getHighScoreUser() {
return this->gameuser;
}
int GameStats::getHighScore()
{
return this->highScore;
}
int GameStats::getHighScoreSum()
{
return this->highScoreSum;
}
GameStats::~GameStats()
{
for (auto user : vectorGameUsers) {
delete user;
}
vectorGameUsers.clear();
}
}