aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cmd.cc
blob: a1fd760d2ac4c8819fb1c011e2a7fe6e73d995c9 (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
#include "cmd.h"
#include "../settings.h"
#include <vector>
#include <string>
#include <filesystem>
#include <algorithm>

namespace cmd {
    std::vector<std::string> getVirtDirs() {
        return {"weapons", "armor", "spells", "creatures"};
    }

    // Not idempotent: only do once!
    std::filesystem::path getTruePath(std::filesystem::path virtPath) {
        std::filesystem::path p;
        auto virtPaths = getVirtDirs();
        if(std::find(virtPaths.begin(), virtPaths.end(), *virtPath.begin()) != virtPaths.end()) {
            p = settings::getString(*virtPath.begin());
            // Erase root (part to be replaced by virtPaths)
            std::filesystem::path tmp;
            auto it = virtPath.begin();
            while(++it != virtPath.end()) {
                tmp /= *it;
            }
            virtPath = tmp;
        } else {
            p = settings::getString("savedir");
        }
        p /= virtPath;
        if(std::filesystem::directory_entry(p.string() + ".json").is_regular_file()) return p.string() + ".json";
        return p;
    }

    std::string formatRoll(std::string name, std::string type, int rolled, int bonus) {
        std::stringstream text;
        text << name << " " << type << ": " << rolled << " (d20) + " << bonus << " (" << name << " " << type << " bonus) = " << rolled + bonus << std::endl;
        return text.str();
    }

    std::vector<rules::Qualifier> parseQualifiers(std::map<std::string, std::string> flags) {
        std::vector<rules::Qualifier> qualifiers;
        for(auto flagPair : flags) {
            auto flag = flagPair.first;
            if(flag == "m" || flag == "magical") {
                qualifiers.push_back(rules::Qualifier::Magical());
            } else if(flag == "s" || flag == "silvered") {
                qualifiers.push_back(rules::Qualifier::Silvered());
            } else if(flag == "a" || flag == "adamantine") {
                qualifiers.push_back(rules::Qualifier::Adamantine());
            }
        }
        return qualifiers;
    }
}