From 2c61d1b23f628d89e2c6a44a4c878e21f11749c2 Mon Sep 17 00:00:00 2001 From: nordsoft Date: Mon, 28 Aug 2023 03:14:58 +0400 Subject: [PATCH] Use precision clocks for timer --- server/CGameHandler.cpp | 11 ++++++++--- server/TurnTimerHandler.cpp | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 4e8d1b62e..b7371bd4f 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -999,18 +999,23 @@ void CGameHandler::run(bool resume) turnOrder->onGameStarted(); //wait till game is done + auto clockLast = std::chrono::high_resolution_clock::now(); while(lobby->getState() == EServerState::GAMEPLAY) { + const auto clockDuration = std::chrono::high_resolution_clock::now() - clockLast; + const int timePassed = std::chrono::duration_cast(clockDuration).count(); + clockLast += clockDuration; + const int waitTime = 100; //ms for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player) if(gs->isPlayerMakingTurn(player)) - turnTimerHandler.onPlayerMakingTurn(player, waitTime); + turnTimerHandler.onPlayerMakingTurn(player, timePassed); if(gs->curB) - turnTimerHandler.onBattleLoop(waitTime); + turnTimerHandler.onBattleLoop(timePassed); - boost::this_thread::sleep_for(boost::chrono::milliseconds(waitTime)); + boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } } diff --git a/server/TurnTimerHandler.cpp b/server/TurnTimerHandler.cpp index b5d86827b..6db8dbb17 100644 --- a/server/TurnTimerHandler.cpp +++ b/server/TurnTimerHandler.cpp @@ -72,7 +72,7 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime) int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit); if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already - && timers[player].turnTimer % frequency == 0) + && timers[player].turnTimer / 100 * 100 % frequency == 0) { TurnTimeUpdate ttu; ttu.player = state.color; @@ -132,7 +132,7 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack) if(!player.isValidPlayer()) return; - if(timers[player].battleTimer < si->turnTimerInfo.battleTimer) + if(timers[player].battleTimer == 0) timers[player].battleTimer = timers[player].creatureTimer; timers[player].creatureTimer = si->turnTimerInfo.creatureTimer; @@ -177,7 +177,7 @@ void TurnTimerHandler::onBattleLoop(int waitTime) ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit; if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already - && tTimer.creatureTimer % frequency == 0) + && (tTimer.creatureTimer / 100 * 100 % frequency) == 0) { TurnTimeUpdate ttu; ttu.player = state.color;