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

Code review fixes

This commit is contained in:
nordsoft 2023-08-29 15:48:42 +04:00
parent c8243313b8
commit 03496e6738
3 changed files with 27 additions and 22 deletions

View File

@ -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<std::chrono::milliseconds>(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));
}
}

View File

@ -28,9 +28,9 @@ TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
void TurnTimerHandler::onGameplayStart(PlayerColor player)
{
std::lock_guard<std::recursive_mutex> guard(mx);
if(const auto * si = gameHandler.getStartInfo())
{
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> guard(mx);
if(const auto * si = gameHandler.getStartInfo())
{
if(si->turnTimerInfo.isEnabled())
{
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || gs->curB || !si->turnTimerInfo.isEnabled())
return;
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
return;
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
return;
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
return;
std::lock_guard<std::recursive_mutex> guard(mx);
ui8 side = 0;
const CStack * stack = nullptr;
bool isTactisPhase = gs->curB.get()->battleTacticDist() > 0;

View File

@ -31,14 +31,16 @@ class TurnTimerHandler
std::map<PlayerColor, int> 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);
};