1 #include <Entities/ScoreFile.hpp> 2 #include <Entities/BoardParser.hpp> 3 #include <Engine/Helpers/File.hpp> 4 #include <Engine/Helpers/String.hpp> 5 #include <Engine/Helpers/INI.hpp> 6 #include <Engine/Helpers/Base64.hpp> 25 board_scroll_delay(0),
26 board_scroll_left(false),
27 board_scroll_right(false),
28 board_scroll_up(false),
29 board_scroll_down(false)
42 if (this->
level.empty())
47 this->speed == other.
speed &&
48 this->board_scroll_delay == other.board_scroll_delay &&
49 this->board_scroll_left == other.board_scroll_left &&
50 this->board_scroll_right == other.board_scroll_right &&
51 this->board_scroll_up == other.board_scroll_up &&
52 this->board_scroll_down == other.board_scroll_down &&
61 this->speed == other.
speed &&
62 this->board_scroll_delay == other.board_scroll_delay &&
63 this->board_scroll_left == other.board_scroll_left &&
64 this->board_scroll_right == other.board_scroll_right &&
65 this->board_scroll_up == other.board_scroll_up &&
66 this->board_scroll_down == other.board_scroll_down);
79 Utils::File::rm_f(Globals::Config::scoresFile);
83 for (
size_t i = 0; i < files.size(); i++)
86 Utils::File::rm_f(files[i]);
112 if (this->level_name.empty())
113 score_file = Globals::Config::scoresFile;
115 if (! Utils::File::exists(score_file))
120 file.open(score_file.c_str());
122 std::stringstream buffer;
123 buffer << file.rdbuf();
126 std::stringstream contents;
127 contents << Utils::Base64::decode(buffer.str());
130 INI::Parser ini(contents);
135 std::string version = ini[
"version"];
140 Globals::Error::old_version_score_file =
true;
147 for (INI::Level::Sections::const_iterator it = ini.top().ordered_sections.begin();
148 it != ini.top().ordered_sections.end();
153 INI::Level ini_score = (*it)->second;
156 entry.
level = ini_score[
"level"];
157 entry.
points = Utils::String::to<unsigned int>(ini_score[
"points"]);
158 entry.
speed = Utils::String::to<unsigned int>(ini_score[
"speed"]);
159 entry.
fruits = Utils::String::to<int>(ini_score[
"fruits"]);
160 entry.
random_walls = Utils::String::to<bool>(ini_score[
"random_walls"]);
161 entry.
teleport = Utils::String::to<bool>(ini_score[
"teleport"]);
163 entry.board_scroll_delay = Utils::String::to<int>(ini_score[
"board_scroll_delay"]);
164 entry.board_scroll_left = Utils::String::to<bool>(ini_score[
"board_scroll_left"]);
165 entry.board_scroll_right = Utils::String::to<bool>(ini_score[
"board_scroll_right"]);
166 entry.board_scroll_up = Utils::String::to<bool>(ini_score[
"board_scroll_up"]);
167 entry.board_scroll_down = Utils::String::to<bool>(ini_score[
"board_scroll_down"]);
169 int board_size = Utils::String::to<int>(ini_score[
"board_size"]);
170 entry.
board_size = Globals::Game::intToBoardSize(board_size);
172 this->entries.push_back(entry);
178 tmp_score.
level = this->level_name;
179 tmp_score.
speed = Globals::Game::starting_speed;
180 tmp_score.
fruits = Globals::Game::fruits_at_once;
182 tmp_score.
teleport = Globals::Game::teleport;
183 tmp_score.
board_size = Globals::Game::board_size;
184 tmp_score.board_scroll_delay = Globals::Game::board_scroll_delay;
185 tmp_score.board_scroll_left = Globals::Game::board_scroll_left;
186 tmp_score.board_scroll_right = Globals::Game::board_scroll_right;
187 tmp_score.board_scroll_up = Globals::Game::board_scroll_up;
188 tmp_score.board_scroll_down = Globals::Game::board_scroll_down;
190 for (
size_t i = 0; i < (this->entries.size()); i++)
192 if (tmp_score.
isLike(this->entries[i]))
200 this->entries.push_back(tmp_score);
201 this->
highScore = &(this->entries[this->entries.size() - 1]);
214 if (this->level_name.empty())
215 score_file = Globals::Config::scoresFile;
219 if (! Utils::File::exists(score_file))
221 Utils::File::create(score_file);
223 if (! Utils::File::exists(score_file))
230 ini.top().addKey(
"version", std::string(VERSION));
233 for (
size_t i = 0; i < (this->entries.size()); i++)
235 std::string score_name =
"score" + Utils::String::toString(i);
237 ini.top().addGroup(score_name);
239 ini(score_name).addKey(
"level", this->entries[i].level);
240 ini(score_name).addKey(
"points", Utils::String::toString(this->entries[i].points));
241 ini(score_name).addKey(
"speed", Utils::String::toString(this->entries[i].speed));
242 ini(score_name).addKey(
"fruits", Utils::String::toString(this->entries[i].fruits));
244 ini(score_name).addKey(
"random_walls", Utils::String::toString(this->entries[i].random_walls));
245 ini(score_name).addKey(
"teleport", Utils::String::toString(this->entries[i].teleport));
247 int board_size = Globals::Game::boardSizeToInt(this->entries[i].board_size);
248 ini(score_name).addKey(
"board_size", Utils::String::toString(board_size));
250 ini(score_name).addKey(
"board_scroll_delay", Utils::String::toString(this->entries[i].board_scroll_delay));
251 ini(score_name).addKey(
"board_scroll_left", Utils::String::toString(this->entries[i].board_scroll_left));
252 ini(score_name).addKey(
"board_scroll_right", Utils::String::toString(this->entries[i].board_scroll_right));
253 ini(score_name).addKey(
"board_scroll_up", Utils::String::toString(this->entries[i].board_scroll_up));
254 ini(score_name).addKey(
"board_scroll_down", Utils::String::toString(this->entries[i].board_scroll_down));
257 std::stringstream contents;
261 file.open(score_file.c_str());
262 file << Utils::Base64::encode(contents.str());
270 this->entries.push_back(*score);
271 this->
highScore = &(this->entries[this->entries.size() - 1]);
276 if (! score->
isLike(*this->highScore))
ScoreEntry * highScore
Maximum high score obtained for the current game.
static std::string extension
Default extension to save the score files.
int fruits
How many fruits at once were allowed on this level.
std::string level
On which level the user made this score.
Custom exception class to specify an error that occurred during a level loading.
ScoreEntry()
Creates an empty score entry.
bool random_walls
If random walls were spawned on this level.
Globals::Game::BoardSize board_size
How large was the game board on this score.
char version[3]
Game version (format MMP - Major Minor Patch).
void save()
Saves all the current scores on the file.
bool teleport
If teleport was enabled on this level.
static std::string directory
Default directory where we store the game score files.
void load()
Loads all high score entries based on a level name.
A single entry on the high-score file.
static void eraseAll()
Erases all high score files.
Container for global settings on the game.
bool handle(ScoreEntry *score)
Checks if #score is the highest score and make it so.
bool isLike(ScoreEntry &other)
Tells if both scores were made on exact same game settings.
unsigned int points
How many points the user got.
unsigned int speed
Under which game speed the score was made.
ScoreFile(std::string levelName)
Creates a new score handler for the level #levelName.