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() { public void incGamesStarted() {
int value; gamesStarted.incrementAndGet();
do {
value = gamesStarted.get();
} while (!gamesStarted.compareAndSet(value, value + 1));
} }
public void incGamesEnded() { public void incGamesEnded() {
int value; gamesEnded.incrementAndGet();
do {
value = gamesEnded.get();
} while (!gamesEnded.compareAndSet(value, value + 1));
} }
public void incTournamentsStarted() { public void incTournamentsStarted() {
int value; tournamentsStarted.incrementAndGet();
do {
value = tournamentsStarted.get();
} while (!tournamentsStarted.compareAndSet(value, value + 1));
} }
public void incTournamentsEnded() { public void incTournamentsEnded() {
int value; tournamentsEnded.incrementAndGet();
do {
value = tournamentsEnded.get();
} while (!tournamentsEnded.compareAndSet(value, value + 1));
} }
public void incReconnects() { public void incReconnects() {
int value; reconnects.incrementAndGet();
do {
value = reconnects.get();
} while (!reconnects.compareAndSet(value, value + 1));
} }
public void incLostConnection() { public void incLostConnection() {
int value; lostConnection.incrementAndGet();
do {
value = lostConnection.get();
} while (!lostConnection.compareAndSet(value, value + 1));
} }
} }

View file

@ -151,7 +151,7 @@ class GoldberryRiverDaughterToEffect extends OneShotEffect {
max, MultiAmountType.COUNTERS, game); max, MultiAmountType.COUNTERS, game);
total = choices.stream().reduce(0, Integer::sum); 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. // Move the counters. Make sure some counters were actually moved.
boolean movedCounters = false; boolean movedCounters = false;

View file

@ -90,7 +90,7 @@ class MagesContestEffect extends OneShotEffect {
break; 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"); game.informPlayers(winner.getLogName() + " has won the contest with a high bid of " + highBid + " life");
winner.loseLife(highBid, game, source, false); winner.loseLife(highBid, game, source, false);
if (winner == you) { if (winner == you) {

View file

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

View file

@ -129,10 +129,14 @@ class PossibilityStormEffect extends OneShotEffect {
Card card; Card card;
do { do {
card = library.getFromTop(game); card = library.getFromTop(game);
if (card != null) { if (card == null) {
spellController.moveCardsToExile(card, source, game, true, source.getSourceId(), sourceObject.getIdName()); 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) if (card != null && sharesType(card, spell.getCardType(game), game)
&& !card.isLand(game) && !card.isLand(game)

View file

@ -59,19 +59,21 @@ class PrimalSurgeEffect extends OneShotEffect {
return false; return false;
} }
boolean repeat;
do { do {
repeat = false;
Card card = controller.getLibrary().getFromTop(game); Card card = controller.getLibrary().getFromTop(game);
if (card != null) { if (card == null) {
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);
repeat = true;
}
} }
} 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; return true;
} }

View file

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

View file

@ -1,4 +1,3 @@
package mage.cards.r; package mage.cards.r;
import java.util.Collections; import java.util.Collections;
@ -114,6 +113,8 @@ class RicochetEffect extends OneShotEffect {
playerRolls.put(player, 7); playerRolls.put(player, 7);
} }
} }
// roll until only 1 min result
do { do {
for (Player player : playerRolls.keySet()) { for (Player player : playerRolls.keySet()) {
playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6)); // bad outcome - ai must choose lowest value 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 { do {
card = opponent.getLibrary().getFromTop(game); card = opponent.getLibrary().getFromTop(game);
if (card == null) { if (card == null) {
continue; break;
} }
if (card.isLand(game)) { 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 { } else {
PlayFromNotOwnHandZoneTargetEffect.exileAndPlayFromExile(game, source, card, TargetController.YOU, Duration.EndOfTurn, true, false, true); PlayFromNotOwnHandZoneTargetEffect.exileAndPlayFromExile(game, source, card, TargetController.YOU, Duration.EndOfTurn, true, false, true);
break; break;
} }
} while (card != null && card.isLand(game)); } while (card.isLand(game));
return true; return true;
} }

View file

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

View file

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