From 08256ab0c77d11c076b96057c937275838db12e3 Mon Sep 17 00:00:00 2001 From: jadebenn Date: Sun, 8 Dec 2024 15:05:43 -0600 Subject: [PATCH] use steady_clock race timer --- dNet/dServer.h | 4 +++ .../02_server/Map/AG/NpcAgCourseStarter.cpp | 25 +++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/dNet/dServer.h b/dNet/dServer.h index f958fc408..5add696e1 100644 --- a/dNet/dServer.h +++ b/dNet/dServer.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include "RakPeerInterface.h" #include "ReplicaManager.h" @@ -80,6 +81,8 @@ class dServer { const ServerType GetServerType() const { return mServerType; } + [[nodiscard]] std::chrono::time_point GetStartTime() const { return m_startTime; } + private: bool Startup(); void Shutdown(); @@ -114,4 +117,5 @@ class dServer { SystemAddress mMasterSystemAddress; std::string mMasterIP; int mMasterPort; + std::chrono::time_point m_startTime = std::chrono::steady_clock::now(); }; diff --git a/dScripts/02_server/Map/AG/NpcAgCourseStarter.cpp b/dScripts/02_server/Map/AG/NpcAgCourseStarter.cpp index 515603a0f..b6ed242c4 100644 --- a/dScripts/02_server/Map/AG/NpcAgCourseStarter.cpp +++ b/dScripts/02_server/Map/AG/NpcAgCourseStarter.cpp @@ -7,6 +7,8 @@ #include "eMissionState.h" #include "MissionComponent.h" #include +#include +#include "dServer.h" void NpcAgCourseStarter::OnStartup(Entity* self) { @@ -47,12 +49,13 @@ void NpcAgCourseStarter::OnMessageBoxResponse(Entity* self, Entity* sender, int3 GameMessages::SendActivityStart(self->GetObjectID(), sender->GetSystemAddress()); auto* data = scriptedActivityComponent->AddActivityPlayerData(sender->GetObjectID()); - if (data->values[1] != 0) return; - time_t startTime = std::time(0) + 4; // Offset for starting timer - - data->values[1] = *reinterpret_cast(&startTime); + const auto raceStartTime = std::chrono::steady_clock::now() - Game::server->GetStartTime() + + std::chrono::seconds(4); // Offset for starting timer + const auto fRaceStartTime = std::chrono::duration>(raceStartTime).count(); + data->values[1] = fRaceStartTime; + LOG_DEBUG("Race started at: %0.f s", fRaceStartTime); Game::entityManager->SerializeEntity(self); } else if (identifier == u"FootRaceCancel") { @@ -80,25 +83,27 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std LWOOBJID_EMPTY, "", sender->GetSystemAddress()); scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID()); } else if (args == "course_finish") { - time_t endTime = std::time(0); - time_t finish = (endTime - *reinterpret_cast(&data->values[1])); + const auto raceEndTime = std::chrono::steady_clock::now() - Game::server->GetStartTime(); + const auto fRaceEndTime = std::chrono::duration>(raceEndTime).count(); + const auto raceTimeElapsed = fRaceEndTime - data->values[1]; - data->values[2] = *reinterpret_cast(&finish); + data->values[2] = raceTimeElapsed; + LOG_DEBUG("Race time elapsed: %0.f s", raceTimeElapsed); auto* missionComponent = sender->GetComponent(); if (missionComponent != nullptr) { missionComponent->ForceProgressTaskType(1884, 1, 1, false); - missionComponent->Progress(eMissionTaskType::PERFORM_ACTIVITY, -finish, self->GetObjectID(), + missionComponent->Progress(eMissionTaskType::PERFORM_ACTIVITY, -raceTimeElapsed, self->GetObjectID(), "performact_time"); } Game::entityManager->SerializeEntity(self); - LeaderboardManager::SaveScore(sender->GetObjectID(), scriptedActivityComponent->GetActivityID(), static_cast(finish)); + LeaderboardManager::SaveScore(sender->GetObjectID(), scriptedActivityComponent->GetActivityID(), raceTimeElapsed); GameMessages::SendNotifyClientObject(self->GetObjectID(), u"ToggleLeaderBoard", scriptedActivityComponent->GetActivityID(), 0, sender->GetObjectID(), "", sender->GetSystemAddress()); - GameMessages::SendNotifyClientObject(self->GetObjectID(), u"stop_timer", 1, finish, LWOOBJID_EMPTY, "", + GameMessages::SendNotifyClientObject(self->GetObjectID(), u"stop_timer", 1, raceTimeElapsed, LWOOBJID_EMPTY, "", sender->GetSystemAddress()); scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID());