From 38e20390ddf38edd74447ef7db2af660b8e0ff32 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Mon, 24 Aug 2020 19:04:13 +0200 Subject: add all available commands (in this branch? @Max where are the others?) --- src/RelationshipHandler.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/RelationshipHandler.cpp (limited to 'src/RelationshipHandler.cpp') diff --git a/src/RelationshipHandler.cpp b/src/RelationshipHandler.cpp new file mode 100644 index 0000000..5aab81b --- /dev/null +++ b/src/RelationshipHandler.cpp @@ -0,0 +1,88 @@ +#include "Response.hpp" +#include "RelationshipHandler.hpp" + +using std::string; +using std::vector; + +vector Handler::RelationShipHandler::buildCalArr(vector nameV) { + string ges = nameV.at(0) + nameV.at(1); + + for (auto &c : ges) { + c = tolower(c); + } + + vector v{}; + for (auto c1 : ges) { + int count = 0; + for (auto c2: ges) { + if (c1 == c2) { + ++count; + } + } + v.push_back(count); + } + + return v; +} + +int Handler::RelationShipHandler::calculate(vector names) { + sort(names.begin(), names.end()); + vector nums = buildCalArr(names); + + while (nums.size() > 2) { + vector tempNums{}; + for (std::size_t i = 0; i < nums.size() / 2; i++) { + int temp = nums.at(i) + nums.at(nums.size() - i - 1); + if (nums.size() % 2 == 1 && i == nums.size() / 2) { + tempNums.push_back(nums.at(i)); + } else { + if (temp < 10) { + tempNums.push_back(temp); + } else { + tempNums.push_back(temp / 10); + tempNums.push_back(temp % 10); + } + } + } + if (nums.size() % 2 == 1) { + tempNums.push_back(nums.at(nums.size() / 2)); + } + nums.clear(); + for (auto i: tempNums) { + nums.push_back(i); + } + } + + return 10 * nums.at(0) + nums.at(1); +} + +bool Handler::RelationShipHandler::isAlphaNum(const string &str) { + for (char i: str) { + if (!isalnum(i)) { + return false; + } + } + return true; +} + +string Handler::RelationShipHandler::rsStart(vector const &names) { + //vector names = removeDupWord(str); + if (names.size() != 2) { + return "Gib zwei Namen ein Du Troll!"; + } + if (!isAlphaNum(names.at(0))) { + return "Seit wann enthælt ein Name Sonderzeichen oder Zahlen? Spüre den Zorn von N!"; + } + if (!isAlphaNum(names.at(1))) { + return "Seit wann enthælt ein Name Sonderzeichen oder Zahlen? Spüre den Zorn von N!"; + } + + int result = calculate(names); + return (names.at(0) + " und " + names.at(1) + " passen nach Angaben von N zu " + std::to_string(result) + + "% zusammen. Gratuliere!\n"); +} + +Handler::json Handler::relationShipHandler(const string &arguments, const string &session, void *payload) { + (void) payload; + return Response::simple_response(RelationShipHandler::rsStart(tokenizeArguments(arguments))); +} -- cgit v1.2.3-54-g00ecf