1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Extended timer info to exhange between client and server

This commit is contained in:
nordsoft 2023-08-30 03:11:46 +04:00
parent f30f00faa0
commit 9a42abe2a7
6 changed files with 67 additions and 83 deletions

View File

@ -140,7 +140,6 @@ CPlayerInterface::CPlayerInterface(PlayerColor Player):
firstCall = 1; //if loading will be overwritten in serialize firstCall = 1; //if loading will be overwritten in serialize
autosaveCount = 0; autosaveCount = 0;
isAutoFightOn = false; isAutoFightOn = false;
timerEnabled = true;
duringMovement = false; duringMovement = false;
ignoreEvents = false; ignoreEvents = false;
numOfMovedArts = 0; numOfMovedArts = 0;
@ -272,8 +271,6 @@ void CPlayerInterface::yourTurn(QueryID queryID)
makingTurn = true; makingTurn = true;
adventureInt->onPlayerTurnStarted(playerID); adventureInt->onPlayerTurnStarted(playerID);
} }
timerEnabled = false;
} }
acceptTurn(queryID); acceptTurn(queryID);
} }
@ -326,7 +323,6 @@ void CPlayerInterface::acceptTurn(QueryID queryID)
} }
cb->selectionMade(0, queryID); cb->selectionMade(0, queryID);
timerEnabled = true;
} }
void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose) void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose)
@ -2127,8 +2123,3 @@ std::optional<BattleAction> CPlayerInterface::makeSurrenderRetreatDecision(const
{ {
return std::nullopt; return std::nullopt;
} }
bool CPlayerInterface::isTimerEnabled() const
{
return timerEnabled;
}

View File

@ -57,7 +57,6 @@ namespace boost
/// Central class for managing user interface logic /// Central class for managing user interface logic
class CPlayerInterface : public CGameInterface, public IUpdateable class CPlayerInterface : public CGameInterface, public IUpdateable
{ {
bool timerEnabled;
bool duringMovement; bool duringMovement;
bool ignoreEvents; bool ignoreEvents;
size_t numOfMovedArts; size_t numOfMovedArts;
@ -208,8 +207,6 @@ public: // public interface for use by client via LOCPLINT access
///returns true if all events are processed internally ///returns true if all events are processed internally
bool capturedAllEvents(); bool capturedAllEvents();
bool isTimerEnabled() const;
CPlayerInterface(PlayerColor Player); CPlayerInterface(PlayerColor Player);
~CPlayerInterface(); ~CPlayerInterface();

View File

@ -102,22 +102,27 @@ void TurnTimerWidget::tick(uint32_t msPassed)
if(!LOCPLINT || !LOCPLINT->cb) if(!LOCPLINT || !LOCPLINT->cb)
return; return;
for (PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p) for(PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p)
{ {
auto player = p; auto player = p;
if(LOCPLINT->battleInt) if(LOCPLINT->battleInt)
{ {
if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack()) if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack())
player = stack->getOwner(); player = stack->getOwner();
else
continue;
if(p != player)
continue;
} }
else if(!LOCPLINT->cb->isPlayerMakingTurn(player))
if(p != player || !LOCPLINT->cb->isPlayerMakingTurn(player))
continue; continue;
auto time = LOCPLINT->cb->getPlayerTurnTime(player); auto time = LOCPLINT->cb->getPlayerTurnTime(player);
if(LOCPLINT->isTimerEnabled()) if(time.isActive)
cachedTurnTime -= msPassed; cachedTurnTime -= msPassed;
if(cachedTurnTime < 0) cachedTurnTime = 0; //do not go below zero
if(cachedTurnTime < 0)
cachedTurnTime = 0; //do not go below zero
if(lastPlayer != player) if(lastPlayer != player)
{ {
@ -140,15 +145,10 @@ void TurnTimerWidget::tick(uint32_t msPassed)
auto * playerInfo = LOCPLINT->cb->getPlayer(player); auto * playerInfo = LOCPLINT->cb->getPlayer(player);
if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman())) if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman()))
{ {
if(LOCPLINT->battleInt) if(time.isBattle)
{ timeCheckAndUpdate(time.creatureTimer);
if(time.isBattleEnabled())
timeCheckAndUpdate(time.creatureTimer);
}
else else
{
timeCheckAndUpdate(time.turnTimer); timeCheckAndUpdate(time.turnTimer);
}
} }
else else
timeCheckAndUpdate(0); timeCheckAndUpdate(0);

View File

@ -19,6 +19,9 @@ struct DLL_LINKAGE TurnTimerInfo
int battleTimer = 0; //in ms, counts down during battles when creature timer runs out int battleTimer = 0; //in ms, counts down during battles when creature timer runs out
int creatureTimer = 0; //in ms, counts down when player is choosing action in battle int creatureTimer = 0; //in ms, counts down when player is choosing action in battle
bool isActive = true; //should be paused if set to false
bool isBattle = false; //indicator for current timer mode
bool isEnabled() const; bool isEnabled() const;
bool isBattleEnabled() const; bool isBattleEnabled() const;
@ -29,6 +32,8 @@ struct DLL_LINKAGE TurnTimerInfo
h & baseTimer; h & baseTimer;
h & battleTimer; h & battleTimer;
h & creatureTimer; h & creatureTimer;
h & isActive;
h & isBattle;
} }
}; };

