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

summaryrefslogtreecommitdiff
path: root/source/game/StarCollectionDatabase.cpp
blob: d2b6161bbb33dabba3dcae1afb318c5cd30cf581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 const& e) {
    throw CollectionDatabaseException(strf("Collection '{}' not found", collectionName), e);
  }
}

List<Collectable> CollectionDatabase::collectables(String const& collectionName) const {
  try {
    return m_collectables.get(collectionName).values();
  } catch (MapException const& e) {
    throw CollectionDatabaseException(strf("Collection '{}' not found", collectionName), e);
  }
}

Collectable CollectionDatabase::collectable(String const& collectionName, String const& collectableName) const {
  try {
    return m_collectables.get(collectionName).get(collectableName);
  } catch (MapException const& e) {
    throw CollectionDatabaseException(strf("Collectable '{}' not found in collection '{}'", 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->itemShared(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;
}

}