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

Move turn timer logic from GameHandler class

This commit is contained in:
nordsoft 2023-08-14 03:20:27 +04:00
parent 3c9c302fd2
commit e414af221b
5 changed files with 132 additions and 28 deletions

View File

@ -1570,6 +1570,7 @@ CGameHandler::CGameHandler(CVCMIServer * lobby)
, complainNoCreatures("No creatures to split")
, complainNotEnoughCreatures("Cannot split that stack, not enough creatures!")
, complainInvalidSlot("Invalid slot accessed!")
, turnTimerHandler(*this)
{
QID = 1;
IObjectInterface::cb = this;
@ -2019,7 +2020,11 @@ void CGameHandler::run(bool resume)
events::GameResumed::defaultExecute(serverEventBus.get());
auto playerTurnOrder = generatePlayerTurnOrder();
if(!resume)
for(auto & playerColor : playerTurnOrder)
turnTimerHandler.onGameplayStart(gs->players[playerColor]);
while(lobby->state == EServerState::GAMEPLAY)
{
if(!resume)
@ -2068,13 +2073,7 @@ void CGameHandler::run(bool resume)
yt.daysWithoutCastle = playerState->daysWithoutCastle;
applyAndSend(&yt);
if(gs->getStartInfo()->turnTimerInfo.turnTimer > 0) //turn timer check
{
TurnTimeUpdate ttu;
ttu.player = player;
ttu.turnTimer = gs->getStartInfo()->turnTimerInfo;
applyAndSend(&ttu);
}
turnTimerHandler.onPlayerGetTurn(gs->players[player]);
}
};
@ -2084,29 +2083,10 @@ void CGameHandler::run(bool resume)
{
//wait till turn is done
const int waitTime = 100; //ms
int turnTimePropagateFrequency = 5000; //do not send updates too frequently
boost::unique_lock<boost::mutex> lock(states.mx);
while(states.players.at(playerColor).makingTurn && lobby->state == EServerState::GAMEPLAY)
{
if(gs->getStartInfo()->turnTimerInfo.isEnabled() && !gs->curB) //turn timer check
{
if(gs->players[playerColor].turnTimer.turnTimer > 0)
{
gs->players[playerColor].turnTimer.turnTimer -= waitTime;
if(gs->players[playerColor].status == EPlayerStatus::INGAME //do not send message if player is not active already
&& gs->players[playerColor].turnTimer.turnTimer % turnTimePropagateFrequency == 0)
{
TurnTimeUpdate ttu;
ttu.player = playerColor;
ttu.turnTimer = gs->players[playerColor].turnTimer;
applyAndSend(&ttu);
}
}
else if(!queries.topQuery(playerColor)) //wait for replies to avoid pending queries
states.players.at(playerColor).makingTurn = false; //force end turn
}
turnTimerHandler.onPlayerMakingTurn(gs->players[playerColor], waitTime);
static time_duration p = milliseconds(waitTime);
states.cv.timed_wait(lock, p);
}

View File

@ -17,6 +17,7 @@
#include "../lib/battle/BattleAction.h"
#include "../lib/ScriptHandler.h"
#include "CQuery.h"
#include "TurnTimerHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -100,6 +101,8 @@ class CGameHandler : public IGameCallback, public CBattleInfoCallback, public En
CVCMIServer * lobby;
std::shared_ptr<CApplier<CBaseForGHApply>> applier;
std::unique_ptr<boost::thread> battleThread;
TurnTimerHandler turnTimerHandler;
public:
std::unique_ptr<HeroPoolProcessor> heroPool;

View File

@ -9,6 +9,7 @@ set(server_SRCS
CVCMIServer.cpp
NetPacksServer.cpp
NetPacksLobbyServer.cpp
TurnTimerHandler.cpp
)
set(server_HEADERS
@ -22,6 +23,7 @@ set(server_HEADERS
CVCMIServer.h
LobbyNetPackVisitors.h
ServerNetPackVisitors.h
TurnTimerHandler.h
)
assign_source_group(${server_SRCS} ${server_HEADERS})

View File

@ -0,0 +1,86 @@
/*
* TurnTimerHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "TurnTimerHandler.h"
#include "CGameHandler.h"
#include "../lib/CPlayerState.h"
#include "../lib/gameState/CGameState.h"
#include "../lib/StartInfo.h"
TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
gameHandler(gh)
{
}
void TurnTimerHandler::onGameplayStart(PlayerState & state)
{
if(const auto * si = gameHandler.getStartInfo())
{
if(si->turnTimerInfo.isEnabled())
{
TurnTimeUpdate ttu;
ttu.player = state.color;
ttu.turnTimer = si->turnTimerInfo;
ttu.turnTimer.turnTimer = 0;
gameHandler.applyAndSend(&ttu);
}
}
}
void TurnTimerHandler::onPlayerGetTurn(PlayerState & state)
{
if(const auto * si = gameHandler.getStartInfo())
{
if(si->turnTimerInfo.isEnabled())
{
state.turnTimer.baseTimer += state.turnTimer.turnTimer;
state.turnTimer.turnTimer = si->turnTimerInfo.turnTimer;
TurnTimeUpdate ttu;
ttu.player = state.color;
ttu.turnTimer = state.turnTimer;
gameHandler.applyAndSend(&ttu);
}
}
}
void TurnTimerHandler::onPlayerMakingTurn(PlayerState & state, int waitTime)
{
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs)
return;
if(si->turnTimerInfo.isEnabled() && !gs->curB)
{
if(state.turnTimer.turnTimer > 0)
{
state.turnTimer.turnTimer -= waitTime;
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
&& state.turnTimer.turnTimer % turnTimePropagateFrequency == 0)
{
TurnTimeUpdate ttu;
ttu.player = state.color;
ttu.turnTimer = state.turnTimer;
gameHandler.applyAndSend(&ttu);
}
}
else if(state.turnTimer.baseTimer > 0)
{
state.turnTimer.turnTimer = state.turnTimer.baseTimer;
state.turnTimer.baseTimer = 0;
onPlayerMakingTurn(state, waitTime);
}
else if(!gameHandler.queries.topQuery(state.color)) //wait for replies to avoid pending queries
gameHandler.states.players.at(state.color).makingTurn = false; //force end turn
}
}

33
server/TurnTimerHandler.h Normal file
View File

@ -0,0 +1,33 @@
/*
* TurnTimerHandler.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
class PlayerColor;
struct PlayerState;
VCMI_LIB_NAMESPACE_END
class CGameHandler;
class TurnTimerHandler
{
CGameHandler & gameHandler;
const int turnTimePropagateFrequency = 5000;
public:
TurnTimerHandler(CGameHandler &);
void onGameplayStart(PlayerState & state);
void onPlayerGetTurn(PlayerState & state);
void onPlayerMakingTurn(PlayerState & state, int waitTime);
};