feat: add millisecond tracking for racing (#1615)

* add millisecond tracking

Update RacingControlComponent.cpp

* remove const ig?

* is this what you wanted
This commit is contained in:
David Markowitz 2024-10-26 20:59:50 -07:00 committed by GitHub
parent 33a8efdd22
commit 102e3556cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 25 deletions

View File

@ -45,7 +45,6 @@ RacingControlComponent::RacingControlComponent(Entity* parent)
m_LoadedPlayers = 0; m_LoadedPlayers = 0;
m_LoadTimer = 0; m_LoadTimer = 0;
m_Finished = 0; m_Finished = 0;
m_StartTime = 0;
m_EmptyTimer = 0; m_EmptyTimer = 0;
m_SoloRacing = Game::config->GetValue("solo_racing") == "1"; m_SoloRacing = Game::config->GetValue("solo_racing") == "1";
@ -427,9 +426,9 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
outBitStream.Write(player.playerID); outBitStream.Write(player.playerID);
outBitStream.Write(player.data[0]); outBitStream.Write(player.data[0]);
if (player.finished != 0) outBitStream.Write<float>(player.raceTime); if (player.finished != 0) outBitStream.Write<float>(player.raceTime.count() / 1000.0f);
else outBitStream.Write(player.data[1]); else outBitStream.Write(player.data[1]);
if (player.finished != 0) outBitStream.Write<float>(player.bestLapTime); if (player.finished != 0) outBitStream.Write<float>(player.bestLapTime.count() / 1000.0f);
else outBitStream.Write(player.data[2]); else outBitStream.Write(player.data[2]);
if (player.finished == 1) outBitStream.Write<float>(1.0f); if (player.finished == 1) outBitStream.Write<float>(1.0f);
else outBitStream.Write(player.data[3]); else outBitStream.Write(player.data[3]);
@ -490,8 +489,8 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
if (player.finished == 0) continue; if (player.finished == 0) continue;
outBitStream.Write1(); // Has more data outBitStream.Write1(); // Has more data
outBitStream.Write(player.playerID); outBitStream.Write(player.playerID);
outBitStream.Write<float>(player.bestLapTime); outBitStream.Write<float>(player.bestLapTime.count() / 1000.0f);
outBitStream.Write<float>(player.raceTime); outBitStream.Write<float>(player.raceTime.count() / 1000.0f);
} }
outBitStream.Write0(); // No more data outBitStream.Write0(); // No more data
@ -721,7 +720,7 @@ void RacingControlComponent::Update(float deltaTime) {
Game::entityManager->SerializeEntity(m_Parent); Game::entityManager->SerializeEntity(m_Parent);
m_StartTime = std::time(nullptr); m_StartTime = std::chrono::high_resolution_clock::now();
} }
m_StartTimer += deltaTime; m_StartTimer += deltaTime;
@ -810,46 +809,45 @@ void RacingControlComponent::Update(float deltaTime) {
// Reached the start point, lapped // Reached the start point, lapped
if (respawnIndex == 0) { if (respawnIndex == 0) {
time_t lapTime = std::time(nullptr) - (player.lap == 0 ? m_StartTime : player.lapTime); const auto now = std::chrono::high_resolution_clock::now();
const auto lapTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - (player.lap == 0 ? m_StartTime : player.lapTime));
// Cheating check // Cheating check
if (lapTime < 40) { if (lapTime.count() < 40000) {
continue; continue;
} }
player.lap++; player.lapTime = now;
player.lapTime = std::time(nullptr); if (player.bestLapTime > lapTime || player.lap == 0) {
if (player.bestLapTime == 0 || player.bestLapTime > lapTime) {
player.bestLapTime = lapTime; player.bestLapTime = lapTime;
LOG("Best lap time (%llu)", lapTime); LOG("Best lap time (%llu)", lapTime);
} }
player.lap++;
auto* missionComponent = auto* missionComponent =
playerEntity->GetComponent<MissionComponent>(); playerEntity->GetComponent<MissionComponent>();
if (missionComponent != nullptr) { if (missionComponent != nullptr) {
// Progress lap time tasks // Progress lap time tasks
missionComponent->Progress(eMissionTaskType::RACING, (lapTime) * 1000, static_cast<LWOOBJID>(eRacingTaskParam::LAP_TIME)); missionComponent->Progress(eMissionTaskType::RACING, lapTime.count(), static_cast<LWOOBJID>(eRacingTaskParam::LAP_TIME));
if (player.lap == 3) { if (player.lap == 3) {
m_Finished++; m_Finished++;
player.finished = m_Finished; player.finished = m_Finished;
const auto raceTime = const auto raceTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_StartTime);
(std::time(nullptr) - m_StartTime);
player.raceTime = raceTime; player.raceTime = raceTime;
LOG("Completed time %llu, %llu", LOG("Completed time %llums %fs", raceTime.count(), raceTime.count() / 1000.0f);
raceTime, raceTime * 1000);
LeaderboardManager::SaveScore(playerEntity->GetObjectID(), m_ActivityID, static_cast<float>(player.raceTime), static_cast<float>(player.bestLapTime), static_cast<float>(player.finished == 1)); LeaderboardManager::SaveScore(playerEntity->GetObjectID(), m_ActivityID, static_cast<float>(player.raceTime.count()) / 1000, static_cast<float>(player.bestLapTime.count()) / 1000, static_cast<float>(player.finished == 1));
// Entire race time // Entire race time
missionComponent->Progress(eMissionTaskType::RACING, (raceTime) * 1000, static_cast<LWOOBJID>(eRacingTaskParam::TOTAL_TRACK_TIME)); missionComponent->Progress(eMissionTaskType::RACING, player.raceTime.count(), static_cast<LWOOBJID>(eRacingTaskParam::TOTAL_TRACK_TIME));
missionComponent->Progress(eMissionTaskType::RACING, 0, static_cast<LWOOBJID>(eRacingTaskParam::COMPETED_IN_RACE)); // Progress task for competing in a race missionComponent->Progress(eMissionTaskType::RACING, 0, static_cast<LWOOBJID>(eRacingTaskParam::COMPETED_IN_RACE)); // Progress task for competing in a race
missionComponent->Progress(eMissionTaskType::RACING, player.smashedTimes, static_cast<LWOOBJID>(eRacingTaskParam::SAFE_DRIVER)); // Finish a race without being smashed. missionComponent->Progress(eMissionTaskType::RACING, player.smashedTimes, static_cast<LWOOBJID>(eRacingTaskParam::SAFE_DRIVER)); // Finish a race without being smashed.
@ -873,8 +871,8 @@ void RacingControlComponent::Update(float deltaTime) {
} }
} }
LOG("Lapped (%i) in (%llu)", player.lap, LOG("Lapped (%i) in (%llums %fs)", player.lap,
lapTime); lapTime.count(), lapTime.count() / 1000.0f);
} }
LOG("Reached point (%i)/(%i)", player.respawnIndex, LOG("Reached point (%i)/(%i)", player.respawnIndex,

View File

@ -8,6 +8,7 @@
#include "Entity.h" #include "Entity.h"
#include "Component.h" #include "Component.h"
#include "eReplicaComponentType.h" #include "eReplicaComponentType.h"
#include <chrono>
/** /**
* Information for each player in the race * Information for each player in the race
@ -72,12 +73,12 @@ struct RacingPlayerInfo {
/** /**
* The fastest lap time of the player * The fastest lap time of the player
*/ */
time_t bestLapTime = 0; std::chrono::milliseconds bestLapTime;
/** /**
* The current lap time of the player * The current lap time of the player
*/ */
time_t lapTime = 0; std::chrono::high_resolution_clock::time_point lapTime;
/** /**
* The number of times this player smashed their car * The number of times this player smashed their car
@ -97,7 +98,7 @@ struct RacingPlayerInfo {
/** /**
* Unused * Unused
*/ */
time_t raceTime = 0; std::chrono::milliseconds raceTime;
}; };
/** /**
@ -231,7 +232,7 @@ private:
/** /**
* The time the race was started * The time the race was started
*/ */
time_t m_StartTime; std::chrono::high_resolution_clock::time_point m_StartTime;
/** /**
* Timer for tracking how long a player was alone in this race * Timer for tracking how long a player was alone in this race