refactor: fixed potential game freeze in some infinite cycles, disconnects and top library's card usage (related to #11285);

This commit is contained in:
Oleg Agafonov 2024-10-24 13:19:05 +04:00
parent cfad27ffae
commit b51fdae550
11 changed files with 45 additions and 50 deletions

View file

@ -149,45 +149,27 @@ public enum ServerMessagesUtil {
}
public void incGamesStarted() {
int value;
do {
value = gamesStarted.get();
} while (!gamesStarted.compareAndSet(value, value + 1));
gamesStarted.incrementAndGet();
}
public void incGamesEnded() {
int value;
do {
value = gamesEnded.get();
} while (!gamesEnded.compareAndSet(value, value + 1));
gamesEnded.incrementAndGet();
}
public void incTournamentsStarted() {
int value;
do {
value = tournamentsStarted.get();
} while (!tournamentsStarted.compareAndSet(value, value + 1));
tournamentsStarted.incrementAndGet();
}
public void incTournamentsEnded() {
int value;
do {
value = tournamentsEnded.get();
} while (!tournamentsEnded.compareAndSet(value, value + 1));
tournamentsEnded.incrementAndGet();
}
public void incReconnects() {
int value;
do {
value = reconnects.get();
} while (!reconnects.compareAndSet(value, value + 1));
reconnects.incrementAndGet();
}
public void incLostConnection() {
int value;
do {
value = lostConnection.get();
} while (!lostConnection.compareAndSet(value, value + 1));
lostConnection.incrementAndGet();
}
}

View file

@ -151,7 +151,7 @@ class GoldberryRiverDaughterToEffect extends OneShotEffect {
max, MultiAmountType.COUNTERS, game);
total = choices.stream().reduce(0, Integer::sum);
} while (total < 1);
} while (total < 1 && controller.canRespond());
// Move the counters. Make sure some counters were actually moved.
boolean movedCounters = false;

View file

@ -90,7 +90,7 @@ class MagesContestEffect extends OneShotEffect {
break;
}
}
} while (!Objects.equals(currentPlayer, winner));
} while (you.canRespond() && spellController.canRespond() && !Objects.equals(currentPlayer, winner));
game.informPlayers(winner.getLogName() + " has won the contest with a high bid of " + highBid + " life");
winner.loseLife(highBid, game, source, false);
if (winner == you) {

View file

@ -162,9 +162,11 @@ class NicolBolasGodPharaohPlusTwoEffect extends OneShotEffect {
do {
card = library.getFromTop(game);
if (card == null) {
continue;
break;
}
if (!opponent.moveCards(card, Zone.EXILED, source, game)) {
break;
}
opponent.moveCards(card, Zone.EXILED, source, game);
if (card.isLand(game)) {
continue;
}
@ -172,8 +174,7 @@ class NicolBolasGodPharaohPlusTwoEffect extends OneShotEffect {
effect.setTargetPointer(new FixedTarget(card, game));
game.addEffect(effect, source);
break;
} while (library.hasCards()
&& card != null);
} while (library.hasCards());
return true;
}
}

View file

@ -129,10 +129,14 @@ class PossibilityStormEffect extends OneShotEffect {
Card card;
do {
card = library.getFromTop(game);
if (card != null) {
spellController.moveCardsToExile(card, source, game, true, source.getSourceId(), sourceObject.getIdName());
if (card == null) {
break;
}
} while (library.hasCards() && card != null && !sharesType(card, spell.getCardType(game), game));
if (!spellController.moveCardsToExile(card, source, game, true, source.getSourceId(), sourceObject.getIdName())) {
break;
};
} while (library.hasCards() && !sharesType(card, spell.getCardType(game), game));
if (card != null && sharesType(card, spell.getCardType(game), game)
&& !card.isLand(game)

View file

@ -59,19 +59,21 @@ class PrimalSurgeEffect extends OneShotEffect {
return false;
}
boolean repeat;
do {
repeat = false;
Card card = controller.getLibrary().getFromTop(game);
if (card != null) {
controller.moveCards(card, Zone.EXILED, source, game);
if (card.isPermanent(game)
&& controller.chooseUse(Outcome.PutCardInPlay, "Put " + card.getName() + " onto the battlefield?", source, game)) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
repeat = true;
}
if (card == null) {
break;
}
} while (controller.canRespond() && repeat);
if (!controller.moveCards(card, Zone.EXILED, source, game)) {
break;
}
if (card.isPermanent(game)
&& controller.chooseUse(Outcome.PutCardInPlay, "Put " + card.getName() + " onto the battlefield?", source, game)) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
continue;
}
break;
} while (controller.canRespond());
return true;
}

View file

@ -109,7 +109,7 @@ class ResourcefulDefenseMoveCounterEffect extends OneShotEffect {
max, MultiAmountType.COUNTERS, game);
total = choices.stream().reduce(0, Integer::sum);
} while (total < 0);
} while (total < 0 && controller.canRespond());
// Move the counters. Make sure some counters were actually moved.
for (int i = 0; i < choices.size(); i++) {

View file

@ -1,4 +1,3 @@
package mage.cards.r;
import java.util.Collections;
@ -114,6 +113,8 @@ class RicochetEffect extends OneShotEffect {
playerRolls.put(player, 7);
}
}
// roll until only 1 min result
do {
for (Player player : playerRolls.keySet()) {
playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6)); // bad outcome - ai must choose lowest value

View file

@ -66,15 +66,17 @@ class StolenGoodsEffect extends OneShotEffect {
do {
card = opponent.getLibrary().getFromTop(game);
if (card == null) {
continue;
break;
}
if (card.isLand(game)) {
opponent.moveCardsToExile(card, source, game, true, source.getSourceId(), CardUtil.createObjectRealtedWindowTitle(source, game, null));
if (!opponent.moveCardsToExile(card, source, game, true, source.getSourceId(), CardUtil.createObjectRealtedWindowTitle(source, game, null))) {
break;
}
} else {
PlayFromNotOwnHandZoneTargetEffect.exileAndPlayFromExile(game, source, card, TargetController.YOU, Duration.EndOfTurn, true, false, true);
break;
}
} while (card != null && card.isLand(game));
} while (card.isLand(game));
return true;
}

View file

@ -60,7 +60,9 @@ class SuntailSquadronEffect extends OneShotEffect {
}
Effect effect = new ConjureCardEffect("Suntail Hawk");
do {
effect.apply(game, source);
if (!effect.apply(game, source)) {
break;
}
} while (player.getHand().size() < 7);
return true;
}

View file

@ -63,7 +63,7 @@ class TimesifterEffect extends OneShotEffect {
List<UUID> playersExiling = game.getState().getPlayersInRange(source.getControllerId(), game);
do {
int highestCMC = Integer.MIN_VALUE;
List<UUID> playersWithHighestCMC = new ArrayList<>(1);
List<UUID> playersWithHighestCMC = new ArrayList<>();
for (UUID playerId : playersExiling) {
Player player = game.getPlayer(playerId);
if (player != null) {
@ -84,6 +84,7 @@ class TimesifterEffect extends OneShotEffect {
}
playersExiling = new ArrayList<>(playersWithHighestCMC);
} while (playersExiling.size() > 1);
for (UUID playerId : playersExiling) {
Effect effect = new AddExtraTurnTargetEffect();
effect.setTargetPointer(new FixedTarget(playerId));