diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 02:45:56 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 02:45:56 +0100 |
commit | 991c9aac53fa3562b0fdc03d74b398052b207d2c (patch) | |
tree | daf1d16ecac1de62093d9019e2b9b355f12d81f6 | |
parent | b9e738502260b8f448289c9888203971c7749c76 (diff) |
load objects kinda working, going to bed
-rw-r--r-- | frontend/DB.cpp | 24 | ||||
-rw-r--r-- | frontend/DB.h | 47 | ||||
-rw-r--r-- | 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<const char*>(int index) { + return reinterpret_cast<const char *>(sqlite3_column_text(this->parent.stmt.get(), index)); +} + +template <> +int DBQueryRow::col<int>(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 <sqlite3.h> class DB; +class DBStatement; + +class DBQueryRow { +public: + DBQueryRow(DBStatement &); + +public: + template <typename T> + 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<sqlite3_stmt, std::function<void(sqlite3_stmt*)>> 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<sqlite3_stmt, std::function<void(sqlite3_stmt*)>> 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 <memory> +#include <unordered_map> #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<string, ObjectType> 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<const char *>(0); + const char * description = row.col<const char *>(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) { |