File indexing completed on 2024-05-12 17:12:33
0001 /*************************************************************************** 0002 * Copyright (C) 2020 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software 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 "instantmessaging/dicemessage.h" 0021 0022 #include <QJsonArray> 0023 #include <QJsonDocument> 0024 #include <QJsonObject> 0025 #include <QMap> 0026 0027 #include <diceparser/parsingtoolbox.h> 0028 0029 namespace InstantMessaging 0030 { 0031 namespace 0032 { 0033 QString replacePlaceHolder(const QString& str, const QJsonObject& obj) 0034 { 0035 auto result= str; 0036 0037 auto instructions= obj["instructions"].toArray(); 0038 0039 QStringList allDiceResult; 0040 std::transform(instructions.begin(), instructions.end(), std::back_inserter(allDiceResult), 0041 [](const QJsonValue& jsonVal) 0042 { 0043 auto instruction= jsonVal.toObject(); 0044 auto diceval= instruction["diceval"].toObject(); 0045 return diceval["string"].toString(); 0046 }); 0047 0048 result= result.replace("%1", obj["scalar"].toString()); 0049 result= result.replace("%2", allDiceResult.join(",")); 0050 result= result.replace("%3", obj["scalar"].toString()); 0051 0052 QStringList values; 0053 std::transform(instructions.begin(), instructions.end(), std::back_inserter(values), 0054 [](const QJsonValue& jsonVal) 0055 { 0056 auto inst= jsonVal.toObject(); 0057 auto scalar= inst["scalar"].toDouble(); 0058 return QString::number(scalar); 0059 }); 0060 0061 QMap<Dice::ERROR_CODE, QString> errorMap; 0062 qDebug() << "values" << values; 0063 result= ParsingToolBox::replaceVariableToValue(result, values, errorMap); 0064 result= ParsingToolBox::replacePlaceHolderFromJson(result, obj); 0065 0066 return result; 0067 } 0068 0069 } // namespace 0070 DiceMessage::DiceMessage(const QString& ownerId, const QString& writer, const QDateTime& time, QObject* parent) 0071 : MessageBase(ownerId, writer, time, MessageInterface::MessageType::Dice, parent) 0072 { 0073 } 0074 0075 QString DiceMessage::text() const 0076 { 0077 return m_text; 0078 } 0079 0080 QString DiceMessage::command() const 0081 { 0082 return m_command; 0083 } 0084 0085 QString DiceMessage::details() const 0086 { 0087 return m_details; 0088 } 0089 0090 QString DiceMessage::result() const 0091 { 0092 return m_result; 0093 } 0094 0095 QString DiceMessage::comment() const 0096 { 0097 return m_comment; 0098 } 0099 0100 void DiceMessage::setText(const QString& text) 0101 { 0102 if(m_text == text) 0103 return; 0104 m_text= text; 0105 emit textChanged(); 0106 computeResult(); 0107 } 0108 0109 void DiceMessage::computeResult() 0110 { 0111 QJsonDocument doc= QJsonDocument::fromJson(m_text.toUtf8()); 0112 auto obj= doc.object(); 0113 m_command= obj["command"].toString(); 0114 m_comment= obj["comment"].toString(); 0115 m_result= obj["scalar"].toString(); 0116 auto instructions= obj["instructions"].toArray(); 0117 0118 QStringList diceArray; 0119 for(auto instruction : instructions) 0120 { 0121 auto objInst= instruction.toObject(); 0122 auto dicevalues= objInst["diceval"].toArray(); 0123 0124 int previousFace= -1; 0125 QStringList vec; 0126 int i= dicevalues.count() - 1; 0127 for(auto val : dicevalues) 0128 { 0129 bool islast= (i == 0); 0130 auto objVal= val.toObject(); 0131 auto face= objVal["face"].toInt(); 0132 auto strValue= objVal["string"].toString(); 0133 auto color= objVal["color"].toString(); 0134 /*auto highlight= objVal["highlight"].toBool(); 0135 auto hasColor= !color.isEmpty();*/ 0136 0137 if((face != previousFace && previousFace != -1)) 0138 { 0139 // qDebug() << m_text << obj; 0140 diceArray << QString("d%1:(%2)").arg(previousFace).arg(vec.join(" ")); 0141 vec.clear(); 0142 } 0143 0144 vec.append(strValue); 0145 previousFace= face; 0146 0147 if(islast) 0148 diceArray << QString("d%1:(%2)").arg(face).arg(vec.join(" ")); 0149 --i; 0150 } 0151 0152 auto scalar= objInst["scalar"].toString(); 0153 auto string= objInst["string"].toString(); 0154 0155 if(!string.isEmpty()) 0156 { 0157 m_result= replacePlaceHolder(string, obj); 0158 } 0159 } 0160 0161 m_details= diceArray.join(" - "); 0162 } 0163 } // namespace InstantMessaging