diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index d620eaf..3495dd6 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -128,7 +128,7 @@ public slots: virtual ScStw::StatusCode cancel(); // setters - bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); + bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); bool setSoundVolume(double volume); virtual bool addTimer(ScStwTimer *timer); bool setCompetitionMode(bool competitionMode); @@ -163,7 +163,10 @@ private slots: void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState); // settings - bool writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value); + 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(); diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 04c258f..48e8ed9 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -42,15 +42,17 @@ ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent) // init settings this->competitionMode = false; + this->competitionModeChangedRecently = 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->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(); } } @@ -94,7 +96,7 @@ ScStw::StatusCode ScStwRace::stop() { if(this->competitionMode) return this->cancel(); - qDebug() << "+ [INFO] stopping race"; + qDebug() << "[INFO][RACE] stopping race"; double timeOfStop = QDateTime::currentMSecsSinceEpoch(); @@ -158,7 +160,7 @@ ScStw::StatusCode ScStwRace::reset() { return ScStw::CurrentStateNotVaildForOperationError; } - qDebug() << "+ [INFO] resetting race"; + qDebug() << "[INFO][RACE] resetting race"; ScStw::StatusCode returnCode = ScStw::Success; @@ -233,7 +235,6 @@ bool ScStwRace::playSoundsAndStartTimers() { return true; // The check if all timers are ready has already happened at this point - qDebug() << "now playing at marks sound"; if(!this->doDelayAndSoundOfCurrentStartState()) return false; @@ -241,7 +242,6 @@ bool ScStwRace::playSoundsAndStartTimers() { if(!this->isStarting()) return false; - qDebug() << "Now in waiting state"; this->setState(WAITING); // do climber readiness tests @@ -299,11 +299,11 @@ bool ScStwRace::playSoundsAndStartTimers() { } while(this->startDelayTimer->remainingTime() > 0 || !timerTriggered || !this->getIsReadyForNextState()); - qDebug() << "[RACE][DEBUG] Wait finished, starting now!"; + qDebug() << "[DEBUG][RACE] Wait finished, starting now!"; // play ready tone if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) { - qDebug() << "Ready sound redturned false!"; + qDebug() << "[ERROR][RACE] Ready sound returned false!"; this->technicalIncident(); return false; } @@ -320,20 +320,16 @@ bool ScStwRace::playSoundsAndStartTimers() { } while(loopExitCode != LoopManualExit || !this->getIsReadyForNextState()); } else { - qDebug() << "now playing ready sound"; if(!this->doDelayAndSoundOfCurrentStartState()) { this->cancel(); return false; } } - qDebug() << "now in starting state"; - // enter starting state this->setState(STARTING); // play start tone - qDebug() << "now playing start sound"; double timeOfSoundPlaybackStart; if(!this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart)) { this->technicalIncident(); @@ -350,13 +346,13 @@ bool ScStwRace::playSoundsAndStartTimers() { } } if(!startOk) { - qDebug() << "[ERROR][START] error staring all timers"; + qDebug() << "[ERROR][RACE] error staring all timers"; this->technicalIncident(); return false; } if(!this->soundPlayer->waitForSoundFinish()) { - qDebug() << "[ERROR][START] start sound wait error"; + qDebug() << "[ERROR][RACE] start sound wait error"; this->technicalIncident(); return false; } @@ -442,7 +438,6 @@ void ScStwRace::setState(RaceState newState) { } void ScStwRace::refreshTimerStates() { - qDebug() << "[INFO][MAIN] refreshing timer states"; // check if the race is over @@ -483,8 +478,6 @@ bool ScStwRace::getIsReadyForNextState() { if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) { - qDebug() << "Timer ready state is: " << timer->getReadyState(); - timer->technicalIncident(); foreach (ScStwTimer *subTimer, this->timers) { @@ -534,31 +527,10 @@ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wants } } -bool ScStwRace::setCompetitionMode(bool competitionMode) { - if(this->settings != nullptr) - return false; - - if(this->competitionMode == competitionMode) - return true; - - qDebug() << "Setting competition mode to " << competitionMode; - - this->competitionMode = competitionMode; - - if(this->state != IDLE) - this->competitionModeChangedRecently = true; - else if(this->competitionMode) - this->enableAllTimers(); - - return true; -} - void ScStwRace::enableAllTimers() { if(this->state != IDLE) return; - qDebug() << "ENABLING ALL TIMERS"; - foreach (ScStwTimer *timer, this->timers) { timer->setDisabled(false); } @@ -611,20 +583,53 @@ QVariantMap ScStwRace::getCurrentStartDelay() { return currentStartDelay; } -bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay) { +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->writeStartSoundSettingKey(sound, "Enabled", enabled); - this->writeStartSoundSettingKey(sound, "Delay", delay); + this->writeStartSoundSetting(sound, "Enabled", enabled); + this->writeStartSoundSetting(sound, "Delay", delay); return true; } -bool ScStwRace::writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) { +bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) { if(key != "Delay" && key != "Enabled") return false; @@ -644,6 +649,13 @@ 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; @@ -681,21 +693,25 @@ bool ScStwRace::addTimer(ScStwTimer *timer) { } void ScStwRace::reloadAllSettings() { - this->writeStartSoundSetting( + qDebug() << "reloading all settings"; + + this->setStartSoundSetting( ScStwSoundPlayer::AtYourMarks, this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting).toBool(), - this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble() + this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble(), + true ); - this->writeStartSoundSetting( + this->setStartSoundSetting( ScStwSoundPlayer::Ready, this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting).toBool(), - this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble() + this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble(), + true ); - this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble()); + this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble(), true); - this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool()); + this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool(), true); } void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { @@ -704,7 +720,7 @@ void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { return; } - qDebug() << "Setting changed"; + qDebug() << "Setting chagned: key: " << keyInt << " level: " << keyLevel << " value: " << value; switch (keyLevel) { case 0: { @@ -712,22 +728,22 @@ void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { ScStwSettings::BaseStationSetting key = static_cast(keyInt); switch (key) { case ScStwSettings::AtYourMarksSoundEnableSetting: - this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Enabled", value); + this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Enabled", value); break; case ScStwSettings::AtYourMarksSoundDelaySetting: - this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Delay", value); + this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Delay", value); break; case ScStwSettings::ReadySoundEnableSetting: - this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Enabled", value); + this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Enabled", value); break; case ScStwSettings::ReadySoundDelaySetting: - this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Delay", value); + this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Delay", value); break; case ScStwSettings::SoundVolumeSetting: - this->setSoundVolume(value.toDouble()); + this->setSoundVolume(value.toDouble(), true); break; case ScStwSettings::CompetitionModeSetting: - this->setCompetitionMode(value.toBool()); + this->setCompetitionMode(value.toBool(), true); default: return; @@ -774,6 +790,8 @@ QVariantMap ScStwRace::getDetails() { if(this->state == WAITING) tmpDetails.insert("isReadyForNextState", this->getIsReadyForNextState()); + else + tmpDetails.insert("isReadyForNextState", true); return tmpDetails; }