From 158ea1c382e34ff035d262d478a64e9b759ae858 Mon Sep 17 00:00:00 2001 From: Max Kusatz Date: Tue, 11 Aug 2020 17:36:12 +0200 Subject: First Steps of Backend --- .gitignore | 8 +++ CMakeLists.txt | 8 +++ src/N-Commands/KlingerHandler.cpp | 10 +++ src/N-Commands/KlingerHandler.h | 16 +++++ src/N-Commands/RelationshipHandler.cpp | 128 +++++++++++++++++++++++++++++++++ src/N-Commands/RelationshipHandler.h | 10 +++ src/main.cpp | 46 ++++++++++++ 7 files changed, 226 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/N-Commands/KlingerHandler.cpp create mode 100644 src/N-Commands/KlingerHandler.h create mode 100644 src/N-Commands/RelationshipHandler.cpp create mode 100644 src/N-Commands/RelationshipHandler.h create mode 100644 src/main.cpp 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 +#include +#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 +#include +#include +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 removeDupWord(const string& str) { + vector 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 buildCalArr(vector nameV) { + string ges = nameV.at(0) + nameV.at(1); + + for(auto& c : ges) { + c = tolower(c); + } + + vector 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 names) { + sort(names.begin(), names.end()); + vector nums = buildCalArr(names); + + while(nums.size() > 2) { + vector tempNums = {}; + for(std::size_t i=0;i 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 " + 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{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 +#include +#include + +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 +#include + + +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 -- cgit v1.2.3-54-g00ecf