Debian Codegraph @

source/code/stageclearhandler.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2015 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 http://www.blockattack.net
21 ===========================================================================
22 */
23
24 #include "nlohmann/json.hpp"
25 #include "stageclearhandler.hpp"
26 #include "SDL.h"
27 #include <vector>
28 #include <iostream>
29 #include <sstream>
30 #include "sago/SagoMisc.hpp"
31
32 using json = nlohmann::json;
33
34 //paths
35 const char* const stageClearSaveName = "stageClear.json.SCsave";
36
37 struct StageClearElement {
38 bool cleared = false;
39 int time = 0;
40 int score = 0;
41 };
42
43 void to_json(json& j, const StageClearElement& p) {
44 j = json{ {"cleared", p.cleared}, {"time", p.time}, {"score", p.score}};
45 }
46
47 void from_json(const json& j, StageClearElement& p) {
48 p.cleared = j.at("cleared").get<bool>();
49 p.time = j.at("time").get<int>();
50 p.score = j.at("score").get<int>();
51 }
52
53 std::vector<StageClearElement> stages(nrOfStageLevels);
54
55 //std::vector<bool> stageCleared(nrOfStageLevels); //vector that tells if a stage is cleared
56 //std::vector<Sint32> stageTimes(nrOfStageLevels); //For statistical puposes
57 //std::vector<Sint32> stageScores(nrOfStageLevels); //--||--
58 std::vector<StageClearElement> parScores(nrOfStageLevels);
59 Sint32 totalScore = 0;
60 Sint32 totalTime = 0;
61
62
63 static void SaveStageClearStages() {
64 json j = json{ {"stages", stages} };
65 std::string s = j.dump(4);
66 sago::WriteFileContent(stageClearSaveName, s);
67 }
68
69 void StageClearSetClear(int Level, int score, int time) {
70
71 stages.at(Level).cleared = true;
72 int gameEndedAfter = time;
73 if (stages.at(Level).score<score) {
74 stages[Level].score = score;
75 stages[Level].time = gameEndedAfter;
76 }
77 SaveStageClearStages();
78 }
79
80
81
82 void LoadStageClearStages() {
83 std::string readFileContent = sago::GetFileContent(stageClearSaveName);
84 if (readFileContent.length() > 0) {
85 json j = json::parse(readFileContent);
86 try {
87 j.at("stages").get_to(stages);
88 } catch (json::exception& e) {
89 std::cerr << "Failed to read highscore " << stageClearSaveName << " due to formatting errors. Resetting the file. Reason: " <<
90 e.what() << "\n";
91 stages.clear();
92 }
93 }
94 else {
95 stages.clear();
96 }
97 readFileContent = sago::GetFileContent("misc/stage_clear_par.json");
98 json j = json::parse(readFileContent);
99 try {
100 j.at("stages").get_to(parScores);
101 } catch (json::exception& e) {
102 std::cerr << "Failed to read par times. Reason: " << e.what() << "\n";
103 }
104 stages.resize(nrOfStageLevels);
105 totalScore = 0;
106 totalTime = 0;
107 for (const StageClearElement& s : stages) {
108 totalScore += s.score;
109 totalTime += s.time;
110 }
111 }
112
113 int GetTotalScore() {
114 return totalScore;
115 }
116 int GetTotalTime() {
117 return totalTime;
118 }
119
120 int GetNrOfLevels() {
121 return nrOfStageLevels;
122 }
123
124 bool IsStageCleared(int level) {
125 return stages.at(level).cleared;
126 }
127
128 int GetStageParScore(int level) {
129 return parScores.at(level).score;
130 }
131
132 int GetStageScores(int level) {
133 return stages.at(level).score;
134 }
135
136 int GetStageTime(int level) {
137 return stages.at(level).time;
138 }