File indexing completed on 2024-04-28 05:37:00

0001 /***************************************************************************
0002  *  Copyright (C) 2017 by Renaud Guezennec                             *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   Rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include <diceparser_qobject/diceroller.h>
0021 
0022 #include <QJsonObject>
0023 #include <QtConcurrent>
0024 
0025 DiceRoller::DiceRoller(QObject* parent) : QObject(parent) {}
0026 
0027 DiceRoller::~DiceRoller()= default;
0028 
0029 QString DiceRoller::diceList() const
0030 {
0031     return m_diceList;
0032 }
0033 
0034 QString DiceRoller::resultStr() const
0035 {
0036     return m_resultStr;
0037 }
0038 
0039 QString DiceRoller::command() const
0040 {
0041     return m_command;
0042 }
0043 
0044 qreal DiceRoller::result() const
0045 {
0046     return m_result;
0047 }
0048 
0049 void DiceRoller::setCommand(const QString& cmd)
0050 {
0051     if(m_command != cmd)
0052     {
0053         m_command= cmd;
0054         emit commandChanged();
0055     }
0056 }
0057 
0058 void DiceRoller::readErrorAndWarning()
0059 {
0060     setError(
0061         tr("Error:\n%1\nWarnings:\n%2").arg(m_diceparser.humanReadableError(), m_diceparser.humanReadableWarning()));
0062 }
0063 
0064 void DiceRoller::start()
0065 {
0066     auto future= QtConcurrent::run([this]() {
0067         if(m_diceparser.parseLine(m_command))
0068         {
0069             m_diceparser.start();
0070             readErrorAndWarning();
0071             auto jsonstr= m_diceparser.resultAsJSon([](const QString& value, const QString&, bool) { return value; });
0072             QJsonDocument doc= QJsonDocument::fromJson(jsonstr.toLocal8Bit());
0073             auto json= doc.object();
0074             m_result= json["scalar"].toString().toDouble();
0075             emit resultChanged();
0076         }
0077     });
0078 }
0079 
0080 QString DiceRoller::error() const
0081 {
0082     return m_error;
0083 }
0084 
0085 QList<DiceAlias*>* DiceRoller::aliases() const
0086 {
0087     return m_diceparser.aliases();
0088 }
0089 
0090 DiceParser* DiceRoller::parser()
0091 {
0092     return &m_diceparser;
0093 }
0094 
0095 void DiceRoller::setError(const QString& error)
0096 {
0097     if(m_error == error)
0098         return;
0099 
0100     m_error= error;
0101     emit errorOccurs();
0102 }
0103 
0104 /*QString DiceRoller::diceToText(QList<ExportedDiceResult>& diceList)
0105 {
0106     QStringList global;
0107     for(auto& dice : diceList)
0108     {
0109         QStringList resultGlobal;
0110         auto const& keys= dice.keys();
0111         for(auto& face : keys)
0112         {
0113             QStringList result;
0114             auto list= dice.value(face);
0115             for(auto diceResult : list)
0116             {
0117                 for(const HighLightDice& tmp : diceResult)
0118                 {
0119                     QStringList diceListStr;
0120                     QStringList diceListChildren;
0121                     int i= 0;
0122                     for(qint64& dievalue : tmp.result())
0123                     {
0124                         QString prefix("%1");
0125                         if(i == 0)
0126                         {
0127                             diceListStr << prefix.arg(QString::number(dievalue));
0128                         }
0129                         else
0130                         {
0131                             diceListChildren << prefix.arg(QString::number(dievalue));
0132                         }
0133                         ++i;
0134                     }
0135                     if(!diceListChildren.isEmpty())
0136                     {
0137                         diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
0138                     }
0139                     result << diceListStr.join(' ');
0140                 }
0141 
0142 if(keys.size() > 1)
0143 {
0144     resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face);
0145 }
0146 else
0147 {
0148     resultGlobal << result;
0149 }
0150 }
0151 }
0152 global << resultGlobal.join(' ');
0153 }
0154 return global.join(" ; ");
0155 }*/
0156 
0157 /*if(m_diceparser.parseLine(m_command))
0158     {
0159         m_diceparser.start();
0160         if(m_diceparser.errorMap().isEmpty())
0161         {
0162             bool homogeneous;
0163             QList<ExportedDiceResult> list;
0164             m_diceparser.lastDiceResult(list, homogeneous);
0165             QString diceText= diceToText(list);
0166             QString scalarText;
0167             QString str;
0168 
0169 qreal result= 0;
0170 QString resultStr;
0171 if(m_diceparser.hasIntegerResultNotInFirst())
0172 {
0173     auto values= m_diceparser.lastIntegerResults();
0174     QStringList strLst;
0175     for(auto& val : values)
0176     {
0177         result+= val;
0178         strLst << QString::number(val);
0179     }
0180     scalarText= QString("%1").arg(strLst.join(','));
0181 }
0182 else if(!list.isEmpty())
0183 {
0184     auto values= m_diceparser.getSumOfDiceResult();
0185     QStringList strLst;
0186     for(auto val : values)
0187     {
0188         result+= val;
0189         strLst << QString::number(val);
0190     }
0191     scalarText= QString("%1").arg(strLst.join(','));
0192 }
0193 
0194 if(m_diceparser.hasStringResult())
0195 {
0196     bool ok;
0197     QStringList allStringlist= m_diceparser.getAllStringResult(ok);
0198     QString stringResult= allStringlist.join(" ; ");
0199     stringResult.replace("%1", scalarText);
0200     stringResult.replace("%2", diceText.trimmed());
0201     str= stringResult;
0202 }
0203 else
0204 {
0205     resultStr= scalarText;
0206 }
0207 if(!m_diceparser.getComment().isEmpty())
0208 {
0209     resultStr+= m_diceparser.getComment() + "\n";
0210 }
0211 resultStr+= str + "\n";
0212 m_resultStr= resultStr;
0213 m_result= result;
0214 m_diceList= diceText.trimmed();
0215 emit resultStrChanged();
0216 emit resultChanged();
0217 emit diceListChanged();
0218 }
0219 }
0220 
0221 if(!m_diceparser.getErrorMap().isEmpty())
0222 {
0223     auto errors= m_diceparser.getErrorMap();
0224     setError(errors.first());
0225 }
0226 */