diff options
author | Your Name <you@example.com> | 2021-05-02 12:46:51 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-02 12:46:51 -0400 |
commit | 8614137f7f32f2c9f3c11419110cd70dd7f3b505 (patch) | |
tree | 98d68a2cec0fb5afcbccd64111b6e9aac67e3f32 /src | |
parent | 77b43093c7b157e8ff0b68af4efd0eff226da35a (diff) | |
download | dmtool-8614137f7f32f2c9f3c11419110cd70dd7f3b505.tar.gz dmtool-8614137f7f32f2c9f3c11419110cd70dd7f3b505.tar.bz2 dmtool-8614137f7f32f2c9f3c11419110cd70dd7f3b505.zip |
Code refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/dmtool.cc | 57 |
1 files changed, 25 insertions, 32 deletions
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<std::string> args) { } } -void damage(std::vector<std::string> args) { +std::tuple<fs::path, std::shared_ptr<creature::Creature>, int> getDmgHealAmnt(const std::string& subcommand, 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()); + 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<std::string> args) { + fs::path p; + std::shared_ptr<creature::Creature> 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<std::string> 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<std::string> args) {} void set(std::vector<std::string> args) {} void add(std::vector<std::string> 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); |