diff options
Diffstat (limited to 'frontend')
-rw-r--r-- | frontend/DB.cpp | 4 | ||||
-rw-r--r-- | frontend/DB.h | 6 | ||||
-rw-r--r-- | frontend/GameData.cpp | 38 | ||||
-rw-r--r-- | frontend/GameData.h | 17 | ||||
-rw-r--r-- | frontend/cmd/hit.cpp | 2 |
5 files changed, 38 insertions, 29 deletions
diff --git a/frontend/DB.cpp b/frontend/DB.cpp index 28a1c3d..0f52433 100644 --- a/frontend/DB.cpp +++ b/frontend/DB.cpp @@ -17,11 +17,11 @@ DB::DB(const string & path) { throw Exception("sqlite3_open_v2: %d", ret); } -DBStatement DB::prepare(const string & query) { +DBStatement DB::prepare(const string & query) const { return DBStatement(*this, query); } -DBStatement::DBStatement(DB & parent, const string & query) : parent(parent) { +DBStatement::DBStatement(const DB & parent, const string & query) : parent(parent) { sqlite3_stmt * stmt = NULL; int ret = sqlite3_prepare_v2(this->parent.db.get(), query.c_str(), query.size(), &stmt, NULL); this->stmt = { diff --git a/frontend/DB.h b/frontend/DB.h index 2746e09..e3abfbf 100644 --- a/frontend/DB.h +++ b/frontend/DB.h @@ -54,7 +54,7 @@ class DBStatement { friend class DBQueryRowRange; public: - DBStatement(DB &, const std::string & query); + DBStatement(const DB &, const std::string & query); public: DBStatement & reset(); @@ -68,7 +68,7 @@ public: private: std::unique_ptr<sqlite3_stmt, std::function<void(sqlite3_stmt*)>> stmt; int param_index = 1; - DB & parent; + const DB & parent; }; class DB { @@ -76,7 +76,7 @@ class DB { public: DB(const std::string & path); - DBStatement prepare(const std::string & query); + DBStatement prepare(const std::string & query) const; private: std::unique_ptr<sqlite3, std::function<void(sqlite3*)>> db = NULL; diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp index ee05d4b..a716a57 100644 --- a/frontend/GameData.cpp +++ b/frontend/GameData.cpp @@ -17,12 +17,8 @@ GameData & GameData::get_instance() { return instance; } -GameData::GameData() { - this->db = make_unique<DB>("kerkersendraken.db"); -} - Enemy * GameData::create_enemy(const string & name) const { - static DBStatement query = this->db->prepare(R"( + static DBStatement query = this->db.prepare(R"( select naam, omschrijving, @@ -46,8 +42,7 @@ Enemy * GameData::create_enemy(const string & name) const { // TODO: min/max objects(?) enemy->set_health(row.col<int>(4)); enemy->set_attack(static_cast<float>(row.col<int>(5)) / 100); - enemy->set_damage_min(row.col<int>(6)); - enemy->set_damage_max(row.col<int>(7)); + enemy->set_damage({ row.col<int>(6), row.col<int>(7) }); return enemy.release(); } catch (...) { return EnemyFactory::create_enemy(name.c_str()); @@ -64,7 +59,7 @@ static const unordered_map<string, ObjectType> type_map = { }; Object * GameData::create_object(const string & name) const { - static DBStatement query = this->db->prepare(R"( + static DBStatement query = this->db.prepare(R"( select type, omschrijving, @@ -97,7 +92,7 @@ Object * GameData::create_object(const string & name) const { } Location * GameData::create_location(const string & name) const{ - static DBStatement query = this->db->prepare(R"( + static DBStatement query = this->db.prepare(R"( select naam, beschrijving @@ -117,8 +112,8 @@ Location * GameData::create_location(const string & name) const{ } } -void GameData::leaderbord_add(const string & name, unsigned int gold) const { - static DBStatement stmt = this->db->prepare(R"( +void GameData::leaderbord_add(const string & name, unsigned int gold) { + static DBStatement stmt = this->db.prepare(R"( insert into Leaderboard (naam, goudstukken) values (?, ?) )"); @@ -130,7 +125,7 @@ void GameData::leaderbord_add(const string & name, unsigned int gold) const { } void GameData::leaderbord_print() const { - static DBStatement query = this->db->prepare(R"( + static DBStatement query = this->db.prepare(R"( select naam, goudstukken from Leaderboard order by goudstukken desc @@ -147,14 +142,15 @@ void GameData::leaderbord_print() const { } } -vector<string> GameData::random_locations(unsigned count) { - static DBStatement query = this->db->prepare(R"( +vector<string> GameData::random_names(const string & table, unsigned count) const { + static DBStatement query = this->db.prepare(R"( select naam - from Locaties + from ? order by random() limit ? )"); query.reset() + .bind(table.c_str()) .bind(count) ; @@ -165,3 +161,15 @@ vector<string> GameData::random_locations(unsigned count) { return names; } +vector<string> GameData::random_locations(unsigned count) const { + return this->random_names("Locaties", count); +} + +vector<string> GameData::random_objects(unsigned count) const { + return this->random_names("Objecten", count); +} + +vector<string> GameData::random_enemies(unsigned count) const { + return this->random_names("Vijanden", count); +} + diff --git a/frontend/GameData.h b/frontend/GameData.h index 0d7577d..b0c9415 100644 --- a/frontend/GameData.h +++ b/frontend/GameData.h @@ -1,7 +1,5 @@ #pragma once -#include <memory> - #include "DB.h" class Enemy; @@ -12,21 +10,24 @@ class GameData { public: static GameData & get_instance(); +private: + DB db { "kerkersendraken.db" }; + public: Enemy * create_enemy(const std::string & name) const; Object * create_object(const std::string & name) const; Location * create_location(const std::string & name) const; public: - void leaderbord_add(const std::string & name, unsigned int gold) const; + void leaderbord_add(const std::string & name, unsigned int gold); void leaderbord_print() const; - std::vector<std::string> random_locations(unsigned count = 1); -private: - GameData(); - virtual ~GameData() = default; +public: + std::vector<std::string> random_objects(unsigned count = 1) const; + std::vector<std::string> random_locations(unsigned count = 1) const; + std::vector<std::string> random_enemies(unsigned count = 1) const; private: - std::unique_ptr<DB> db = nullptr; + std::vector<std::string> random_names(const std::string & table, unsigned count) const; }; diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp index 8866f60..94bde03 100644 --- a/frontend/cmd/hit.cpp +++ b/frontend/cmd/hit.cpp @@ -19,7 +19,7 @@ void GameController::cmd_hit(string & target_name) { if (rng.rand_double() > player.get_attack()) { lprtf("Je hebt gemist!\n"); } else { - unsigned damage = rng.rand_int(player.weapon->get_damage_min(), player.weapon->get_damage_max() + 1); + unsigned damage = rng.rand_int(player.weapon->get_damage()); enemy->take_damage(damage); lprtf("Je hebt %s geraakt en %d schade aangericht!\n", enemy->get_displayname().c_str(), damage); } |