diff options
| -rw-r--r-- | diceroller.cpp | 191 | ||||
| -rw-r--r-- | diceroller.h | 69 | ||||
| -rw-r--r-- | qmltypesregister.cpp | 14 | ||||
| -rw-r--r-- | qmltypesregister.h | 7 |
4 files changed, 281 insertions, 0 deletions
diff --git a/diceroller.cpp b/diceroller.cpp new file mode 100644 index 0000000..56d5097 --- /dev/null +++ b/diceroller.cpp @@ -0,0 +1,191 @@ +/*************************************************************************** +* Copyright (C) 2017 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* Rolisteam is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include "diceroller.h" + +DiceRoller::DiceRoller() + : m_diceparser(new DiceParser()) +{ + +} + +QString DiceRoller::getDiceList() const +{ + return m_diceList; +} + +QString DiceRoller::getResultStr() const +{ + return m_resultStr; +} + + +QString DiceRoller::getCommand() const +{ + return m_command; +} + +qreal DiceRoller::getResult() const +{ + return m_result; +} + +void DiceRoller::setCommand(const QString &cmd) +{ + if(m_command != cmd) + { + m_command = cmd; + emit commandChanged(); + } +} + +QString DiceRoller::diceToText(QList<ExportedDiceResult>& diceList) +{ + QStringList global; + for(auto dice : diceList) + { + QStringList resultGlobal; + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + for (const HighLightDice& tmp : diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + for(int i =0; i < tmp.getResult().size(); ++i) + { + qint64 dievalue = tmp.getResult()[i]; + QString prefix("%1"); + + if(i==0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } + } + if(!diceListChildren.isEmpty()) + { + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); + } + result << diceListStr.join(' '); + } + + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); + } + else + { + resultGlobal << result; + } + } + global << resultGlobal.join(' '); + } + return global.join(" ; "); +} +void DiceRoller::start() +{ + if(m_diceparser->parseLine(m_command)) + { + m_diceparser->start(); + if(m_diceparser->getErrorMap().isEmpty()) + { + bool homogeneous; + QList<ExportedDiceResult> list; + m_diceparser->getLastDiceResult(list,homogeneous); + QString diceText = diceToText(list); + QString scalarText; + QString str; + + qreal result = 0; + QString resultStr; + if(m_diceparser->hasIntegerResultNotInFirst()) + { + auto values = m_diceparser->getLastIntegerResults(); + QStringList strLst; + for(auto val : values ) + { + result += val; + strLst << QString::number(val); + } + scalarText = QString("%1").arg(strLst.join(',')); + } + else if(!list.isEmpty()) + { + auto values = m_diceparser->getSumOfDiceResult(); + QStringList strLst; + for(auto val : values ) + { + result += val; + strLst << QString::number(val); + } + scalarText = QString("%1").arg(strLst.join(',')); + } + + if(m_diceparser->hasStringResult()) + { + bool ok; + QStringList allStringlist = m_diceparser->getAllStringResult(ok); + QString stringResult = allStringlist.join(" ; "); + stringResult.replace("%1",scalarText); + stringResult.replace("%2",diceText.trimmed()); + str = stringResult; + } + if(!m_diceparser->getComment().isEmpty()) + { + resultStr+=m_diceparser->getComment()+"\n"; + } + resultStr += str+"\n"; + m_resultStr = resultStr; + m_result = result; + m_diceList = diceText.trimmed(); + emit resultStrChanged(); + emit resultChanged(); + emit diceListChanged(); + } + + } + + if(!m_diceparser->getErrorMap().isEmpty()) + { + auto errors = m_diceparser->getErrorMap(); + setError(errors.first()); + } + + +} + +QString DiceRoller::getError() const +{ + return m_error; +} + +void DiceRoller::setError(const QString &error) +{ + if(m_error != error) + { + m_error = error; + emit errorOccurs(); + } +} + diff --git a/diceroller.h b/diceroller.h new file mode 100644 index 0000000..3881522 --- /dev/null +++ b/diceroller.h @@ -0,0 +1,69 @@ +/*************************************************************************** +* Copyright (C) 2017 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* Rolisteam is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#ifndef DICEROLLER_H +#define DICEROLLER_H + +#include <QObject> +#include "diceparser.h" + +class DiceRoller : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal result READ getResult NOTIFY resultChanged) + Q_PROPERTY(QString dicelist READ getDiceList NOTIFY diceListChanged) + Q_PROPERTY(QString resultStr READ getResultStr NOTIFY resultStrChanged) + Q_PROPERTY(QString command READ getCommand WRITE setCommand NOTIFY commandChanged) + Q_PROPERTY(QString error READ getError WRITE setError NOTIFY errorOccurs) + +public: + DiceRoller(); + + + QString getDiceList() const; + QString getResultStr() const; + QString getCommand() const; + qreal getResult() const; + void setCommand(const QString& cmd); + + QString getError() const; + void setError(const QString &error); + +signals: + void resultChanged(); + void diceListChanged(); + void resultStrChanged(); + void commandChanged(); + void errorOccurs(); + +public slots: + void start(); + +protected: + QString diceToText(QList<ExportedDiceResult> &diceList); +private: + DiceParser* m_diceparser; + qreal m_result; + QString m_diceList; + QString m_resultStr; + QString m_command; + QString m_error; +}; + +#endif // DICEROLLER_H diff --git a/qmltypesregister.cpp b/qmltypesregister.cpp new file mode 100644 index 0000000..adc13de --- /dev/null +++ b/qmltypesregister.cpp @@ -0,0 +1,14 @@ + +#include "qmltypesregister.h" + +#include "diceroller.h" + + +void registerQmlTypes() +{ + + qmlRegisterType<DiceRoller>("Dice", 1, 0, "DiceRoller"); + + + +} diff --git a/qmltypesregister.h b/qmltypesregister.h new file mode 100644 index 0000000..10b3607 --- /dev/null +++ b/qmltypesregister.h @@ -0,0 +1,7 @@ +#ifndef REGISTER_QML_TYPE_H +#define REGISTER_QML_TYPE_H + + +void registerQmlTypes(); + +#endif //REGISTER_QML_TYPE_H |