aboutsummaryrefslogtreecommitdiff
path: root/src/dmtool.cc
blob: 4aee1cfd9b4f343d4dcd9f24fdba75057c67e227 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "entry.h"
#include "settings.h"
#include <iostream>
#include <vector>
#include <string>
#include <filesystem>
#include <system_error>

using namespace std;

namespace fs = std::filesystem;

void usage(string exename) {
    cout << "Usage:" << endl;
    string indOpt = "  " + exename + " ";
    string indDesc = "    ";
    cout << indOpt << "[ls] [subfolder]" << endl;
    cout << indDesc << "List creatures and objects." << endl;
    cout << indOpt << "cp old-path new-path" << endl;
    cout << indDesc << "Copy old-path to new-path." << endl;
    cout << indOpt << "mv old-path new-path" << endl;
    cout << indDesc << "Move old-path to new-path." << endl;
    cout << indOpt << "rm path" << endl;
    cout << indDesc << "Remove existing creature, object, or directory." << endl;
    cout << indOpt << "roll path name" << endl;
    cout << indDesc << "Roll a skill check, save, or attack." << endl;
    cout << indOpt << "damage path amount [type]" << endl;
    cout << indDesc << "Damage creature by amount. Type defaults to \"force\"." << endl;
    cout << indOpt << "heal path amount" << endl;
    cout << indDesc << "Heal creature by amount." << endl;
    cout << indOpt << "reset path" << endl;
    cout << indDesc << "Reset creature to full health (as if completing a long rest)." << endl;
    cout << indOpt << "set path field value" << endl;
    cout << indDesc << "Set a field to a new value, where field is any of:" << endl;
    cout << indDesc << "  ability (str, dex, con, int, wis, cha); value is new ability score" << endl;
    cout << indDesc << "  skill (athletics, \"sleight of hand\", etc.); value is (none|proficient|expert)" << endl;
    cout << indDesc << "  name; value is new given name." << endl;
    cout << indOpt << "add path entry" << endl;
    cout << indDesc << "Add entry to creature, where entry is an item or spell." << endl;
    cout << indOpt << "help" << endl;
    cout << indDesc << "Show this help." << endl;
}

void print(const fs::path& path) {
    auto e = entry::Entry::create(utils::loadJson(path));
    cout << e->getText() << endl;
}

fs::path getBaseDir() {
    return settings::getString("savedir");
}

fs::path getTruePath(const fs::path& virtPath) {
    fs::path p = getBaseDir() / virtPath;
    if(fs::directory_entry(p.string() + ".json").is_regular_file()) return p.string() + ".json";
    return p;
}

// Ensure the system is set up correctly
void initFS() {
    fs::directory_entry de = fs::directory_entry(getBaseDir());
    if(! de.exists()) {
        fs::create_directories(de);
        fs::copy(settings::getString("weapon"), de.path() / "weapons");
        fs::copy(settings::getString("armor"), de.path() / "armor");
        fs::copy(settings::getString("spellcasting"), de.path() / "spells");
        fs::copy(settings::getString("monsters"), de.path() / "creatures");
    }
}

void list(const fs::path& p) {
    fs::path truePath = getTruePath(p);
    if(fs::directory_entry(truePath).is_regular_file()) {
        print(truePath);
    }
    else if(fs::directory_entry(truePath).is_directory()) {
        for(fs::directory_entry de : filesystem::directory_iterator(truePath)) {
            if(de.is_directory()) {
                cout << de.path().filename().string() << "/";
            } else {
                cout << de.path().stem().string();
            }
            cout << "" << endl;
        }
    }
    else {
        cerr << "Unknown path " << p << endl;
    }
}

void list(vector<string> args) {
    if(args.empty()) {
        list("");
    } else {
        for(string dir : args) {
            list(dir);
        }
    }
}

void cp(vector<string> args) {
    if(args.size() != 2) {
        cerr << "Subcommand 'cp' expected 2 arguments but got " << args.size() << endl;
    }
    fs::path src = getTruePath(args[0]);
    fs::path dest = getTruePath(args[1]);
    fs::copy(src, dest);
}

void mv(vector<string> args) {}
void rm(vector<string> args) {}
void roll(vector<string> args) {}
void damage(vector<string> args) {}
void heal(vector<string> args) {}
void reset(vector<string> args) {}
void set(vector<string> args) {}
void add(vector<string> args) {}

int main(int argc, char *argv[]) {
    string exename = argv[0];
    vector<string> args(&argv[1], &argv[argc]);
    try {
        initFS();
    } catch (fs::filesystem_error& e) {
        cerr << e.what() << endl;
        return 1;
    }
    if(args.empty()) {
        list(args);
        return 0;
    }
    string cmd = args[0];
    vector<string> argsOrig(args);
    args.erase(args.begin());
    if(cmd == "ls") list(args);
    else if(cmd == "cp") cp(args);
    else if(cmd == "mv") mv(args);
    else if(cmd == "rm") rm(args);
    else if(cmd == "roll") roll(args);
    else if(cmd == "damage") damage(args);
    else if(cmd == "heal") heal(args);
    else if(cmd == "reset") reset(args);
    else if(cmd == "set") set(args);
    else if(cmd == "add") add(args);
    else if(cmd == "help") usage(exename);
    else list(argsOrig);
    return 0;
}