refactor: additional todos for players lists

This commit is contained in:
Oleg Agafonov 2024-07-01 16:25:55 +04:00
parent 1e2d179410
commit ce15d190e1
5 changed files with 31 additions and 19 deletions

View file

@ -66,7 +66,7 @@ class BarbarianBullyEffect extends OneShotEffect {
return false;
}
boolean costPaid = false;
for (UUID playerId : game.getState().getPlayerList(source.getControllerId())) {
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;

View file

@ -159,20 +159,23 @@ public interface Game extends MageItem, Serializable, Copyable<Game> {
Player getPlayerOrPlaneswalkerController(UUID playerId);
/**
* Static players list from start of the game. Use it to find player by ID.
* Static players list from start of the game. Use it to find player by ID or in game engine.
*/
Players getPlayers();
/**
* Static players list from start of the game. Use it to interate by starting turn order.
* WARNING, it's ignore range and leaved players, so use it game engine only
* WARNING, it's ignore range and leaved players, so use it by game engine only
*/
// TODO: check usage of getPlayerList in cards and replace by game.getState().getPlayersInRange
PlayerList getPlayerList();
/**
* Returns opponents list in range for the given playerId. Use it to interate by starting turn order.
* Warning, it will return dead players until end of turn.
*
* Warning, it will return leaved players until end of turn. For dialogs and one shot effects use excludeLeavedPlayers
*/
// TODO: check usage of getOpponents in cards and replace with correct call of excludeLeavedPlayers
default Set<UUID> getOpponents(UUID playerId) {
return getOpponents(playerId, false);
}
@ -181,9 +184,9 @@ public interface Game extends MageItem, Serializable, Copyable<Game> {
* Returns opponents list in range for the given playerId. Use it to interate by starting turn order.
* Warning, it will return dead players until end of turn.
*
* @param excludeDeadPlayers exclude dead player immediately without waiting range update on next turn
* @param excludeLeavedPlayers exclude dead player immediately without waiting range update on next turn
*/
default Set<UUID> getOpponents(UUID playerId, boolean excludeDeadPlayers) {
default Set<UUID> getOpponents(UUID playerId, boolean excludeLeavedPlayers) {
Player player = getPlayer(playerId);
if (player == null) {
return new LinkedHashSet<>();
@ -192,7 +195,7 @@ public interface Game extends MageItem, Serializable, Copyable<Game> {
return this.getPlayerList().stream()
.filter(opponentId -> !opponentId.equals(playerId))
.filter(player::hasPlayerInRange)
.filter(opponentId -> !excludeDeadPlayers || !getPlayer(opponentId).hasLost())
.filter(opponentId -> !excludeLeavedPlayers || getPlayer(opponentId).isInGame())
.collect(Collectors.toCollection(LinkedHashSet::new));
}

View file

@ -247,6 +247,10 @@ public abstract class GameCommanderImpl extends GameImpl {
@Override
protected boolean checkStateBasedActions() {
for (Player player : getPlayers().values()) {
if (!player.isInGame()) {
continue;
}
for (UUID commanderId : this.getCommandersIds(player, CommanderCardType.COMMANDER_OR_OATHBREAKER, false)) {
CommanderInfoWatcher damageWatcher = getState().getWatcher(CommanderInfoWatcher.class, commanderId);
if (damageWatcher == null) {
@ -255,7 +259,7 @@ public abstract class GameCommanderImpl extends GameImpl {
for (Map.Entry<UUID, Integer> entrySet : damageWatcher.getDamageToPlayer().entrySet()) {
if (entrySet.getValue() > 20) {
Player opponent = getPlayer(entrySet.getKey());
if (opponent != null && !opponent.hasLost() && player.isInGame()) {
if (opponent != null && !opponent.hasLost()) {
opponent.lost(this);
}
}

View file

@ -719,6 +719,8 @@ public class GameState implements Serializable, Copyable<GameState> {
/**
* Returns a list of all players of the game ignoring range or if a player
* has lost or left the game.
*
* Warning, it's ignore range, must be used by game engine only.
*/
public PlayerList getPlayerList() {
return playerList;
@ -728,13 +730,12 @@ public class GameState implements Serializable, Copyable<GameState> {
* Returns a list of all active players of the game, setting the playerId to
* the current player of the list.
*
* @param playerId
* @return playerList
* Warning, it's ignore range, must be used by game engine only.
*/
public PlayerList getPlayerList(UUID playerId) {
PlayerList newPlayerList = new PlayerList();
for (Player player : players.values()) {
if (!player.hasLeft() && !player.hasLost()) {
if (player.isInGame()) {
newPlayerList.add(player.getId());
}
}
@ -742,26 +743,29 @@ public class GameState implements Serializable, Copyable<GameState> {
return newPlayerList;
}
// TODO: check usage of getPlayersInRange in cards and replace with correct call of excludeLeavedPlayers
public PlayerList getPlayersInRange(UUID playerId, Game game) {
return getPlayersInRange(playerId, game, false);
}
/**
* Returns a list of all active players of the game in range of playerId,
* also setting the playerId to the first/current player of the list. Also
* returning the other players in turn order.
* <p>
* Not safe for continuous effects, see rule 800.4k (effects must work until
* end of turn even after player leaves) Use Player.InRange() to find active
* players list at the start of the turn
* Continuous effects, triggers and other must include leaved players, see rule 800.4k (effects must work until
* end of turn even after player leaves). But one short effects and dialogs must use actual players list.
*
* @param playerId
* @param game
* @return playerList
* @param excludeLeavedPlayers - true for dialogs and one short effects, false for triggers and continuous effects
*/
public PlayerList getPlayersInRange(UUID playerId, Game game) {
public PlayerList getPlayersInRange(UUID playerId, Game game, boolean excludeLeavedPlayers) {
PlayerList newPlayerList = new PlayerList();
Player currentPlayer = game.getPlayer(playerId);
if (currentPlayer != null) {
// must fill PlayerList by table added order (same as main game)
for (Player player : players.values()) {
if (player.isInGame() && currentPlayer.hasPlayerInRange(player.getId())) {
if ((!excludeLeavedPlayers || player.isInGame())
&& currentPlayer.hasPlayerInRange(player.getId())) {
newPlayerList.add(player.getId());
}
}

View file

@ -277,6 +277,7 @@ public interface Player extends MageItem, Copyable<Player> {
void idleTimeout(Game game);
// TODO: research usage of !hasLeft() && !hasLost() replace it by isInGame() if possible
boolean hasLeft();
/**