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

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 "updater/controller/audioplayerupdater.h"
0021 
0022 #include <array>
0023 
0024 #include "controller/audiocontroller.h"
0025 #include "controller/audioplayercontroller.h"
0026 
0027 #include "data/campaign.h"
0028 #include "model/musicmodel.h"
0029 #include "network/receiveevent.h"
0030 #include "worker/iohelper.h"
0031 #include "worker/messagehelper.h"
0032 
0033 NetMsg::Action state2Action(AudioPlayerController::State state)
0034 {
0035     NetMsg::Action netAction= NetMsg::ChangePositionSong;
0036     switch(state)
0037     {
0038     case AudioPlayerController::PlayingState:
0039         netAction= NetMsg::PlaySong;
0040         break;
0041     case AudioPlayerController::StoppedState:
0042         netAction= NetMsg::StopSong;
0043         break;
0044     case AudioPlayerController::PausedState:
0045         netAction= NetMsg::PauseSong;
0046         break;
0047     default:
0048         break;
0049     }
0050     return netAction;
0051 }
0052 
0053 AudioPlayerUpdater::AudioPlayerUpdater(campaign::CampaignManager* campaign, AudioController* controller,
0054                                        QObject* parent)
0055     : QObject(parent), m_ctrl(controller), m_campaign(campaign)
0056 {
0057 }
0058 
0059 void AudioPlayerUpdater::setLocalIsGM(bool m)
0060 {
0061     m_localIsGM= m;
0062 
0063     if(m_localIsGM)
0064     {
0065         initSignalForGM();
0066         ReceiveEvent::removeNetworkReceiver(NetMsg::MusicCategory, this);
0067     }
0068     else
0069         ReceiveEvent::registerNetworkReceiver(NetMsg::MusicCategory, this);
0070 }
0071 
0072 void AudioPlayerUpdater::initSignalForGM()
0073 {
0074     auto ctrl1= m_ctrl->firstController();
0075     auto ctrl2= m_ctrl->secondController();
0076     auto ctrl3= m_ctrl->thirdController();
0077 
0078     auto model1= ctrl1->model();
0079     auto model2= ctrl2->model();
0080     auto model3= ctrl3->model();
0081 
0082     auto func1= [ctrl1, this]()
0083     {
0084         if(!m_localIsGM)
0085             return;
0086         IOHelper::writeJsonObjectIntoFile(
0087             m_campaign->placeDirectory(campaign::Campaign::Place::FIRST_AUDIO_PLAYER_FILE),
0088             IOHelper::saveAudioPlayerController(ctrl1));
0089     };
0090     auto func2= [ctrl2, this]()
0091     {
0092         if(!m_localIsGM)
0093             return;
0094         IOHelper::writeJsonObjectIntoFile(
0095             m_campaign->placeDirectory(campaign::Campaign::Place::SECOND_AUDIO_PLAYER_FILE),
0096             IOHelper::saveAudioPlayerController(ctrl2));
0097     };
0098     auto func3= [ctrl3, this]()
0099     {
0100         if(!m_localIsGM)
0101             return;
0102         IOHelper::writeJsonObjectIntoFile(
0103             m_campaign->placeDirectory(campaign::Campaign::Place::THIRD_AUDIO_PLAYER_FILE),
0104             IOHelper::saveAudioPlayerController(ctrl3));
0105     };
0106 
0107     connect(ctrl1, &AudioPlayerController::volumeChanged, this, func1);
0108     connect(ctrl1, &AudioPlayerController::visibleChanged, this, func1);
0109     connect(ctrl1, &AudioPlayerController::modeChanged, this, func1);
0110     connect(model1, &MusicModel::rowsInserted, this, func1);
0111     connect(model1, &MusicModel::rowsRemoved, this, func1);
0112     connect(model1, &MusicModel::modelReset, this, func1);
0113 
0114     connect(ctrl2, &AudioPlayerController::volumeChanged, this, func2);
0115     connect(ctrl2, &AudioPlayerController::visibleChanged, this, func2);
0116     connect(ctrl2, &AudioPlayerController::modeChanged, this, func2);
0117     connect(model2, &MusicModel::rowsInserted, this, func2);
0118     connect(model2, &MusicModel::rowsRemoved, this, func2);
0119     connect(model2, &MusicModel::modelReset, this, func2);
0120 
0121     connect(ctrl3, &AudioPlayerController::volumeChanged, this, func3);
0122     connect(ctrl3, &AudioPlayerController::visibleChanged, this, func3);
0123     connect(ctrl3, &AudioPlayerController::modeChanged, this, func3);
0124     connect(model3, &MusicModel::rowsInserted, this, func3);
0125     connect(model3, &MusicModel::rowsRemoved, this, func3);
0126     connect(model3, &MusicModel::modelReset, this, func3);
0127 
0128     if(m_campaign)
0129         connect(m_campaign, &campaign::CampaignManager::campaignLoaded, this,
0130                 [ctrl1, ctrl2, ctrl3, this]()
0131                 {
0132                     qDebug() << "campaignLOADED" << m_localIsGM;
0133                     if(!m_localIsGM)
0134                         return;
0135                     bool ok;
0136                     IOHelper::fetchAudioPlayerController(
0137                         ctrl1, IOHelper::loadJsonFileIntoObject(
0138                                    m_campaign->placeDirectory(campaign::Campaign::Place::FIRST_AUDIO_PLAYER_FILE), ok));
0139 
0140                     IOHelper::fetchAudioPlayerController(
0141                         ctrl2,
0142                         IOHelper::loadJsonFileIntoObject(
0143                             m_campaign->placeDirectory(campaign::Campaign::Place::SECOND_AUDIO_PLAYER_FILE), ok));
0144 
0145                     IOHelper::fetchAudioPlayerController(
0146                         ctrl3, IOHelper::loadJsonFileIntoObject(
0147                                    m_campaign->placeDirectory(campaign::Campaign::Place::THIRD_AUDIO_PLAYER_FILE), ok));
0148                 });
0149 
0150     // Network
0151     // play new song
0152     connect(ctrl1, &AudioPlayerController::startPlayingSong, this,
0153             [](const QString& song, qint64 time)
0154             { MessageHelper::sendOffPlaySong(song, time, AudioController::First); });
0155     connect(ctrl2, &AudioPlayerController::startPlayingSong, this,
0156             [](const QString& song, qint64 time)
0157             { MessageHelper::sendOffPlaySong(song, time, AudioController::Second); });
0158     connect(ctrl2, &AudioPlayerController::startPlayingSong, this,
0159             [](const QString& song, qint64 time)
0160             { MessageHelper::sendOffPlaySong(song, time, AudioController::Third); });
0161 
0162     // play/stop/pause
0163     connect(ctrl1, &AudioPlayerController::stateChanged, this,
0164             [](AudioPlayerController::State state)
0165             {
0166                 auto netAction= state2Action(state);
0167                 qDebug() << "first: state change" << state;
0168                 if(netAction == NetMsg::ChangePositionSong)
0169                     return;
0170                 MessageHelper::sendOffMusicPlayerOrder(netAction, AudioController::First);
0171             });
0172     connect(ctrl2, &AudioPlayerController::stateChanged, this,
0173             [](AudioPlayerController::State state)
0174             {
0175                 auto netAction= state2Action(state);
0176                 qDebug() << "second: state change" << state;
0177                 if(netAction == NetMsg::ChangePositionSong)
0178                     return;
0179                 MessageHelper::sendOffMusicPlayerOrder(netAction, AudioController::Second);
0180             });
0181     connect(ctrl2, &AudioPlayerController::stateChanged, this,
0182             [](AudioPlayerController::State state)
0183             {
0184                 auto netAction= state2Action(state);
0185                 qDebug() << "third: state change" << state;
0186                 if(netAction == NetMsg::ChangePositionSong)
0187                     return;
0188                 MessageHelper::sendOffMusicPlayerOrder(netAction, AudioController::Third);
0189             });
0190 
0191     // timer changed
0192     connect(ctrl1, &AudioPlayerController::timeChanged, this,
0193             [](qint64 time)
0194             {
0195                 MessageHelper::sendOffTime(time, AudioController::First);
0196             });
0197     connect(ctrl2, &AudioPlayerController::timeChanged, this,
0198             [](qint64 time)
0199             {
0200                 MessageHelper::sendOffTime(time, AudioController::Second);
0201             });
0202     connect(ctrl2, &AudioPlayerController::timeChanged, this,
0203             [](qint64 time)
0204             {
0205                 MessageHelper::sendOffTime(time, AudioController::Third);
0206             });
0207 
0208 }
0209 
0210 NetWorkReceiver::SendType AudioPlayerUpdater::processMessage(NetworkMessageReader* msg)
0211 {
0212     qDebug() << "network message received" << msg;
0213     int id= msg->uint8();
0214     auto res= NetWorkReceiver::NONE;
0215 
0216     std::array<AudioPlayerController*, 3> array(
0217         {m_ctrl->firstController(), m_ctrl->secondController(), m_ctrl->thirdController()});
0218 
0219     if(id >= 3 || id < 0)
0220         return res;
0221 
0222     auto ctrl= array[id];
0223 
0224     NetMsg::Action action= msg->action();
0225     switch(action)
0226     {
0227     case NetMsg::PlaySong:
0228         ctrl->setTime(msg->int64());
0229         ctrl->play();
0230         break;
0231     case NetMsg::PauseSong:
0232         ctrl->pause();
0233         break;
0234     case NetMsg::ChangePositionSong:
0235         ctrl->setTime(msg->int64());
0236         break;
0237     case NetMsg::StopSong:
0238         ctrl->stop();
0239         break;
0240     case NetMsg::NewSong:
0241     {
0242         auto title= msg->string32();
0243         auto time= msg->int64();
0244         ctrl->nwNewSong(title, time);
0245     }
0246     break;
0247     default:
0248         break;
0249     }
0250     return NetWorkReceiver::AllExceptSender;
0251 }