View File

@ -31,11 +31,11 @@ void TurnTimerHandler::onGameplayStart(PlayerColor player)
std::lock_guard<std::recursive_mutex> guard(mx); std::lock_guard<std::recursive_mutex> guard(mx);
if(const auto * si = gameHandler.getStartInfo()) if(const auto * si = gameHandler.getStartInfo())
{ {
timerInfo[player].timer = si->turnTimerInfo; timers[player] = si->turnTimerInfo;
timerInfo[player].timer.turnTimer = 0; timers[player].turnTimer = 0;
timerInfo[player].isEnabled = true; timers[player].isActive = true;
timerInfo[player].isBattle = false; timers[player].isBattle = false;
timerInfo[player].lastUpdate = std::numeric_limits<int>::max(); lastUpdate[player] = std::numeric_limits<int>::max();
} }
} }
@ -43,17 +43,16 @@ void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled)
{ {
std::lock_guard<std::recursive_mutex> guard(mx); std::lock_guard<std::recursive_mutex> guard(mx);
assert(player.isValidPlayer()); assert(player.isValidPlayer());
timerInfo[player].isEnabled = enabled; timers[player].isActive = enabled;
} }
void TurnTimerHandler::sendTimerUpdate(PlayerColor player) void TurnTimerHandler::sendTimerUpdate(PlayerColor player)
{ {
auto & info = timerInfo[player];
TurnTimeUpdate ttu; TurnTimeUpdate ttu;
ttu.player = player; ttu.player = player;
ttu.turnTimer = info.timer; ttu.turnTimer = timers[player];
gameHandler.sendAndApply(&ttu); gameHandler.sendAndApply(&ttu);
info.lastUpdate = 0; lastUpdate[player] = 0;
} }
void TurnTimerHandler::onPlayerGetTurn(PlayerColor player) void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
@ -63,10 +62,10 @@ void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
{ {
if(si->turnTimerInfo.isEnabled()) if(si->turnTimerInfo.isEnabled())
{ {
auto & info = timerInfo[player]; auto & timer = timers[player];
if(si->turnTimerInfo.baseTimer > 0) if(si->turnTimerInfo.baseTimer > 0)
info.timer.baseTimer += info.timer.turnTimer; timer.baseTimer += timer.turnTimer;
info.timer.turnTimer = si->turnTimerInfo.turnTimer; timer.turnTimer = si->turnTimerInfo.turnTimer;
sendTimerUpdate(player); sendTimerUpdate(player);
} }
@ -91,14 +90,13 @@ bool TurnTimerHandler::timerCountDown(int & timer, int initialTimer, PlayerColor
{ {
if(timer > 0) if(timer > 0)
{ {
auto & info = timerInfo[player];
timer -= waitTime; timer -= waitTime;
info.lastUpdate += waitTime; lastUpdate[player] += waitTime;
int frequency = (timer > turnTimePropagateThreshold int frequency = (timer > turnTimePropagateThreshold
&& initialTimer - timer > turnTimePropagateThreshold) && initialTimer - timer > turnTimePropagateThreshold)
? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit; ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
if(info.lastUpdate >= frequency) if(lastUpdate[player] >= frequency)
sendTimerUpdate(player); sendTimerUpdate(player);
return true; return true;
@ -114,16 +112,16 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
if(!si || !gs || !si->turnTimerInfo.isEnabled()) if(!si || !gs || !si->turnTimerInfo.isEnabled())
return; return;
auto & info = timerInfo[player]; auto & timer = timers[player];
const auto * state = gameHandler.getPlayerState(player); const auto * state = gameHandler.getPlayerState(player);
if(state && state->human && info.isEnabled && !info.isBattle && state->status == EPlayerStatus::INGAME) if(state && state->human && timer.isActive && !timer.isBattle && state->status == EPlayerStatus::INGAME)
{ {
if(!timerCountDown(info.timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime)) if(!timerCountDown(timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
{ {
if(info.timer.baseTimer > 0) if(timer.baseTimer > 0)
{ {
info.timer.turnTimer = info.timer.baseTimer; timer.turnTimer = timer.baseTimer;
info.timer.baseTimer = 0; timer.baseTimer = 0;
onPlayerMakingTurn(player, 0); onPlayerMakingTurn(player, 0);
} }
else if(!gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries else if(!gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries
@ -164,10 +162,10 @@ void TurnTimerHandler::onBattleStart()
{ {
if(i.isValidPlayer()) if(i.isValidPlayer())
{ {
auto & info = timerInfo[i]; auto & timer = timers[i];
info.isBattle = true; timer.isBattle = true;
info.timer.battleTimer = (pvpBattle ? si->turnTimerInfo.battleTimer : 0); timer.battleTimer = (pvpBattle ? si->turnTimerInfo.battleTimer : 0);
info.timer.creatureTimer = (pvpBattle ? si->turnTimerInfo.creatureTimer : si->turnTimerInfo.battleTimer); timer.creatureTimer = (pvpBattle ? si->turnTimerInfo.creatureTimer : si->turnTimerInfo.battleTimer);
sendTimerUpdate(i); sendTimerUpdate(i);
} }
@ -191,12 +189,12 @@ void TurnTimerHandler::onBattleEnd()
{ {
if(i.isValidPlayer() && !pvpBattle) if(i.isValidPlayer() && !pvpBattle)
{ {
auto & info = timerInfo[i]; auto & timer = timers[i];
info.isBattle = false; timer.isBattle = false;
if(si->turnTimerInfo.baseTimer && info.timer.baseTimer == 0) if(si->turnTimerInfo.baseTimer && timer.baseTimer == 0)
info.timer.baseTimer = info.timer.creatureTimer; timer.baseTimer = timer.creatureTimer;
else if(si->turnTimerInfo.turnTimer && info.timer.turnTimer == 0) else if(si->turnTimerInfo.turnTimer && timer.turnTimer == 0)
info.timer.turnTimer = info.timer.creatureTimer; timer.turnTimer = timer.creatureTimer;
sendTimerUpdate(i); sendTimerUpdate(i);
} }
@ -215,10 +213,10 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack)
{ {
auto player = stack.getOwner(); auto player = stack.getOwner();
auto & info = timerInfo[player]; auto & timer = timers[player];
if(info.timer.battleTimer == 0) if(timer.battleTimer == 0)
info.timer.battleTimer = info.timer.creatureTimer; timer.battleTimer = timer.creatureTimer;
info.timer.creatureTimer = si->turnTimerInfo.creatureTimer; timer.creatureTimer = si->turnTimerInfo.creatureTimer;
sendTimerUpdate(player); sendTimerUpdate(player);
} }
@ -254,16 +252,16 @@ void TurnTimerHandler::onBattleLoop(int waitTime)
if(!state || state->status != EPlayerStatus::INGAME || !state->human) if(!state || state->status != EPlayerStatus::INGAME || !state->human)
return; return;
auto & info = timerInfo[player]; auto & timer = timers[player];
if(info.isEnabled && info.isBattle && !timerCountDown(info.timer.creatureTimer, si->turnTimerInfo.creatureTimer, player, waitTime)) if(timer.isActive && timer.isBattle && !timerCountDown(timer.creatureTimer, si->turnTimerInfo.creatureTimer, player, waitTime))
{ {
if(isPvpBattle()) if(isPvpBattle())
{ {
if(info.timer.battleTimer > 0) if(timer.battleTimer > 0)
{ {
info.timer.creatureTimer = info.timer.battleTimer; timer.creatureTimer = timer.battleTimer;
timerCountDown(info.timer.creatureTimer, info.timer.battleTimer, player, 0); timerCountDown(timer.creatureTimer, timer.battleTimer, player, 0);
info.timer.battleTimer = 0; timer.battleTimer = 0;
} }
else else
{ {
@ -281,17 +279,17 @@ void TurnTimerHandler::onBattleLoop(int waitTime)
} }
else else
{ {
if(info.timer.turnTimer > 0) if(timer.turnTimer > 0)
{ {
info.timer.creatureTimer = info.timer.turnTimer; timer.creatureTimer = timer.turnTimer;
timerCountDown(info.timer.creatureTimer, info.timer.turnTimer, player, 0); timerCountDown(timer.creatureTimer, timer.turnTimer, player, 0);
info.timer.turnTimer = 0; timer.turnTimer = 0;
} }
else if(info.timer.baseTimer > 0) else if(timer.baseTimer > 0)
{ {
info.timer.creatureTimer = info.timer.baseTimer; timer.creatureTimer = timer.baseTimer;
timerCountDown(info.timer.creatureTimer, info.timer.baseTimer, player, 0); timerCountDown(timer.creatureTimer, timer.baseTimer, player, 0);
info.timer.baseTimer = 0; timer.baseTimer = 0;
} }
else else
{ {

View File

@ -23,19 +23,12 @@ class CGameHandler;
class TurnTimerHandler class TurnTimerHandler
{ {
struct PlayerTimerInfo
{
TurnTimerInfo timer;
bool isEnabled = true;
bool isBattle = false;
int lastUpdate = 0;
};
CGameHandler & gameHandler; CGameHandler & gameHandler;
const int turnTimePropagateFrequency = 5000; const int turnTimePropagateFrequency = 5000;
const int turnTimePropagateFrequencyCrit = 1000; const int turnTimePropagateFrequencyCrit = 1000;
const int turnTimePropagateThreshold = 3000; const int turnTimePropagateThreshold = 3000;
std::map<PlayerColor, PlayerTimerInfo> timerInfo; std::map<PlayerColor, TurnTimerInfo> timers;
std::map<PlayerColor, int> lastUpdate;
std::recursive_mutex mx; std::recursive_mutex mx;
void onPlayerMakingTurn(PlayerColor player, int waitTime); void onPlayerMakingTurn(PlayerColor player, int waitTime);