-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrentrace.cpp
124 lines (104 loc) · 4.16 KB
/
currentrace.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
/**
* 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 "currentrace.h"
#include "racepilot.h"
#include <QDebug>
#include <QTableWidget>
#include "sfx.h"
#include <QSqlQueryModel>
#include "mainwindow.h"
#include "currentpilotracelap.h"
#include <QLabel>
CurrentRace::CurrentRace(QObject *parent) : QObject(parent)
{
this->m_pModelRace = NULL;
m_uiRaceState = CR_RACE_STATE_NOT_RUNNING;
m_iFastestLapTime = 0;
}
void CurrentRace::incommingPilotSignal(QString token){
//is there a running race?
if(m_uiRaceState == CR_RACE_STATE_NOT_RUNNING){
return;
}
qDebug() << QString("CurrentRace::incommingPilotSignal: %1").arg(token);
RacePilot *pilot = this->getPilotByToken(token);
// add the pilot to the race list or add it if it can be found
if(pilot == NULL){
pilot = RacePilot::getByQuadToken(token);
if(pilot){
pilot->setCurrentRaceID(this->m_pModelRace->record(0).value("id").toInt()); // setting the current race id
this->m_listPilots << pilot;
RLTDatabase::instance()->addPilotToRace(this->m_pModelRace->record(0).value("id").toInt(),pilot->getID());
qDebug() << QString("CurrentRace::incommingPilotSignal: added pilot to current race list count %1").arg(this->m_listPilots.size());
}
}else{
qDebug() << "CurrentRace::incommingPilotSignal: pilot found in current race list";
}
if(pilot){
pilot->fireLapTime();
}
emit pilotDataChanged();
emit fastedLapChanged();
}
RacePilot* CurrentRace::getPilotByToken(QString token){
for (int i = 0; i < m_listPilots.size(); ++i) {
if (m_listPilots.at(i)->getQuadToken().compare(token) == 0){
return m_listPilots.at(i);
}
}
return NULL;
}
/*
* starts a new race, needs the race_id which got created former for the race
*/
void CurrentRace::startRace(int race_id,MainWindow *window){
this->m_pModelRace = new QSqlQueryModel();
this->m_pModelRace->setQuery(QString("SELECT * FROM races WHERE id=%1").arg(race_id));
window->setCurrentRaceTitle(this->m_pModelRace->record(0).value("title").toString());
m_uiRaceState = CR_RACE_STATE_RUNNING;
m_iFastestLapTime = 0;
}
void CurrentRace::stopRace(){
m_uiRaceState = CR_RACE_STATE_NOT_RUNNING;
this->m_listPilots.clear();
emit raceFinished();
}
RacePilot* CurrentRace::getFastestPilot(){
RacePilot *fastestPilot = NULL;
for(int i = 0; i < this->m_listPilots.size(); i++){
RacePilot *pilot = this->m_listPilots.at(i);
if(fastestPilot == NULL && pilot != NULL && pilot->getFastedLap()){
fastestPilot = pilot;
}else if(pilot && fastestPilot){
if(fastestPilot->getFastedLap() && pilot->getFastedLap()){
if(fastestPilot->getFastedLap()->lapTime() > pilot->getFastedLap()->lapTime()){
fastestPilot = pilot;
}
}
}
}
if(fastestPilot){
if(m_iFastestLapTime == 0){
m_iFastestLapTime = fastestPilot->getFastedLap()->lapTime();
SFX::instance()->playFastestLap();
}else{
if(m_iFastestLapTime > fastestPilot->getFastedLap()->lapTime()){
m_iFastestLapTime = fastestPilot->getFastedLap()->lapTime();
SFX::instance()->playFastestLap();
}
}
}
return fastestPilot;
}
QList<RacePilot*>* CurrentRace::getPilotsList(){
return &m_listPilots;
}