From be6aebd4588c67b19ec83c926a7a1c9a42ca4164 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Tue, 6 Oct 2020 13:58:42 +0200 Subject: [PATCH] many many fixes --- ScStwLibraries/headers/ScStw.hpp | 14 +++--- ScStwLibraries/headers/scstwrace.h | 1 + ScStwLibraries/headers/scstwtimer.h | 4 ++ ScStwLibraries/sources/scstwrace.cpp | 72 ++++++++++++++++++++------- ScStwLibraries/sources/scstwtimer.cpp | 13 ++++- 5 files changed, 77 insertions(+), 27 deletions(-) diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index 46aba53..13ba196 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -112,16 +112,16 @@ public: FirmwareAlreadyUpToDateInfo = 304, - - CommandNotFoundError = 404, - ClientSessionAlreadyActiveError = 407, - UpdateSignatureInvalidError = 402, - TimestampTooSmallError = 406, - RequiredParameterNotGivenError = 405, - CurrentStateNotVaildForOperationError = 403, AccessDeniedError = 401, + UpdateSignatureInvalidError = 402, + CurrentStateNotVaildForOperationError = 403, + CommandNotFoundError = 404, + RequiredParameterNotGivenError = 405, + TimestampTooSmallError = 406, + ClientSessionAlreadyActiveError = 407, NoSessionActiveError = 408, ItemNotFoundError = 409, + LastTimerCannotBeDisabledError = 410, UpdateFailedError = 500, diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index d00a978..7fc59c9 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -146,6 +146,7 @@ private slots: bool playSoundsAndStartTimers(); ScStwSoundPlayer::PlayResult doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackStart = nullptr); void technicalIncident(); + ScStw::StatusCode setTimerDisabled(ScStwTimer* timer, bool disabled); virtual void refreshCompetitionMode(); diff --git a/ScStwLibraries/headers/scstwtimer.h b/ScStwLibraries/headers/scstwtimer.h index bdbe780..abfa291 100644 --- a/ScStwLibraries/headers/scstwtimer.h +++ b/ScStwLibraries/headers/scstwtimer.h @@ -265,6 +265,10 @@ public slots: */ virtual ScStwTimer::ReadyState getReadyState(); + bool isRunning(); + + bool isDisabled(); + protected slots: /*! diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 954c0f0..f5d251a 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -112,8 +112,8 @@ ScStw::StatusCode ScStwRace::reset() { ScStw::StatusCode returnCode = ScStw::Success; - foreach(ScStwTimer *speedTimer, this->timers){ - if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED && speedTimer->getState() != ScStwTimer::IDLE) { + foreach(ScStwTimer *timer, this->timers){ + if(!timer->reset() && timer->getState() != ScStwTimer::DISABLED && timer->getState() != ScStwTimer::IDLE) { returnCode = ScStw::InternalErrorTimerOperationFailed; } } @@ -154,15 +154,36 @@ ScStw::StatusCode ScStwRace::cancel() { } ScStw::StatusCode ScStwRace::setTimerDisabled(int timerId, bool disabled) { - if(this->state != IDLE && this->state != WAITING) - return ScStw::CurrentStateNotVaildForOperationError; - if(timerId < 0 || timerId - 1 > this->timers.length()) return ScStw::ItemNotFoundError; - this->timers[timerId]->setDisabled(disabled); + return this->setTimerDisabled(this->timers[timerId], disabled); +} + +ScStw::StatusCode ScStwRace::setTimerDisabled(ScStwTimer* timer, bool disabled) { + qDebug() << "[INFO][RACE] Setting timer "<< timer->getLetter() << " to disabled: " << disabled << " this state: " << this->state; + if(this->state != IDLE && this->state != WAITING) + return ScStw::CurrentStateNotVaildForOperationError; + + if(!this->timers.contains(timer)) + return ScStw::ItemNotFoundError; + + int enabledTimerCount = 0; + + foreach(ScStwTimer *timer, this->timers) { + if(timer->getState() != ScStwTimer::DISABLED) + enabledTimerCount ++; + } + + if(disabled && enabledTimerCount <= 1) + return ScStw::LastTimerCannotBeDisabledError; + + qDebug() << "[INFO][RACE] Setting timer "<< timer->getLetter() << " to disabled: " << disabled; + + timer->setDisabled(disabled); return ScStw::Success; + } void ScStwRace::handleTimerReadyStateChange(ScStwTimer::ReadyState readyState) { @@ -435,16 +456,18 @@ void ScStwRace::handleTimerStop() { // find out which timer has won double lowestStoppedTime = -1; - QList timersWhichHaveWon; // iterate through all timers and find the lowest time that was stopped foreach(ScStwTimer * timer, this->timers) { - if(timer->getCurrentTime() <= lowestStoppedTime || lowestStoppedTime < 0) { + qDebug() << "Current stopped time is: " << timer->getCurrentTime(); + if(!timer->isRunning() && !timer->isDisabled() && (timer->getCurrentTime() <= lowestStoppedTime || lowestStoppedTime < 0)) { // this is the timer with the lowest stopped time lowestStoppedTime = timer->getCurrentTime(); } } + qDebug() << "LOWEST Stop time is: " << lowestStoppedTime; + // append the timer(s) with the lowest stopped time to the winner list foreach(ScStwTimer * timer, this->timers) { if(timer->getCurrentTime() <= lowestStoppedTime) @@ -469,6 +492,7 @@ void ScStwRace::handleFalseStart() { if( timer->getState() >= ScStwTimer::FAILING && timer->getState() <= ScStwTimer::FAILED && + !timer->isDisabled() && ( timer->getReactionTime() < lowestReactionTime || lowestReactionTime == -4000 @@ -490,6 +514,7 @@ void ScStwRace::handleFalseStart() { if( timer->getState() >= ScStwTimer::FAILING && timer->getState() <= ScStwTimer::FAILED && + !timer->isDisabled() && timer->getReactionTime() <= lowestReactionTime ) { // this is the timer with the lowest stopped time @@ -563,7 +588,7 @@ bool ScStwRace::getIsReadyForNextState() { break; case WAITING: { foreach (ScStwTimer *timer, this->timers) { - if(timer->getReadyState() != ScStwTimer::IsReady) + if(timer->getReadyState() != ScStwTimer::IsReady && timer->getReadyState() != ScStwTimer::IsDisabled) return false; } break; @@ -584,11 +609,14 @@ bool ScStwRace::getIsReadyForNextState() { * @param {ScStwExtensionControlledTimer*} timer timer to be enabled */ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wantsToBeDisabled) { + qDebug() << "Handling timer wants to be disabled"; if(this->competitionMode) return; if(this->state == IDLE) { - timer->setDisabled(wantsToBeDisabled); + + qDebug() << "Handling timer wants to be disabled: " << wantsToBeDisabled; + this->setTimerDisabled(timer, wantsToBeDisabled); } } @@ -603,17 +631,20 @@ void ScStwRace::refreshCompetitionMode() { qDebug() << "[INFO][RACE] Setting competition mode to " << currentCompetitionMode; this->competitionMode = currentCompetitionMode; + } - if(this->competitionMode) { - foreach (ScStwTimer *timer, this->timers) { - timer->setDisabled(false); - } + 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()); - } + } + else { + foreach(ScStwTimer* timer, this->timers) { + if( + (timer->getWantsToBeDisabled() && timer->getState() != ScStwTimer::DISABLED) || + (!timer->getWantsToBeDisabled() && timer->getState() == ScStwTimer::DISABLED) + ) + this->handleTimerWantsToBeDisabledChange(timer, timer->getWantsToBeDisabled()); } } } @@ -686,6 +717,9 @@ bool ScStwRace::addTimer(ScStwTimer *timer) { if(this->competitionMode && timer->getState() == ScStwTimer::DISABLED) timer->setDisabled(false); + else if(!this->competitionMode) + this->handleTimerWantsToBeDisabledChange(timer, timer->getWantsToBeDisabled()); + return true; diff --git a/ScStwLibraries/sources/scstwtimer.cpp b/ScStwLibraries/sources/scstwtimer.cpp index 49ed38e..01c0b02 100644 --- a/ScStwLibraries/sources/scstwtimer.cpp +++ b/ScStwLibraries/sources/scstwtimer.cpp @@ -278,14 +278,17 @@ QString ScStwTimer::getText() { void ScStwTimer::setDisabled(bool disabled) { if(disabled) this->setState(DISABLED); - else + else { this->setState(IDLE); + } } void ScStwTimer::setWantsToBeDisabled(bool wantsToBeDisabled) { if(this->wantsToBeDisabled == wantsToBeDisabled) return; + qDebug() << "Wants to be disabled changed: " << wantsToBeDisabled; + this->wantsToBeDisabled = wantsToBeDisabled; emit this->wantsToBeDisabledChanged(this, wantsToBeDisabled); @@ -294,3 +297,11 @@ void ScStwTimer::setWantsToBeDisabled(bool wantsToBeDisabled) { bool ScStwTimer::getWantsToBeDisabled() { return this->wantsToBeDisabled; } + +bool ScStwTimer::isRunning() { + return this->state == RUNNING; +} + +bool ScStwTimer::isDisabled() { + return this->state == DISABLED; +}