#include "entry.h" #include "settings.h" #include #include #include #include #include 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 args) { if(args.empty()) { list(""); } else { for(string dir : args) { list(dir); } } } void cp(vector 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 args) {} void rm(vector args) {} void roll(vector args) {} void damage(vector args) {} void heal(vector args) {} void reset(vector args) {} void set(vector args) {} void add(vector args) {} int main(int argc, char *argv[]) { string exename = argv[0]; vector 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 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; }