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/client/scstwremoterace.cpp

295 lines
9.8 KiB
C++
Raw Normal View History

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-10-02 19:56:08 +02:00
#include "scstwremoterace.h"
ScStwRemoteRace::ScStwRemoteRace(ScStwClient *scStwClient, ScStwSettings *settings, QObject *parent) : ScStwRace(settings, parent)
{
2020-10-03 11:10:15 +02:00
this->isReadyForNextState = true;
2020-10-02 16:49:24 +02:00
this->scStwClient = scStwClient;
2020-10-03 11:10:15 +02:00
this->scStwClient->addSignalSubscription(ScStw::RaceDetailsChanged);
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteRace::handleClientStateChange);
2020-10-02 19:56:08 +02:00
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteRace::handleBaseStationSignal);
}
// --------------------------
// --- Main Functionality ---
// --------------------------
ScStw::StatusCode ScStwRemoteRace::start(bool asyncronous) {
if(this->local())
return ScStwRace::start(asyncronous);
2020-10-02 19:56:08 +02:00
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::IDLE && this->getState() != ScStwRace::WAITING)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- starting race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StartRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::cancel() {
if(this->local())
return ScStwRace::cancel();
2020-10-02 16:49:24 +02:00
if(this->getState() != PREPAIRING && this->getState() != WAITING && this->getState() != STARTING && this->getState() != RUNNING)
return ScStw::CurrentStateNotVaildForOperationError;
2020-10-02 16:49:24 +02:00
qDebug() << "+ --- stopping race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::CancelRaceCommand);
2020-05-26 17:57:32 +02:00
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::stop() {
if(this->local())
return ScStwRace::stop();
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::RUNNING && this->getState() != ScStwRace::STARTING)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- stopping race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StopRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::reset() {
if(this->local())
return ScStwRace::reset();
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::STOPPED && this->getState() != ScStwRace::INCIDENT)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- resetting race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::ResetRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
// -------------------------
// --- Base Station sync ---
// -------------------------
void ScStwRemoteRace::handleClientStateChange() {
switch (this->scStwClient->getState()) {
case ScStwClient::CONNECTED:
this->localTimers.clear();
this->localTimers = this->timers;
this->timers.clear();
break;
case ScStwClient::DISCONNECTED:
foreach(ScStwRemoteTimer *remoteTimer, this->remoteTimers)
remoteTimer->deleteLater();
this->remoteTimers.clear();
2020-04-19 13:09:24 +02:00
this->timers.clear();
this->timers = this->localTimers;
this->localTimers.clear();
emit this->timersChanged();
this->competitionMode = false;
2020-04-19 13:09:24 +02:00
this->setState(IDLE);
break;
default:
break;
}
}
ScStwRemoteRace::RaceMode ScStwRemoteRace::getMode() {
if(this->scStwClient->getState() == ScStwClient::CONNECTED)
return ScStwRemoteRace::REMOTE;
else
return ScStwRemoteRace::LOCAL;
}
bool ScStwRemoteRace::local() {
return this->getMode() == LOCAL;
}
void ScStwRemoteRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) {
if(this->local())
return ScStwRace::handleSettingChange(keyInt, keyLevel, value);
}
/**
* @brief ScStwAppBackend::handleBaseStationUpdate
*
* Function to handle an update, sent by the base station, which indicates
* that some remote value (like a state) has changed
*
* @param data
*/
2020-10-02 19:56:08 +02:00
void ScStwRemoteRace::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) {
2020-04-19 13:09:24 +02:00
//qDebug() << "got signal: " << data;
switch (key) {
2020-10-03 11:10:15 +02:00
case ScStw::RaceDetailsChanged:
{
2020-10-03 11:10:15 +02:00
this->refreshDetails(data.toMap());
break;
}
2020-10-03 11:10:15 +02:00
default:
return;
}
2020-10-03 11:10:15 +02:00
}
2020-10-02 16:49:24 +02:00
2020-10-03 11:10:15 +02:00
void ScStwRemoteRace::refreshDetails(QVariantMap details) {
// the details of the race have changed:
2020-10-02 16:49:24 +02:00
2020-10-03 11:10:15 +02:00
// state
this->setState(ScStwRace::RaceState(details["state"].toInt()));
2020-10-03 11:10:15 +02:00
// competition mode
if(this->competitionMode != details["competitionMode"].toBool()) {
this->competitionMode = details["competitionMode"].toBool();
emit this->competitionModeChanged();
}
2020-10-03 11:10:15 +02:00
// ready sound enabled
if(this->readySoundEnabled != details["readySoundEnabled"].toBool()) {
this->readySoundEnabled = details["readySoundEnabled"].toBool();
emit this->readySoundEnabledChanged();
}
2020-10-03 11:10:15 +02:00
// current start delay
this->currentStartTotalDelay = details["currentStartDelay"].toMap()["total"].toInt();
this->latestStartDelayProgress = details["currentStartDelay"].toMap()["progress"].toDouble();
this->currentStartDelayStartedAt = QDateTime::currentMSecsSinceEpoch() - (this->currentStartTotalDelay * this->latestStartDelayProgress);
2020-10-03 11:10:15 +02:00
emit this->currentStartDelayChanged();
// timers
this->refreshRemoteTimers(details["timers"].toList());
// isReady
if(this->state == WAITING) {
this->isReadyForNextState = details["isReadyForNextState"].toBool();
emit this->isReadyForNextStateChanged();
}
2020-10-03 11:10:15 +02:00
emit this->detailsChanged();
}
void ScStwRemoteRace::rebuildRemoteTimers(QVariantList remoteTimers) {
// delete all current timers
foreach(ScStwTimer * oldTimer, this->timers){
oldTimer->deleteLater();
}
this->remoteTimers.clear();
this->timers.clear();
foreach(QVariant remoteTimer, remoteTimers){
// create a local timer for each remote timer
ScStwRemoteTimer * timer = new ScStwRemoteTimer(this);
this->timers.append(timer);
this->remoteTimers.append(timer);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
}
}
2020-10-02 19:56:08 +02:00
bool ScStwRemoteRace::refreshRemoteTimers(QVariantList remoteTimers) {
if(remoteTimers.length() != this->remoteTimers.length()){
// local timers are out of sync
this->rebuildRemoteTimers(remoteTimers);
}
foreach(QVariant remoteTimer, remoteTimers){
int currId = remoteTimer.toMap()["id"].toInt();
2020-10-02 19:56:08 +02:00
if(this->remoteTimers.length() <= currId)
this->rebuildRemoteTimers(remoteTimers);
ScStwTimer::TimerState newState = ScStwTimer::TimerState(remoteTimer.toMap()["state"].toInt());
2020-10-02 19:56:08 +02:00
qDebug() << "refreshing timers: id: " << currId << " state: " << newState << " readyState: " << remoteTimer.toMap()["readyState"].toInt() << " currentTime: " << remoteTimer.toMap()["currentTime"].toDouble();
2020-10-02 19:56:08 +02:00
double currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
2020-10-02 19:56:08 +02:00
this->remoteTimers[currId]->setStartTime(currentMSecsSinceEpoch - remoteTimer.toMap()["currentTime"].toDouble());
if(newState >= ScStwTimer::WAITING)
2020-10-02 19:56:08 +02:00
this->remoteTimers[currId]->setStopTime(currentMSecsSinceEpoch);
2020-10-02 19:56:08 +02:00
this->remoteTimers[currId]->setReactionTime(remoteTimer.toMap()["reactionTime"].toDouble());
this->remoteTimers[currId]->setLetter(remoteTimer.toMap()["letter"].toString());
this->remoteTimers[currId]->setReadyState(ScStwTimer::ReadyState(remoteTimer.toMap()["readyState"].toInt()));
2020-06-13 15:57:27 +02:00
2020-10-02 19:56:08 +02:00
this->remoteTimers[currId]->setState(newState);
}
return true;
}
2020-10-02 19:56:08 +02:00
bool ScStwRemoteRace::addTimer(ScStwTimer* timer) {
if(this->local())
return ScStwRace::addTimer(timer);
return false;
}
2020-04-19 13:09:24 +02:00
QVariantMap ScStwRemoteRace::getCurrentStartDelay() {
if(this->local())
return ScStwRace::getCurrentStartDelay();
2020-04-19 13:09:24 +02:00
QVariantMap currentStartDelay = {
{"total", -1.0},
{"progress", -1.0}
};
if(this->currentStartTotalDelay == -1) {
currentStartDelay["progress"] = this->latestStartDelayProgress;
2020-10-02 16:49:24 +02:00
}
else if(this->getState() == PREPAIRING || this->getState() == WAITING) {
2020-04-19 13:09:24 +02:00
// get the total delay and the delay progress of the next action timer
2020-10-02 16:49:24 +02:00
double elapsed = QDateTime::currentMSecsSinceEpoch() - this->currentStartDelayStartedAt;
currentStartDelay["total"] = this->currentStartTotalDelay;
if(elapsed < 0 || elapsed > currentStartDelay["total"].toDouble()) {
elapsed = currentStartDelay["total"].toDouble();
2020-04-19 13:09:24 +02:00
}
currentStartDelay["progress"] = elapsed / currentStartDelay["total"].toDouble();
2020-04-19 13:09:24 +02:00
}
return currentStartDelay;
2020-04-19 13:09:24 +02:00
}
2020-10-03 11:10:15 +02:00
bool ScStwRemoteRace::getIsReadyForNextState() {
if(this->local())
return ScStwRace::getIsReadyForNextState();
2020-10-03 11:10:15 +02:00
return this->isReadyForNextState;
}
bool ScStwRemoteRace::getReadySoundEnabled() {
if(this->local())
return ScStwRace::getReadySoundEnabled();
return this->readySoundEnabled;
}