1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

server queries is now stored as unique_ptr

This commit is contained in:
Ivan Savenko 2023-07-24 00:10:01 +03:00
parent e8e6c02a4a
commit c217d7717a
6 changed files with 47 additions and 32 deletions

View File

@ -381,7 +381,7 @@ void BattleProcessor::startBattlePrimary(const CArmedInstance *army1, const CArm
nextBattleQuery->initialHeroMana[i] = heroes[i]->mana;
}
}
gameHandler->queries.addQuery(nextBattleQuery);
gameHandler->queries->addQuery(nextBattleQuery);
this->battleThread = std::make_unique<boost::thread>(boost::thread(&BattleProcessor::runBattle, this));
}
@ -1384,7 +1384,7 @@ void BattleProcessor::endBattle(int3 tile, const CGHeroInstance * heroAttacker,
if(heroDefender)
battleResult.data->exp[1] = heroDefender->calculateXp(battleResult.data->exp[1]);
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries.topQuery(gameHandler->gameState()->curB->sides[0].color));
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(gameHandler->gameState()->curB->sides[0].color));
if (!battleQuery)
{
logGlobal->error("Cannot find battle query!");
@ -1395,7 +1395,7 @@ void BattleProcessor::endBattle(int3 tile, const CGHeroInstance * heroAttacker,
battleQuery->result = std::make_optional(*battleResult.data);
//Check how many battle gameHandler->queries were created (number of players blocked by battle)
const int queriedPlayers = battleQuery ? (int)boost::count(gameHandler->queries.allQueries(), battleQuery) : 0;
const int queriedPlayers = battleQuery ? (int)boost::count(gameHandler->queries->allQueries(), battleQuery) : 0;
finishingBattle = std::make_unique<FinishingBattleHelper>(battleQuery, queriedPlayers);
// in battles against neutrals, 1st player can ask to replay battle manually
@ -1403,13 +1403,13 @@ void BattleProcessor::endBattle(int3 tile, const CGHeroInstance * heroAttacker,
{
auto battleDialogQuery = std::make_shared<CBattleDialogQuery>(gameHandler, gameHandler->gameState()->curB);
battleResult.data->queryID = battleDialogQuery->queryID;
gameHandler->queries.addQuery(battleDialogQuery);
gameHandler->queries->addQuery(battleDialogQuery);
}
else
battleResult.data->queryID = -1;
//set same battle result for all gameHandler->queries
for(auto q : gameHandler->queries.allQueries())
for(auto q : gameHandler->queries->allQueries())
{
auto otherBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(q);
if(otherBattleQuery)
@ -1424,7 +1424,7 @@ void BattleProcessor::endBattle(int3 tile, const CGHeroInstance * heroAttacker,
void BattleProcessor::endBattleConfirm(const BattleInfo * battleInfo)
{
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries.topQuery(battleInfo->sides.at(0).color));
auto battleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battleInfo->sides.at(0).color));
if(!battleQuery)
{
logGlobal->trace("No battle query, battle end was confirmed by another player");
@ -1619,7 +1619,7 @@ void BattleProcessor::endBattleConfirm(const BattleInfo * battleInfo)
raccepted.winnerSide = finishingBattle->winnerSide;
gameHandler->sendAndApply(&raccepted);
gameHandler->queries.popIfTop(battleQuery);
gameHandler->queries->popIfTop(battleQuery);
//--> continuation (battleAfterLevelUp) occurs after level-up gameHandler->queries are handled or on removing query
}

View File

@ -16,6 +16,7 @@
#include "PlayerMessageProcessor.h"
#include "ServerNetPackVisitors.h"
#include "ServerSpellCastEnvironment.h"
#include "CQuery.h"
#include "../lib/ArtifactUtils.h"
#include "../lib/CArtHandler.h"
@ -255,7 +256,7 @@ void CGameHandler::levelUpHero(const CGHeroInstance * hero)
{
auto levelUpQuery = std::make_shared<CHeroLevelUpDialogQuery>(this, hlu, hero);
hlu.queryID = levelUpQuery->queryID;
queries.addQuery(levelUpQuery);
queries->addQuery(levelUpQuery);
sendAndApply(&hlu);
//level up will be called on query reply
}
@ -396,7 +397,7 @@ void CGameHandler::levelUpCommander(const CCommanderInstance * c)
{
auto commanderLevelUp = std::make_shared<CCommanderLevelUpDialogQuery>(this, clu, hero);
clu.queryID = commanderLevelUp->queryID;
queries.addQuery(commanderLevelUp);
queries->addQuery(commanderLevelUp);
sendAndApply(&clu);
}
}
@ -539,11 +540,11 @@ void CGameHandler::handleReceivedPack(CPackForServer * pack)
vstd::clear_pointer(pack);
}
CGameHandler::CGameHandler(CVCMIServer * lobby)
: lobby(lobby)
, heroPool(std::make_unique<HeroPoolProcessor>(this))
, battles(std::make_unique<BattleProcessor>(this))
, queries(std::make_unique<Queries>())
, playerMessages(std::make_unique<PlayerMessageProcessor>(this))
, complainNoCreatures("No creatures to split")
, complainNotEnoughCreatures("Cannot split that stack, not enough creatures!")
@ -1219,7 +1220,7 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, boo
LOG_TRACE_PARAMS(logGlobal, "Hero %s starts movement from %s to %s", h->getNameTranslated() % tmh.start.toString() % tmh.end.toString());
auto moveQuery = std::make_shared<CHeroMovementQuery>(this, tmh, h);
queries.addQuery(moveQuery);
queries->addQuery(moveQuery);
if (leavingTile == LEAVING_TILE)
leaveTile();
@ -1246,7 +1247,7 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, boo
visitObjectOnTile(t, h);
}
queries.popIfTop(moveQuery);
queries->popIfTop(moveQuery);
logGlobal->trace("Hero %s ends movement", h->getNameTranslated());
return result != TryMoveHero::FAILED;
};
@ -1408,7 +1409,7 @@ void CGameHandler::setOwner(const CGObjectInstance * obj, const PlayerColor owne
void CGameHandler::showBlockingDialog(BlockingDialog *iw)
{
auto dialogQuery = std::make_shared<CBlockingDialogQuery>(this, *iw);
queries.addQuery(dialogQuery);
queries->addQuery(dialogQuery);
iw->queryID = dialogQuery->queryID;
sendToAllClients(iw);
}
@ -1416,7 +1417,7 @@ void CGameHandler::showBlockingDialog(BlockingDialog *iw)
void CGameHandler::showTeleportDialog(TeleportDialog *iw)
{
auto dialogQuery = std::make_shared<CTeleportDialogQuery>(this, *iw);
queries.addQuery(dialogQuery);
queries->addQuery(dialogQuery);
iw->queryID = dialogQuery->queryID;
sendToAllClients(iw);
}
@ -1685,7 +1686,7 @@ void CGameHandler::heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2)
sendAndApply(&hex);
useScholarSkill(hero1,hero2);
queries.addQuery(exchange);
queries->addQuery(exchange);
}
}
@ -3186,13 +3187,13 @@ bool CGameHandler::queryReply(QueryID qid, const JsonNode & answer, PlayerColor
logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid);
logGlobal->trace(answer.toJson());
auto topQuery = queries.topQuery(player);
auto topQuery = queries->topQuery(player);
COMPLAIN_RET_FALSE_IF(!topQuery, "This player doesn't have any queries!");
if(topQuery->queryID != qid)
{
auto currentQuery = queries.getQuery(qid);
auto currentQuery = queries->getQuery(qid);
if(currentQuery != nullptr && currentQuery->endsByPlayerAnswer())
currentQuery->setReply(answer);
@ -3202,7 +3203,7 @@ bool CGameHandler::queryReply(QueryID qid, const JsonNode & answer, PlayerColor
COMPLAIN_RET_FALSE_IF(!topQuery->endsByPlayerAnswer(), "This query cannot be ended by player's answer!");
topQuery->setReply(answer);
queries.popQuery(topQuery);
queries->popQuery(topQuery);
return true;
}
@ -3365,7 +3366,7 @@ void CGameHandler::showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID h
assert(upperArmy);
auto garrisonQuery = std::make_shared<CGarrisonDialogQuery>(this, upperArmy, lowerArmy);
queries.addQuery(garrisonQuery);
queries->addQuery(garrisonQuery);
GarrisonDialog gd;
gd.hid = hid;
@ -3419,10 +3420,10 @@ bool CGameHandler::isAllowedExchange(ObjectInstanceID id1, ObjectInstanceID id2)
}
//Ongoing garrison exchange - usually picking from top garison (from o1 to o2), but who knows
auto dialog = std::dynamic_pointer_cast<CGarrisonDialogQuery>(queries.topQuery(o1->tempOwner));
auto dialog = std::dynamic_pointer_cast<CGarrisonDialogQuery>(queries->topQuery(o1->tempOwner));
if (!dialog)
{
dialog = std::dynamic_pointer_cast<CGarrisonDialogQuery>(queries.topQuery(o2->tempOwner));
dialog = std::dynamic_pointer_cast<CGarrisonDialogQuery>(queries->topQuery(o2->tempOwner));
}
if (dialog)
{
@ -3463,7 +3464,7 @@ void CGameHandler::objectVisited(const CGObjectInstance * obj, const CGHeroInsta
}
}
visitQuery = std::make_shared<CObjectVisitQuery>(this, visitedObject, h, visitedObject->visitablePos());
queries.addQuery(visitQuery); //TODO real visit pos
queries->addQuery(visitQuery); //TODO real visit pos
HeroVisit hv;
hv.objId = obj->id;
@ -3478,7 +3479,7 @@ void CGameHandler::objectVisited(const CGObjectInstance * obj, const CGHeroInsta
ObjectVisitStarted::defaultExecute(serverEventBus.get(), startVisit, h->tempOwner, h->id, obj->id);
if(visitQuery)
queries.popIfTop(visitQuery); //visit ends here if no queries were created
queries->popIfTop(visitQuery); //visit ends here if no queries were created
}
void CGameHandler::objectVisitEnded(const CObjectVisitQuery & query)
@ -4084,7 +4085,7 @@ bool CGameHandler::isBlockedByQueries(const CPack *pack, PlayerColor player)
if (!strcmp(typeid(*pack).name(), typeid(PlayerMessage).name()))
return false;
auto query = queries.topQuery(player);
auto query = queries->topQuery(player);
if (query && query->blocksPack(pack))
{
complain(boost::str(boost::format(
@ -4101,7 +4102,7 @@ bool CGameHandler::isBlockedByQueries(const CPack *pack, PlayerColor player)
void CGameHandler::removeAfterVisit(const CGObjectInstance *object)
{
//If the object is being visited, there must be a matching query
for (const auto &query : queries.allQueries())
for (const auto &query : queries->allQueries())
{
if (auto someVistQuery = std::dynamic_pointer_cast<CObjectVisitQuery>(query))
{
@ -4150,7 +4151,7 @@ void CGameHandler::changeFogOfWar(std::unordered_set<int3> &tiles, PlayerColor p
bool CGameHandler::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
{
if (auto topQuery = queries.topQuery(hero->getOwner()))
if (auto topQuery = queries->topQuery(hero->getOwner()))
if (auto visit = std::dynamic_pointer_cast<const CObjectVisitQuery>(topQuery))
return !(visit->visitedObject == obj && visit->visitingHero == hero);

View File

@ -14,13 +14,22 @@
#include "../lib/IGameCallback.h"
#include "../lib/battle/CBattleInfoCallback.h"
#include "../lib/ScriptHandler.h"
#include "CQuery.h"
VCMI_LIB_NAMESPACE_BEGIN
struct SideInBattle;
class IMarket;
class SpellCastEnvironment;
class CConnection;
class CCommanderInstance;
class EVictoryLossCheckResult;
struct CPack;
struct CPackForServer;
struct NewTurn;
struct CGarrisonOperationPack;
struct SetResources;
struct NewStructures;
#if SCRIPTING_ENABLED
namespace scripting
@ -38,6 +47,8 @@ class CVCMIServer;
class CBaseForGHApply;
class PlayerMessageProcessor;
class BattleProcessor;
class Queries;
class CObjectVisitQuery;
struct PlayerStatus
{
@ -76,6 +87,7 @@ public:
std::unique_ptr<HeroPoolProcessor> heroPool;
std::unique_ptr<BattleProcessor> battles;
std::unique_ptr<Queries> queries;
//use enums as parameters, because doMove(sth, true, false, true) is not readable
enum EGuardLook {CHECK_FOR_GUARDS, IGNORE_GUARDS};
@ -90,7 +102,7 @@ public:
//queries stuff
boost::recursive_mutex gsm;
ui32 QID;
Queries queries;
SpellCastEnvironment * spellEnv;

View File

@ -143,7 +143,7 @@ bool CQuery::blockAllButReply(const CPack * pack) const
}
CGhQuery::CGhQuery(CGameHandler * owner):
CQuery(&owner->queries), gh(owner)
CQuery(owner->queries.get()), gh(owner)
{
}

View File

@ -12,6 +12,7 @@
#include "CGameHandler.h"
#include "BattleProcessor.h"
#include "CQuery.h"
#include "HeroPoolProcessor.h"
#include "PlayerMessageProcessor.h"
@ -48,7 +49,7 @@ void ApplyGhNetPackVisitor::visitEndTurn(EndTurn & pack)
}
gh.throwOnWrongPlayer(&pack, pack.player);
if(gh.queries.topQuery(pack.player))
if(gh.queries->topQuery(pack.player))
gh.throwAndComplain(&pack, "Cannot end turn before resolving queries!");
gh.states.setFlag(gs.currentPlayer, &PlayerStatus::makingTurn, false);

View File

@ -9,6 +9,7 @@
*/
#include "StdInc.h"
#include "../lib/gameState/CGameState.h"
#include "CQuery.h"
#include "CGameHandler.h"
#include "ServerSpellCastEnvironment.h"
@ -90,8 +91,8 @@ bool ServerSpellCastEnvironment::moveHero(ObjectInstanceID hid, int3 dst, bool t
void ServerSpellCastEnvironment::genericQuery(Query * request, PlayerColor color, std::function<void(const JsonNode&)> callback)
{
auto query = std::make_shared<CGenericQuery>(&gh->queries, color, callback);
auto query = std::make_shared<CGenericQuery>(gh->queries.get(), color, callback);
request->queryID = query->queryID;
gh->queries.addQuery(query);
gh->queries->addQuery(query);
gh->sendAndApply(request);
}