-
Notifications
You must be signed in to change notification settings - Fork 1
/
racepilot.cpp
153 lines (126 loc) · 4.46 KB
/
racepilot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* OpenRaceLapTimer - Copyright 2015 by airbirds.de, a project of polyvision UG (haftungsbeschränkt)
*
* Author: Alexander B. Bierbrauer
*
* This file is part of OpenRaceLapTimer.
*
* OpenRaceLapTimer 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.
* OpenRaceLapTimer 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 Foobar. If not, see http://www.gnu.org/licenses/.
**/
#include "racepilot.h"
#include <QDebug>
#include <QSqlQuery>
#include "currentpilotracelap.h"
#include <QDebug>
#include "rltdatabase.h"
#include "sfx.h"
#include "settings.h"
RacePilot::RacePilot(QObject *parent) : QObject(parent)
{
m_pCurrentRaceLap = NULL;
m_iCurrentRaceID = 0;
}
void RacePilot::setQuadToken(QString v){
this->m_strQuadToken = v;
}
void RacePilot::setPilotName(QString v){
this->m_strPilotName = v;
}
void RacePilot::setDatabaseID(QString v){
this->m_strDatabaseID =v;
}
RacePilot* RacePilot::getByQuadToken(QString token){
QSqlQuery query;
query.prepare("SELECT * FROM pilots WHERE quad_token=:quad_token;");
query.bindValue(":quad_token", token);
query.exec();
if(query.next()){
RacePilot *t = new RacePilot();
t->setPilotName(query.value("name").toString());
t->setQuadToken(token);
t->setDatabaseID(query.value("id").toString());
return t;
}else{
//qDebug() << "RacePilot::initDatabase(): failed to find pilot -" << query.lastError();
}
return NULL;
}
int RacePilot::lapCount(){
return this->m_listLaps.size();
}
QDateTime RacePilot::currentLapStartTime(){
if(this->m_pCurrentRaceLap){
return m_pCurrentRaceLap->getLapStart();
}
return QDateTime();
}
QString RacePilot::getQuadToken(){
return m_strQuadToken;
}
QString RacePilot::getPilotName(){
return m_strPilotName;
}
void RacePilot::fireLapTime(){
// beginning of the race for this pilot
if(this->m_pCurrentRaceLap == NULL){
this->startLap();
}else{
this->finishLap();
}
}
void RacePilot::startLap(){
this->m_pCurrentRaceLap = new CurrentPilotRaceLap(this->m_listLaps.size() + 1);
this->m_pCurrentRaceLap->startLap();
this->m_listLaps << this->m_pCurrentRaceLap;
qDebug() << QString("RacePilot::fireLapTime: %1 started lap %2 on %3").arg(this->getPilotName()).arg(this->m_listLaps.size()).arg(this->m_pCurrentRaceLap->getLapStart().toString());
SFX::instance()->playBeep();
}
void RacePilot::finishLap(){
// one lap can never be faster than 5 seconds
QDateTime now = QDateTime::currentDateTime();
if(this->m_pCurrentRaceLap->getLapStart().msecsTo(now) < Settings::instance()->getTrackingTimeout()){
return;
}
if(this->m_pCurrentRaceLap->finishLap()){
qDebug() << QString("RacePilot::fireLapTime: %1 finished lap %2 on %3").arg(this->getPilotName()).arg(this->m_listLaps.size()).arg(this->m_pCurrentRaceLap->getLapEnd().toString());
// log the lap to the database
RLTDatabase::instance()->addLapTimeToRace(m_iCurrentRaceID,m_strDatabaseID.toInt(),m_pCurrentRaceLap->getLap(),m_pCurrentRaceLap->lapTime());
// let's start a new lap
this->startLap();
}
}
QString RacePilot::lastLapTimeString(){
CurrentPilotRaceLap * lap = this->m_listLaps.at(this->m_listLaps.size() -2);
if(lap){
return lap->formatedLapTime();
}
return QString("undefined");
}
bool RacePilot::hasLastLapTime(){
if(this->m_listLaps.size() >= 2){
return true;
}
return false;
}
CurrentPilotRaceLap* RacePilot::getFastedLap(){
CurrentPilotRaceLap *fastestLap = NULL;
for(int i = 0; i < this->m_listLaps.size(); i++){
CurrentPilotRaceLap *lap = this->m_listLaps.at(i);
if(lap->isFinished()){
if(fastestLap && fastestLap->lapTime() > lap->lapTime()){
fastestLap = lap;
}else if(fastestLap == NULL){
fastestLap = lap;
}
}
}
return fastestLap;
}
int RacePilot::getID(){
return m_strDatabaseID.toInt();
}
void RacePilot::setCurrentRaceID(int id){
m_iCurrentRaceID = id;
}