diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 19:02:28 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-01 19:02:28 +0100 |
commit | 221b08e07246e84587b4144eca5564ce9ad3a20a (patch) | |
tree | d46a71a331ff51a92c1c332e57e0a7c88851a713 /frontend/GameData.cpp | |
parent | ca3e80a5b474d99391c253d3173117e955e33a20 (diff) |
implement leaderboard functionality
Diffstat (limited to 'frontend/GameData.cpp')
-rw-r--r-- | frontend/GameData.cpp | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp index e62cff1..ee05d4b 100644 --- a/frontend/GameData.cpp +++ b/frontend/GameData.cpp @@ -6,6 +6,7 @@ #include "backend/LocationFactory.h" #include "backend/Object.h" #include "backend/ObjectFactory.h" +#include "backend/print.h" #include "GameData.h" @@ -20,7 +21,7 @@ GameData::GameData() { this->db = make_unique<DB>("kerkersendraken.db"); } -Enemy * GameData::create_enemy(const string & name) { +Enemy * GameData::create_enemy(const string & name) const { static DBStatement query = this->db->prepare(R"( select naam, @@ -62,7 +63,7 @@ static const unordered_map<string, ObjectType> type_map = { { "goudstukken", ObjectType::GOLD }, }; -Object * GameData::create_object(const string & name) { +Object * GameData::create_object(const string & name) const { static DBStatement query = this->db->prepare(R"( select type, @@ -95,7 +96,7 @@ Object * GameData::create_object(const string & name) { } } -Location * GameData::create_location(const string & name) { +Location * GameData::create_location(const string & name) const{ static DBStatement query = this->db->prepare(R"( select naam, @@ -116,7 +117,7 @@ Location * GameData::create_location(const string & name) { } } -void GameData::leaderbord_add(const string & name, unsigned int gold) { +void GameData::leaderbord_add(const string & name, unsigned int gold) const { static DBStatement stmt = this->db->prepare(R"( insert into Leaderboard (naam, goudstukken) values (?, ?) @@ -128,6 +129,24 @@ void GameData::leaderbord_add(const string & name, unsigned int gold) { stmt.execute(); } +void GameData::leaderbord_print() const { + static DBStatement query = this->db->prepare(R"( + select naam, goudstukken + from Leaderboard + order by goudstukken desc + limit 10 + )"); + query.reset(); + printf("\033[1;4m"); + lprtf("%3s %-20s %4s", "#", "Naam", "Goud"); + printf("\033[0m"); + lprtf("\n"); + unsigned int i = 1; + for (DBQueryRow & row : query.rows()) { + lprtf("%3d %-20s %4d\n", i++, row.col<const char *>(0), row.col<int>(1)); + } +} + vector<string> GameData::random_locations(unsigned count) { static DBStatement query = this->db->prepare(R"( select naam |