From 33715e446d06e39467bca45a48e1e24eeb5658c1 Mon Sep 17 00:00:00 2001 From: Hanmac Date: Tue, 10 Jan 2017 19:54:30 +0000 Subject: [PATCH] PlayerCollection: add new class as spezialisation of FCollection --- .gitattributes | 1 + forge-game/src/main/java/forge/game/Game.java | 21 +++--- .../main/java/forge/game/player/Player.java | 54 +++------------ .../forge/game/player/PlayerCollection.java | 66 +++++++++++++++++++ .../forge/game/player/PlayerPredicates.java | 22 +++++++ 5 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 forge-game/src/main/java/forge/game/player/PlayerCollection.java diff --git a/.gitattributes b/.gitattributes index 8f0cb5f6732..51aa9a01755 100644 --- a/.gitattributes +++ b/.gitattributes @@ -623,6 +623,7 @@ forge-game/src/main/java/forge/game/player/GameLossReason.java -text forge-game/src/main/java/forge/game/player/IGameEntitiesFactory.java -text forge-game/src/main/java/forge/game/player/Player.java svneol=native#text/plain forge-game/src/main/java/forge/game/player/PlayerActionConfirmMode.java -text +forge-game/src/main/java/forge/game/player/PlayerCollection.java -text svneol=unset#text/plain forge-game/src/main/java/forge/game/player/PlayerController.java -text forge-game/src/main/java/forge/game/player/PlayerOutcome.java -text forge-game/src/main/java/forge/game/player/PlayerPredicates.java -text svneol=unset#text/plain diff --git a/forge-game/src/main/java/forge/game/Game.java b/forge-game/src/main/java/forge/game/Game.java index 4efd5bdb017..295dbfd4a99 100644 --- a/forge-game/src/main/java/forge/game/Game.java +++ b/forge-game/src/main/java/forge/game/Game.java @@ -45,6 +45,7 @@ import forge.game.phase.PhaseType; import forge.game.phase.Untap; import forge.game.player.IGameEntitiesFactory; import forge.game.player.Player; +import forge.game.player.PlayerCollection; import forge.game.player.PlayerView; import forge.game.player.RegisteredPlayer; import forge.game.replacement.ReplacementHandler; @@ -57,8 +58,6 @@ import forge.game.trigger.TriggerType; import forge.game.zone.*; import forge.trackable.Tracker; import forge.util.Aggregates; -import forge.util.collect.FCollection; -import forge.util.collect.FCollectionView; import forge.util.Visitor; /** @@ -66,9 +65,9 @@ import forge.util.Visitor; */ public class Game { private final GameRules rules; - private final FCollection allPlayers = new FCollection(); - private final FCollection ingamePlayers = new FCollection(); - private final FCollection lostPlayers = new FCollection(); + private final PlayerCollection allPlayers = new PlayerCollection(); + private final PlayerCollection ingamePlayers = new PlayerCollection(); + private final PlayerCollection lostPlayers = new PlayerCollection(); private List activePlanes = null; @@ -272,22 +271,22 @@ public class Game { /** * Gets the players who are still fighting to win. */ - public final FCollectionView getPlayers() { + public final PlayerCollection getPlayers() { return ingamePlayers; } - public final FCollectionView getLostPlayers() { + public final PlayerCollection getLostPlayers() { return lostPlayers; } /** * Gets the players who are still fighting to win, in turn order. */ - public final FCollectionView getPlayersInTurnOrder() { + public final PlayerCollection getPlayersInTurnOrder() { if (turnOrder.isDefaultDirection()) { return ingamePlayers; } - final FCollection players = new FCollection(ingamePlayers); + final PlayerCollection players = new PlayerCollection(ingamePlayers); Collections.reverse(players); return players; } @@ -295,9 +294,9 @@ public class Game { /** * Gets the nonactive players who are still fighting to win, in turn order. */ - public final FCollectionView getNonactivePlayers() { + public final PlayerCollection getNonactivePlayers() { // Don't use getPlayersInTurnOrder to prevent copying the player collection twice - final FCollection players = new FCollection<>(ingamePlayers); + final PlayerCollection players = new PlayerCollection(ingamePlayers); players.remove(phaseHandler.getPlayerTurn()); if (!turnOrder.isDefaultDirection()) { Collections.reverse(players); diff --git a/forge-game/src/main/java/forge/game/player/Player.java b/forge-game/src/main/java/forge/game/player/Player.java index 9ada28f3d03..f2010af7afa 100644 --- a/forge-game/src/main/java/forge/game/player/Player.java +++ b/forge-game/src/main/java/forge/game/player/Player.java @@ -18,7 +18,7 @@ package forge.game.player; import com.google.common.base.Function; -import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -263,14 +263,8 @@ public class Player extends GameEntity implements Comparable { * returns all opponents. * Should keep player relations somewhere in the match structure */ - public final FCollection getOpponents() { - FCollection result = new FCollection(); - for (Player p : game.getPlayers()) { - if (p.isOpponentOf(this)) { - result.add(p); - } - } - return result; + public final PlayerCollection getOpponents() { + return game.getPlayers().filter(PlayerPredicates.isOpponentOf(this)); } public void updateOpponentsForView() { @@ -319,22 +313,16 @@ public class Player extends GameEntity implements Comparable { * returns allied players. * Should keep player relations somewhere in the match structure */ - public final FCollection getAllies() { - FCollection result = new FCollection(); - for (Player p : game.getPlayers()) { - if (!p.isOpponentOf(this)) { - result.add(p); - } - } - return result; + public final PlayerCollection getAllies() { + return game.getPlayers().filter(Predicates.not(PlayerPredicates.isOpponentOf(this))); } /** * returns all other players. * Should keep player relations somewhere in the match structure */ - public final FCollection getAllOtherPlayers() { - FCollection result = new FCollection(game.getPlayers()); + public final PlayerCollection getAllOtherPlayers() { + PlayerCollection result = new PlayerCollection(game.getPlayers()); result.remove(this); return result; } @@ -344,14 +332,7 @@ public class Player extends GameEntity implements Comparable { * Should keep player relations somewhere in the match structure */ public final Player getWeakestOpponent() { - List opponents = getOpponents(); - Player weakest = opponents.get(0); - for (int i = 1; i < opponents.size(); i++) { - if (weakest.getLife() > opponents.get(i).getLife()) { - weakest = opponents.get(i); - } - } - return weakest; + return getOpponents().min(PlayerPredicates.compareByLife()); } public boolean isOpponentOf(Player other) { @@ -1838,7 +1819,7 @@ public class Player extends GameEntity implements Comparable { if (getOutcome() != null && getOutcome().lossState == GameLossReason.Conceded) { return false; } - return (hasKeyword("You can't lose the game.") || Iterables.any(getOpponents(), Predicates.CANT_WIN)); + return (hasKeyword("You can't lose the game.") || Iterables.any(getOpponents(), PlayerPredicates.CANT_WIN)); } public final boolean cantLoseForZeroOrLessLife() { @@ -1952,7 +1933,7 @@ public class Player extends GameEntity implements Comparable { } public final boolean hasSurge() { - FCollection list = getAllies(); + PlayerCollection list = getAllies(); list.add(this); return !CardLists.filterControlledBy(game.getStack().getSpellsCastThisTurn(), list).isEmpty(); } @@ -2490,21 +2471,6 @@ public class Player extends GameEntity implements Comparable { return getName().compareTo(o.getName()); } - public static class Predicates { - public static final Predicate NOT_LOST = new Predicate() { - @Override - public boolean apply(Player p) { - return p.getOutcome() == null || p.getOutcome().hasWon(); - } - }; - public static final Predicate CANT_WIN = new Predicate() { - @Override - public boolean apply(final Player p) { - return p.hasKeyword("You can't win the game."); - } - }; - } - public static class Accessors { public static Function FN_GET_NAME = new Function() { @Override diff --git a/forge-game/src/main/java/forge/game/player/PlayerCollection.java b/forge-game/src/main/java/forge/game/player/PlayerCollection.java new file mode 100644 index 00000000000..33c2ce0b827 --- /dev/null +++ b/forge-game/src/main/java/forge/game/player/PlayerCollection.java @@ -0,0 +1,66 @@ +package forge.game.player; + +import java.util.Collections; +import java.util.Comparator; + +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + +import forge.game.card.CardCollection; +import forge.game.zone.ZoneType; +import forge.util.Aggregates; +import forge.util.collect.FCollection; + +public class PlayerCollection extends FCollection { + + private static final long serialVersionUID = -4374566955977201748L; + + public PlayerCollection() { + } + + public PlayerCollection(Iterable players) { + this.addAll(players); + } + + // card collection functions + public final CardCollection getCardsIn(ZoneType zone) { + CardCollection result = new CardCollection(); + for (Player p : this) { + result.addAll(p.getCardsIn(zone)); + } + return result; + } + + public final CardCollection getCreaturesInPlay() { + CardCollection result = new CardCollection(); + for (Player p : this) { + result.addAll(p.getCreaturesInPlay()); + } + return result; + } + + // filter functions with predicate + public PlayerCollection filter(Predicate pred) { + return new PlayerCollection(Iterables.filter(this, pred)); + } + + // sort functions with Comparator + Player min(Comparator comp) { + return Collections.min(this, comp); + } + Player max(Comparator comp) { + return Collections.min(this, comp); + } + + // value functions with Function + Integer min(Function func) { + return Aggregates.min(this, func); + } + Integer max(Function func) { + return Aggregates.max(this, func); + } + Integer sum(Function func) { + return Aggregates.sum(this, func); + } +} diff --git a/forge-game/src/main/java/forge/game/player/PlayerPredicates.java b/forge-game/src/main/java/forge/game/player/PlayerPredicates.java index 80b2d7e0a82..36375849a15 100644 --- a/forge-game/src/main/java/forge/game/player/PlayerPredicates.java +++ b/forge-game/src/main/java/forge/game/player/PlayerPredicates.java @@ -61,4 +61,26 @@ public final class PlayerPredicates { } }; } + + public static final Comparator compareByLife() { + return new Comparator() { + @Override + public int compare(Player arg0, Player arg1) { + return Integer.compare(arg0.getLife(), arg1.getLife()); + } + }; + } + + public static final Predicate NOT_LOST = new Predicate() { + @Override + public boolean apply(Player p) { + return p.getOutcome() == null || p.getOutcome().hasWon(); + } + }; + public static final Predicate CANT_WIN = new Predicate() { + @Override + public boolean apply(final Player p) { + return p.hasKeyword("You can't win the game."); + } + }; }