This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
shared-libraries/ScStwLibraries/sources/scstwtimer.cpp
Dorian Zedler 1570bd86c9
- some changes to file structure
- started to implement timer, race and sound management
2020-04-13 23:59:36 +02:00

152 lines
3.7 KiB
C++

#include "../headers/scstwtimer.h"
ScStwTimer::ScStwTimer(QObject *parent) : QObject(parent)
{
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
}
bool ScStwTimer::start() {
switch (this->state) {
case STARTING: {
// in case of STARTING, start the race!
this->startTime = QDateTime::currentMSecsSinceEpoch();
this->stopTime = 0;
this->stoppedTime = 0;
this->setState(RUNNING);
return true;
}
default: {
// otherwise the timer is not supposed to be started!
return false;
}
}
}
bool ScStwTimer::cancel() {
if(!(this->state == STARTING || this->state == RUNNING))
return false;
this->setState(CANCELLED);
return true;
}
bool ScStwTimer::stop() {
this->stop(ManualStop);
}
bool ScStwTimer::stop(TimerState result) {
switch (result) {
case WON:
this->setState(WON);
return true;
case LOST:
this->setState(LOST);
return false;
default:
return false;
}
}
bool ScStwTimer::stop(StopReason reason) {
if(this->state != STARTING && this->state != RUNNING && this->state != WAITING){
return false;
}
switch (reason) {
case ManualStop: {
if(this->state == STARTING){
emit startCanceled(false);
this->setState(CANCELLED);
}
else {
this->stopTime = this->date->currentMSecsSinceEpoch();
this->stoppedTime = this->stopTime - this->startTime;
// trigger an external state refresh to set the state to either WON or LOST depending on the other timers values (see MainActivity::refreshTimerStates())
emit this->stopRequested();
}
break;
}
case FailStop: {
this->stoppedTime = 0;
//qDebug() << "+ [INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
if(this->state == RUNNING){
qDebug() << "+ [INFO][TIMER] False Start detected, step 2: " << "start Time: " << startTime << " startpadTriggerTime: " << startPadTriggerTime << " reactionTime: " << reactionTime;
this->setState(FAILED);
}
emit startCanceled(true);
return true;
}
default: {
return false;
}
}
qDebug() << "+ [INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
return true;
}
bool ScStwTimer::reset(){
if( this->state < WON || this->state == DISABLED ){
return false;
}
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->startPadTriggerTime = 0;
this->setState(IDLE);
return true;
}
// ------------------------
// --- helper functions ---
// ------------------------
void ScStwTimer::setState(TimerState newState){
if(this->state == DISABLED && newState != IDLE)
return;
if(this->state != newState) {
this->state = newState;
qDebug() << "+ [INFO][TIMER] timer state changed: " << newState;
emit this->stateChanged();
}
}
ScStwTimer::TimerState ScStwTimer::getState() {
return this->state;
}
double ScStwTimer::getCurrentTime() {
if(this->state == RUNNING){
return this->date->currentMSecsSinceEpoch() - this->startTime;
}
else {
return this->stoppedTime;
}
}
double ScStwTimer::getReactionTime() {
return this->reactionTime;
}
void ScStwTimer::setDisabled(bool disabled) {
if(disabled)
this->setState(DISABLED);
else
this->setState(IDLE);
}