Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add millisecond tracking for racing #1615

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions dGame/dComponents/RacingControlComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ RacingControlComponent::RacingControlComponent(Entity* parent)
m_LoadedPlayers = 0;
m_LoadTimer = 0;
m_Finished = 0;
m_StartTime = 0;
m_EmptyTimer = 0;
m_SoloRacing = Game::config->GetValue("solo_racing") == "1";

Expand Down Expand Up @@ -446,9 +445,9 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
outBitStream.Write(player.playerID);

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]);
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]);
if (player.finished == 1) outBitStream.Write<float>(1.0f);
else outBitStream.Write(player.data[3]);
Expand Down Expand Up @@ -509,8 +508,8 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
if (player.finished == 0) continue;
outBitStream.Write1(); // Has more data
outBitStream.Write(player.playerID);
outBitStream.Write<float>(player.bestLapTime);
outBitStream.Write<float>(player.raceTime);
outBitStream.Write<float>(player.bestLapTime.count() / 1000.0f);
outBitStream.Write<float>(player.raceTime.count() / 1000.0f);
}

outBitStream.Write0(); // No more data
Expand Down Expand Up @@ -740,7 +739,7 @@ void RacingControlComponent::Update(float deltaTime) {

Game::entityManager->SerializeEntity(m_Parent);

m_StartTime = std::time(nullptr);
m_StartTime = std::chrono::high_resolution_clock::now();
}

m_StartTimer += deltaTime;
Expand Down Expand Up @@ -829,46 +828,45 @@ void RacingControlComponent::Update(float deltaTime) {

// Reached the start point, lapped
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
if (lapTime < 40) {
if (lapTime.count() < 40000) {
continue;
}

player.lap++;

player.lapTime = std::time(nullptr);
player.lapTime = now;

if (player.bestLapTime == 0 || player.bestLapTime > lapTime) {
if (player.bestLapTime > lapTime || player.lap == 0) {
player.bestLapTime = lapTime;

LOG("Best lap time (%llu)", lapTime);
}

player.lap++;

auto* missionComponent =
playerEntity->GetComponent<MissionComponent>();

if (missionComponent != nullptr) {

// 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) {
m_Finished++;
player.finished = m_Finished;

const auto raceTime =
(std::time(nullptr) - m_StartTime);
const auto raceTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_StartTime);

player.raceTime = raceTime;

LOG("Completed time %llu, %llu",
raceTime, raceTime * 1000);
LOG("Completed time %llums %fs", raceTime.count(), raceTime.count() / 1000.0f);

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
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));

auto* characterComponent = playerEntity->GetComponent<CharacterComponent>();
if (characterComponent != nullptr) {
Expand All @@ -877,8 +875,8 @@ void RacingControlComponent::Update(float deltaTime) {
}
}

LOG("Lapped (%i) in (%llu)", player.lap,
lapTime);
LOG("Lapped (%i) in (%llums %fs)", player.lap,
lapTime.count(), lapTime.count() / 1000.0f);
}

LOG("Reached point (%i)/(%i)", player.respawnIndex,
Expand Down
9 changes: 5 additions & 4 deletions dGame/dComponents/RacingControlComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Entity.h"
#include "Component.h"
#include "eReplicaComponentType.h"
#include <chrono>

/**
* Information for each player in the race
Expand Down Expand Up @@ -72,12 +73,12 @@ struct RacingPlayerInfo {
/**
* The fastest lap time of the player
*/
time_t bestLapTime = 0;
std::chrono::milliseconds bestLapTime;

/**
* 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
Expand All @@ -97,7 +98,7 @@ struct RacingPlayerInfo {
/**
* Unused
*/
time_t raceTime = 0;
std::chrono::milliseconds raceTime;
};

/**
Expand Down Expand Up @@ -231,7 +232,7 @@ class RacingControlComponent final : public Component {
/**
* 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
Expand Down
Loading