diff options
Diffstat (limited to 'frontend')
-rw-r--r-- | frontend/CMakeLists.txt | 2 | ||||
-rw-r--r-- | frontend/DB.cpp | 30 | ||||
-rw-r--r-- | frontend/DB.h | 18 | ||||
-rw-r--r-- | frontend/GameData.cpp | 15 | ||||
-rw-r--r-- | frontend/GameData.h | 18 | ||||
-rw-r--r-- | frontend/cmd/query.cpp | 33 | ||||
-rw-r--r-- | frontend/generate_dungeon.cpp | 42 | ||||
-rw-r--r-- | frontend/load_dungeon.cpp | 17 |
8 files changed, 118 insertions, 57 deletions
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index 60b830c..d60b44f 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -7,6 +7,8 @@ target_sources(main PUBLIC load_dungeon.cpp generate_dungeon.cpp Exception.cpp + DB.cpp + GameData.cpp ) add_subdirectory(cmd) diff --git a/frontend/DB.cpp b/frontend/DB.cpp new file mode 100644 index 0000000..82933b8 --- /dev/null +++ b/frontend/DB.cpp @@ -0,0 +1,30 @@ +#include "DB.h" +#include "Exception.h" + +DB::DB(const std::string & path) { + sqlite3 * db = NULL; + int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + this->db = { + db, + [] (sqlite3 * db) { + sqlite3_close_v2(db); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_open_v2"); +} + +DB::unique_sqlite3_stmt DB::prepare(const std::string & query) { + sqlite3_stmt * stmt = NULL; + int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); + unique_sqlite3_stmt uniq_stmt = { + stmt, + [] (sqlite3_stmt * stmt) { + sqlite3_finalize(stmt); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_prepare_v2"); + return uniq_stmt; +} + diff --git a/frontend/DB.h b/frontend/DB.h new file mode 100644 index 0000000..b464f9f --- /dev/null +++ b/frontend/DB.h @@ -0,0 +1,18 @@ +#pragma once + +#include <memory> +#include <functional> +#include <sqlite3.h> + +class DB { + typedef std::unique_ptr<sqlite3, std::function<void(sqlite3*)>> unique_sqlite3; + typedef std::unique_ptr<sqlite3_stmt, std::function<void(sqlite3_stmt*)>> unique_sqlite3_stmt; + +public: + DB(const std::string & path); + unique_sqlite3_stmt prepare(const std::string & query); + +private: + unique_sqlite3 db = NULL; +}; + diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp new file mode 100644 index 0000000..ad22aa0 --- /dev/null +++ b/frontend/GameData.cpp @@ -0,0 +1,15 @@ +#include <memory> + +#include "GameData.h" + +using namespace std; + +GameData & GameData::get_instance() { + static GameData instance; + return instance; +} + +GameData::GameData() { + this->db = make_unique<DB>("kerkersendraken.db"); +} + diff --git a/frontend/GameData.h b/frontend/GameData.h new file mode 100644 index 0000000..d4c3712 --- /dev/null +++ b/frontend/GameData.h @@ -0,0 +1,18 @@ +#pragma once + +#include <memory> + +#include "DB.h" + +class GameData { +public: + static GameData & get_instance(); + +private: + GameData(); + virtual ~GameData() = default; + +private: + std::unique_ptr<DB> db = nullptr; +}; + diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index 367d6cd..3a26e9c 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -1,4 +1,5 @@ #include "backend/Location.h" +#include "backend/Object.h" #include "../Player.h" #include "../print.h" @@ -16,17 +17,31 @@ FollowupAction Player::cmd_query(Argv argv) { lprtf("Je staat bij de locatie %s.\n", this->location.get_name()); lprtf("%s\n", this->location.get_description()); - // TODO: visible objects + { + lprtf("Zichtbare objecten: "); + size_t objects = 0; + for (Object * obj : this->location.get_objects()) { + if (obj->get_hidden() == true) continue; + if (objects > 0) lprtf(", "); + lprtf("%s", obj->get_displayname()); + objects++; + } + if (objects == 0) + lprtf("(geen)"); + lprtf("\n"); + } - lprtf("Uitgangen: "); - bool first = true; - for (Direction direction : DIRECTIONS) { - if (this->location.get_exit(direction) == nullptr) continue; - if (!first) lprtf(", "); - lprtf("%s", direction_map.at(direction).c_str()); - first = false; + { + lprtf("Uitgangen: "); + bool first = true; + for (Direction direction : DIRECTIONS) { + if (this->location.get_exit(direction) == nullptr) continue; + if (!first) lprtf(", "); + lprtf("%s", direction_map.at(direction).c_str()); + first = false; + } + lprtf("\n"); } - lprtf("\n"); // TODO: enemies diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp index c6ddcbf..e2a7c74 100644 --- a/frontend/generate_dungeon.cpp +++ b/frontend/generate_dungeon.cpp @@ -1,54 +1,16 @@ #include <memory> -#include <functional> -#include <sqlite3.h> #include "backend/Dungeon.h" #include "generate_dungeon.h" -#include "Exception.h" +#include "GameData.h" using namespace std; -class DB { - typedef unique_ptr<sqlite3, function<void(sqlite3*)>> unique_sqlite3; - typedef unique_ptr<sqlite3_stmt, function<void(sqlite3_stmt*)>> unique_sqlite3_stmt; - -public: - DB(const string & path) { - sqlite3 * db = NULL; - int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); - this->db = { - db, - [] (sqlite3 * db) { - sqlite3_close_v2(db); - }, - }; - if (ret != SQLITE_OK) - throw Exception("sqlite3_open_v2"); - } - - unique_sqlite3_stmt prepare(const string & query) { - sqlite3_stmt * stmt = NULL; - int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); - unique_sqlite3_stmt uniq_stmt = { - stmt, - [] (sqlite3_stmt * stmt) { - sqlite3_finalize(stmt); - }, - }; - if (ret != SQLITE_OK) - throw Exception("sqlite3_prepare_v2"); - return uniq_stmt; - } - -private: - unique_sqlite3 db = NULL; -}; - unique_ptr<Dungeon> generate_dungeon() { unique_ptr<Dungeon> dungeon = make_unique<Dungeon>(); - DB db { "kerkersendraken.db" }; + GameData & gd = GameData::get_instance(); return dungeon; } diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp index e94cbcb..b60a7b9 100644 --- a/frontend/load_dungeon.cpp +++ b/frontend/load_dungeon.cpp @@ -1,3 +1,4 @@ +#include <csignal> #include <memory> #include <filesystem> #include <pugixml.hpp> @@ -54,22 +55,22 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) { temp_map[tag.attribute("id").as_uint()] = { .location = location, .edges = { - tag.attribute("noord").as_uint(0), - tag.attribute("oost").as_uint(0), - tag.attribute("zuid").as_uint(0), - tag.attribute("west").as_uint(0), + [Direction::NORTH] = tag.attribute("noord").as_uint(0), + [Direction::EAST] = tag.attribute("oost").as_uint(0), + [Direction::SOUTH] = tag.attribute("zuid").as_uint(0), + [Direction::WEST] = tag.attribute("west").as_uint(0), }, }; dungeon->add_location(location); } // connect edges after creating all locations - for (auto & [here, temp] : temp_map) { + for (auto & [_, temp] : temp_map) { for (Direction direction : DIRECTIONS) { - unsigned there = temp.edges[direction]; + unsigned id = temp.edges[direction]; if (temp.edges[direction] == 0) continue; - if (!temp_map.contains(there)) continue; - temp.location->set_exit(direction, temp_map[there].location); + if (!temp_map.contains(id)) continue; + temp.location->set_exit(direction, temp_map[id].location); } } |