From 991c9aac53fa3562b0fdc03d74b398052b207d2c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 02:45:56 +0100 Subject: load objects kinda working, going to bed --- frontend/DB.cpp | 24 ++++++++++++++++++++++-- frontend/DB.h | 47 +++++++++++++++++++++++++++++++++++++++++++++-- frontend/GameData.cpp | 26 +++++++++++++++++++++----- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/frontend/DB.cpp b/frontend/DB.cpp index d51601e..28fd164 100644 --- a/frontend/DB.cpp +++ b/frontend/DB.cpp @@ -20,9 +20,9 @@ DBStatement DB::prepare(const string & query) { return DBStatement(*this, query); } -DBStatement::DBStatement(DB & db, const string & query) : db(db) { +DBStatement::DBStatement(DB & parent, const string & query) : parent(parent) { sqlite3_stmt * stmt = NULL; - int ret = sqlite3_prepare_v2(this->db.db.get(), query.c_str(), query.size(), &stmt, NULL); + int ret = sqlite3_prepare_v2(this->parent.db.get(), query.c_str(), query.size(), &stmt, NULL); this->stmt = { stmt, [] (sqlite3_stmt * stmt) { @@ -62,3 +62,23 @@ void DBStatement::execute() { throw Exception("sqlite3_step: %d", ret); } +DBQueryRow DBStatement::row() { + int ret = sqlite3_step(this->stmt.get()); + if (ret != SQLITE_ROW) + throw Exception("sqlite3_step: %d", ret); + + return { *this }; +} + +DBQueryRow::DBQueryRow(DBStatement & parent) : parent(parent) { } + +template <> +const char * DBQueryRow::col(int index) { + return reinterpret_cast(sqlite3_column_text(this->parent.stmt.get(), index)); +} + +template <> +int DBQueryRow::col(int index) { + return sqlite3_column_int(this->parent.stmt.get(), index); +} + diff --git a/frontend/DB.h b/frontend/DB.h index 5c88940..77b3f7a 100644 --- a/frontend/DB.h +++ b/frontend/DB.h @@ -5,9 +5,49 @@ #include class DB; +class DBStatement; + +class DBQueryRow { +public: + DBQueryRow(DBStatement &); + +public: + template + T col(int index); + +private: + DBStatement & parent; +}; + +// class DBQueryRowRange { +// public: +// DBQueryRowRange(); +// +// private: +// class DBQueryRowIterator { +// public: +// DBQueryRowIterator(DBQueryRow & row); +// +// public: +// DBQueryRow & operator * () const; +// DBQueryRowIterator & operator ++ (); +// bool operator != (const DBQueryRowIterator &) const; +// +// private: +// DBQueryRow & row; +// }; +// +// public: +// DBQueryRowIterator begin() const; +// DBQueryRowIterator end() const; +// +// private: +// DBQueryRow row; +// }; class DBStatement { - std::unique_ptr> stmt; + friend class DBQueryRow; + public: DBStatement(DB &, const std::string & query); @@ -17,10 +57,13 @@ public: DBStatement & bind(const int & number); public: void execute(); + DBQueryRow row(); + // DBQueryRowRange rows(); private: + std::unique_ptr> stmt; int param_index = 1; - DB & db; + DB & parent; }; class DB { diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp index 0cf6a1d..b036695 100644 --- a/frontend/GameData.cpp +++ b/frontend/GameData.cpp @@ -1,4 +1,5 @@ #include +#include #include "backend/Enemy.h" #include "backend/EnemyFactory.h" @@ -23,13 +24,28 @@ Enemy * GameData::create_enemy(const string & name) { return enemy; } +static const unordered_map type_map = { + { "teleportatiedrank", ObjectType::CONSUMABLE }, + { "ervaringsdrank", ObjectType::CONSUMABLE }, + { "levenselixer", ObjectType::CONSUMABLE }, + { "wapenrusting", ObjectType::ARMOR }, + { "wapen", ObjectType::WEAPON }, + { "goudstukken", ObjectType::GOLD }, +}; + Object * GameData::create_object(const string & name) { - DBStatement query = this->db->prepare("select type from Objecten where naam = ?"); + DBStatement query = this->db->prepare("select type, omschrijving from Objecten where naam = ?"); query.bind(name); - // TODO: uhhhhhhhhh data ophalen - - Object * object = ObjectFactory::create_object(); - return object; + + try { + auto row = query.row(); + string type = row.col(0); + const char * description = row.col(1); + if (!type_map.contains(type)) throw std::exception(); + return ObjectFactory::create_object(type_map.at(type), name.c_str(), description); + } catch (...) { + return ObjectFactory::create_object(name.c_str()); + } } void GameData::leaderbord_add(const string & name, unsigned int gold) { -- cgit v1.2.3