From 77b43093c7b157e8ff0b68af4efd0eff226da35a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 May 2021 10:12:49 -0400 Subject: Added damage and heal commands --- src/dmtool.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 8 deletions(-) (limited to 'src/dmtool.cc') 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 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 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 args) { } } -void damage(std::vector args) {} -void heal(std::vector args) {} +void damage(std::vector args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr e = instantiate(p); + std::shared_ptr c = std::dynamic_pointer_cast(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 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 args) { + fs::path p = getTruePath(args[0]); + std::shared_ptr e = instantiate(p); + std::shared_ptr c = std::dynamic_pointer_cast(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 args) {} void set(std::vector args) {} void add(std::vector args) {} +const std::map> nargs({ + {"cp", {2}}, + {"mv", {2}}, + {"damage", {2, 3}}, + {"heal", {2}} + }); + +void checkArgs(std::string cmd, std::vector 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 args(&argv[1], &argv[argc]); @@ -241,6 +293,7 @@ int main(int argc, char *argv[]) { std::vector 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); -- cgit v1.2.3