diff options
-rw-r--r-- | .gitignore | 8 | ||||
-rw-r--r-- | CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/N-Commands/KlingerHandler.cpp | 10 | ||||
-rw-r--r-- | src/N-Commands/KlingerHandler.h | 16 | ||||
-rw-r--r-- | src/N-Commands/RelationshipHandler.cpp | 128 | ||||
-rw-r--r-- | src/N-Commands/RelationshipHandler.h | 10 | ||||
-rw-r--r-- | src/main.cpp | 46 |
7 files changed, 226 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ac2612 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +CMakeFiles/* +cmake* +.idea/* +*.api +CMakeCache.txt +Makefile +N.* +*.sqlite
\ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b486d78 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.17) +project(DORgodBotBackend) + +set(CMAKE_CXX_STANDARD 20) + +link_libraries(pthread) +link_libraries(mongoose) +add_executable(DORgodBotBackend src/main.cpp src/N-Commands/KlingerHandler.cpp src/N-Commands/KlingerHandler.h src/N-Commands/RelationshipHandler.cpp src/N-Commands/RelationshipHandler.h)
\ No newline at end of file diff --git a/src/N-Commands/KlingerHandler.cpp b/src/N-Commands/KlingerHandler.cpp new file mode 100644 index 0000000..448532c --- /dev/null +++ b/src/N-Commands/KlingerHandler.cpp @@ -0,0 +1,10 @@ +// +// Created by max on 11.08.20. +// + +#include "KlingerHandler.h" + +void KlingerHandler::onCall(const Mongoose::Request& request, Mongoose::StreamResponse& response) { + std::cout << "Klinger was called\n"; + response << "Bonjour!"; +} diff --git a/src/N-Commands/KlingerHandler.h b/src/N-Commands/KlingerHandler.h new file mode 100644 index 0000000..2fc5267 --- /dev/null +++ b/src/N-Commands/KlingerHandler.h @@ -0,0 +1,16 @@ +// +// Created by max on 11.08.20. +// +#include <mongoose/Server.h> +#include <string> +#ifndef DORGODBOTBACKEND_KLINGERHANDLER_H +#define DORGODBOTBACKEND_KLINGERHANDLER_H + + +class KlingerHandler { +public: + void onCall(const Mongoose::Request& request, Mongoose::StreamResponse& response) ; +}; + + +#endif //DORGODBOTBACKEND_KLINGERHANDLER_H diff --git a/src/N-Commands/RelationshipHandler.cpp b/src/N-Commands/RelationshipHandler.cpp new file mode 100644 index 0000000..808e17c --- /dev/null +++ b/src/N-Commands/RelationshipHandler.cpp @@ -0,0 +1,128 @@ +#include "RelationshipHandler.h" +#include<string> +#include<vector> +#include <algorithm> +using namespace std; + +//Takk til https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/ +//fucking c++ does not has a split function like every other programming language in the world +vector<string> removeDupWord(const string& str) { + vector<string> v = {}; + string word; // = ""; + for(auto x : str) { + if(x == ' ') { + if(word != "/relation") { + v.push_back(word); + } + word = ""; + } else { + word += x; + } + } + v.push_back(word); + return v; +} +vector<int> buildCalArr(vector<string> nameV) { + string ges = nameV.at(0) + nameV.at(1); + + for(auto& c : ges) { + c = tolower(c); + } + + vector<int> v = {}; + for(auto c1 : ges) { + //cout << c1 << '\n'; + int count = 0; + for(auto c2: ges) { + if(c1==c2) { + ++count; + } + } + v.push_back(count); + } + //cout << ges << endl; + + /*for(auto i: v) { + cout << i << ' '; + } + cout << v.size() << "\n";*/ + return v; +} + +int calculate(vector<string> names) { + sort(names.begin(), names.end()); + vector<int> nums = buildCalArr(names); + + while(nums.size() > 2) { + vector<int> 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); + } + /*for(auto i: nums) { + cout << i << ' '; + } + cout << "\n";*/ + } + + return 10*nums.at(0)+nums.at(1); +} + +bool isAlphaNum(const string& str) { + //if(!std::for_each(str.begin(), str.end(), [](char& c) { if(!std::isalnum(c)) return false;})) return false; + + for(char i: str) { + if(!isalnum(i)) { + return false; + } + } + return true; +} + +string rsStart(vector<string> names) { + //vector<string> 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 " + to_string(result) + "% zusammen. Gratuliere!\n"); +} + +void RelationshipHandler::onCall(Mongoose::Request& request, Mongoose::StreamResponse& response) { + + // only react when command was issued after this boot + std::cout << "/relation was called\n"; + response << rsStart(vector<string>{request.get("name1", "Lukas"), request.get("name2", "cpp")}); + + /*if (messagePtr->date > telegram->getBootDate()) { + + log(messagePtr->from->username + ": "+messagePtr->text); + + telegram->sendMessage(messagePtr->chat->id, rsStart(messagePtr->text)); + + }*/ + +} diff --git a/src/N-Commands/RelationshipHandler.h b/src/N-Commands/RelationshipHandler.h new file mode 100644 index 0000000..251cabf --- /dev/null +++ b/src/N-Commands/RelationshipHandler.h @@ -0,0 +1,10 @@ +#pragma once +#include <mongoose.h> +#include <mongoose/Request.h> +#include <mongoose/StreamResponse.h> + +class RelationshipHandler { +public: + void onCall(Mongoose::Request& request, Mongoose::StreamResponse& response); + +}; diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b11a8e7 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,46 @@ +#include "N-Commands/KlingerHandler.h" +#include "N-Commands/RelationshipHandler.h" +#include <mongoose/Server.h> +#include <mongoose/WebController.h> + + +using namespace std; +using namespace Mongoose; + +class MyController : public WebController +{ +public: + void hello(Request &request, StreamResponse &response) + { + response << "Hello " << htmlEntities(request.get("name", "... what's your name ?")) << endl; + } + void klinger(Request &request, StreamResponse &response){ + KlingerHandler klinger; + klinger.onCall(request, response); + } + void relation(Request &request, StreamResponse &response){ + RelationshipHandler relation; + relation.onCall(request, response); + } + + void setup() + { + addRoute("GET", "/hello", MyController, hello); + addRoute("GET", "/klinger", MyController, klinger); + addRoute("GET", "/relation", MyController, relation); + } +}; + + +int main() +{ + MyController myController; + Server server(8080); + server.registerController(&myController); + + server.start(); + + while (1) { + MyController::sleep(10); + } +}
\ No newline at end of file |