From 130213d09dc5684dc2ed7d8c37b8177bb3eac6e4 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sun, 19 Apr 2020 22:39:33 +0200 Subject: [PATCH] some cleanup --- ScStwLibraries/headers/ScStw.hpp | 5 +- ScStwLibraries/headers/scstwrace.h | 26 +++++- ScStwLibraries/headers/scstwtimer.h | 6 -- ScStwLibraries/sources/scstwrace.cpp | 111 +++++++++++--------------- ScStwLibraries/sources/scstwtimer.cpp | 10 +-- 5 files changed, 78 insertions(+), 80 deletions(-) diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index f3590b9..e48abda 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -87,7 +87,10 @@ public: Error = 900, NotConnectedError = 910, TimeoutError = 911, - SettingNotAccessibleError = 901 + SettingNotAccessibleError = 901, + CurrentStateNotVaildForOperation = 904, + InternalError = 950, + InternalErrorTimerOperationFailed = 951 }; Q_ENUM(ScStw::StatusCode) diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index 0393eec..1f20801 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -80,10 +80,28 @@ private: public slots: + /*! + * \brief Function to start the race + * + * \param asyncronous if the function should just start the start sequence and then quit (true) + * or if if should wait until the start sequence is over and quit after that (false) + * \return 200: OK; 904: state not matching + */ int start(bool asyncronous = true); + + /*! + * \brief Function to stop the currently running race + * + * \return 200: OK; 904: state not matching + */ int stop(); + + /*! + * \brief Function to reset a stopped race + * \return + */ int reset(); - void cancelStart(bool falseStart); + int cancel(); // setters bool writeStartActionSetting(StartAction action, bool enabled, int delay); @@ -102,7 +120,13 @@ protected slots: private slots: void refreshTimerStates(); void handleTimerEnable(ScStwTimer* timer); + int handleFalseStart(); bool playSoundsAndStartTimers(StartAction thisAction); + + + /** + * \brief Function to declare the winner and looser timers after a timer has been stopped + */ void handleTimerStop(); signals: diff --git a/ScStwLibraries/headers/scstwtimer.h b/ScStwLibraries/headers/scstwtimer.h index 5357e2b..48dfd17 100644 --- a/ScStwLibraries/headers/scstwtimer.h +++ b/ScStwLibraries/headers/scstwtimer.h @@ -305,12 +305,6 @@ signals: */ void stateChanged(); - /*! - * \brief Emitted when the start was canceled - * \param falseStart Wheter a false start occured - */ - void startCanceled(bool falseStart); - /*! * \brief Emitted when the reaction time changed */ diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 6064131..ed5186c 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -27,15 +27,9 @@ ScStwRace::ScStwRace(QObject *parent) : QObject(parent) // --- Main Functionality --- // -------------------------- -/** - * Function to start the race - * - * @brief ScStwRace::startRace - * @return {int} 200: OK; 904: MAIN state not matching - */ int ScStwRace::start(bool asyncronous) { if(this->state != IDLE) { - return 904; + return ScStw::CurrentStateNotVaildForOperation; } qDebug() << "+ [INFO] starting race"; @@ -48,47 +42,33 @@ int ScStwRace::start(bool asyncronous) { else this->playSoundsAndStartTimers(None); - return 200; + return ScStw::Success; } -/** - * Function to stop the currently running race - * - * @brief ScStwRace::stopRace - * @param {int} type 0: stop; 1: cancel; 2: false start - * @return {int} 200: OK; 904: MAIN state not matching - */ int ScStwRace::stop() { if(this->state != RUNNING && this->state != STARTING) { - return 904; + return ScStw::CurrentStateNotVaildForOperation; } qDebug() << "+ [INFO] stopping race"; double timeOfStop = QDateTime::currentMSecsSinceEpoch(); - int returnCode = 900; - - bool stopOk = true; + int returnCode = ScStw::Success; foreach(ScStwTimer *speedTimer, this->timers){ if(!speedTimer->stop(timeOfStop) && speedTimer->getState() != ScStwTimer::DISABLED){ - stopOk = false; + returnCode = ScStw::InternalErrorTimerOperationFailed; } } - returnCode = stopOk ? 200:904; - - if(returnCode == 200) { + if(returnCode == ScStw::Success) { this->setState(STOPPED); } return returnCode; } -/** - * @brief ScStwRace::handleTimerStop Function to declare the winner and looser timers after a timer has been stopped - */ void ScStwRace::handleTimerStop() { if(this->state == RUNNING) { // find out which timer has won @@ -129,56 +109,64 @@ void ScStwRace::handleTimerStop() { } } -/** - * @brief ScStwRace::resetRace - * @return {int} 200: OK; 904: MAIN state not matching - */ int ScStwRace::reset() { if(this->state != STOPPED) { - return 904; + return ScStw::CurrentStateNotVaildForOperation; } qDebug() << "+ [INFO] resetting race"; - int returnCode = 200; + int returnCode = ScStw::Success; foreach(ScStwTimer *speedTimer, this->timers){ if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED) { - returnCode = 904; + returnCode = ScStw::InternalErrorTimerOperationFailed; } } - if(returnCode == 200){ + if(returnCode == ScStw::Success){ this->setState(IDLE); } return returnCode; } -void ScStwRace::cancelStart(bool falseStart){ - // TODO: FIXME - qDebug() << "+ [INFO] cancelling race: " << falseStart; +int ScStwRace::cancel() { + if(this->state != STARTING && this->state != RUNNING) + return ScStw::CurrentStateNotVaildForOperation; - if(falseStart){ - // cancel all running timers - foreach(ScStwTimer *timer, this->timers) { - timer->cancel(); - } + qDebug() << "[INFO][RACE] cancelling race"; - this->setState(STOPPED); + this->soundPlayer->cancel(this->soundVolume); + this->nextActionTimer->stop(); + this->nextActionLoop->quit(); + this->nextStartAction = None; - this->soundPlayer->cancel(this->soundVolume); + int returnCode = ScStw::Success; + + foreach(ScStwTimer *timer, this->timers){ + if(!timer->cancel() && timer->getState() != ScStwTimer::DISABLED) + returnCode = ScStw::InternalErrorTimerOperationFailed; } - else { - this->soundPlayer->cancel(this->soundVolume); - this->nextActionTimer->stop(); - this->nextActionLoop->quit(); - this->nextStartAction = None; - foreach(ScStwTimer *timer, this->timers){ - timer->cancel(); - } + return returnCode; +} + +int ScStwRace::handleFalseStart() { + if(this->getState() != STARTING && this->getState() != RUNNING) + return ScStw::CurrentStateNotVaildForOperation; + + int returnCode = ScStw::Success; + // cancel all running timers + foreach(ScStwTimer *timer, this->timers) { + if(!timer->cancel() && timer->getState() != ScStwTimer::DISABLED && timer->getState() != ScStwTimer::FAILED) + returnCode = ScStw::InternalErrorTimerOperationFailed; } + + this->setState(STOPPED); + this->soundPlayer->cancel(this->soundVolume); + + return returnCode; } bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) { @@ -243,10 +231,6 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) { } -/** - * @brief ScStwRace::setState - * @param {QString} newState - */ void ScStwRace::setState(RaceState newState) { if(newState != this->state) { qDebug() << "[INFO][RACE] state changed: " << newState; @@ -264,9 +248,6 @@ void ScStwRace::setState(RaceState newState) { } } -/** - * @brief ScStwRace::refreshTimerStates - */ void ScStwRace::refreshTimerStates() { qDebug() << "[INFO][MAIN] refreshing timer states"; @@ -283,12 +264,13 @@ void ScStwRace::refreshTimerStates() { else if(timer->getState() == ScStwTimer::WAITING) { this->handleTimerStop(); } + else if (timer->getState() == ScStwTimer::FAILED) { + this->handleFalseStart(); + } } - if(raceIsOver){ + if(raceIsOver) this->setState(STOPPED); - return; - } } // ------------------------ @@ -329,8 +311,8 @@ QVariantList ScStwRace::getNextStartActionDetails() { return { this->nextStartAction, - nextActionDelay, - nextActionDelayProg + nextActionDelay, + nextActionDelayProg }; } @@ -369,7 +351,6 @@ bool ScStwRace::addTimer(ScStwTimer *timer) { this->timers.append(timer); - connect(timer, &ScStwTimer::startCanceled, this, &ScStwRace::cancelStart); connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates); connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable); connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged); diff --git a/ScStwLibraries/sources/scstwtimer.cpp b/ScStwLibraries/sources/scstwtimer.cpp index e8de52f..fc3df05 100644 --- a/ScStwLibraries/sources/scstwtimer.cpp +++ b/ScStwLibraries/sources/scstwtimer.cpp @@ -84,7 +84,6 @@ bool ScStwTimer::stop(StopReason reason, double timeOfStop) { switch (reason) { case ManualStop: { if(this->state == STARTING){ - emit startCanceled(false); this->setState(CANCELLED); } else { @@ -96,19 +95,16 @@ bool ScStwTimer::stop(StopReason reason, double timeOfStop) { break; } case FailStop: { - qDebug() << "+ [INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime; + qDebug() << "[INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime; this->setState(FAILED); - - emit startCanceled(true); - - return true; + break; } default: { return false; } } - qDebug() << "+ [INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << this->getCurrentTime() << " reactionTime: " << reactionTime; + qDebug() << "[INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << this->getCurrentTime() << " reactionTime: " << reactionTime; return true; }