diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-12-13 16:43:37 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-12-13 16:43:37 +0300 |
commit | c6204f4b682288b882c2d035381f5bacbe5820c5 (patch) | |
tree | 8fdb1ecbf35024fa8710fd1d472a75c3f5154d5f /src | |
parent | c7833cb8a08cf705e9e67124d746f6f7af2e93c3 (diff) | |
download | manaverse-c6204f4b682288b882c2d035381f5bacbe5820c5.tar.gz manaverse-c6204f4b682288b882c2d035381f5bacbe5820c5.tar.bz2 manaverse-c6204f4b682288b882c2d035381f5bacbe5820c5.tar.xz manaverse-c6204f4b682288b882c2d035381f5bacbe5820c5.zip |
Improve /invtostorage chat command.
Diffstat (limited to 'src')
-rw-r--r-- | src/actions/actions.cpp | 86 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 10 | ||||
-rw-r--r-- | src/utils/stringutils.h | 2 |
3 files changed, 59 insertions, 39 deletions
diff --git a/src/actions/actions.cpp b/src/actions/actions.cpp index db2a864e5..a7c0466d2 100644 --- a/src/actions/actions.cpp +++ b/src/actions/actions.cpp @@ -220,6 +220,44 @@ static Item *getItemByInvIndex(const InputEvent &event) return nullptr; } +static int getAmountFromEvent(const InputEvent &event, Item *&item0) +{ + Item *const item = getItemByInvIndex(event); + item0 = item; + if (!item) + return 0; + + std::string str = event.args; + removeToken(str, " "); + + if (str.empty()) + return 0; + + int amount = 0; + if (str[0] == '-') + { + if (str.size() > 1) + { + amount = item->getQuantity() - atoi(str.substr(1).c_str()); + if (amount <= 0 || amount > item->getQuantity()) + amount = item->getQuantity(); + } + } + else if (str == "/") + { + amount = item->getQuantity() / 2; + } + else if (str == "all") + { + amount = item->getQuantity(); + } + else + { + amount = atoi(str.c_str()); + } + return amount; +} + impHandler(emote) { const int emotion = 1 + event.action - InputAction::EMOTE_1; @@ -1387,51 +1425,21 @@ impHandler(useItemInv) impHandler(invToStorage) { - Item *const item = getItemByInvIndex(event); - if (!item) - return true; - - std::string args = event.args; - int amount = 0; - - int idx = args.find(" "); - if (idx > 0) - args = args.substr(idx + 1); + Item *item = nullptr; + const int amount = getAmountFromEvent(event, item); + if (amount) + { + inventoryHandler->moveItem2(Inventory::INVENTORY, + item->getInvIndex(), + amount, + Inventory::STORAGE); + } else - args.clear(); - - if (args.empty()) { ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd, inventoryWindow, item); return true; } - - if (args[0] == '-') - { - if (args.size() > 1) - { - amount = item->getQuantity() - atoi(args.substr(1).c_str()); - if (amount <= 0 || amount > item->getQuantity()) - amount = item->getQuantity(); - } - } - else if (args == "/") - { - amount = item->getQuantity() / 2; - } - else if (args == "all") - { - amount = item->getQuantity(); - } - else - { - amount = atoi(args.c_str()); - } - inventoryHandler->moveItem2(Inventory::INVENTORY, - item->getInvIndex(), - amount, - Inventory::STORAGE); return true; } diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index b308a5abe..6dd11160b 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -761,3 +761,13 @@ uint32_t parseNumber(const std::string &str) i = atoi(str.c_str()); return i; } + +std::string removeToken(std::string &str, const std::string &token) +{ + int idx = str.find(token); + if (idx > 0) + str = str.substr(idx + 1); + else + str.clear(); + return str; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 25dcf9a9d..fa26bf44f 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -242,4 +242,6 @@ bool parse2Int(const std::string &args, int &x, int &y); uint32_t parseNumber(const std::string &str); +std::string removeToken(std::string &str, const std::string &token); + #endif // UTILS_STRINGUTILS_H |