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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#include "diceserver.h"
#include "qhttp/src/qhttpfwd.hpp"
#include "qhttp/src/qhttpserver.hpp"
#include "qhttp/src/qhttpserverrequest.hpp"
#include "qhttp/src/qhttpserverresponse.hpp"
#include <QHostAddress>
#include <QUrl>
DiceServer::DiceServer(int port) : QObject(), m_diceParser(new DiceParser())
{
m_diceParser->setPathToHelp(
"<span><a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">Documentation</a>");
// using namespace ;
m_server= new qhttp::server::QHttpServer(this);
m_server->listen( // listening on 0.0.0.0:8080
QHostAddress::Any, port, [=](qhttp::server::QHttpRequest* req, qhttp::server::QHttpResponse* res) {
req->collectData(1024);
// qhttp::THeaderHash hash = req->headers();
// qDebug() << hash << res->headers() << qhttp::Stringify::toString(req->method()) <<
// qPrintable(req->url().toString()) << req->collectedData().constData();
QString getArg= req->url().toString();
getArg= getArg.replace("/?", "");
QStringList args= getArg.split('&');
QHash<QString, QString> m_hashArgs;
for(auto argument : args)
{
QStringList keyValue= argument.split('=');
if(keyValue.size() == 2)
{
m_hashArgs.insert(keyValue[0], keyValue[1]);
}
}
if(m_hashArgs.contains("cmd"))
{
qDebug() << QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit());
QString result= startDiceParsing(QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit()));
qDebug() << result;
res->setStatusCode(qhttp::ESTATUS_OK);
res->addHeader("Access-Control-Allow-Origin", "*");
res->addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res->addHeader("Access-Control-Allow-Headers", "x-requested-with");
QString html("<!doctype html>\n"
"<html>\n"
"<head>\n"
" <meta charset=\"utf-8\">\n"
" <title>Rolisteam Dice System Webservice</title>\n"
" <style>.dice {color:#FF0000;font-weight: bold;}</style>"
"</head>\n"
"<body>\n"
"%1\n"
"</body>\n"
"</html>\n");
res->end(html.arg(result).toLocal8Bit());
}
else
{
res->setStatusCode(qhttp::ESTATUS_OK);
res->end("No Command found!\n");
}
});
if(!m_server->isListening())
{
qDebug() << "failed to listen";
}
else
{
qDebug() << "Server is On!!";
}
}
DiceServer::~DiceServer()
{
qDebug() << "destructor";
}
QString DiceServer::diceToText(ExportedDiceResult& dice, bool highlight, bool homogeneous)
{
QStringList resultGlobal;
foreach(int face, dice.keys())
{
QStringList result;
QStringList currentStreak;
QList<QStringList> allStreakList;
ListDiceResult diceResult= dice.value(face);
bool previousHighlight= false;
QString previousColor;
QString patternColor("<span class=\"dice\">");
foreach(HighLightDice tmp, diceResult)
{
if(previousColor != tmp.getColor())
{
if(!currentStreak.isEmpty())
{
QStringList list;
list << patternColor + currentStreak.join(',') + "</span>";
allStreakList.append(list);
currentStreak.clear();
}
if(tmp.getColor().isEmpty())
{
patternColor= QStringLiteral("<span class=\"dice\">");
}
else
{
patternColor= QStringLiteral("<span style=\"color:%1;font-weight:bold\">").arg(tmp.getColor());
}
}
QStringList diceListStr;
if((previousHighlight) && (!tmp.isHighlighted()))
{
if(!currentStreak.isEmpty())
{
QStringList list;
list << patternColor + currentStreak.join(',') + "</span>";
allStreakList.append(list);
currentStreak.clear();
}
}
else if((!previousHighlight) && (tmp.isHighlighted()))
{
if(!currentStreak.isEmpty())
{
QStringList list;
list << currentStreak.join(',');
allStreakList.append(list);
currentStreak.clear();
}
}
previousHighlight= tmp.isHighlighted();
previousColor= tmp.getColor();
for(int i= 0; i < tmp.getResult().size(); ++i)
{
qint64 dievalue= tmp.getResult()[i];
diceListStr << QString::number(dievalue);
}
if(diceListStr.size() > 1)
{
QString first= diceListStr.takeFirst();
first= QString("%1 [%2]").arg(first).arg(diceListStr.join(','));
diceListStr.clear();
diceListStr << first;
}
currentStreak << diceListStr.join(' ');
}
if(previousHighlight)
{
QStringList list;
list << patternColor + currentStreak.join(',') + "</span>";
allStreakList.append(list);
}
else
{
if(!currentStreak.isEmpty())
{
QStringList list;
list << currentStreak.join(',');
allStreakList.append(list);
}
}
foreach(QStringList a, allStreakList)
{
result << a;
}
if(dice.keys().size() > 1)
{
resultGlobal << QString(" d%2:(%1)").arg(result.join(",")).arg(face);
}
else
{
resultGlobal << result.join(",");
}
}
return resultGlobal.join("");
}
QString DiceServer::startDiceParsing(QString cmd)
{
QString result("");
bool highlight= true;
if(m_diceParser->parseLine(cmd))
{
m_diceParser->Start();
if(!m_diceParser->getErrorMap().isEmpty())
{
result+= "<span style=\"color: #FF0000\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
}
else
{
ExportedDiceResult list;
bool homogeneous= true;
m_diceParser->getLastDiceResult(list, homogeneous);
QString diceText= diceToText(list, highlight, homogeneous);
QString scalarText;
QString str;
if(m_diceParser->hasIntegerResultNotInFirst())
{
scalarText= QString("%1").arg(m_diceParser->getLastIntegerResult());
}
else if(!list.isEmpty())
{
scalarText= QString("%1").arg(m_diceParser->getSumOfDiceResult());
}
if(highlight)
{
str= QString("Result: <span class=\"dice\">%1</span>, details:[%3 (%2)]")
.arg(scalarText)
.arg(diceText)
.arg(m_diceParser->getDiceCommand());
}
else
{
str= QString("Result: %1, details:[%3 (%2)]")
.arg(scalarText)
.arg(diceText)
.arg(m_diceParser->getDiceCommand());
}
if(m_diceParser->hasStringResult())
{
str= m_diceParser->getStringResult();
}
result+= str + "<br/>";
}
}
else
{
result+= "<span style=\"color: #00FF00\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
}
return result;
}
|