| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2011 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "timing.hpp" |
| 17 | |
| 18 | #include "simple_logger.hpp" |
| 19 | |
| 20 | #include <QString> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | |
| 25 | Timing::Timing(size_t cars, QObject * parent) |
| 26 | : QObject { parent } |
| 27 | , m_times { cars, Timing::Times() } |
| 28 | , m_time { 0ms } |
| 29 | , m_started { false } |
| 30 | , m_lapRecord { -1 } |
| 31 | , m_raceRecord { -1 } |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void Timing::setLapCompleted(size_t index, bool isHuman) |
| 36 | { |
| 37 | Timing::Times & times = m_times.at(index); |
| 38 | times.lap++; |
| 39 | |
| 40 | times.lastLapTime = static_cast<int>(m_time.count() - times.raceTime.count()); |
| 41 | times.raceTime = m_time; |
| 42 | |
| 43 | juzzlin::L().debug() << "Lap (" << times.lap << ") completed for car index=" << index << ": " << times.lastLapTime; |
| 44 | juzzlin::L().debug() << "Current personal best time: " << times.recordLapTime; |
| 45 | |
| 46 | // Check if a new personal record achieved. |
| 47 | if (times.lastLapTime < times.recordLapTime || times.recordLapTime == -1) |
| 48 | { |
| 49 | times.recordLapTime = times.lastLapTime; |
| 50 | juzzlin::L().debug() << "New personal best time: " << times.recordLapTime; |
| 51 | } |
| 52 | |
| 53 | // Check if a new lap record achieved. |
| 54 | // Accept new lap records only by human players. |
| 55 | if (isHuman) |
| 56 | { |
| 57 | juzzlin::L().debug() << "Human lap completed: " << times.lastLapTime; |
| 58 | juzzlin::L().debug() << "Current lap record: " << m_lapRecord; |
| 59 | |
| 60 | if (times.lastLapTime < m_lapRecord || m_lapRecord == -1) |
| 61 | { |
| 62 | m_lapRecord = times.lastLapTime; |
| 63 | juzzlin::L().debug() << "New lap record: " << m_lapRecord; |
| 64 | |
| 65 | emit lapRecordAchieved(m_lapRecord); |
| 66 | } |
| 67 | |
| 68 | emit lapCompleted(index, times.lastLapTime); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void Timing::setRaceCompleted(size_t index, bool state, bool isHuman) |
| 73 | { |
| 74 | auto && times = m_times.at(index); |
| 75 | times.raceCompleted = state; |
| 76 | times.raceTime = m_time; |
| 77 | |
| 78 | if (isHuman) |
| 79 | { |
| 80 | if (times.raceTime.count() < m_raceRecord || m_raceRecord == -1) |
| 81 | { |
| 82 | m_raceRecord = static_cast<int>(times.raceTime.count()); |
| 83 | emit raceRecordAchieved(m_raceRecord); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | bool Timing::raceCompleted(size_t index) const |
| 89 | { |
| 90 | return m_times.at(index).raceCompleted; |
| 91 | } |
| 92 | |
| 93 | void Timing::setIsActive(size_t index, bool state) |
| 94 | { |
| 95 | m_times.at(index).isActive = state; |
| 96 | } |
| 97 | |
| 98 | bool Timing::isActive(size_t index) const |
| 99 | { |
| 100 | return m_times.at(index).isActive; |
| 101 | } |
| 102 | |
| 103 | size_t Timing::lap(size_t index) const |
| 104 | { |
| 105 | return m_times.at(index).lap; |
| 106 | } |
| 107 | |
| 108 | size_t Timing::leadersLap() const |
| 109 | { |
| 110 | if (m_times.empty()) |
| 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | return std::max_element( |
| 117 | m_times.begin(), m_times.end(), |
| 118 | [](auto const & a, auto const & b) { |
| 119 | return a.lap < b.lap; |
| 120 | }) |
| 121 | ->lap; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | int Timing::currentLapTime(size_t index) const |
| 126 | { |
| 127 | return !m_started ? 0 : static_cast<int>(m_time.count() - m_times.at(index).raceTime.count()); |
| 128 | } |
| 129 | |
| 130 | int Timing::recordLapTime(size_t index) const |
| 131 | { |
| 132 | return !m_started ? -1 : m_times.at(index).recordLapTime; |
| 133 | } |
| 134 | |
| 135 | std::chrono::milliseconds Timing::raceTime() const |
| 136 | { |
| 137 | return m_started ? m_time : 0ms; |
| 138 | } |
| 139 | |
| 140 | std::chrono::milliseconds Timing::raceTime(size_t index) const |
| 141 | { |
| 142 | return !m_times.at(index).raceCompleted ? raceTime() : m_times.at(index).raceTime; |
| 143 | } |
| 144 | |
| 145 | int Timing::recordRaceTime(size_t index) const |
| 146 | { |
| 147 | return !m_started ? -1 : m_times.at(index).recordRaceTime; |
| 148 | } |
| 149 | |
| 150 | int Timing::lapRecord() const |
| 151 | { |
| 152 | return m_lapRecord; |
| 153 | } |
| 154 | |
| 155 | void Timing::setLapRecord(int msecs) |
| 156 | { |
| 157 | m_lapRecord = msecs; |
| 158 | } |
| 159 | |
| 160 | int Timing::raceRecord() const |
| 161 | { |
| 162 | return m_raceRecord; |
| 163 | } |
| 164 | |
| 165 | void Timing::setRaceRecord(int msecs) |
| 166 | { |
| 167 | m_raceRecord = msecs; |
| 168 | } |
| 169 | |
| 170 | int Timing::lastLapTime(size_t index) const |
| 171 | { |
| 172 | return !m_started ? -1 : m_times.at(index).lastLapTime; |
| 173 | } |
| 174 | |
| 175 | void Timing::start() |
| 176 | { |
| 177 | m_time = 0ms; |
| 178 | m_started = true; |
| 179 | } |
| 180 | |
| 181 | void Timing::stop() |
| 182 | { |
| 183 | m_started = false; |
| 184 | } |
| 185 | |
| 186 | void Timing::reset() |
| 187 | { |
| 188 | m_time = 0ms; |
| 189 | m_started = false; |
| 190 | |
| 191 | for (Timing::Times & time : m_times) |
| 192 | { |
| 193 | time = Timing::Times(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void Timing::tick(std::chrono::milliseconds timeStep) |
| 198 | { |
| 199 | if (m_started) |
| 200 | { |
| 201 | m_time += timeStep; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | std::wstring Timing::msecsToString(int msec) |
| 206 | { |
| 207 | if (msec < 0) |
| 208 | { |
| 209 | return L"--:--.--"; |
| 210 | } |
| 211 | |
| 212 | const int hr = msec % 3600000; |
| 213 | const int mm = hr / 60000; |
| 214 | const int mr = hr % 60000; |
| 215 | const int ss = mr / 1000; |
| 216 | const int ms = mr % 1000; |
| 217 | |
| 218 | return QString {}.asprintf("%02d:%02d.%02d", mm, ss, ms / 10).toStdWString(); |
| 219 | } |