1 #include "pch.h"
2 #include "timer.h"
3 #include "unittest.h"
4
5
Load(const std::string & trackrecordspath,float stagingtime)6 bool TIMER::Load(const std::string & trackrecordspath, float stagingtime)
7 {
8 Unload();
9
10 car.clear();
11
12 pretime = stagingtime;
13
14 trackrecordsfile = trackrecordspath;
15
16 trackrecords.Load(trackrecordsfile);
17
18 loaded = true;
19
20 return true;
21 }
22
Unload()23 void TIMER::Unload()
24 {
25 if (loaded)
26 {
27 trackrecords.Write(true, trackrecordsfile);
28 //std::cout << "Writing records to: " << trackrecordsfile << endl;
29 }
30 trackrecords.Clear();
31 loaded = false;
32
33 netw_lap = 1; // reset, expect 1st lap
34 }
35
Tick(float dt)36 void TIMER::Tick(float dt)
37 {
38 if (pretime > 0.f && !waiting)
39 { pretime -= dt;
40 dt = 0.f;
41 }
42 if (waiting)
43 dt = 0.f;
44
45 for (std::vector <LAPINFO>::iterator i = car.begin(); i != car.end(); ++i)
46 i->Tick(dt);
47 }
48
Lap(const int carId,const bool countit,bool bTrackReverse)49 bool TIMER::Lap(const int carId, const bool countit, bool bTrackReverse)
50 {
51 //assert(carId < car.size());
52 if (carId >= car.size()) return false; //-
53 bool newbest = false; // new lap best time
54
55 if (countit)
56 {
57 std::stringstream secstr;
58 secstr << "sector 0";
59 std::string lastcar;
60 /*if (trackrecords.GetParam("last.car", lastcar))
61 {
62 if (lastcar != car[carId].GetCarType()) //clear last lap time
63 trackrecords.SetParam("last.sector 0", (float)0.0);
64 }*/
65 trackrecords.SetParam(std::string(bTrackReverse ? "rev_" : "") + "last." + secstr.str(), (float) car[carId].GetTime());
66 trackrecords.SetParam(std::string(bTrackReverse ? "rev_" : "") + "last.car", car[carId].GetCarType());
67
68 float prevbest = 0;
69 bool haveprevbest = trackrecords.GetParam(
70 car[carId].GetCarType() + (bTrackReverse ? "_rev" : "") + "." + secstr.str(), prevbest);
71 if (car[carId].GetTime() < prevbest || !haveprevbest)
72 {
73 trackrecords.SetParam(
74 car[carId].GetCarType() + (bTrackReverse ? "_rev" : "") + "." + secstr.str(), (float) car[carId].GetTime());
75 newbest = true;
76 }
77 }
78
79 car[carId].Lap(countit);
80 if (loaded)
81 trackrecords.Write(true, trackrecordsfile);
82
83 return newbest;
84 }
85
86
LapNetworkTime(const int carId,int lap,const double curtime)87 bool TIMER::LapNetworkTime(const int carId, int lap, const double curtime)
88 {
89 //if (lap == netw_lap) // deny same lap..
90 if (curtime > 4.f) // sec
91 {
92 car[carId].LapWithTime(true, curtime);
93
94 ++netw_lap; // allow only once per lap
95 return true;
96 }
97 return false;
98 }
99