changed settings approach

This commit is contained in:
Dorian Zedler 2020-10-03 17:45:54 +02:00
parent 4dd4bc21f9
commit 6709a1fd2b
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
2 changed files with 105 additions and 227 deletions

View file

@ -61,7 +61,6 @@ class ScStwRace : public QObject
Q_PROPERTY(QVariantMap details READ getDetails NOTIFY detailsChanged) Q_PROPERTY(QVariantMap details READ getDetails NOTIFY detailsChanged)
public: public:
explicit ScStwRace(QObject *parent = nullptr);
explicit ScStwRace(ScStwSettings *settings, QObject *parent = nullptr); explicit ScStwRace(ScStwSettings *settings, QObject *parent = nullptr);
friend class ScStwRemoteRace; friend class ScStwRemoteRace;
@ -84,17 +83,7 @@ private:
// settings // settings
ScStwSettings *settings; ScStwSettings *settings;
double soundVolume;
bool competitionMode; 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<ScStwSoundPlayer::StartSound, QVariantMap> startSoundSettings;
enum LoopExitTypes { enum LoopExitTypes {
LoopAutomaticExit = 0, LoopAutomaticExit = 0,
@ -127,11 +116,7 @@ public slots:
virtual ScStw::StatusCode reset(); virtual ScStw::StatusCode reset();
virtual ScStw::StatusCode cancel(); virtual ScStw::StatusCode cancel();
// setters
bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay);
bool setSoundVolume(double volume);
virtual bool addTimer(ScStwTimer *timer); virtual bool addTimer(ScStwTimer *timer);
bool setCompetitionMode(bool competitionMode);
// getters // getters
RaceState getState(); RaceState getState();
@ -150,7 +135,6 @@ private slots:
int handleFalseStart(); int handleFalseStart();
bool playSoundsAndStartTimers(); bool playSoundsAndStartTimers();
bool doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart = nullptr); bool doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart = nullptr);
void enableAllTimers();
void technicalIncident(); void technicalIncident();
/** /**
@ -158,18 +142,18 @@ private slots:
*/ */
void handleTimerStop(); 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(); bool isStarting();
virtual bool getIsReadyForNextState(); virtual bool getIsReadyForNextState();
void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState); 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: signals:
void startTimers(); void startTimers();
void stopTimers(int type); void stopTimers(int type);

View file

