diff options
Diffstat (limited to 'src/dmtool.cc')
-rw-r--r-- | src/dmtool.cc | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/src/dmtool.cc b/src/dmtool.cc index c644026..18af9e8 100644 --- a/src/dmtool.cc +++ b/src/dmtool.cc @@ -161,18 +161,12 @@ void cp(fs::path src, fs::path dest) { } void cp(std::vector<std::string> args) { - if(args.size() != 2) { - throw std::runtime_error("Subcommand 'cp' expected 2 arguments but got " + std::to_string(args.size())); - } // Operate by intantiating and saving // We do recursive! cp(getTruePath(args[0]), getTruePath(args[1])); } void mv(std::vector<std::string> args) { - if(args.size() != 2) { - throw std::runtime_error("Subcommand 'mv' expected 2 arguments but got " + std::to_string(args.size())); - } fs::rename(getTruePath(args[0]), getTruePath(args[1])); } @@ -218,12 +212,70 @@ void roll(std::vector<std::string> args) { } } -void damage(std::vector<std::string> args) {} -void heal(std::vector<std::string> args) {} +void damage(std::vector<std::string> args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr<entry::Entry> e = instantiate(p); + std::shared_ptr<creature::Creature> c = std::dynamic_pointer_cast<creature::Creature>(e); + if(! c) { + throw std::runtime_error("Subcommand 'damage' expected a creature but was given an instance of " + e->getType()); + } + int dmg = 0; + try { + dmg = std::stoi(args[1]); + } catch(std::exception& e) { + throw std::runtime_error("Subcommand 'damage' expected an integer but was given " + args[1]); + } + std::string dmgType = "force"; + if(args.size() == 3) { + dmgType = args[2]; + } + std::vector<std::string> qualifiers; // TODO + int initHP = c->getHP(); + c->applyDamage(dmg, dmgType, qualifiers); + std::cout << "Applying " << dmg << " " << dmgType << " damage to " << c->getGivenName() << " the " << c->getCreatureName() << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl; + save(c, p); +} + +void heal(std::vector<std::string> args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr<entry::Entry> e = instantiate(p); + std::shared_ptr<creature::Creature> c = std::dynamic_pointer_cast<creature::Creature>(e); + if(! c) { + throw std::runtime_error("Subcommand 'heal' expected a creature but was given an instance of " + e->getType()); + } + int amnt = 0; + try { + amnt = std::stoi(args[1]); + } catch(std::exception& e) { + throw std::runtime_error("Subcommand 'heal' expected an integer but was given " + args[1]); + } + int initHP = c->getHP(); + c->applyHealing(amnt); + std::cout << "Healing " << c->getGivenName() << " the " << c->getCreatureName() << " by " << amnt << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl; + save(c, p); +} + + void reset(std::vector<std::string> args) {} void set(std::vector<std::string> args) {} void add(std::vector<std::string> args) {} +const std::map<std::string, std::vector<int>> nargs({ + {"cp", {2}}, + {"mv", {2}}, + {"damage", {2, 3}}, + {"heal", {2}} + }); + +void checkArgs(std::string cmd, std::vector<std::string> args) { + if(nargs.contains(cmd)) { + auto& allowed = nargs.at(cmd); + if(std::find(allowed.begin(), allowed.end(), args.size()) == allowed.end()) { + throw std::runtime_error("Subcommand '" + cmd + "' expected " + utils::join(allowed, ", ") + " arguments but got " + std::to_string(args.size())); + } + } +} + int main(int argc, char *argv[]) { std::string exename = argv[0]; std::vector<std::string> args(&argv[1], &argv[argc]); @@ -241,6 +293,7 @@ int main(int argc, char *argv[]) { std::vector<std::string> argsOrig(args); args.erase(args.begin()); try { + checkArgs(cmd, args); if(cmd == "ls") list(args); else if(cmd == "cp") cp(args); else if(cmd == "mkdir") mkdir(args); |