PlayerCollection: add new class as spezialisation of FCollection<Player>

This commit is contained in:
Hanmac
2017-01-10 19:54:30 +00:00
parent 6902491797
commit 33715e446d
5 changed files with 109 additions and 55 deletions

1
.gitattributes vendored
View File

@@ -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

View File

@@ -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<Player> allPlayers = new FCollection<Player>();
private final FCollection<Player> ingamePlayers = new FCollection<Player>();
private final FCollection<Player> lostPlayers = new FCollection<Player>();
private final PlayerCollection allPlayers = new PlayerCollection();
private final PlayerCollection ingamePlayers = new PlayerCollection();
private final PlayerCollection lostPlayers = new PlayerCollection();
private List<Card> activePlanes = null;
@@ -272,22 +271,22 @@ public class Game {
/**
* Gets the players who are still fighting to win.
*/
public final FCollectionView<Player> getPlayers() {
public final PlayerCollection getPlayers() {
return ingamePlayers;
}
public final FCollectionView<Player> getLostPlayers() {
public final PlayerCollection getLostPlayers() {
return lostPlayers;
}
/**
* Gets the players who are still fighting to win, in turn order.
*/
public final FCollectionView<Player> getPlayersInTurnOrder() {
public final PlayerCollection getPlayersInTurnOrder() {
if (turnOrder.isDefaultDirection()) {
return ingamePlayers;
}
final FCollection<Player> players = new FCollection<Player>(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<Player> getNonactivePlayers() {
public final PlayerCollection getNonactivePlayers() {
// Don't use getPlayersInTurnOrder to prevent copying the player collection twice
final FCollection<Player> players = new FCollection<>(ingamePlayers);
final PlayerCollection players = new PlayerCollection(ingamePlayers);
players.remove(phaseHandler.getPlayerTurn());
if (!turnOrder.isDefaultDirection()) {
Collections.reverse(players);

View File

@@ -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<Player> {
* returns all opponents.
* Should keep player relations somewhere in the match structure
*/
public final FCollection<Player> getOpponents() {
FCollection<Player> result = new FCollection<Player>();
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<Player> {
* returns allied players.
* Should keep player relations somewhere in the match structure
*/
public final FCollection<Player> getAllies() {
FCollection<Player> result = new FCollection<Player>();
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<Player> getAllOtherPlayers() {
FCollection<Player> result = new FCollection<Player>(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<Player> {
* Should keep player relations somewhere in the match structure
*/
public final Player getWeakestOpponent() {
List<Player> 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<Player> {
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<Player> {
}
public final boolean hasSurge() {
FCollection<Player> 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<Player> {
return getName().compareTo(o.getName());
}
public static class Predicates {
public static final Predicate<Player> NOT_LOST = new Predicate<Player>() {
@Override
public boolean apply(Player p) {
return p.getOutcome() == null || p.getOutcome().hasWon();
}
};
public static final Predicate<Player> CANT_WIN = new Predicate<Player>() {
@Override
public boolean apply(final Player p) {
return p.hasKeyword("You can't win the game.");
}
};
}
public static class Accessors {
public static Function<Player, String> FN_GET_NAME = new Function<Player, String>() {
@Override

View File

@@ -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<Player> {
private static final long serialVersionUID = -4374566955977201748L;
public PlayerCollection() {
}
public PlayerCollection(Iterable<Player> 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<Player> pred) {
return new PlayerCollection(Iterables.filter(this, pred));
}
// sort functions with Comparator
Player min(Comparator<Player> comp) {
return Collections.min(this, comp);
}
Player max(Comparator<Player> comp) {
return Collections.min(this, comp);
}
// value functions with Function
Integer min(Function<Player, Integer> func) {
return Aggregates.min(this, func);
}
Integer max(Function<Player, Integer> func) {
return Aggregates.max(this, func);
}
Integer sum(Function<Player, Integer> func) {
return Aggregates.sum(this, func);
}
}

View File

@@ -61,4 +61,26 @@ public final class PlayerPredicates {
}
};
}
public static final Comparator<Player> compareByLife() {
return new Comparator<Player>() {
@Override
public int compare(Player arg0, Player arg1) {
return Integer.compare(arg0.getLife(), arg1.getLife());
}
};
}
public static final Predicate<Player> NOT_LOST = new Predicate<Player>() {
@Override
public boolean apply(Player p) {
return p.getOutcome() == null || p.getOutcome().hasWon();
}
};
public static final Predicate<Player> CANT_WIN = new Predicate<Player>() {
@Override
public boolean apply(final Player p) {
return p.hasKeyword("You can't win the game.");
}
};
}