Просмотр исходного кода

Blocked most of incoming network packs during turns of other players

Ivan Savenko 1 год назад
Родитель
Сommit
ac11a0e639
3 измененных файлов с 48 добавлено и 1 удалено
  1. 6 0
      server/CGameHandler.cpp
  2. 41 0
      server/NetPacksServer.cpp
  3. 1 1
      server/processors/TurnOrderProcessor.h

+ 6 - 0
server/CGameHandler.cpp

@@ -1748,6 +1748,12 @@ void CGameHandler::throwIfWrongOwner(CPackForServer * pack, ObjectInstanceID id)
 	}
 }
 
+void CGameHandler::throwIfPlayerNotActive(CPackForServer * pack)
+{
+	if (!turnOrder->isPlayerMakingTurn(pack->player))
+		throwNotAllowedAction(pack);
+}
+
 void CGameHandler::throwIfWrongPlayer(CPackForServer * pack)
 {
 	throwIfWrongPlayer(pack, pack->player);

+ 41 - 0
server/NetPacksServer.cpp

@@ -47,18 +47,21 @@ void ApplyGhNetPackVisitor::visitGamePause(GamePause & pack)
 void ApplyGhNetPackVisitor::visitEndTurn(EndTurn & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
 	result = gh.turnOrder->onPlayerEndsTurn(pack.player);
 }
 
 void ApplyGhNetPackVisitor::visitDismissHero(DismissHero & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
 	result = gh.removeObject(gh.getObj(pack.hid), pack.player);
 }
 
 void ApplyGhNetPackVisitor::visitMoveHero(MoveHero & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
 
 	for (auto const & dest : pack.path)
 	{
@@ -75,6 +78,7 @@ void ApplyGhNetPackVisitor::visitMoveHero(MoveHero & pack)
 void ApplyGhNetPackVisitor::visitCastleTeleportHero(CastleTeleportHero & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
 
 	result = gh.teleportHero(pack.hid, pack.dest, pack.source, pack.player);
 }
@@ -82,48 +86,63 @@ void ApplyGhNetPackVisitor::visitCastleTeleportHero(CastleTeleportHero & pack)
 void ApplyGhNetPackVisitor::visitArrangeStacks(ArrangeStacks & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.arrangeStacks(pack.id1, pack.id2, pack.what, pack.p1, pack.p2, pack.val, pack.player);
 }
 
 void ApplyGhNetPackVisitor::visitBulkMoveArmy(BulkMoveArmy & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.srcArmy);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.bulkMoveArmy(pack.srcArmy, pack.destArmy, pack.srcSlot);
 }
 
 void ApplyGhNetPackVisitor::visitBulkSplitStack(BulkSplitStack & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.bulkSplitStack(pack.src, pack.srcOwner, pack.amount);
 }
 
 void ApplyGhNetPackVisitor::visitBulkMergeStacks(BulkMergeStacks & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.bulkMergeStacks(pack.src, pack.srcOwner);
 }
 
 void ApplyGhNetPackVisitor::visitBulkSmartSplitStack(BulkSmartSplitStack & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.bulkSmartSplitStack(pack.src, pack.srcOwner);
 }
 
 void ApplyGhNetPackVisitor::visitDisbandCreature(DisbandCreature & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.id);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.disbandCreature(pack.id, pack.pos);
 }
 
 void ApplyGhNetPackVisitor::visitBuildStructure(BuildStructure & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.tid);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.buildStructure(pack.tid, pack.bid);
 }
 
 void ApplyGhNetPackVisitor::visitRecruitCreatures(RecruitCreatures & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
 	// ownership checks are inside recruitCreatures
 	result = gh.recruitCreatures(pack.tid, pack.dst, pack.crid, pack.amount, pack.level, pack.player);
 }
@@ -131,6 +150,8 @@ void ApplyGhNetPackVisitor::visitRecruitCreatures(RecruitCreatures & pack)
 void ApplyGhNetPackVisitor::visitUpgradeCreature(UpgradeCreature & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.id);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.upgradeCreature(pack.id, pack.pos, pack.cid);
 }
 
@@ -139,6 +160,8 @@ void ApplyGhNetPackVisitor::visitGarrisonHeroSwap(GarrisonHeroSwap & pack)
 	const CGTownInstance * town = gh.getTown(pack.tid);
 	if(!gh.isPlayerOwns(&pack, pack.tid) && !(town->garrisonHero && gh.isPlayerOwns(&pack, town->garrisonHero->id)))
 		gh.throwNotAllowedAction(&pack); //neither town nor garrisoned hero (if present) is ours
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.garrisonSwap(pack.tid);
 }
 
@@ -146,6 +169,8 @@ void ApplyGhNetPackVisitor::visitExchangeArtifacts(ExchangeArtifacts & pack)
 {
 	if(gh.getHero(pack.src.artHolder))
 		gh.throwIfWrongPlayer(&pack, gh.getOwner(pack.src.artHolder)); //second hero can be ally
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.moveArtifact(pack.player, pack.src, pack.dst);
 }
 
