mirror of
https://github.com/magefree/mage.git
synced 2025-12-26 13:32:06 -08:00
* GUI: fixed non-closeable choose dialog with empty list on server's error (example: Cabal Therapy, see #8065);
This commit is contained in:
parent
d587cc9151
commit
62474c12a4
3 changed files with 28 additions and 74 deletions
|
|
@ -8,7 +8,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
public class ChoiceCreatureType extends ChoiceImpl {
|
||||
|
||||
private static String DEFAULT_MESSAGE = "Choose a creature type";
|
||||
private static final String DEFAULT_MESSAGE = "Choose a creature type";
|
||||
|
||||
public ChoiceCreatureType() {
|
||||
this(true, DEFAULT_MESSAGE, null);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package mage.choices;
|
||||
|
||||
import mage.util.RandomUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
|
@ -9,9 +10,11 @@ import java.util.*;
|
|||
*/
|
||||
public class ChoiceImpl implements Choice {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Choice.class);
|
||||
|
||||
protected boolean chosenNormal;
|
||||
protected boolean chosenSpecial;
|
||||
protected final boolean required;
|
||||
protected boolean required;
|
||||
protected String choice;
|
||||
protected String choiceKey;
|
||||
protected Set<String> choices = new LinkedHashSet<>();
|
||||
|
|
@ -109,6 +112,7 @@ public class ChoiceImpl implements Choice {
|
|||
@Override
|
||||
public void setChoices(Set<String> choices) {
|
||||
this.choices = choices;
|
||||
protectFromEmptyChoices();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -149,6 +153,7 @@ public class ChoiceImpl implements Choice {
|
|||
@Override
|
||||
public void setKeyChoices(Map<String, String> choices) {
|
||||
keyChoices = choices;
|
||||
protectFromEmptyChoices();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -314,4 +319,25 @@ public class ChoiceImpl implements Choice {
|
|||
public ChoiceHintType getHintType() {
|
||||
return this.hintType;
|
||||
}
|
||||
|
||||
private void protectFromEmptyChoices() {
|
||||
// if there are no choices then required must be disabled to allow user to close a dialog
|
||||
// example: database error on too low memory, see Brain Pry and 500 Mb server
|
||||
|
||||
// normal situation
|
||||
if (!this.required) {
|
||||
return;
|
||||
}
|
||||
|
||||
// special checkbox can allow empty choices
|
||||
if (this.specialEnabled && this.specialCanBeEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.choices.isEmpty() && this.keyChoices.isEmpty()) {
|
||||
// it can be a server problems or wrong card code
|
||||
this.required = false;
|
||||
logger.error("Empty choice dialog in " + this.getClass().getCanonicalName(), new Throwable());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
|
||||
package mage.choices;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.players.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class Choices extends ArrayList<Choice> {
|
||||
|
||||
protected Outcome outcome;
|
||||
|
||||
public Choices() {
|
||||
}
|
||||
|
||||
public Choices(final Choices choices) {
|
||||
this.outcome = choices.outcome;
|
||||
for (Choice choice : choices) {
|
||||
this.add(choice.copy());
|
||||
}
|
||||
}
|
||||
|
||||
public Choices copy() {
|
||||
return new Choices(this);
|
||||
}
|
||||
|
||||
public List<Choice> getUnchosen() {
|
||||
return stream()
|
||||
.filter(choice -> !choice.isChosen())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void clearChosen() {
|
||||
for (Choice choice : this) {
|
||||
choice.clearChoice();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isChosen() {
|
||||
for (Choice choice : this) {
|
||||
if (!choice.isChosen()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean choose(Game game, Ability source) {
|
||||
if (this.size() > 0) {
|
||||
Player player = game.getPlayer(source.getControllerId());
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
while (!isChosen()) {
|
||||
Choice choice = this.getUnchosen().get(0);
|
||||
if (!player.choose(outcome, choice, game)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue