2020-06-07 14:43:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
** ScStw Libraries
|
|
|
|
** Copyright (C) 2020 Itsblue development
|
|
|
|
**
|
|
|
|
** This program is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
#include "../headers/scstwtimer.h"
|
|
|
|
|
2020-10-09 15:18:56 +02:00
|
|
|
ScStwTimer::ScStwTimer(QObject* parent) : ScStwTimer("", parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScStwTimer::ScStwTimer(QString letter, QObject *parent) : QObject(parent)
|
2020-04-11 23:41:34 +02:00
|
|
|
{
|
2020-06-13 15:57:27 +02:00
|
|
|
if(letter.length() > 1)
|
|
|
|
this->letter = letter[0];
|
|
|
|
else
|
|
|
|
this->letter = letter;
|
|
|
|
|
2021-01-09 23:12:28 +01:00
|
|
|
//qDebug() << "Timer created with letter: " << letter;
|
2020-10-03 13:06:14 +02:00
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
this->startTime = 0;
|
|
|
|
this->stopTime = 0;
|
|
|
|
this->reactionTime = 0;
|
2020-10-02 19:56:08 +02:00
|
|
|
this->wantsToBeDisabled = false;
|
2020-04-14 00:08:58 +02:00
|
|
|
|
|
|
|
this->state = IDLE;
|
2020-04-11 23:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwTimer::start() {
|
2020-04-14 00:08:58 +02:00
|
|
|
return this->start(QDateTime::currentMSecsSinceEpoch());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwTimer::start(double timeOfStart) {
|
2020-04-11 23:41:34 +02:00
|
|
|
switch (this->state) {
|
2020-04-14 00:08:58 +02:00
|
|
|
case IDLE: {
|
|
|
|
// in case of IDLE, start the race!
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-04-14 00:08:58 +02:00
|
|
|
this->startTime = timeOfStart;
|
2020-04-11 23:41:34 +02:00
|
|
|
this->stopTime = 0;
|
|
|
|
|
2020-04-14 00:08:58 +02:00
|
|
|
if(timeOfStart - QDateTime::currentMSecsSinceEpoch() > 0) {
|
|
|
|
this->setState(STARTING);
|
|
|
|
QTimer::singleShot(timeOfStart - QDateTime::currentMSecsSinceEpoch(), [=](){
|
|
|
|
if(this->state == STARTING)
|
|
|
|
this->setState(RUNNING);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this->setState(RUNNING);
|
2020-04-11 23:41:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// otherwise the timer is not supposed to be started!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:53:40 +02:00
|
|
|
void ScStwTimer::technicalIncident() {
|
|
|
|
qDebug() << "[INFO][TIMER] Timer got a technical incident";
|
|
|
|
|
|
|
|
this->setState(INCIDENT);
|
|
|
|
}
|
|
|
|
|
2020-10-04 15:22:43 +02:00
|
|
|
bool ScStwTimer::wildcard() {
|
|
|
|
if(this->state != STARTING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this->setState(WILDCARD);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:08:58 +02:00
|
|
|
void ScStwTimer::handleClimberStart(double timeOfStart) {
|
|
|
|
this->reactionTime = timeOfStart - this->startTime;
|
|
|
|
qDebug() << "+ [INFO][TIMER] reaction time: " << this->reactionTime;
|
|
|
|
|
2020-10-04 17:25:30 +02:00
|
|
|
if(this->reactionTime <= 0 && this->reactionTime){
|
2020-10-04 16:31:49 +02:00
|
|
|
qDebug() << "[INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime;
|
|
|
|
this->setState(FAILING);
|
2020-04-14 00:08:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 10:49:11 +02:00
|
|
|
emit this->reactionTimeChanged();
|
2020-04-14 00:08:58 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
bool ScStwTimer::cancel() {
|
2020-04-14 00:08:58 +02:00
|
|
|
if(!(this->state == IDLE || this->state == STARTING || this->state == RUNNING))
|
2020-04-11 23:41:34 +02:00
|
|
|
return false;
|
|
|
|
|
2020-10-01 01:54:39 +02:00
|
|
|
qDebug() << "[INFO][TIMER] Timer was cancelled";
|
2020-10-03 13:06:14 +02:00
|
|
|
this->reactionTime = 0;
|
|
|
|
this->startTime = 0;
|
|
|
|
this->stopTime = 0;
|
2020-10-01 01:54:39 +02:00
|
|
|
|
|
|
|
this->setState(CANCELLED);
|
2020-04-11 23:41:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwTimer::stop() {
|
2020-04-15 19:21:32 +02:00
|
|
|
return this->stop(QDateTime::currentMSecsSinceEpoch());
|
2020-04-11 23:41:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
bool ScStwTimer::stop(double timeOfStop) {
|
2020-10-04 17:25:30 +02:00
|
|
|
if(this->state != RUNNING)
|
2020-04-11 23:41:34 +02:00
|
|
|
return false;
|
|
|
|
|
2020-10-04 17:25:30 +02:00
|
|
|
this->stopTime = timeOfStop;
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-10-04 17:25:30 +02:00
|
|
|
// trigger an external state refresh to set the state to either WON or LOST depending on the other timers values (see ScStwRace::refreshTimerStates())
|
|
|
|
this->setState(WAITING);
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-04-19 22:39:33 +02:00
|
|
|
qDebug() << "[INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << this->getCurrentTime() << " reactionTime: " << reactionTime;
|
2020-04-11 23:41:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
bool ScStwTimer::setResult(TimerState result) {
|
2020-10-04 17:25:30 +02:00
|
|
|
|
|
|
|
QList<ScStwTimer::TimerState> allowedStates = {
|
|
|
|
STARTING,
|
|
|
|
RUNNING,
|
|
|
|
WAITING,
|
|
|
|
WON,
|
|
|
|
FAILING,
|
|
|
|
FAILED
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!allowedStates.contains(this->state))
|
2020-04-15 19:21:32 +02:00
|
|
|
return false;
|
|
|
|
|
2020-10-04 17:25:30 +02:00
|
|
|
/* The reasons why it has to accept all these states:
|
|
|
|
*
|
|
|
|
* STARTING: To set a timer to WILDCARD when its opponent has done a false start
|
|
|
|
* RUNNING: To set a timer to WILDCARD when its opponent has done a false start that was received with a delay
|
|
|
|
* WAITING: To set a timer to either WON or LOST
|
|
|
|
* WON: To set a timer to LOST when its opponent has won the race but their trigger was delayed
|
|
|
|
* FAILING: To set a timer to either FAILED or WILDCARD
|
|
|
|
* FAILED: To set a timer to WILDCARD when its opponent did an earlier false start but their tirgger was delayed
|
|
|
|
*/
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
switch (result) {
|
|
|
|
case LOST:
|
2020-10-04 17:25:30 +02:00
|
|
|
case WON:
|
|
|
|
if(this->state == WAITING || this->state == WON) {
|
2020-10-04 16:04:22 +02:00
|
|
|
this->setState(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WILDCARD:
|
2020-10-04 17:25:30 +02:00
|
|
|
case FAILED:
|
2020-10-04 18:11:44 +02:00
|
|
|
if(this->state == STARTING || this->state == RUNNING || this->state == FAILED || this->state == FAILING) {
|
2020-10-04 16:04:22 +02:00
|
|
|
this->setState(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2020-04-15 19:21:32 +02:00
|
|
|
default:
|
2020-10-04 16:04:22 +02:00
|
|
|
break;
|
2020-04-15 19:21:32 +02:00
|
|
|
}
|
2020-10-04 16:04:22 +02:00
|
|
|
|
|
|
|
return false;
|
2020-04-15 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
bool ScStwTimer::reset(){
|
|
|
|
if( this->state < WON || this->state == DISABLED ){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->startTime = 0;
|
|
|
|
this->stopTime = 0;
|
|
|
|
this->reactionTime = 0;
|
|
|
|
|
|
|
|
this->setState(IDLE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:02:53 +02:00
|
|
|
ScStwTimer::ReadyState ScStwTimer::getReadyState() {
|
|
|
|
return this->state == IDLE ? ScStwTimer::IsReady : ScStwTimer::NotInIdleState;
|
|
|
|
}
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
// ------------------------
|
|
|
|
// --- helper functions ---
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
void ScStwTimer::setState(TimerState newState){
|
|
|
|
if(this->state == DISABLED && newState != IDLE)
|
|
|
|
return;
|
|
|
|
|
2020-09-29 12:53:40 +02:00
|
|
|
if(this->state == INCIDENT && newState != IDLE)
|
|
|
|
return;
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
if(this->state != newState) {
|
|
|
|
this->state = newState;
|
2020-10-04 16:04:22 +02:00
|
|
|
qDebug() << "[INFO][TIMER] timer state changed: " << newState;
|
2020-10-04 16:31:49 +02:00
|
|
|
emit this->stateChanged(newState);
|
2020-04-11 23:41:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScStwTimer::TimerState ScStwTimer::getState() {
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
double ScStwTimer::getCurrentTime() {
|
2020-04-15 19:21:32 +02:00
|
|
|
switch (this->state) {
|
|
|
|
case RUNNING:
|
|
|
|
return QDateTime::currentMSecsSinceEpoch() - this->startTime;
|
|
|
|
default: {
|
|
|
|
if(this->state == WAITING || this->state == WON || this->state == LOST)
|
2020-06-13 18:24:27 +02:00
|
|
|
return abs(this->stopTime - this->startTime);
|
2020-04-15 19:21:32 +02:00
|
|
|
else
|
|
|
|
return -1;
|
2020-04-11 23:41:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double ScStwTimer::getReactionTime() {
|
|
|
|
return this->reactionTime;
|
|
|
|
}
|
|
|
|
|
2020-06-13 12:42:55 +02:00
|
|
|
QString ScStwTimer::getLetter() {
|
|
|
|
return this->letter;
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:57:00 +02:00
|
|
|
QString ScStwTimer::getText() {
|
2020-06-13 15:57:27 +02:00
|
|
|
QString newText = "";
|
|
|
|
int newTime = 0;
|
2020-04-17 19:57:00 +02:00
|
|
|
switch (this->state) {
|
|
|
|
case ScStwTimer::IDLE:
|
2020-06-13 15:57:27 +02:00
|
|
|
newTime = 0;
|
2020-04-17 19:57:00 +02:00
|
|
|
break;
|
|
|
|
case ScStwTimer::STARTING:
|
2020-06-13 15:57:27 +02:00
|
|
|
newTime = 0;
|
2020-04-17 19:57:00 +02:00
|
|
|
break;
|
|
|
|
case ScStwTimer::RUNNING:
|
2020-06-13 15:57:27 +02:00
|
|
|
newTime = this->getCurrentTime();
|
2020-04-17 19:57:00 +02:00
|
|
|
break;
|
2020-10-04 17:55:39 +02:00
|
|
|
case ScStwTimer::WAITING:
|
|
|
|
newText = "please wait...";
|
|
|
|
break;
|
2020-04-17 19:57:00 +02:00
|
|
|
case ScStwTimer::WON:
|
2020-06-13 15:57:27 +02:00
|
|
|
newTime = this->getCurrentTime();
|
2020-04-17 19:57:00 +02:00
|
|
|
break;
|
|
|
|
case ScStwTimer::LOST:
|
2020-06-13 15:57:27 +02:00
|
|
|
newTime = this->getCurrentTime();
|
2020-04-17 19:57:00 +02:00
|
|
|
break;
|
2020-10-04 17:55:39 +02:00
|
|
|
case ScStwTimer::FAILING:
|
|
|
|
newText = "please wait...";
|
|
|
|
break;
|
2020-04-17 19:57:00 +02:00
|
|
|
case ScStwTimer::FAILED:
|
|
|
|
newText = "false start";
|
|
|
|
break;
|
2020-10-04 16:05:16 +02:00
|
|
|
case ScStwTimer::WILDCARD:
|
|
|
|
newText = "wildcard";
|
|
|
|
break;
|
2020-04-17 19:57:00 +02:00
|
|
|
case ScStwTimer::CANCELLED:
|
|
|
|
newText = "cancelled";
|
|
|
|
break;
|
2020-10-01 01:54:39 +02:00
|
|
|
case ScStwTimer::INCIDENT:
|
2020-10-05 20:41:43 +02:00
|
|
|
newText = "Technical incident!";
|
2020-10-01 01:54:39 +02:00
|
|
|
break;
|
2020-04-17 19:57:00 +02:00
|
|
|
case ScStwTimer::DISABLED:
|
|
|
|
newText = "---";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-13 15:57:27 +02:00
|
|
|
if(newText == "")
|
|
|
|
newText = QString::number( newTime / 1000.0, 'f', 3 ).rightJustified(6, '0');
|
|
|
|
|
2020-04-17 19:57:00 +02:00
|
|
|
return newText;
|
|
|
|
}
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
void ScStwTimer::setDisabled(bool disabled) {
|
|
|
|
if(disabled)
|
|
|
|
this->setState(DISABLED);
|
2020-10-06 13:58:42 +02:00
|
|
|
else {
|
2020-04-11 23:41:34 +02:00
|
|
|
this->setState(IDLE);
|
2020-10-06 13:58:42 +02:00
|
|
|
}
|
2020-04-11 23:41:34 +02:00
|
|
|
}
|
2020-08-09 13:29:37 +02:00
|
|
|
|
|
|
|
void ScStwTimer::setWantsToBeDisabled(bool wantsToBeDisabled) {
|
|
|
|
if(this->wantsToBeDisabled == wantsToBeDisabled)
|
|
|
|
return;
|
|
|
|
|
2020-10-06 13:58:42 +02:00
|
|
|
qDebug() << "Wants to be disabled changed: " << wantsToBeDisabled;
|
|
|
|
|
2020-08-09 13:29:37 +02:00
|
|
|
this->wantsToBeDisabled = wantsToBeDisabled;
|
|
|
|
|
|
|
|
emit this->wantsToBeDisabledChanged(this, wantsToBeDisabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwTimer::getWantsToBeDisabled() {
|
|
|
|
return this->wantsToBeDisabled;
|
|
|
|
}
|
2020-10-06 13:58:42 +02:00
|
|
|
|
|
|
|
bool ScStwTimer::isRunning() {
|
|
|
|
return this->state == RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwTimer::isDisabled() {
|
|
|
|
return this->state == DISABLED;
|
|
|
|
}
|