From 221b08e07246e84587b4144eca5564ce9ad3a20a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 1 Nov 2024 19:02:28 +0100 Subject: implement leaderboard functionality --- frontend/GameData.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'frontend/GameData.cpp') 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("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 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(0), row.col(1)); + } +} + vector GameData::random_locations(unsigned count) { static DBStatement query = this->db->prepare(R"( select naam -- cgit v1.2.3