@ -18,13 +18,10 @@
#include "../headers/scstwrace.h" #include "../headers/scstwrace.h"
ScStwRace::ScStwRace(QObject * parent) : ScStwRace(nullptr, parent)
{
}
ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent) ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent)
{ {
this->state = IDLE; this->state = IDLE;
this->competitionMode = false;
// configure the loop that waits for the sound effect to finish // configure the loop that waits for the sound effect to finish
this->soundPlayer = new ScStwSoundPlayer(); this->soundPlayer = new ScStwSoundPlayer();
@ -41,19 +38,8 @@ ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent)
connect(this, &ScStwRace::stateChanged, this, &ScStwRace::detailsChanged); connect(this, &ScStwRace::stateChanged, this, &ScStwRace::detailsChanged);
// init settings // 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; this->settings = settings;
if(this->settings != nullptr) { this->refreshCompetitionMode();
connect(this->settings, &ScStwSettings::settingChanged, this, &ScStwRace::handleSettingChange);
this->reloadAllSettings();
}
} }
// -------------------------- // --------------------------
@ -74,6 +60,8 @@ ScStw::StatusCode ScStwRace::start(bool asyncronous) {
return ScStw::CurrentStateNotVaildForOperationError; return ScStw::CurrentStateNotVaildForOperationError;
} }
this->refreshCompetitionMode();
if(!this->getIsReadyForNextState()) if(!this->getIsReadyForNextState())
return ScStw::TimersNotReadyError; return ScStw::TimersNotReadyError;
@ -225,7 +213,7 @@ int ScStwRace::handleFalseStart() {
this->setState(STOPPED); this->setState(STOPPED);
this->soundPlayer->cancel(); this->soundPlayer->cancel();
this->soundPlayer->play(ScStwSoundPlayer::FalseStart, this->soundVolume); this->soundPlayer->play(ScStwSoundPlayer::FalseStart, this->getSoundVolume());
return returnCode; return returnCode;
} }
@ -249,14 +237,14 @@ bool ScStwRace::playSoundsAndStartTimers() {
// wait until both climbers are ready // wait until both climbers are ready
// if the automatic ready tone is enabled, wait for the climbers to become 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"; qDebug() << "[RACE][INFO] Now waiting for climbers";
// get delay // get delay
int minimumReadyDelay = 1000; int minimumReadyDelay = 1000;
if(this->startSoundSettings[ScStwSoundPlayer::Ready]["Delay"].toInt() > 1000) if(this->getSoundDelaySetting(ScStwSoundPlayer::Ready) > 1000)
minimumReadyDelay = this->startSoundSettings[ScStwSoundPlayer::Ready]["Delay"].toInt(); minimumReadyDelay = this->getSoundDelaySetting(ScStwSoundPlayer::Ready);
this->startDelayTimer->setInterval(minimumReadyDelay); this->startDelayTimer->setInterval(minimumReadyDelay);
@ -302,7 +290,7 @@ bool ScStwRace::playSoundsAndStartTimers() {
qDebug() << "[DEBUG][RACE] Wait finished, starting now!"; qDebug() << "[DEBUG][RACE] Wait finished, starting now!";
// play ready tone // 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!"; qDebug() << "[ERROR][RACE] Ready sound returned false!";
this->technicalIncident(); this->technicalIncident();
return false; return false;
@ -369,28 +357,14 @@ bool ScStwRace::playSoundsAndStartTimers() {
} }
bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart) { bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart) {
ScStwSoundPlayer::StartSound sound; ScStwSoundPlayer::StartSound sound = this->getSoundForState(this->state);
switch (this->state) { if(this->getSoundEnabledSetting(sound)) {
case PREPAIRING: if(sound != ScStwSoundPlayer::Start && this->getSoundDelaySetting(sound) > 0) {
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) {
// perform the delay before the start // perform the delay before the start
// get delay // get delay
int thisSoundDelay = this->startSoundSettings[sound]["Delay"].toInt(); int thisSoundDelay = this->getSoundDelaySetting(sound);
// perform next action // perform next action
if(thisSoundDelay > 0) { if(thisSoundDelay > 0) {
@ -407,7 +381,7 @@ bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackSt
if(!this->isStarting()) if(!this->isStarting())
return false; return false;
if(!this->soundPlayer->play(sound, this->soundVolume, timeOfSoundPlaybackStart)) if(!this->soundPlayer->play(sound, this->getSoundVolume(), timeOfSoundPlaybackStart))
return false; return false;
} }
@ -421,18 +395,7 @@ void ScStwRace::setState(RaceState newState) {
emit this->stateChanged(newState); emit this->stateChanged(newState);
if(this->state == IDLE) { if(this->state == IDLE) {
// if we changed to IDLE -> handle timer enable / disable this->refreshCompetitionMode();
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());
}
}
} }
} }
} }
@ -527,12 +490,29 @@ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wants
} }
} }
void ScStwRace::enableAllTimers() { void ScStwRace::refreshCompetitionMode() {
if(this->state != IDLE) if(this->state != IDLE)
return; return;
foreach (ScStwTimer *timer, this->timers) { bool currentCompetitionMode = this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool();
timer->setDisabled(false);
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) { switch (this->state) {
case WAITING: case WAITING:
if(!this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()) if(!this->getSoundEnabledSetting(ScStwSoundPlayer::Ready))
return currentStartDelay; return currentStartDelay;
if(!this->getIsReadyForNextState()) { if(!this->getIsReadyForNextState()) {
// indicate that we are waiting for climbers and the progress shall be zero // indicate that we are waiting for climbers and the progress shall be zero
@ -555,7 +535,7 @@ QVariantMap ScStwRace::getCurrentStartDelay() {
} }
break; break;
case PREPAIRING: { case PREPAIRING: {
if(!this->startSoundSettings[ScStwSoundPlayer::AtYourMarks]["Enabled"].toBool()) if(!this->getSoundEnabledSetting(ScStwSoundPlayer::AtYourMarks))
return currentStartDelay; return currentStartDelay;
break; break;
@ -583,88 +563,6 @@ QVariantMap ScStwRace::getCurrentStartDelay() {
return currentStartDelay; 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) { bool ScStwRace::addTimer(ScStwTimer *timer) {
if(this->state != IDLE) if(this->state != IDLE)
return false; 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<ScStwSettings::BaseStationSetting>(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() { ScStwRace::RaceState ScStwRace::getState() {
return this->state; return this->state;
} }
@ -761,6 +598,64 @@ QList<ScStwTimer*> ScStwRace::getTimers() {
return this->timers; 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 ScStwRace::getTimerDetailList() {
QVariantList tmpTimers; QVariantList tmpTimers;
@ -770,7 +665,6 @@ QVariantList ScStwRace::getTimerDetailList() {
tmpTimer.insert("state", timer->getState()); tmpTimer.insert("state", timer->getState());
tmpTimer.insert("currentTime", timer->getCurrentTime()); tmpTimer.insert("currentTime", timer->getCurrentTime());
tmpTimer.insert("reactionTime", timer->getReactionTime()); tmpTimer.insert("reactionTime", timer->getReactionTime());
tmpTimer.insert("text", timer->getText());
tmpTimer.insert("letter", timer->getLetter()); tmpTimer.insert("letter", timer->getLetter());
tmpTimer.insert("readyState", timer->getReadyState()); tmpTimer.insert("readyState", timer->getReadyState());
tmpTimers.append(tmpTimer); tmpTimers.append(tmpTimer);
@ -784,7 +678,7 @@ QVariantMap ScStwRace::getDetails() {
tmpDetails.insert("state", this->getState()); tmpDetails.insert("state", this->getState());
tmpDetails.insert("competitionMode", this->competitionMode); 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("currentStartDelay", this->getCurrentStartDelay());
tmpDetails.insert("timers", this->getTimerDetailList()); tmpDetails.insert("timers", this->getTimerDetailList());
@ -813,5 +707,5 @@ bool ScStwRace::getCompetitionMode() {
} }
bool ScStwRace::getReadySoundEnabled() { bool ScStwRace::getReadySoundEnabled() {
return this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool(); return this->getSoundEnabledSetting(ScStwSoundPlayer::Ready);
} }