aboutsummaryrefslogtreecommitdiff
path: root/src/entry.cc
blob: 9c9b3da36b563d5d934960839276ce09f2609235 (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
#include "entry.h"
#include "utils.h"
#include "features/feature.h"
#include "item.h"
#include "spell.h"
#include "creature.h"
#include <stdexcept>

namespace entry {
    // Returns either a feature, an item, a creature, or a spell
    std::shared_ptr<Entry> Entry::create(const nlohmann::json& data) {
        if(data["entry"] == "feature") {
            return Feature::create(data);
        } else if(data["entry"] == "item") {
            return Item::create(data);
        } else if(data["entry"] == "creatures") {
            return utils::loadDFromJson<Entry, creature::Creature>(data);
        } else if(data["entry"] == "spells") {
            return utils::loadDFromJson<Entry, Spell>(data);
        }
        throw std::invalid_argument("Invalid entry: " + std::string(data["entry"]));
    }

    struct entryImpl {
        std::string entry;
        std::string name;
        std::string type;
        std::string text;
    };
    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(entryImpl, entry, name, type, text);

    NLOHMANN_FRIEND_DEFS_BASE(Entry, data);

    Entry::Entry() : data(new entryImpl()) {}

    Entry::Entry(const std::string& entry, const std::string& name, const std::string& type, const std::string& text) : data(new entryImpl()) {
        data->entry = entry;
        data->name = name;
        data->type = type;
        data->text = text;
    }

    std::string Entry::getName(void) const {return data->name;}
    std::string Entry::getType(void) const {return data->type;}
    std::string Entry::getText(void) const {return data->text;}
    std::string Entry::getText(const creature::Creature& c) const {
        return getName() + " (" + getType() + "): " + getText();
    }
    void Entry::setText(const std::string& t) {data->text = t;}
}