File indexing completed on 2024-05-12 05:39:26

0001 #include "diceserver.h"
0002 #include "qhttp/src/qhttpfwd.hpp"
0003 #include "qhttp/src/qhttpserver.hpp"
0004 #include "qhttp/src/qhttpserverrequest.hpp"
0005 #include "qhttp/src/qhttpserverresponse.hpp"
0006 #include <QHostAddress>
0007 #include <QUrl>
0008 
0009 DiceServer::DiceServer(int port) : QObject(), m_diceParser(new DiceParser())
0010 {
0011     m_diceParser->setPathToHelp(
0012         "<span><a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">Documentation</a>");
0013     // using namespace ;
0014     m_server= new qhttp::server::QHttpServer(this);
0015     m_server->listen( // listening on 0.0.0.0:8080
0016         QHostAddress::Any, port, [=](qhttp::server::QHttpRequest* req, qhttp::server::QHttpResponse* res) {
0017             req->collectData(1024);
0018 
0019             // qhttp::THeaderHash hash = req->headers();
0020             // qDebug() << hash << res->headers() << qhttp::Stringify::toString(req->method()) <<
0021             // qPrintable(req->url().toString()) << req->collectedData().constData();
0022             QString getArg= req->url().toString();
0023             getArg= getArg.replace("/?", "");
0024             QStringList args= getArg.split('&');
0025             QHash<QString, QString> m_hashArgs;
0026             for(auto argument : args)
0027             {
0028                 QStringList keyValue= argument.split('=');
0029                 if(keyValue.size() == 2)
0030                 {
0031                     m_hashArgs.insert(keyValue[0], keyValue[1]);
0032                 }
0033             }
0034 
0035             if(m_hashArgs.contains("cmd"))
0036             {
0037                 qDebug() << QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit());
0038                 QString result= startDiceParsing(QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit()));
0039                 qDebug() << result;
0040 
0041                 res->setStatusCode(qhttp::ESTATUS_OK);
0042                 res->addHeader("Access-Control-Allow-Origin", "*");
0043                 res->addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
0044                 res->addHeader("Access-Control-Allow-Headers", "x-requested-with");
0045 
0046                 QString html("<!doctype html>\n"
0047                              "<html>\n"
0048                              "<head>\n"
0049                              "  <meta charset=\"utf-8\">\n"
0050                              "  <title>Rolisteam Dice System Webservice</title>\n"
0051                              "  <style>.dice {color:#FF0000;font-weight: bold;}</style>"
0052                              "</head>\n"
0053                              "<body>\n"
0054                              "%1\n"
0055                              "</body>\n"
0056                              "</html>\n");
0057 
0058                 res->end(html.arg(result).toLocal8Bit());
0059             }
0060             else
0061             {
0062                 res->setStatusCode(qhttp::ESTATUS_OK);
0063                 res->end("No Command found!\n");
0064             }
0065         });
0066     if(!m_server->isListening())
0067     {
0068         qDebug() << "failed to listen";
0069     }
0070     else
0071     {
0072         qDebug() << "Server is On!!";
0073     }
0074 }
0075 
0076 DiceServer::~DiceServer()
0077 {
0078     qDebug() << "destructor";
0079 }
0080 QString DiceServer::diceToText(ExportedDiceResult& dice, bool highlight, bool homogeneous)
0081 {
0082     QStringList resultGlobal;
0083     foreach(int face, dice.keys())
0084     {
0085         QStringList result;
0086         QStringList currentStreak;
0087         QList<QStringList> allStreakList;
0088         ListDiceResult diceResult= dice.value(face);
0089         bool previousHighlight= false;
0090         QString previousColor;
0091         QString patternColor("<span class=\"dice\">");
0092         foreach(HighLightDice tmp, diceResult)
0093         {
0094             if(previousColor != tmp.getColor())
0095             {
0096                 if(!currentStreak.isEmpty())
0097                 {
0098                     QStringList list;
0099                     list << patternColor + currentStreak.join(',') + "</span>";
0100                     allStreakList.append(list);
0101                     currentStreak.clear();
0102                 }
0103                 if(tmp.getColor().isEmpty())
0104                 {
0105                     patternColor= QStringLiteral("<span class=\"dice\">");
0106                 }
0107                 else
0108                 {
0109                     patternColor= QStringLiteral("<span style=\"color:%1;font-weight:bold\">").arg(tmp.getColor());
0110                 }
0111             }
0112             QStringList diceListStr;
0113             if((previousHighlight) && (!tmp.isHighlighted()))
0114             {
0115                 if(!currentStreak.isEmpty())
0116                 {
0117                     QStringList list;
0118                     list << patternColor + currentStreak.join(',') + "</span>";
0119                     allStreakList.append(list);
0120                     currentStreak.clear();
0121                 }
0122             }
0123             else if((!previousHighlight) && (tmp.isHighlighted()))
0124             {
0125                 if(!currentStreak.isEmpty())
0126                 {
0127                     QStringList list;
0128                     list << currentStreak.join(',');
0129                     allStreakList.append(list);
0130                     currentStreak.clear();
0131                 }
0132             }
0133             previousHighlight= tmp.isHighlighted();
0134             previousColor= tmp.getColor();
0135             for(int i= 0; i < tmp.getResult().size(); ++i)
0136             {
0137                 qint64 dievalue= tmp.getResult()[i];
0138                 diceListStr << QString::number(dievalue);
0139             }
0140             if(diceListStr.size() > 1)
0141             {
0142                 QString first= diceListStr.takeFirst();
0143                 first= QString("%1 [%2]").arg(first).arg(diceListStr.join(','));
0144                 diceListStr.clear();
0145                 diceListStr << first;
0146             }
0147             currentStreak << diceListStr.join(' ');
0148         }
0149 
0150         if(previousHighlight)
0151         {
0152             QStringList list;
0153             list << patternColor + currentStreak.join(',') + "</span>";
0154             allStreakList.append(list);
0155         }
0156         else
0157         {
0158             if(!currentStreak.isEmpty())
0159             {
0160                 QStringList list;
0161                 list << currentStreak.join(',');
0162                 allStreakList.append(list);
0163             }
0164         }
0165         foreach(QStringList a, allStreakList)
0166         {
0167             result << a;
0168         }
0169         if(dice.keys().size() > 1)
0170         {
0171             resultGlobal << QString(" d%2:(%1)").arg(result.join(",")).arg(face);
0172         }
0173         else
0174         {
0175             resultGlobal << result.join(",");
0176         }
0177     }
0178     return resultGlobal.join("");
0179 }
0180 
0181 QString DiceServer::startDiceParsing(QString cmd)
0182 {
0183     QString result("");
0184     bool highlight= true;
0185     if(m_diceParser->parseLine(cmd))
0186     {
0187         m_diceParser->Start();
0188         if(!m_diceParser->getErrorMap().isEmpty())
0189         {
0190             result+= "<span style=\"color: #FF0000\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
0191         }
0192         else
0193         {
0194             ExportedDiceResult list;
0195             bool homogeneous= true;
0196             m_diceParser->getLastDiceResult(list, homogeneous);
0197             QString diceText= diceToText(list, highlight, homogeneous);
0198             QString scalarText;
0199             QString str;
0200 
0201             if(m_diceParser->hasIntegerResultNotInFirst())
0202             {
0203                 scalarText= QString("%1").arg(m_diceParser->getLastIntegerResult());
0204             }
0205             else if(!list.isEmpty())
0206             {
0207                 scalarText= QString("%1").arg(m_diceParser->getSumOfDiceResult());
0208             }
0209             if(highlight)
0210             {
0211                 str= QString("Result: <span class=\"dice\">%1</span>, details:[%3 (%2)]")
0212                          .arg(scalarText)
0213                          .arg(diceText)
0214                          .arg(m_diceParser->getDiceCommand());
0215             }
0216             else
0217             {
0218                 str= QString("Result: %1, details:[%3 (%2)]")
0219                          .arg(scalarText)
0220                          .arg(diceText)
0221                          .arg(m_diceParser->getDiceCommand());
0222             }
0223 
0224             if(m_diceParser->hasStringResult())
0225             {
0226                 str= m_diceParser->getStringResult();
0227             }
0228             result+= str + "<br/>";
0229         }
0230     }
0231     else
0232     {
0233         result+= "<span style=\"color: #00FF00\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
0234     }
0235 
0236     return result;
0237 }