GUI: hand - added default card sorting on mulligan (lands > other > creatures; mana value, name);

This commit is contained in:
Oleg Agafonov 2023-12-14 22:13:07 +04:00
parent 525ca9a1a2
commit 10641f8d63
10 changed files with 188 additions and 35 deletions

View file

@ -7,10 +7,7 @@ import mage.game.Game;
import mage.util.Copyable;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.*;
public interface Cards extends Set<UUID>, Serializable, Copyable<Cards> {
@ -47,7 +44,7 @@ public interface Cards extends Set<UUID>, Serializable, Copyable<Cards> {
Set<Card> getCards(FilterCard filter, UUID playerId, Ability source, Game game);
String getValue(Game game);
String getValue(Game game); // AI related code to find changes in game state
/**
* Get a collection view of the unique non-null cards in this set.
@ -82,4 +79,6 @@ public interface Cards extends Set<UUID>, Serializable, Copyable<Cards> {
* @param game The ongoing game.
*/
void removeZone(Zone zone, Game game);
void sortCards(Game game, Comparator<? super Card> comparator);
}

View file

@ -209,4 +209,20 @@ public class CardsImpl extends LinkedHashSet<UUID> implements Cards, Serializabl
public void removeZone(Zone zone, Game game) {
removeIf(uuid -> game.getState().getZone(uuid) == zone);
}
@Override
public void sortCards(Game game, Comparator<? super Card> comparator) {
// workaround to sort linked list - re-create it, it must be safe for game
List<Card> newList = this
.stream()
.map(game::getCard)
.filter(Objects::nonNull)
.sorted(comparator)
.collect(Collectors.toList());
if (newList.size() != this.size()) {
throw new IllegalStateException("Wrong code usage: found unknown card id in hand while sorting, game is broken");
}
this.clear();
this.addAll(newList.stream().map(Card::getId).collect(Collectors.toList()));
}
}

View file

@ -12,7 +12,7 @@ public abstract class Mulligan implements Serializable {
protected final int freeMulligans;
protected final Map<UUID, Integer> usedFreeMulligans = new HashMap<>();
public Mulligan(int freeMulligans) {
Mulligan(int freeMulligans) {
this.freeMulligans = freeMulligans;
}
@ -93,5 +93,8 @@ public abstract class Mulligan implements Serializable {
public void drawHand(int numCards, Player player, Game game){
player.drawCards(numCards, null, game);
// default hand sorting
player.getHand().sortCards(game, new MulliganDefaultHandSorter());
}
}

View file

@ -0,0 +1,34 @@
package mage.game.mulligan;
import mage.cards.Card;
import java.util.Comparator;
/**
* GUI related, hand card sorting for mulligan
*
* @author JayDi85
*/
public class MulliganDefaultHandSorter implements Comparator<Card> {
@Override
public int compare(Card c1, Card c2) {
// groups: lands > other > creatures
// inside group: by mana value, by name
// lands
if (c1.isLand() != c2.isLand()) {
return Boolean.compare(c2.isLand(), c1.isLand());
}
// creatures
if (c1.isCreature() != c2.isCreature()) {
return Boolean.compare(c1.isCreature(), c2.isCreature());
}
// by mana
if (c1.getManaValue() != c2.getManaValue()) {
return Integer.compare(c1.getManaValue(), c2.getManaValue());
}
// by name
return c1.getName().compareTo(c2.getName());
}
}

View file

@ -66,6 +66,9 @@ public class SmoothedLondonMulligan extends LondonMulligan {
} else { //not enough cards in library or hand, just do a normal draw instead
player.drawCards(numCards, null, game);
}
// default hand sorting
player.getHand().sortCards(game, new MulliganDefaultHandSorter());
}
@Override