diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index b97ef40e3..cbe6505b5 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -999,20 +999,13 @@ void CGameHandler::run(bool resume) turnOrder->onGameStarted(); //wait till game is done - auto clockLast = std::chrono::high_resolution_clock::now(); + auto clockLast = std::chrono::steady_clock::now(); while(lobby->getState() == EServerState::GAMEPLAY) { - const auto clockDuration = std::chrono::high_resolution_clock::now() - clockLast; + const auto clockDuration = std::chrono::steady_clock::now() - clockLast; const int timePassed = std::chrono::duration_cast(clockDuration).count(); clockLast += clockDuration; - - for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player) - if(gs->isPlayerMakingTurn(player)) - turnTimerHandler.onPlayerMakingTurn(player, timePassed); - - if(gs->curB) - turnTimerHandler.onBattleLoop(timePassed); - + turnTimerHandler.update(timePassed); boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } } diff --git a/server/TurnTimerHandler.cpp b/server/TurnTimerHandler.cpp index 495aca009..6020de0be 100644 --- a/server/TurnTimerHandler.cpp +++ b/server/TurnTimerHandler.cpp @@ -28,9 +28,9 @@ TurnTimerHandler::TurnTimerHandler(CGameHandler & gh): void TurnTimerHandler::onGameplayStart(PlayerColor player) { + std::lock_guard guard(mx); if(const auto * si = gameHandler.getStartInfo()) { - std::lock_guard guard(mx); timers[player] = si->turnTimerInfo; timers[player].turnTimer = 0; timerEnabled[player] = true; @@ -47,11 +47,11 @@ void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled) void TurnTimerHandler::onPlayerGetTurn(PlayerColor player) { + std::lock_guard guard(mx); if(const auto * si = gameHandler.getStartInfo()) { if(si->turnTimerInfo.isEnabled()) { - std::lock_guard guard(mx); if(si->turnTimerInfo.baseTimer > 0) timers[player].baseTimer += timers[player].turnTimer; timers[player].turnTimer = si->turnTimerInfo.turnTimer; @@ -65,15 +65,28 @@ void TurnTimerHandler::onPlayerGetTurn(PlayerColor player) } } +void TurnTimerHandler::update(int waitTime) +{ + std::lock_guard guard(mx); + if(const auto * gs = gameHandler.gameState()) + { + for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player) + if(gs->isPlayerMakingTurn(player)) + onPlayerMakingTurn(player, waitTime); + + if(gs->curB) + onBattleLoop(waitTime); + } +} + void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime) { + std::lock_guard guard(mx); const auto * gs = gameHandler.gameState(); const auto * si = gameHandler.getStartInfo(); if(!si || !gs || gs->curB || !si->turnTimerInfo.isEnabled()) return; - std::lock_guard guard(mx); - auto & state = gs->players.at(player); if(state.human && timerEnabled[player]) @@ -107,12 +120,11 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime) void TurnTimerHandler::onBattleStart() { + std::lock_guard guard(mx); const auto * gs = gameHandler.gameState(); const auto * si = gameHandler.getStartInfo(); if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled()) return; - - std::lock_guard guard(mx); auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER); auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER); @@ -135,13 +147,12 @@ void TurnTimerHandler::onBattleStart() void TurnTimerHandler::onBattleNextStack(const CStack & stack) { + std::lock_guard guard(mx); const auto * gs = gameHandler.gameState(); const auto * si = gameHandler.getStartInfo(); if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled()) return; - std::lock_guard guard(mx); - auto player = stack.getOwner(); if(!player.isValidPlayer()) @@ -160,13 +171,12 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack) void TurnTimerHandler::onBattleLoop(int waitTime) { + std::lock_guard guard(mx); const auto * gs = gameHandler.gameState(); const auto * si = gameHandler.getStartInfo(); if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled()) return; - std::lock_guard guard(mx); - ui8 side = 0; const CStack * stack = nullptr; bool isTactisPhase = gs->curB.get()->battleTacticDist() > 0; diff --git a/server/TurnTimerHandler.h b/server/TurnTimerHandler.h index 367898325..572831da3 100644 --- a/server/TurnTimerHandler.h +++ b/server/TurnTimerHandler.h @@ -31,14 +31,16 @@ class TurnTimerHandler std::map timerLastUpdate; std::recursive_mutex mx; + void onPlayerMakingTurn(PlayerColor player, int waitTime); + void onBattleLoop(int waitTime); + public: TurnTimerHandler(CGameHandler &); void onGameplayStart(PlayerColor player); void onPlayerGetTurn(PlayerColor player); - void onPlayerMakingTurn(PlayerColor player, int waitTime); void onBattleStart(); void onBattleNextStack(const CStack & stack); - void onBattleLoop(int waitTime); + void update(int waitTime); void setTimerEnabled(PlayerColor player, bool enabled); };