File indexing completed on 2024-05-19 05:40:49

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 "updater/media/charactersheetupdater.h"
0021 
0022 #include <QJsonDocument>
0023 
0024 #include "charactersheet/charactersheet.h"
0025 #include "charactersheet/charactersheetmodel.h"
0026 #include "charactersheet/csitem.h"
0027 #include "controller/view_controller/charactersheetcontroller.h"
0028 #include "data/character.h"
0029 #include "network/networkmessagewriter.h"
0030 #include "worker/messagehelper.h"
0031 
0032 CharacterSheetUpdater::CharacterSheetUpdater(campaign::CampaignManager* campaign, QObject* parent)
0033     : MediaUpdaterInterface(campaign, parent)
0034 {
0035     // comment
0036 }
0037 
0038 void CharacterSheetUpdater::addMediaController(MediaControllerBase* ctrl)
0039 {
0040     auto csCtrl= dynamic_cast<CharacterSheetController*>(ctrl);
0041     if(!csCtrl)
0042         return;
0043 
0044     m_ctrls.append(csCtrl);
0045 
0046     connect(csCtrl, &CharacterSheetController::share, this,
0047             [this](CharacterSheetController* ctrl, CharacterSheet* sheet, CharacterSheetUpdater::SharingMode mode,
0048                    Character* character, const QStringList& recipients)
0049             { shareCharacterSheetTo(ctrl, sheet, mode, character, recipients); });
0050 
0051     connect(csCtrl, &CharacterSheetController::modifiedChanged, this,
0052             []() {
0053 
0054             });
0055 }
0056 
0057 void CharacterSheetUpdater::shareCharacterSheetTo(CharacterSheetController* ctrl, CharacterSheet* sheet,
0058                                                   CharacterSheetUpdater::SharingMode mode, Character* character,
0059                                                   const QStringList& recipients, bool gmToPlayer)
0060 {
0061     if(!sheet || (recipients.isEmpty() && mode != SharingMode::ALL))
0062         return;
0063 
0064     if(localIsGM())
0065     {
0066         auto it= std::find_if(std::begin(m_sharingData), std::end(m_sharingData),
0067                               [sheet](const CSSharingInfo& info) { return sheet->uuid() == info.sheetId; });
0068 
0069         if(it != std::end(m_sharingData))
0070         {
0071             auto list= it->recipients;
0072             for(auto rec : list)
0073             {
0074                 MessageHelper::stopSharingSheet(it->sheetId, it->ctrlId, rec);
0075             }
0076             m_sharingData.erase(it);
0077         }
0078 
0079         m_sharingData.append(
0080             {ctrl->uuid(), sheet->uuid(), character ? character->uuid() : QString(), mode, sheet, recipients});
0081         MessageHelper::shareCharacterSheet(sheet, character, ctrl);
0082     }
0083 
0084     connect(sheet, &CharacterSheet::updateField, this,
0085             [recipients, mode, ctrl](CharacterSheet* sheet, CSItem* itemSheet, const QString& path)
0086             {
0087                 if(nullptr == sheet)
0088                     return;
0089 
0090                 NetworkMessageWriter msg(NetMsg::CharacterSheetCategory, NetMsg::updateFieldCharacterSheet);
0091                 if(mode != SharingMode::ALL)
0092                 {
0093                     msg.setRecipientList(recipients, NetworkMessage::OneOrMany);
0094                 }
0095                 msg.string8(ctrl->uuid());
0096                 msg.string8(sheet->uuid());
0097                 msg.string32(path);
0098                 QJsonObject object;
0099                 itemSheet->saveDataItem(object);
0100                 QJsonDocument doc;
0101                 doc.setObject(object);
0102                 msg.byteArray32(doc.toJson());
0103                 msg.sendToServer();
0104             });
0105 }