From 8614137f7f32f2c9f3c11419110cd70dd7f3b505 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 May 2021 12:46:51 -0400 Subject: Code refactoring --- src/dmtool.cc | 57 +++++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/dmtool.cc b/src/dmtool.cc index 18af9e8..8ade820 100644 --- a/src/dmtool.cc +++ b/src/dmtool.cc @@ -212,50 +212,43 @@ void roll(std::vector args) { } } -void damage(std::vector args) { +std::tuple, int> getDmgHealAmnt(const std::string& subcommand, 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()); + throw std::runtime_error("Subcommand '" + subcommand + "' 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]); + throw std::runtime_error("Subcommand '" + subcommand + "' expected an integer but was given " + args[1]); } + return {p, c, amnt}; +} + +void healOrDamage(bool heal, std::vector args) { + fs::path p; + std::shared_ptr c; + int amnt; + std::string healOrDamage = heal? "heal" : "damage"; + std::tie(p, c, amnt) = getDmgHealAmnt(healOrDamage, args); int initHP = c->getHP(); - c->applyHealing(amnt); - std::cout << "Healing " << c->getGivenName() << " the " << c->getCreatureName() << " by " << amnt << ". HP: " << initHP << " -> " << c->getHP() << "." << std::endl; + if(heal) { + c->applyHealing(amnt); + } else { + std::string dmgType = "force"; + if(args.size() == 3) { + dmgType = args[2]; + } + std::vector qualifiers; // TODO + c->applyDamage(amnt, dmgType, qualifiers); + } + std::cout << (heal? "Healing " : "Damaging ") << 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) {} @@ -300,8 +293,8 @@ int main(int argc, char *argv[]) { 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 == "damage") healOrDamage(false, args); + else if(cmd == "heal") healOrDamage(true, args); else if(cmd == "reset") reset(args); else if(cmd == "set") set(args); else if(cmd == "add") add(args); -- cgit v1.2.3