1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Fix timer synchornization

This commit is contained in:
nordsoft 2023-08-28 04:52:02 +04:00
parent 68b11dbe38
commit 21af69e0b7
2 changed files with 11 additions and 2 deletions

View File

@ -34,6 +34,7 @@ void TurnTimerHandler::onGameplayStart(PlayerColor player)
timers[player] = si->turnTimerInfo;
timers[player].turnTimer = 0;
timerEnabled[player] = true;
timerLastUpdate[player] = std::numeric_limits<int>::max();
}
}
@ -59,6 +60,7 @@ void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
ttu.player = player;
ttu.turnTimer = timers[player];
gameHandler.sendAndApply(&ttu);
timerLastUpdate[player] = 0;
}
}
}
@ -79,15 +81,17 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
if(timers[player].turnTimer > 0)
{
timers[player].turnTimer -= waitTime;
timerLastUpdate[player] += 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 / 100 * 100 % frequency == 0)
&& timerLastUpdate[player] >= frequency)
{
TurnTimeUpdate ttu;
ttu.player = state.color;
ttu.turnTimer = timers[player];
gameHandler.sendAndApply(&ttu);
timerLastUpdate[player] = 0;
}
}
else if(timers[player].baseTimer > 0)
@ -124,6 +128,7 @@ void TurnTimerHandler::onBattleStart()
ttu.player = i;
ttu.turnTimer = timers[i];
gameHandler.sendAndApply(&ttu);
timerLastUpdate[i] = 0;
}
}
}
@ -150,6 +155,7 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack)
ttu.player = player;
ttu.turnTimer = timers[player];
gameHandler.sendAndApply(&ttu);
timerLastUpdate[player] = 0;
}
void TurnTimerHandler::onBattleLoop(int waitTime)
@ -182,17 +188,19 @@ void TurnTimerHandler::onBattleLoop(int waitTime)
if(tTimer.creatureTimer > 0)
{
tTimer.creatureTimer -= waitTime;
timerLastUpdate[state.color] += waitTime;
int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
&& si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
&& (tTimer.creatureTimer / 100 * 100 % frequency) == 0)
&& timerLastUpdate[state.color] >= frequency)
{
TurnTimeUpdate ttu;
ttu.player = state.color;
ttu.turnTimer = tTimer;
gameHandler.sendAndApply(&ttu);
timerLastUpdate[state.color] = 0;
}
return true;
}

View File

@ -28,6 +28,7 @@ class TurnTimerHandler
const int turnTimePropagateThreshold = 3000;
std::map<PlayerColor, TurnTimerInfo> timers;
std::map<PlayerColor, bool> timerEnabled;
std::map<PlayerColor, int> timerLastUpdate;
std::recursive_mutex mx;
public: