Debian Codegraph @

source/code/highscore.cpp

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