aboutsummaryrefslogtreecommitdiffstats
path: root/src/N-Commands/RelationshipHandler.cpp
blob: 46991a375be67cfc8f225fd738c64eeeeea535d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "RelationshipHandler.hpp"
#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::JsonResponse& response) {

    std::cout << "/relation was called\n";

    response["text"] = rsStart(vector<string>{request.get("name1", "Lukas"), request.get("name2", "cpp")});

    response["success"] = "1";
    response["session"] = "NULL";
    /*if (messagePtr->date > telegram->getBootDate()) {

        log(messagePtr->from->username + ": "+messagePtr->text);

        telegram->sendMessage(messagePtr->chat->id, rsStart(messagePtr->text));

    }*/

}