* Fixed AI game freeze on put cards to library (#5023);

This commit is contained in:
Oleg Agafonov 2019-04-18 15:46:48 +04:00
parent 13cb3a443a
commit 184af3f681

View file

@ -880,30 +880,27 @@ public abstract class PlayerImpl implements Player, Serializable {
if (!cardsToLibrary.isEmpty()) { if (!cardsToLibrary.isEmpty()) {
Cards cards = new CardsImpl(cardsToLibrary); // prevent possible ConcurrentModificationException Cards cards = new CardsImpl(cardsToLibrary); // prevent possible ConcurrentModificationException
if (!anyOrder) { if (!anyOrder) {
while (!cards.isEmpty()) { // random order
Card card = cards.getRandom(game); List<UUID> ids = new ArrayList<>(cards);
if (card != null) { Collections.shuffle(ids);
cards.remove(card); for (UUID id : ids) {
moveObjectToLibrary(card.getId(), source == null ? null : source.getSourceId(), game, false, false); moveObjectToLibrary(id, source == null ? null : source.getSourceId(), game, false, false);
} else {
return false;// probably cards were removed because player left the game
}
} }
} else { } else {
// user defined order
TargetCard target = new TargetCard(Zone.ALL, new FilterCard("card ORDER to put on the BOTTOM of your library (last one chosen will be bottommost)")); TargetCard target = new TargetCard(Zone.ALL, new FilterCard("card ORDER to put on the BOTTOM of your library (last one chosen will be bottommost)"));
target.setRequired(true); target.setRequired(true);
while (cards.size() > 1) { while (cards.size() > 1 && this.canRespond() && this.choose(Outcome.Neutral, cards, target, game)) {
this.choose(Outcome.Neutral, cards, target, game);
if (!canRespond()) {
return false;
}
UUID targetObjectId = target.getFirstTarget(); UUID targetObjectId = target.getFirstTarget();
if (targetObjectId == null) {
break;
}
cards.remove(targetObjectId); cards.remove(targetObjectId);
moveObjectToLibrary(targetObjectId, source == null ? null : source.getSourceId(), game, false, false); moveObjectToLibrary(targetObjectId, source == null ? null : source.getSourceId(), game, false, false);
target.clearChosen(); target.clearChosen();
} }
if (cards.size() == 1) { for (UUID c : cards) {
moveObjectToLibrary(cards.iterator().next(), source == null ? null : source.getSourceId(), game, false, false); moveObjectToLibrary(c, source == null ? null : source.getSourceId(), game, false, false);
} }
} }
} }
@ -945,30 +942,27 @@ public abstract class PlayerImpl implements Player, Serializable {
Cards cards = new CardsImpl(cardsToLibrary); // prevent possible ConcurrentModificationException Cards cards = new CardsImpl(cardsToLibrary); // prevent possible ConcurrentModificationException
UUID sourceId = (source == null ? null : source.getSourceId()); UUID sourceId = (source == null ? null : source.getSourceId());
if (!anyOrder) { if (!anyOrder) {
while (!cards.isEmpty()) { // random order
Card card = cards.getRandom(game); List<UUID> ids = new ArrayList<>(cards);
if (card != null) { Collections.shuffle(ids);
cards.remove(card.getId()); for (UUID id : ids) {
moveObjectToLibrary(card.getId(), source == null ? null : source.getSourceId(), game, true, false); moveObjectToLibrary(id, source == null ? null : source.getSourceId(), game, true, false);
} else {
return false; // probably cards were removed because player left the game
}
} }
} else { } else {
TargetCard target = new TargetCard(Zone.LIBRARY, new FilterCard("card ORDER to put on the TOP of your library (last one chosen will be topmost)")); // user defined order
TargetCard target = new TargetCard(Zone.ALL, new FilterCard("card ORDER to put on the TOP of your library (last one chosen will be topmost)"));
target.setRequired(true); target.setRequired(true);
while (cards.size() > 1) { while (cards.size() > 1 && this.canRespond() && this.choose(Outcome.Neutral, cards, target, game)) {
this.choose(Outcome.Neutral, cards, target, game);
if (!canRespond()) {
return false;
}
UUID targetObjectId = target.getFirstTarget(); UUID targetObjectId = target.getFirstTarget();
if (targetObjectId == null) {
break;
}
cards.remove(targetObjectId); cards.remove(targetObjectId);
moveObjectToLibrary(targetObjectId, sourceId, game, true, false); moveObjectToLibrary(targetObjectId, source == null ? null : source.getSourceId(), game, true, false);
target.clearChosen(); target.clearChosen();
} }
if (cards.size() == 1) { for (UUID c : cards) {
moveObjectToLibrary(cards.iterator().next(), sourceId, game, true, false); moveObjectToLibrary(c, source == null ? null : source.getSourceId(), game, true, false);
} }
} }
} }