diff --git a/ScStwLibraries/headers/client/scstwremoterace.h b/ScStwLibraries/headers/client/scstwremoterace.h index 505cb4e..c5eb83d 100644 --- a/ScStwLibraries/headers/client/scstwremoterace.h +++ b/ScStwLibraries/headers/client/scstwremoterace.h @@ -29,7 +29,7 @@ class ScStwRemoteRace : public ScStwRace { Q_OBJECT public: - ScStwRemoteRace(ScStwClient *scStwClient, QObject *parent = nullptr); + ScStwRemoteRace(ScStwClient *scStwClient, ScStwSettings *settings = nullptr, QObject *parent = nullptr); enum RaceMode { LOCAL, @@ -60,13 +60,14 @@ public slots: private slots: void handleBaseStationSignal(ScStw::SignalKey key, QVariant data); + void handleSettingChange(int keyInt, int keyLevel, QVariant value); + bool refreshRemoteTimers(QVariantList remoteTimers); void rebuildRemoteTimers(QVariantList remoteTimers); void refreshDetails(QVariantMap details); void handleClientStateChange(); RaceMode getMode(); bool local(); - }; #endif // SCSTWREMOTEMONITORRACE_H diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index 7d6ac3c..d620eaf 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -25,6 +25,7 @@ #include #include "scstwtimer.h" #include "scstwsoundplayer.h" +#include "scstwsettings.h" class ScStwRemoteRace; @@ -61,6 +62,7 @@ class ScStwRace : public QObject public: explicit ScStwRace(QObject *parent = nullptr); + explicit ScStwRace(ScStwSettings *settings, QObject *parent = nullptr); friend class ScStwRemoteRace; @@ -80,7 +82,8 @@ private: // sounds ScStwSoundPlayer * soundPlayer; - // some settings + // settings + ScStwSettings *settings; double soundVolume; bool competitionMode; bool competitionModeChangedRecently; @@ -128,7 +131,7 @@ public slots: bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); bool setSoundVolume(double volume); virtual bool addTimer(ScStwTimer *timer); - void setCompetitionMode(bool competitionMode); + bool setCompetitionMode(bool competitionMode); // getters RaceState getState(); @@ -148,6 +151,7 @@ private slots: bool playSoundsAndStartTimers(); bool doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart = nullptr); void enableAllTimers(); + void technicalIncident(); /** * \brief Function to declare the winner and looser timers after a timer has been stopped @@ -158,6 +162,11 @@ private slots: virtual bool getIsReadyForNextState(); void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState); + // settings + bool writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value); + virtual void handleSettingChange(int keyInt, int keyLevel, QVariant value); + void reloadAllSettings(); + signals: void startTimers(); void stopTimers(int type); diff --git a/ScStwLibraries/sources/client/scstwremoterace.cpp b/ScStwLibraries/sources/client/scstwremoterace.cpp index aca8b6e..1dd7acc 100644 --- a/ScStwLibraries/sources/client/scstwremoterace.cpp +++ b/ScStwLibraries/sources/client/scstwremoterace.cpp @@ -18,7 +18,7 @@ #include "scstwremoterace.h" -ScStwRemoteRace::ScStwRemoteRace(ScStwClient *scStwClient, QObject *parent) : ScStwRace(parent) +ScStwRemoteRace::ScStwRemoteRace(ScStwClient *scStwClient, ScStwSettings *settings, QObject *parent) : ScStwRace(settings, parent) { this->isReadyForNextState = true; @@ -90,7 +90,6 @@ ScStw::StatusCode ScStwRemoteRace::reset() { return ScStw::StatusCode(reply["status"].toInt()); } - // ------------------------- // --- Base Station sync --- // ------------------------- @@ -130,6 +129,12 @@ bool ScStwRemoteRace::local() { return this->getMode() == LOCAL; } +void ScStwRemoteRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { + if(this->local()) + return ScStwRace::handleSettingChange(keyInt, keyLevel, value); +} + + /** * @brief ScStwAppBackend::handleBaseStationUpdate * diff --git a/ScStwLibraries/sources/client/scstwremotesettings.cpp b/ScStwLibraries/sources/client/scstwremotesettings.cpp index 4128256..aaecf45 100644 --- a/ScStwLibraries/sources/client/scstwremotesettings.cpp +++ b/ScStwLibraries/sources/client/scstwremotesettings.cpp @@ -37,6 +37,8 @@ QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel) return ScStwSettings::readSetting(key, keyInt, keyLevel); + qDebug() << "Setting read: keyLevel: " << keyLevel << " key: " << key; + return this->scStwClient->readRemoteSetting(ScStwSettings::BaseStationSetting(keyInt)); } @@ -44,6 +46,9 @@ bool ScStwRemoteSettings::writeSetting(QString key, QVariant value, int keyInt, if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel) return ScStwSettings::writeSetting(key, value, keyInt, keyLevel); + + qDebug() << "Setting write: keyLevel: " << keyLevel << " key: " << key << " value: " << value; + ScStw::StatusCode res = this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value); return res == ScStw::Success; diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 86a60af..04c258f 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -18,7 +18,11 @@ #include "../headers/scstwrace.h" -ScStwRace::ScStwRace(QObject *parent) : QObject(parent) +ScStwRace::ScStwRace(QObject * parent) : ScStwRace(nullptr, parent) +{ +} + +ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent) { this->state = IDLE; @@ -36,12 +40,18 @@ ScStwRace::ScStwRace(QObject *parent) : QObject(parent) connect(this, &ScStwRace::timersChanged, this, &ScStwRace::detailsChanged); connect(this, &ScStwRace::stateChanged, this, &ScStwRace::detailsChanged); - // write default settings + // init settings + this->competitionMode = false; + this->soundVolume = 1; this->startSoundSettings.insert(ScStwSoundPlayer::Start, {{"Enabled", true}, {"Delay", 1}}); this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, false, 0); this->writeStartSoundSetting(ScStwSoundPlayer::Ready, false, 0); this->setSoundVolume(1.0); - this->competitionMode = false; + + this->settings = settings; + if(this->settings != nullptr) { + connect(this->settings, &ScStwSettings::settingChanged, this, &ScStwRace::handleSettingChange); + } } // -------------------------- @@ -81,6 +91,9 @@ ScStw::StatusCode ScStwRace::stop() { return ScStw::CurrentStateNotVaildForOperationError; } + if(this->competitionMode) + return this->cancel(); + qDebug() << "+ [INFO] stopping race"; double timeOfStop = QDateTime::currentMSecsSinceEpoch(); @@ -150,7 +163,7 @@ ScStw::StatusCode ScStwRace::reset() { ScStw::StatusCode returnCode = ScStw::Success; foreach(ScStwTimer *speedTimer, this->timers){ - if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED) { + if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED && speedTimer->getState() != ScStwTimer::IDLE) { returnCode = ScStw::InternalErrorTimerOperationFailed; } } @@ -190,6 +203,13 @@ ScStw::StatusCode ScStwRace::cancel() { return returnCode; } +void ScStwRace::technicalIncident() { + foreach(ScStwTimer *timer, this->timers){ + timer->cancel(); + } + this->setState(INCIDENT); +} + int ScStwRace::handleFalseStart() { if(this->getState() != STARTING && this->getState() != RUNNING) return ScStw::CurrentStateNotVaildForOperationError; @@ -284,7 +304,7 @@ bool ScStwRace::playSoundsAndStartTimers() { // play ready tone if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) { qDebug() << "Ready sound redturned false!"; - this->setState(INCIDENT); + this->technicalIncident(); return false; } } @@ -316,7 +336,7 @@ bool ScStwRace::playSoundsAndStartTimers() { qDebug() << "now playing start sound"; double timeOfSoundPlaybackStart; if(!this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart)) { - this->setState(INCIDENT); + this->technicalIncident(); return false; } @@ -331,13 +351,13 @@ bool ScStwRace::playSoundsAndStartTimers() { } if(!startOk) { qDebug() << "[ERROR][START] error staring all timers"; - this->setState(INCIDENT); + this->technicalIncident(); return false; } if(!this->soundPlayer->waitForSoundFinish()) { qDebug() << "[ERROR][START] start sound wait error"; - this->setState(INCIDENT); + this->technicalIncident(); return false; } @@ -474,7 +494,7 @@ bool ScStwRace::getIsReadyForNextState() { subTimer->setState(ScStwTimer::CANCELLED); } - this->setState(INCIDENT); + this->technicalIncident(); qDebug() << "[ERROR][RACE] Could not start due to not-ready timers"; @@ -514,9 +534,12 @@ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wants } } -void ScStwRace::setCompetitionMode(bool competitionMode) { +bool ScStwRace::setCompetitionMode(bool competitionMode) { + if(this->settings != nullptr) + return false; + if(this->competitionMode == competitionMode) - return; + return true; qDebug() << "Setting competition mode to " << competitionMode; @@ -526,6 +549,8 @@ void ScStwRace::setCompetitionMode(bool competitionMode) { this->competitionModeChangedRecently = true; else if(this->competitionMode) this->enableAllTimers(); + + return true; } void ScStwRace::enableAllTimers() { @@ -587,20 +612,38 @@ QVariantMap ScStwRace::getCurrentStartDelay() { } bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay) { + if(this->settings != nullptr) + return false; + if(sound != ScStwSoundPlayer::AtYourMarks && sound != ScStwSoundPlayer::Ready) return false; - QVariantMap setting = {{"Enabled", enabled}, {"Delay", delay}}; + this->writeStartSoundSettingKey(sound, "Enabled", enabled); + this->writeStartSoundSettingKey(sound, "Delay", delay); + + return true; +} + +bool ScStwRace::writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) { + + if(key != "Delay" && key != "Enabled") + return false; if(!this->startSoundSettings.contains(sound)) - this->startSoundSettings.insert(sound, setting); + this->startSoundSettings.insert(sound, {}); + + if(!this->startSoundSettings[sound].contains(key)) + this->startSoundSettings[sound].insert(key, value); else - this->startSoundSettings[sound] = setting; + this->startSoundSettings[sound][key] = value; return true; } bool ScStwRace::setSoundVolume(double volume) { + if(this->settings != nullptr) + return false; + if(volume >= 0 && volume <= 1) { this->soundVolume = volume; return true; @@ -637,6 +680,63 @@ bool ScStwRace::addTimer(ScStwTimer *timer) { } +void ScStwRace::reloadAllSettings() { + this->writeStartSoundSetting( + ScStwSoundPlayer::AtYourMarks, + this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting).toBool(), + this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble() + ); + + this->writeStartSoundSetting( + ScStwSoundPlayer::Ready, + this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting).toBool(), + this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble() + ); + + this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble()); + + this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool()); +} + +void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { + if(keyInt == -1) { + this->reloadAllSettings(); + return; + } + + qDebug() << "Setting changed"; + + switch (keyLevel) { + case 0: { + // BaseStationSetting!! + ScStwSettings::BaseStationSetting key = static_cast(keyInt); + switch (key) { + case ScStwSettings::AtYourMarksSoundEnableSetting: + this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Enabled", value); + break; + case ScStwSettings::AtYourMarksSoundDelaySetting: + this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Delay", value); + break; + case ScStwSettings::ReadySoundEnableSetting: + this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Enabled", value); + break; + case ScStwSettings::ReadySoundDelaySetting: + this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Delay", value); + break; + case ScStwSettings::SoundVolumeSetting: + this->setSoundVolume(value.toDouble()); + break; + case ScStwSettings::CompetitionModeSetting: + this->setCompetitionMode(value.toBool()); + + default: + return; + } + break; + } + } +} + ScStwRace::RaceState ScStwRace::getState() { return this->state; } diff --git a/ScStwLibraries/sources/scstwsetting.cpp b/ScStwLibraries/sources/scstwsetting.cpp index 2dcc0ee..c499f4f 100644 --- a/ScStwLibraries/sources/scstwsetting.cpp +++ b/ScStwLibraries/sources/scstwsetting.cpp @@ -35,8 +35,6 @@ QVariant ScStwSetting::getValue() { this->hasToReload = false; } - qDebug() << "Setting read: keyLevel: " << this->keyLevel << " key: " << this->key << " value: " << this->valueCache; - return this->valueCache; } diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index 260881f..7a10865 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -36,6 +36,13 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile); this->registerKeyLevelConverters(ScStwSettings::KeyLevel, &ScStwSettings::keyToString, &ScStwSettings::keyToType); + + this->setDefaultSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting, false); + this->setDefaultSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting, 0); + this->setDefaultSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting, false); + this->setDefaultSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting, 0); + this->setDefaultSetting(ScStwSettings::SoundVolumeSetting, 1); + this->setDefaultSetting(ScStwSettings::CompetitionModeSetting, false); } QVariant ScStwSettings::readSetting(BaseStationSetting key) {