@@ -155,11 +180,15 @@ void ApplyGhNetPackVisitor::visitBulkExchangeArtifacts(BulkExchangeArtifacts & p
 		gh.throwIfWrongOwner(&pack, pack.srcHero);
 	if(pack.swap)
 		gh.throwIfWrongOwner(&pack, pack.dstHero);
+
+	gh.throwIfPlayerNotActive(&pack);
 	result = gh.bulkMoveArtifacts(pack.player, pack.srcHero, pack.dstHero, pack.swap, pack.equipped, pack.backpack);
 }
 
 void ApplyGhNetPackVisitor::visitManageBackpackArtifacts(ManageBackpackArtifacts & pack)
 {
+	gh.throwIfPlayerNotActive(&pack);
+
 	if(gh.getPlayerRelations(pack.player, gh.getOwner(pack.artHolder)) != PlayerRelations::ENEMIES)
 	{
 		if(pack.cmd == ManageBackpackArtifacts::ManageCmd::SCROLL_LEFT)
@@ -191,18 +220,21 @@ void ApplyGhNetPackVisitor::visitManageEquippedArtifacts(ManageEquippedArtifacts
 void ApplyGhNetPackVisitor::visitAssembleArtifacts(AssembleArtifacts & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.heroID);
+	// gh.throwIfPlayerNotActive(&pack); // Might happen when player captures artifacts in battle?
 	result = gh.assembleArtifacts(pack.heroID, pack.artifactSlot, pack.assemble, pack.assembleTo);
 }
 
 void ApplyGhNetPackVisitor::visitEraseArtifactByClient(EraseArtifactByClient & pack)
 {
 	gh.throwIfWrongPlayer(&pack, gh.getOwner(pack.al.artHolder));
+	gh.throwIfPlayerNotActive(&pack);
 	result = gh.eraseArtifactByClient(pack.al);
 }
 
 void ApplyGhNetPackVisitor::visitBuyArtifact(BuyArtifact & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
 	result = gh.buyArtifact(pack.hid, pack.aid);
 }
 
@@ -213,6 +245,7 @@ void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
 	const auto * market = dynamic_cast<const IMarket*>(object);
 
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
 
 	if(!object)
 		gh.throwAndComplain(&pack, "Invalid market object");
@@ -317,12 +350,15 @@ void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
 void ApplyGhNetPackVisitor::visitSetFormation(SetFormation & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.setFormation(pack.hid, pack.formation);
 }
 
 void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
 
 	result = gh.heroPool->hireHero(pack.tid, pack.hid, pack.player, pack.nhid);
 }
@@ -330,6 +366,7 @@ void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack)
 void ApplyGhNetPackVisitor::visitBuildBoat(BuildBoat & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	gh.throwIfPlayerNotActive(&pack);
 
 	if(gh.getPlayerRelations(gh.getOwner(pack.objid), pack.player) == PlayerRelations::ENEMIES)
 		gh.throwAndComplain(&pack, "Can't build boat at enemy shipyard");
@@ -355,6 +392,7 @@ void ApplyGhNetPackVisitor::visitQueryReply(QueryReply & pack)
 void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
 {
 	gh.throwIfWrongPlayer(&pack);
+	// allowed even if it is not our turn - will be filtered by battle sides
 
 	result = gh.battles->makePlayerBattleAction(pack.battleID, pack.player, pack.ba);
 }
@@ -362,12 +400,15 @@ void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
 void ApplyGhNetPackVisitor::visitDigWithHero(DigWithHero & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.id);
+	gh.throwIfPlayerNotActive(&pack);
+
 	result = gh.dig(gh.getHero(pack.id));
 }
 
 void ApplyGhNetPackVisitor::visitCastAdvSpell(CastAdvSpell & pack)
 {
 	gh.throwIfWrongOwner(&pack, pack.hid);
+	gh.throwIfPlayerNotActive(&pack);
 
 	if (!pack.sid.hasValue())
 		gh.throwNotAllowedAction(&pack);

+ 1 - 1
server/processors/TurnOrderProcessor.h

@@ -71,13 +71,13 @@ class TurnOrderProcessor : boost::noncopyable
 	void doEndPlayerTurn(PlayerColor which);
 
 	bool isPlayerAwaitsTurn(PlayerColor which) const;
-	bool isPlayerMakingTurn(PlayerColor which) const;
 	bool isPlayerAwaitsNewDay(PlayerColor which) const;
 
 public:
 	TurnOrderProcessor(CGameHandler * owner);
 
 	bool isContactAllowed(PlayerColor left, PlayerColor right) const;
+	bool isPlayerMakingTurn(PlayerColor which) const;
 
 	/// Add new player to handle (e.g. on game start)
 	void addPlayer(PlayerColor which);