From 6709a1fd2b39446c965f24a341c0d3d714e6b0ad Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sat, 3 Oct 2020 17:45:54 +0200 Subject: [PATCH] changed settings approach --- ScStwLibraries/headers/scstwrace.h | 32 +-- ScStwLibraries/sources/scstwrace.cpp | 300 +++++++++------------------ 2 files changed, 105 insertions(+), 227 deletions(-) diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index 3495dd6..210a64a 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -61,7 +61,6 @@ class ScStwRace : public QObject Q_PROPERTY(QVariantMap details READ getDetails NOTIFY detailsChanged) public: - explicit ScStwRace(QObject *parent = nullptr); explicit ScStwRace(ScStwSettings *settings, QObject *parent = nullptr); friend class ScStwRemoteRace; @@ -84,17 +83,7 @@ private: // settings ScStwSettings *settings; - double soundVolume; bool competitionMode; - bool competitionModeChangedRecently; - - /*! - * \brief stores the start action settings - * - * \details Stores the settings for the action ScStwRace::AtYourMarks and ScStwRace::Ready. The settings keys are: "Enabled" and "Delay" - */ - QMap startSoundSettings; - enum LoopExitTypes { LoopAutomaticExit = 0, @@ -127,11 +116,7 @@ public slots: virtual ScStw::StatusCode reset(); virtual ScStw::StatusCode cancel(); - // setters - bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); - bool setSoundVolume(double volume); virtual bool addTimer(ScStwTimer *timer); - bool setCompetitionMode(bool competitionMode); // getters RaceState getState(); @@ -150,7 +135,6 @@ private slots: int handleFalseStart(); bool playSoundsAndStartTimers(); bool doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart = nullptr); - void enableAllTimers(); void technicalIncident(); /** @@ -158,18 +142,18 @@ private slots: */ void handleTimerStop(); + virtual void refreshCompetitionMode(); + + double getSoundVolume(); + ScStwSoundPlayer::StartSound getSoundForState(ScStwRace::RaceState state); + bool getSoundEnabledSetting(ScStwSoundPlayer::StartSound sound); + int getSoundDelaySetting(ScStwSoundPlayer::StartSound sound); + + bool isStarting(); virtual bool getIsReadyForNextState(); void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState); - // settings - bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay, bool force); - bool setSoundVolume(double volume, bool force); - bool setCompetitionMode(bool competitionMode, bool force); - bool writeStartSoundSetting(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/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 48e8ed9..a3ef689 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -18,13 +18,10 @@ #include "../headers/scstwrace.h" -ScStwRace::ScStwRace(QObject * parent) : ScStwRace(nullptr, parent) -{ -} - ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent) { this->state = IDLE; + this->competitionMode = false; // configure the loop that waits for the sound effect to finish this->soundPlayer = new ScStwSoundPlayer(); @@ -41,19 +38,8 @@ ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent) connect(this, &ScStwRace::stateChanged, this, &ScStwRace::detailsChanged); // init settings - this->competitionMode = false; - this->competitionModeChangedRecently = false; - this->soundVolume = 1; - this->startSoundSettings.insert(ScStwSoundPlayer::Start, {{"Enabled", true}, {"Delay", 1}}); - this->setStartSoundSetting(ScStwSoundPlayer::AtYourMarks, false, 0); - this->setStartSoundSetting(ScStwSoundPlayer::Ready, false, 0); - this->setSoundVolume(1.0); - this->settings = settings; - if(this->settings != nullptr) { - connect(this->settings, &ScStwSettings::settingChanged, this, &ScStwRace::handleSettingChange); - this->reloadAllSettings(); - } + this->refreshCompetitionMode(); } // -------------------------- @@ -74,6 +60,8 @@ ScStw::StatusCode ScStwRace::start(bool asyncronous) { return ScStw::CurrentStateNotVaildForOperationError; } + this->refreshCompetitionMode(); + if(!this->getIsReadyForNextState()) return ScStw::TimersNotReadyError; @@ -225,7 +213,7 @@ int ScStwRace::handleFalseStart() { this->setState(STOPPED); this->soundPlayer->cancel(); - this->soundPlayer->play(ScStwSoundPlayer::FalseStart, this->soundVolume); + this->soundPlayer->play(ScStwSoundPlayer::FalseStart, this->getSoundVolume()); return returnCode; } @@ -249,14 +237,14 @@ bool ScStwRace::playSoundsAndStartTimers() { // wait until both climbers are ready // if the automatic ready tone is enabled, wait for the climbers to become ready - if(this->competitionMode && this->startSoundSettings.contains(ScStwSoundPlayer::Ready) && this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()) { + if(this->competitionMode && this->getSoundEnabledSetting(ScStwSoundPlayer::Ready)) { qDebug() << "[RACE][INFO] Now waiting for climbers"; // get delay int minimumReadyDelay = 1000; - if(this->startSoundSettings[ScStwSoundPlayer::Ready]["Delay"].toInt() > 1000) - minimumReadyDelay = this->startSoundSettings[ScStwSoundPlayer::Ready]["Delay"].toInt(); + if(this->getSoundDelaySetting(ScStwSoundPlayer::Ready) > 1000) + minimumReadyDelay = this->getSoundDelaySetting(ScStwSoundPlayer::Ready); this->startDelayTimer->setInterval(minimumReadyDelay); @@ -302,7 +290,7 @@ bool ScStwRace::playSoundsAndStartTimers() { qDebug() << "[DEBUG][RACE] Wait finished, starting now!"; // play ready tone - if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) { + if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->getSoundVolume())) { qDebug() << "[ERROR][RACE] Ready sound returned false!"; this->technicalIncident(); return false; @@ -369,28 +357,14 @@ bool ScStwRace::playSoundsAndStartTimers() { } bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart) { - ScStwSoundPlayer::StartSound sound; + ScStwSoundPlayer::StartSound sound = this->getSoundForState(this->state); - switch (this->state) { - case PREPAIRING: - sound = ScStwSoundPlayer::AtYourMarks; - break; - case WAITING: - sound = ScStwSoundPlayer::Ready; - break; - case STARTING: - sound = ScStwSoundPlayer::Start; - break; - default: - return false; - } - - if(this->startSoundSettings.contains(sound) && this->startSoundSettings[sound]["Enabled"].toBool()) { - if(sound != ScStwSoundPlayer::Start && this->startSoundSettings[sound]["Delay"].toInt() > 0) { + if(this->getSoundEnabledSetting(sound)) { + if(sound != ScStwSoundPlayer::Start && this->getSoundDelaySetting(sound) > 0) { // perform the delay before the start // get delay - int thisSoundDelay = this->startSoundSettings[sound]["Delay"].toInt(); + int thisSoundDelay = this->getSoundDelaySetting(sound); // perform next action if(thisSoundDelay > 0) { @@ -407,7 +381,7 @@ bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackSt if(!this->isStarting()) return false; - if(!this->soundPlayer->play(sound, this->soundVolume, timeOfSoundPlaybackStart)) + if(!this->soundPlayer->play(sound, this->getSoundVolume(), timeOfSoundPlaybackStart)) return false; } @@ -421,18 +395,7 @@ void ScStwRace::setState(RaceState newState) { emit this->stateChanged(newState); if(this->state == IDLE) { - // if we changed to IDLE -> handle timer enable / disable - if(this->competitionModeChangedRecently && this->competitionMode) { - this->enableAllTimers(); - this->competitionModeChangedRecently = false; - } - - if(!this->competitionMode) { - foreach(ScStwTimer* timer, this->timers) { - if(timer->getWantsToBeDisabled() && timer->getState() != ScStwTimer::DISABLED) - this->handleTimerWantsToBeDisabledChange(timer, timer->getWantsToBeDisabled()); - } - } + this->refreshCompetitionMode(); } } } @@ -527,12 +490,29 @@ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wants } } -void ScStwRace::enableAllTimers() { +void ScStwRace::refreshCompetitionMode() { if(this->state != IDLE) return; - foreach (ScStwTimer *timer, this->timers) { - timer->setDisabled(false); + bool currentCompetitionMode = this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool(); + + if(this->competitionMode != currentCompetitionMode) { + + qDebug() << "[INFO][RACE] Setting competition mode to " << currentCompetitionMode; + + this->competitionMode = currentCompetitionMode; + + if(this->competitionMode) { + foreach (ScStwTimer *timer, this->timers) { + timer->setDisabled(false); + } + } + else { + foreach(ScStwTimer* timer, this->timers) { + if(timer->getWantsToBeDisabled() && timer->getState() != ScStwTimer::DISABLED) + this->handleTimerWantsToBeDisabledChange(timer, timer->getWantsToBeDisabled()); + } + } } } @@ -546,7 +526,7 @@ QVariantMap ScStwRace::getCurrentStartDelay() { switch (this->state) { case WAITING: - if(!this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()) + if(!this->getSoundEnabledSetting(ScStwSoundPlayer::Ready)) return currentStartDelay; if(!this->getIsReadyForNextState()) { // indicate that we are waiting for climbers and the progress shall be zero @@ -555,7 +535,7 @@ QVariantMap ScStwRace::getCurrentStartDelay() { } break; case PREPAIRING: { - if(!this->startSoundSettings[ScStwSoundPlayer::AtYourMarks]["Enabled"].toBool()) + if(!this->getSoundEnabledSetting(ScStwSoundPlayer::AtYourMarks)) return currentStartDelay; break; @@ -583,88 +563,6 @@ QVariantMap ScStwRace::getCurrentStartDelay() { return currentStartDelay; } -bool ScStwRace::setCompetitionMode(bool competitionMode) { - if(this->settings != nullptr) - return false; - - return this->setCompetitionMode(competitionMode, true); -} - -bool ScStwRace::setCompetitionMode(bool competitionMode, bool force) { - if(!force) - return false; - - if(this->competitionMode == competitionMode) - return true; - - qDebug() << "[INFO][RACE] Setting competition mode to " << competitionMode; - - this->competitionMode = competitionMode; - - if(this->state != IDLE) - this->competitionModeChangedRecently = true; - else if(this->competitionMode) - this->enableAllTimers(); - - return true; -} - -bool ScStwRace::setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay) { - if(this->settings != nullptr) - return false; - - return this->setStartSoundSetting(sound, enabled, delay, true); -} - -bool ScStwRace::setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay, bool force) { - if(!force) - return false; - - if(sound != ScStwSoundPlayer::AtYourMarks && sound != ScStwSoundPlayer::Ready) - return false; - - this->writeStartSoundSetting(sound, "Enabled", enabled); - this->writeStartSoundSetting(sound, "Delay", delay); - - return true; -} - -bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) { - - if(key != "Delay" && key != "Enabled") - return false; - - if(!this->startSoundSettings.contains(sound)) - this->startSoundSettings.insert(sound, {}); - - if(!this->startSoundSettings[sound].contains(key)) - this->startSoundSettings[sound].insert(key, value); - else - this->startSoundSettings[sound][key] = value; - - return true; -} - -bool ScStwRace::setSoundVolume(double volume) { - if(this->settings != nullptr) - return false; - - return this->setSoundVolume(volume, true); -} - -bool ScStwRace::setSoundVolume(double volume, bool force) { - if(!force) - return false; - - if(volume >= 0 && volume <= 1) { - this->soundVolume = volume; - return true; - } - else { - return false; - } -} - bool ScStwRace::addTimer(ScStwTimer *timer) { if(this->state != IDLE) return false; @@ -692,67 +590,6 @@ bool ScStwRace::addTimer(ScStwTimer *timer) { } -void ScStwRace::reloadAllSettings() { - qDebug() << "reloading all settings"; - - this->setStartSoundSetting( - ScStwSoundPlayer::AtYourMarks, - this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting).toBool(), - this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble(), - true - ); - - this->setStartSoundSetting( - ScStwSoundPlayer::Ready, - this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting).toBool(), - this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble(), - true - ); - - this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble(), true); - - this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool(), true); -} - -void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { - if(keyInt == -1) { - this->reloadAllSettings(); - return; - } - - qDebug() << "Setting chagned: key: " << keyInt << " level: " << keyLevel << " value: " << value; - - switch (keyLevel) { - case 0: { - // BaseStationSetting!! - ScStwSettings::BaseStationSetting key = static_cast(keyInt); - switch (key) { - case ScStwSettings::AtYourMarksSoundEnableSetting: - this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Enabled", value); - break; - case ScStwSettings::AtYourMarksSoundDelaySetting: - this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Delay", value); - break; - case ScStwSettings::ReadySoundEnableSetting: - this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Enabled", value); - break; - case ScStwSettings::ReadySoundDelaySetting: - this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Delay", value); - break; - case ScStwSettings::SoundVolumeSetting: - this->setSoundVolume(value.toDouble(), true); - break; - case ScStwSettings::CompetitionModeSetting: - this->setCompetitionMode(value.toBool(), true); - - default: - return; - } - break; - } - } -} - ScStwRace::RaceState ScStwRace::getState() { return this->state; } @@ -761,6 +598,64 @@ QList ScStwRace::getTimers() { return this->timers; } +double ScStwRace::getSoundVolume() { + return this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble(); +} + +ScStwSoundPlayer::StartSound ScStwRace::getSoundForState(ScStwRace::RaceState state) { + switch (state) { + case PREPAIRING: + return ScStwSoundPlayer::AtYourMarks; + break; + case WAITING: + return ScStwSoundPlayer::Ready; + break; + case STARTING: + return ScStwSoundPlayer::Start; + break; + default: + return ScStwSoundPlayer::StartSound(-1); + } +} + +bool ScStwRace::getSoundEnabledSetting(ScStwSoundPlayer::StartSound sound) { + ScStwSettings::BaseStationSetting soundEnabledSetting; + switch (sound) { + case ScStwSoundPlayer::AtYourMarks: + soundEnabledSetting = ScStwSettings::AtYourMarksSoundEnableSetting; + break; + case ScStwSoundPlayer::Ready: + soundEnabledSetting = ScStwSettings::ReadySoundEnableSetting; + break; + case ScStwSoundPlayer::Start: + return true; + break; + default: + return false; + } + + return this->settings->readSetting(soundEnabledSetting).toBool(); +} + +int ScStwRace::getSoundDelaySetting(ScStwSoundPlayer::StartSound sound) { + ScStwSettings::BaseStationSetting soundDelaySetting; + switch (sound) { + case ScStwSoundPlayer::AtYourMarks: + soundDelaySetting = ScStwSettings::AtYourMarksSoundDelaySetting; + break; + case ScStwSoundPlayer::Ready: + soundDelaySetting = ScStwSettings::ReadySoundDelaySetting; + break; + case ScStwSoundPlayer::Start: + return 0; + break; + default: + return -1; + } + + return this->settings->readSetting(soundDelaySetting).toInt(); +} + QVariantList ScStwRace::getTimerDetailList() { QVariantList tmpTimers; @@ -770,7 +665,6 @@ QVariantList ScStwRace::getTimerDetailList() { tmpTimer.insert("state", timer->getState()); tmpTimer.insert("currentTime", timer->getCurrentTime()); tmpTimer.insert("reactionTime", timer->getReactionTime()); - tmpTimer.insert("text", timer->getText()); tmpTimer.insert("letter", timer->getLetter()); tmpTimer.insert("readyState", timer->getReadyState()); tmpTimers.append(tmpTimer); @@ -784,7 +678,7 @@ QVariantMap ScStwRace::getDetails() { tmpDetails.insert("state", this->getState()); tmpDetails.insert("competitionMode", this->competitionMode); - tmpDetails.insert("readySoundEnabled", this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()); + tmpDetails.insert("readySoundEnabled", this->getSoundEnabledSetting(ScStwSoundPlayer::Ready)); tmpDetails.insert("currentStartDelay", this->getCurrentStartDelay()); tmpDetails.insert("timers", this->getTimerDetailList()); @@ -813,5 +707,5 @@ bool ScStwRace::getCompetitionMode() { } bool ScStwRace::getReadySoundEnabled() { - return this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool(); + return this->getSoundEnabledSetting(ScStwSoundPlayer::Ready); }