1#include <Game/ScoreFile.hpp>
2#include <Game/BoardParser.hpp>
3#include <Misc/Utils.hpp>
4#include <Config/INI.hpp>
23 board_scroll_delay(0),
24 board_scroll_left(false),
25 board_scroll_right(false),
26 board_scroll_up(false),
27 board_scroll_down(false)
40 if (this->
level.empty())
45 this->speed == other.
speed &&
46 this->board_scroll_delay == other.board_scroll_delay &&
47 this->board_scroll_left == other.board_scroll_left &&
48 this->board_scroll_right == other.board_scroll_right &&
49 this->board_scroll_up == other.board_scroll_up &&
50 this->board_scroll_down == other.board_scroll_down &&
59 this->speed == other.
speed &&
60 this->board_scroll_delay == other.board_scroll_delay &&
61 this->board_scroll_left == other.board_scroll_left &&
62 this->board_scroll_right == other.board_scroll_right &&
63 this->board_scroll_up == other.board_scroll_up &&
64 this->board_scroll_down == other.board_scroll_down);
81 for (
size_t i = 0; i < files.size(); i++)
110 if (this->level_name.empty())
111 score_file = Globals::Config::scoresFile;
118 file.open(score_file.c_str());
120 std::stringstream buffer;
121 buffer << file.rdbuf();
124 std::stringstream contents;
133 std::string version = ini[
"version"];
138 Globals::Error::old_version_score_file =
true;
154 entry.
level = ini_score[
"level"];
155 entry.
points = Utils::String::to<unsigned int>(ini_score[
"points"]);
156 entry.
speed = Utils::String::to<unsigned int>(ini_score[
"speed"]);
157 entry.
fruits = Utils::String::to<int>(ini_score[
"fruits"]);
158 entry.
random_walls = Utils::String::to<bool>(ini_score[
"random_walls"]);
159 entry.
teleport = Utils::String::to<bool>(ini_score[
"teleport"]);
161 entry.board_scroll_delay = Utils::String::to<int>(ini_score[
"board_scroll_delay"]);
162 entry.board_scroll_left = Utils::String::to<bool>(ini_score[
"board_scroll_left"]);
163 entry.board_scroll_right = Utils::String::to<bool>(ini_score[
"board_scroll_right"]);
164 entry.board_scroll_up = Utils::String::to<bool>(ini_score[
"board_scroll_up"]);
165 entry.board_scroll_down = Utils::String::to<bool>(ini_score[
"board_scroll_down"]);
167 int board_size = Utils::String::to<int>(ini_score[
"board_size"]);
168 entry.
board_size = Globals::Game::intToBoardSize(board_size);
170 this->entries.push_back(entry);
176 tmp_score.
level = this->level_name;
177 tmp_score.
speed = Globals::Game::starting_speed;
178 tmp_score.
fruits = Globals::Game::fruits_at_once;
180 tmp_score.
teleport = Globals::Game::teleport;
181 tmp_score.
board_size = Globals::Game::board_size;
182 tmp_score.board_scroll_delay = Globals::Game::board_scroll_delay;
183 tmp_score.board_scroll_left = Globals::Game::board_scroll_left;
184 tmp_score.board_scroll_right = Globals::Game::board_scroll_right;
185 tmp_score.board_scroll_up = Globals::Game::board_scroll_up;
186 tmp_score.board_scroll_down = Globals::Game::board_scroll_down;
188 for (
size_t i = 0; i < (this->entries.size()); i++)
190 if (tmp_score.
isLike(this->entries[i]))
198 this->entries.push_back(tmp_score);
199 this->
highScore = &(this->entries[this->entries.size() - 1]);
212 if (this->level_name.empty())
213 score_file = Globals::Config::scoresFile;
228 ini.
top().
addKey(
"version", std::string(VERSION));
231 for (
size_t i = 0; i < (this->entries.size()); i++)
233 std::string score_name =
"score" + Utils::String::toString(i);
237 ini(score_name).addKey(
"level", this->entries[i].level);
238 ini(score_name).addKey(
"points", Utils::String::toString(this->entries[i].points));
239 ini(score_name).addKey(
"speed", Utils::String::toString(this->entries[i].speed));
240 ini(score_name).addKey(
"fruits", Utils::String::toString(this->entries[i].fruits));
242 ini(score_name).addKey(
"random_walls", Utils::String::toString(this->entries[i].random_walls));
243 ini(score_name).addKey(
"teleport", Utils::String::toString(this->entries[i].teleport));
245 int board_size = Globals::Game::boardSizeToInt(this->entries[i].board_size);
246 ini(score_name).addKey(
"board_size", Utils::String::toString(board_size));
248 ini(score_name).addKey(
"board_scroll_delay", Utils::String::toString(this->entries[i].board_scroll_delay));
249 ini(score_name).addKey(
"board_scroll_left", Utils::String::toString(this->entries[i].board_scroll_left));
250 ini(score_name).addKey(
"board_scroll_right", Utils::String::toString(this->entries[i].board_scroll_right));
251 ini(score_name).addKey(
"board_scroll_up", Utils::String::toString(this->entries[i].board_scroll_up));
252 ini(score_name).addKey(
"board_scroll_down", Utils::String::toString(this->entries[i].board_scroll_down));
255 std::stringstream contents;
259 file.open(score_file.c_str());
268 this->entries.push_back(*score);
269 this->
highScore = &(this->entries[this->entries.size() - 1]);
274 if (! score->
isLike(*this->highScore))
277 if ((score->
points) > (this->highScore->points))
Loads, reads and parses the contents of an INI file (or string).
void dump(std::ostream &stream)
Outputs the contents of the INI file to #stream.
Level & top()
Returns the top level of this INI file.
void create()
Creates a blank INI registry.
Custom exception class to specify an error that occurred during a level loading.
static std::string directory
Default directory where we store the game score files.
bool handle(ScoreEntry *score)
Checks if #score is the highest score and make it so.
ScoreFile(std::string levelName)
Creates a new score handler for the level #levelName.
static void eraseAll()
Erases all high score files.
void load()
Loads all high score entries based on a level name.
ScoreEntry * highScore
Maximum high score obtained for the current game.
static std::string extension
Default extension to save the score files.
void save()
Saves all the current scores on the file.
All global settings to the game.
char version[3]
Game version (format MMP - Major Minor Patch).
std::string encode(std::string str)
Transforms #str into a Base64 equivalent.
std::string decode(std::string const &s)
Transforms a Base64-encoded #str into it's regular string equivalent.
std::vector< std::string > ls(std::string path)
Lists all files withing #path.
bool create(std::string path)
Creates empty file #path.
void rm_f(std::string path)
Forcibly removes file within #path.
std::string extension(std::string path)
Returns the extension of a file.
bool exists(std::string path)
Tells if #path exists.
Contains a "scope" of the INI file.
Sections ordered_sections
All Sections in the original order of the INI file.
void addKey(std::string name, std::string value)
Creates a new key #name with #value.
void addGroup(std::string name)
Creates a new child group with #name.
A single entry on the high-score file.
bool random_walls
If random walls were spawned on this level.
bool isLike(ScoreEntry &other)
Tells if both scores were made on exact same game settings.
std::string level
On which level the user made this score.
int fruits
How many fruits at once were allowed on this level.
bool teleport
If teleport was enabled on this level.
unsigned int speed
Under which game speed the score was made.
unsigned int points
How many points the user got.
ScoreEntry()
Creates an empty score entry.
Globals::Game::BoardSize board_size
How large was the game board on this score.