File indexing completed on 2024-09-15 13:04:33
0001 /*************************************************************************** 0002 * Copyright (C) 2021 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 "undoCmd/rollinitcommand.h" 0021 0022 #include "controller/item_controllers/characteritemcontroller.h" 0023 #include "data/character.h" 0024 #include "diceparser_qobject/diceroller.h" 0025 #include "worker/iohelper.h" 0026 0027 RollInitCommand::RollInitCommand(const QList<QPointer<vmap::CharacterItemController>>& data, DiceRoller* dice) 0028 : m_data(data), m_diceparser(dice) 0029 { 0030 m_formerValues.reserve(m_data.size()); 0031 std::transform(std::begin(m_data), std::end(m_data), std::back_inserter(m_formerValues), 0032 [](const QPointer<vmap::CharacterItemController>& item) -> InitInfo 0033 { 0034 if(!item) 0035 return {false, 0}; 0036 auto character= item->character(); 0037 if(character->hasInitScore()) 0038 return {true, character->getInitiativeScore()}; 0039 else 0040 return {false, 0}; 0041 }); 0042 } 0043 0044 void RollInitCommand::undo() 0045 { 0046 int i= 0; 0047 std::for_each(std::begin(m_data), std::end(m_data), 0048 [&i, this](const QPointer<vmap::CharacterItemController>& item) 0049 { 0050 if(!item) 0051 return; 0052 auto character= item->character(); 0053 auto info= m_formerValues[i]; 0054 character->setInitiativeScore(info.m_score); 0055 character->setHasInitiative(info.m_hasInit); 0056 ++i; 0057 }); 0058 } 0059 0060 void RollInitCommand::redo() 0061 { 0062 std::for_each(std::begin(m_data), std::end(m_data), 0063 [this](const QPointer<vmap::CharacterItemController>& item) 0064 { 0065 qDebug() << "run init for " << item.get(); 0066 if(!item || !m_diceparser) 0067 return; 0068 auto diceparser= m_diceparser->parser(); 0069 auto character= item->character(); 0070 if(!character) 0071 return; 0072 auto cmd= character->initCommand(); 0073 if(diceparser->parseLine(cmd)) 0074 { 0075 diceparser->start(); 0076 auto jsonstr= diceparser->resultAsJSon([](const QString& value, const QString&, bool) 0077 { return value; }); 0078 m_diceparser->readErrorAndWarning(); 0079 auto json= IOHelper::textByteArrayToJsonObj(jsonstr.toLocal8Bit()); 0080 qDebug() << "jsonstr" << jsonstr << " value:" << json["scalar"].toString().toInt(); 0081 character->setInitiativeScore(static_cast<int>(json["scalar"].toString().toInt())); 0082 } 0083 }); 0084 } 0085 0086 ///////////////////////////////////////////////// 0087 // 0088 // clean up command 0089 // 0090 ///////////////////////////////////////////////// 0091 CleanUpRollCommand::CleanUpRollCommand(const QList<QPointer<vmap::CharacterItemController>>& data) : m_data(data) 0092 { 0093 m_formerValues.reserve(m_data.size()); 0094 std::transform(std::begin(m_data), std::end(m_data), std::back_inserter(m_formerValues), 0095 [](const QPointer<vmap::CharacterItemController>& item) -> InitInfo 0096 { 0097 if(!item) 0098 return {false, 0}; 0099 auto character= item->character(); 0100 if(character->hasInitScore()) 0101 return {true, character->getInitiativeScore()}; 0102 else 0103 return {false, 0}; 0104 }); 0105 } 0106 0107 void CleanUpRollCommand::undo() 0108 { 0109 int i= 0; 0110 std::for_each(std::begin(m_data), std::end(m_data), 0111 [&i, this](const QPointer<vmap::CharacterItemController>& item) 0112 { 0113 if(!item) 0114 return; 0115 auto character= item->character(); 0116 auto info= m_formerValues[i]; 0117 character->setInitiativeScore(info.m_score); 0118 character->setHasInitiative(info.m_hasInit); 0119 ++i; 0120 }); 0121 } 0122 0123 void CleanUpRollCommand::redo() 0124 { 0125 std::for_each(std::begin(m_data), std::end(m_data), 0126 [](const QPointer<vmap::CharacterItemController>& item) 0127 { 0128 if(!item) 0129 return; 0130 auto character= item->character(); 0131 character->setHasInitiative(false); 0132 }); 0133 }