Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarCollectionDatabase.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/game/StarCollectionDatabase.cpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/game/StarCollectionDatabase.cpp')
-rw-r--r--source/game/StarCollectionDatabase.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/source/game/StarCollectionDatabase.cpp b/source/game/StarCollectionDatabase.cpp
new file mode 100644
index 0000000..1f6d84d
--- /dev/null
+++ b/source/game/StarCollectionDatabase.cpp
@@ -0,0 +1,128 @@
+#include "StarCollectionDatabase.hpp"
+#include "StarRoot.hpp"
+#include "StarAssets.hpp"
+#include "StarMonsterDatabase.hpp"
+#include "StarItemDatabase.hpp"
+
+namespace Star {
+
+EnumMap<CollectionType> const CollectionTypeNames {
+ {CollectionType::Generic, "generic"},
+ {CollectionType::Item, "item"},
+ {CollectionType::Monster, "monster"}
+};
+
+Collection::Collection() : name(), title(), type() {}
+
+Collection::Collection(String const& name, CollectionType type, String const& title) : name(name), title(title), type(type) {}
+
+Collectable::Collectable() : name(), order(), title(), description(), icon() {}
+
+Collectable::Collectable(String const& name, int order, String const& title, String const& description, String const& icon)
+ : name(name), order(order), title(title), description(description), icon(icon) {};
+
+CollectionDatabase::CollectionDatabase() {
+ auto assets = Root::singleton().assets();
+ auto files = assets->scanExtension("collection");
+ assets->queueJsons(files);
+ for (auto file : files) {
+ auto config = assets->json(file);
+
+ Collection collection;
+
+ collection.name = config.getString("name");
+ collection.title = config.getString("title", collection.name);
+ collection.type = CollectionTypeNames.getLeft(config.getString("type", "generic"));
+
+ m_collectables[collection.name] = {};
+ for (auto pair : config.get("collectables").iterateObject()) {
+ Collectable collectable;
+ if (collection.type == CollectionType::Monster)
+ collectable = parseMonsterCollectable(pair.first, pair.second);
+ else if (collection.type == CollectionType::Item)
+ collectable = parseItemCollectable(pair.first, pair.second);
+ else
+ collectable = parseGenericCollectable(pair.first, pair.second);
+
+ m_collectables[collection.name][collectable.name] = collectable;
+ }
+
+ m_collections[collection.name] = collection;
+ }
+}
+
+List<Collection> CollectionDatabase::collections() const {
+ return m_collections.values();
+}
+
+Collection CollectionDatabase::collection(String const& collectionName) const {
+ try {
+ return m_collections.get(collectionName);
+ } catch (MapException e) {
+ throw CollectionDatabaseException(strf("Collection '%s' not found", collectionName), e);
+ }
+}
+
+List<Collectable> CollectionDatabase::collectables(String const& collectionName) const {
+ try {
+ return m_collectables.get(collectionName).values();
+ } catch (MapException e) {
+ throw CollectionDatabaseException(strf("Collection '%s' not found", collectionName), e);
+ }
+}
+
+Collectable CollectionDatabase::collectable(String const& collectionName, String const& collectableName) const {
+ try {
+ return m_collectables.get(collectionName).get(collectableName);
+ } catch (MapException e) {
+ throw CollectionDatabaseException(strf("Collectable '%s' not found in collection '%s'", collectableName, collectionName), e);
+ }
+}
+
+bool CollectionDatabase::hasCollectable(String const& collectionName, String const& collectableName) const {
+ return (m_collections.contains(collectionName) && m_collectables.get(collectionName).contains(collectableName));
+}
+
+Collectable CollectionDatabase::parseGenericCollectable(String const& name, Json const& config) const {
+ Collectable collectable;
+ collectable.name = name;
+ collectable.order = config.getInt("order", 0);
+
+ collectable.title = config.getString("title", "");
+ collectable.description = config.getString("description", "");
+ collectable.icon = config.getString("icon", "");
+
+ return collectable;
+}
+
+Collectable CollectionDatabase::parseMonsterCollectable(String const& name, Json const& config) const {
+ Collectable collectable = parseGenericCollectable(name, config);
+ auto seed = 0; // use a static seed to utilize caching
+ auto variant = Root::singleton().monsterDatabase()->monsterVariant(config.getString("monsterType"), seed);
+
+ collectable.title = variant.shortDescription.value("");
+ collectable.description = variant.description.value("");
+
+ return collectable;
+}
+
+Collectable CollectionDatabase::parseItemCollectable(String const& name, Json const& config) const {
+ Collectable collectable = parseGenericCollectable(name, config);
+ auto itemDatabase = Root::singleton().itemDatabase();
+ auto item = itemDatabase->item(ItemDescriptor(config.getString("item")));
+
+ collectable.title = item->friendlyName();
+ collectable.description = item->description();
+
+ if (config.contains("icon")) {
+ collectable.icon = config.getString("icon");
+ } else {
+ auto inventoryIcon = item->instanceValue("inventoryIcon", "");
+ if (inventoryIcon.isType(Json::Type::String))
+ collectable.icon = AssetPath::relativeTo(itemDatabase->itemConfig(item->name(), JsonObject()).directory, inventoryIcon.toString());
+ }
+
+ return collectable;
+}
+
+}