| ecef5838 |
sago007 |
2015-11-24 20:01 |
1 |
/* |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
2 |
=========================================================================== |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
3 |
blockattack - Block Attack - Rise of the Blocks |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
4 |
Copyright (C) 2005-2012 Poul Sander |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
5 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
6 |
This program is free software: you can redistribute it and/or modify |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
7 |
it under the terms of the GNU General Public License as published by |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
8 |
the Free Software Foundation, either version 2 of the License, or |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
9 |
(at your option) any later version. |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
10 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
11 |
This program is distributed in the hope that it will be useful, |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
14 |
GNU General Public License for more details. |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
15 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
16 |
You should have received a copy of the GNU General Public License |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
17 |
along with this program. If not, see http://www.gnu.org/licenses/ |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
18 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
19 |
Source information and contacts persons can be found at |
| 83d6c18d |
sago007 |
2018-03-31 16:28 |
20 |
https://blockattack.net |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
21 |
=========================================================================== |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
22 |
*/ |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
23 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
24 |
#include "highscore.h" |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
25 |
#include "os.hpp" |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
26 |
#include "sago/SagoMisc.hpp" |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
27 |
#include <algorithm> |
| b5321636 |
sago007 |
2019-02-09 18:47 |
28 |
#include "common.h" |
| 16476649 |
Poul Sander |
2022-01-09 10:29 |
29 |
#include <fmt/core.h> |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
30 |
#include "nlohmann/json.hpp" |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
31 |
|
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
32 |
using json = nlohmann::json; |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
33 |
|
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
34 |
void to_json(json& j, const record& p) { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
35 |
j = json{ {"Name", p.name}, {"Score", p.score}}; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
36 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
37 |
|
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
38 |
void from_json(const json& j, record& p) { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
39 |
p.name = j.at("Name").get<std::string>(); |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
40 |
p.score = j.at("Score").get<int>(); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
41 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
42 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
43 |
/* |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
44 |
This sorts in reverse order. So the highest will be first |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
45 |
*/ |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
46 |
bool record_sorter (const record& i,const record& j) { |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
47 |
return (i.score > j.score); |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
48 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
49 |
|
| 4379ec19 |
Poul Sander |
2019-01-30 18:27 |
50 |
Highscore::Highscore(const std::string& type, double speed) : filename(type+".json.dat"), type(type), speed(speed) { |
| b5321636 |
sago007 |
2019-02-09 18:47 |
51 |
if (speed < 0.4) { |
| b5321636 |
sago007 |
2019-02-09 18:47 |
52 |
//Use special filenames for higher speeds (higher speed = lower number) |
| 16476649 |
Poul Sander |
2022-01-09 10:29 |
53 |
filename = fmt::format("{}_{:.4f}.json.dat", type, speed); |
| b5321636 |
sago007 |
2019-02-09 18:47 |
54 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
55 |
std::string readFileContent = sago::GetFileContent(filename.c_str()); |
| e567627f |
Poul Sander |
2016-02-17 18:47 |
56 |
if (readFileContent.length() > 0) { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
57 |
json j = json::parse(readFileContent); |
| ad2d11ab |
Poul Sander |
2016-02-17 19:06 |
58 |
try { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
59 |
j.at("highscore").get_to(table); |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
60 |
} catch (json::exception& e) { |
| ad2d11ab |
Poul Sander |
2016-02-17 19:06 |
61 |
std::cerr << "Failed to read highscore " << filename << " due to formatting errors. Resetting the file. Reason: " << |
| 161a010c |
sago007 |
2016-05-06 14:00 |
62 |
e.what() << "\n"; |
| ad2d11ab |
Poul Sander |
2016-02-17 19:06 |
63 |
table.clear(); |
| e567627f |
Poul Sander |
2016-02-17 18:47 |
64 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
65 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
66 |
if (table.size() < top) { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
67 |
for (size_t i = table.size(); i<top; i++) { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
68 |
record r; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
69 |
r.name = "Poul Sander"; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
70 |
r.score = 2000 - i*100; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
71 |
table.push_back(r); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
72 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
73 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
74 |
std::stable_sort(table.begin(), table.end(), record_sorter); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
75 |
table.resize(top); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
76 |
writeFile(); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
77 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
78 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
79 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
80 |
void Highscore::writeFile() { |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
81 |
json j = json{ { "highscore", table } }; |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
82 |
std::string s = j.dump(4); |
| 26c50dce |
Poul Sander |
2024-05-11 12:59 |
83 |
sago::WriteFileContent(filename.c_str(), s); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
84 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
85 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
86 |
bool Highscore::isHighScore(int newScore) { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
87 |
if (newScore>table.back().score) { |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
88 |
return true; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
89 |
} |
| 30412ca6 |
sago007 |
2016-03-25 19:35 |
90 |
return false; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
91 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
92 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
93 |
void Highscore::addScore(const std::string& newName, int newScore) { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
94 |
record r; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
95 |
r.name = newName; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
96 |
r.score = newScore; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
97 |
table.push_back(r); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
98 |
std::stable_sort(table.begin(), table.end(), record_sorter); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
99 |
table.resize(top); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
100 |
Highscore::writeFile(); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
101 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
102 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
103 |
record Highscore::getScoreNumber(int room) { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
104 |
record ret; |
| c7f2082f |
sago007 |
2016-03-13 11:48 |
105 |
if (room < static_cast<int>(table.size()) ) { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
106 |
ret = table.at(room); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
107 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
108 |
return ret; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
109 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
110 |
|
|
|
1970-01-01 00:00 |
111 |
|