gui: fixed broken watcher view mode (closes #11481)

This commit is contained in:
Oleg Agafonov 2023-11-28 08:03:58 +04:00
parent 6a1f4a35d4
commit 2eac541b80
8 changed files with 125 additions and 41 deletions

View file

@ -10,7 +10,6 @@ import javax.swing.event.DocumentListener;
import mage.abilities.hint.HintUtils; import mage.abilities.hint.HintUtils;
import mage.client.cards.BigCard; import mage.client.cards.BigCard;
import mage.client.chat.ChatPanelBasic;
import mage.client.components.MageDesktopIconifySupport; import mage.client.components.MageDesktopIconifySupport;
import mage.client.util.GUISizeHelper; import mage.client.util.GUISizeHelper;
import mage.client.util.SettingsManager; import mage.client.util.SettingsManager;
@ -232,7 +231,7 @@ public class CardHintsHelperDialog extends MageDialog implements MageDesktopIcon
.orElse(null); .orElse(null);
// hand // hand
this.lastGameView.getHand().values().forEach(card -> { this.lastGameView.getMyHand().values().forEach(card -> {
this.lastHints.add(new CardHintInfo(currentPlayer, "hand", card)); this.lastHints.add(new CardHintInfo(currentPlayer, "hand", card));
}); });

View file

@ -144,7 +144,7 @@ public final class GamePanel extends javax.swing.JPanel {
return; return;
} }
this.game.getHand().values().forEach(c -> this.allCardsIndex.put(c.getId(), c)); this.game.getMyHand().values().forEach(c -> this.allCardsIndex.put(c.getId(), c));
this.game.getStack().values().forEach(c -> this.allCardsIndex.put(c.getId(), c)); this.game.getStack().values().forEach(c -> this.allCardsIndex.put(c.getId(), c));
this.game.getExile() this.game.getExile()
.stream() .stream()
@ -789,7 +789,7 @@ public final class GamePanel extends javax.swing.JPanel {
} }
} }
if (playerId != null) { if (playerId != null) {
handCards.put(YOUR_HAND, lastGameData.game.getHand()); handCards.put(YOUR_HAND, lastGameData.game.getMyHand());
// Get opponents hand cards if available (only possible for players) // Get opponents hand cards if available (only possible for players)
if (lastGameData.game.getOpponentHands() != null) { if (lastGameData.game.getOpponentHands() != null) {
for (Map.Entry<String, SimpleCardsView> hand : lastGameData.game.getOpponentHands().entrySet()) { for (Map.Entry<String, SimpleCardsView> hand : lastGameData.game.getOpponentHands().entrySet()) {
@ -811,7 +811,7 @@ public final class GamePanel extends javax.swing.JPanel {
if (playerId != null) { if (playerId != null) {
// set visible only if we have any other hand visible than ours // set visible only if we have any other hand visible than ours
btnSwitchHands.setVisible(handCards.size() > 1); btnSwitchHands.setVisible(handCards.size() > 1);
boolean change = (handCardsOfOpponentAvailable != (lastGameData.game.getOpponentHands() != null)); boolean change = (handCardsOfOpponentAvailable == (lastGameData.game.getOpponentHands() == null));
if (change) { if (change) {
handCardsOfOpponentAvailable = !handCardsOfOpponentAvailable; handCardsOfOpponentAvailable = !handCardsOfOpponentAvailable;
if (handCardsOfOpponentAvailable) { if (handCardsOfOpponentAvailable) {
@ -1507,7 +1507,7 @@ public final class GamePanel extends javax.swing.JPanel {
// hand // hand
if (needZone == Zone.HAND || needZone == Zone.ALL) { if (needZone == Zone.HAND || needZone == Zone.ALL) {
for (CardView card : lastGameData.game.getHand().values()) { for (CardView card : lastGameData.game.getMyHand().values()) {
if (needSelectable.contains(card.getId())) { if (needSelectable.contains(card.getId())) {
card.setChoosable(true); card.setChoosable(true);
} }

View file

@ -24,7 +24,6 @@ import mage.game.stack.StackAbility;
import mage.game.stack.StackObject; import mage.game.stack.StackObject;
import mage.players.PlayableObjectsList; import mage.players.PlayableObjectsList;
import mage.players.Player; import mage.players.Player;
import mage.watchers.common.CastSpellLastTurnWatcher;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.Serializable; import java.io.Serializable;
@ -44,14 +43,15 @@ public class GameView implements Serializable {
private final int priorityTime; private final int priorityTime;
private final int bufferTime; private final int bufferTime;
private final List<PlayerView> players = new ArrayList<>(); private final List<PlayerView> players = new ArrayList<>();
private CardsView hand; private UUID myPlayerId = null; // null for watcher
private final CardsView myHand = new CardsView();
private PlayableObjectsList canPlayObjects; private PlayableObjectsList canPlayObjects;
private Map<String, SimpleCardsView> opponentHands; private Map<String, SimpleCardsView> opponentHands;
private Map<String, SimpleCardsView> watchedHands; private Map<String, SimpleCardsView> watchedHands;
private final CardsView stack = new CardsView(); private final CardsView stack = new CardsView();
private final List<ExileView> exiles = new ArrayList<>(); private final List<ExileView> exiles = new ArrayList<>();
private final List<RevealedView> revealed = new ArrayList<>(); private final List<RevealedView> revealed = new ArrayList<>();
private List<LookedAtView> lookedAt = new ArrayList<>(); private final List<LookedAtView> lookedAt = new ArrayList<>();
private final List<RevealedView> companion = new ArrayList<>(); private final List<RevealedView> companion = new ArrayList<>();
private final List<CombatGroupView> combat = new ArrayList<>(); private final List<CombatGroupView> combat = new ArrayList<>();
private final TurnPhase phase; private final TurnPhase phase;
@ -61,18 +61,20 @@ public class GameView implements Serializable {
private final String priorityPlayerName; private final String priorityPlayerName;
private final int turn; private final int turn;
private boolean special = false; private boolean special = false;
private final boolean isPlayer; // false = watching user
private final boolean rollbackTurnsAllowed; private final boolean rollbackTurnsAllowed;
public GameView(GameState state, Game game, UUID createdForPlayerId, UUID watcherUserId) { public GameView(GameState state, Game game, UUID createdForPlayerId, UUID watcherUserId) {
Player createdForPlayer = null; Player createdForPlayer = null;
this.isPlayer = createdForPlayerId != null;
this.priorityTime = game.getPriorityTime(); this.priorityTime = game.getPriorityTime();
this.bufferTime = game.getBufferTime(); this.bufferTime = game.getBufferTime();
for (Player player : state.getPlayers().values()) { for (Player player : state.getPlayers().values()) {
players.add(new PlayerView(player, state, game, createdForPlayerId, watcherUserId)); PlayerView playerView = new PlayerView(player, state, game, createdForPlayerId, watcherUserId);
players.add(playerView);
if (player.getId().equals(createdForPlayerId)) { if (player.getId().equals(createdForPlayerId)) {
createdForPlayer = player; createdForPlayer = player;
this.myPlayerId = player.getId();
this.myHand.putAll(new CardsView(game, player.getHand().getCards(game)));
} }
} }
for (StackObject stackObject : state.getStack()) { for (StackObject stackObject : state.getStack()) {
@ -159,6 +161,11 @@ public class GameView implements Serializable {
for (String name : state.getRevealed().keySet()) { for (String name : state.getRevealed().keySet()) {
revealed.add(new RevealedView(name, state.getRevealed().get(name), game)); revealed.add(new RevealedView(name, state.getRevealed().get(name), game));
} }
if (this.myPlayerId != null) {
for (String name : state.getLookedAt(this.myPlayerId).keySet()){
lookedAt.add(new LookedAtView(name, state.getLookedAt(this.myPlayerId).get(name), game));
}
}
for (String name : state.getCompanion().keySet()) { for (String name : state.getCompanion().keySet()) {
// Only show the companion window when the companion is still outside the game. // Only show the companion window when the companion is still outside the game.
if (state.getCompanion().get(name).stream().anyMatch(cardId -> state.getZone(cardId) == Zone.OUTSIDE)) { if (state.getCompanion().get(name).stream().anyMatch(cardId -> state.getZone(cardId) == Zone.OUTSIDE)) {
@ -184,9 +191,9 @@ public class GameView implements Serializable {
for (CombatGroup combatGroup : state.getCombat().getGroups()) { for (CombatGroup combatGroup : state.getCombat().getGroups()) {
combat.add(new CombatGroupView(combatGroup, game)); combat.add(new CombatGroupView(combatGroup, game));
} }
if (isPlayer) { // no watcher if (this.myPlayerId != null) { // no watcher
// has only to be set for active player with priority (e.g. pay mana by delve or Quenchable Fire special action) // has only to be set for active player with priority (e.g. pay mana by delve or Quenchable Fire special action)
if (priorityPlayer != null && createdForPlayer != null && createdForPlayerId != null && createdForPlayer.isGameUnderControl() if (priorityPlayer != null && createdForPlayer != null && createdForPlayer.isGameUnderControl()
&& (createdForPlayerId.equals(priorityPlayer.getId()) // player controls the turn && (createdForPlayerId.equals(priorityPlayer.getId()) // player controls the turn
|| createdForPlayer.getPlayersUnderYourControl().contains(priorityPlayer.getId()))) { // player controls active players turn || createdForPlayer.getPlayersUnderYourControl().contains(priorityPlayer.getId()))) { // player controls active players turn
this.special = !state.getSpecialActions().getControlledBy(priorityPlayer.getId(), priorityPlayer.isInPayManaMode()).isEmpty(); this.special = !state.getSpecialActions().getControlledBy(priorityPlayer.getId(), priorityPlayer.isInPayManaMode()).isEmpty();
@ -227,12 +234,16 @@ public class GameView implements Serializable {
return players; return players;
} }
public CardsView getHand() { public CardsView getMyHand() {
return hand; return myHand;
} }
public void setHand(CardsView hand) { public PlayerView getMyPlayer() {
this.hand = hand; if (this.myPlayerId == null) {
return null;
} else {
return players.stream().filter(p -> p.getPlayerId().equals(this.myPlayerId)).findFirst().orElse(null);
}
} }
public Map<String, SimpleCardsView> getOpponentHands() { public Map<String, SimpleCardsView> getOpponentHands() {
@ -279,10 +290,6 @@ public class GameView implements Serializable {
return companion; return companion;
} }
public void setLookedAt(List<LookedAtView> list) {
this.lookedAt = list;
}
public List<CombatGroupView> getCombat() { public List<CombatGroupView> getCombat() {
return combat; return combat;
} }
@ -316,7 +323,7 @@ public class GameView implements Serializable {
} }
public boolean isPlayer() { public boolean isPlayer() {
return isPlayer; return this.myPlayerId != null;
} }
public PlayableObjectsList getCanPlayObjects() { public PlayableObjectsList getCanPlayObjects() {

View file

@ -209,27 +209,26 @@ public class GameSessionPlayer extends GameSessionWatcher {
* @return * @return
*/ */
public static GameView prepareGameView(Game game, UUID playerId, UUID userId) { public static GameView prepareGameView(Game game, UUID playerId, UUID userId) {
Player player = game.getPlayer(playerId); Player player = game.getPlayer(playerId); // null for watcher
GameView gameView = new GameView(game.getState(), game, playerId, null); GameView gameView = new GameView(game.getState(), game, playerId, null);
gameView.setHand(new CardsView(game, player.getHand().getCards(game))); if (player != null) {
if (gameView.getPriorityPlayerName().equals(player.getName())) { if (gameView.getPriorityPlayerName().equals(player.getName())) {
gameView.setCanPlayObjects(player.getPlayableObjects(game, Zone.ALL)); gameView.setCanPlayObjects(player.getPlayableObjects(game, Zone.ALL));
} }
}
processControlledPlayers(game, player, gameView); processControlledPlayers(game, player, gameView);
processWatchedHands(game, userId, gameView); processWatchedHands(game, userId, gameView);
//TODO: should player who controls another player's turn be able to look at all these cards? //TODO: should player who controls another player's turn be able to look at all these cards?
List<LookedAtView> list = new ArrayList<>();
for (Entry<String, Cards> entry : game.getState().getLookedAt(playerId).entrySet()) {
list.add(new LookedAtView(entry.getKey(), entry.getValue(), game));
}
gameView.setLookedAt(list);
return gameView; return gameView;
} }
private static void processControlledPlayers(Game game, Player player, GameView gameView) { private static void processControlledPlayers(Game game, Player player, GameView gameView) {
if (player == null) {
// ignore watcher
return;
}
if (!player.getPlayersUnderYourControl().isEmpty()) { if (!player.getPlayersUnderYourControl().isEmpty()) {
Map<String, SimpleCardsView> handCards = new HashMap<>(); Map<String, SimpleCardsView> handCards = new HashMap<>();
for (UUID controlledPlayerId : player.getPlayersUnderYourControl()) { for (UUID controlledPlayerId : player.getPlayersUnderYourControl()) {

View file

@ -104,8 +104,8 @@ public class LoadCallbackClient implements CallbackClient {
session.sendPlayerUUID(gameId, playerId); session.sendPlayerUUID(gameId, playerId);
return; return;
case "Select a card to discard": case "Select a card to discard":
log.info(getLogStartInfo() + "hand size: " + gameView.getHand().size()); log.info(getLogStartInfo() + "hand size: " + gameView.getMyHand().size());
SimpleCardView card = gameView.getHand().values().iterator().next(); SimpleCardView card = gameView.getMyHand().values().iterator().next();
session.sendPlayerUUID(gameId, card.getId()); session.sendPlayerUUID(gameId, card.getId());
return; return;
default: default:

View file

@ -27,8 +27,8 @@ public class CardIconsTest extends CardTestPlayerBase {
// hand (not visible) // hand (not visible)
runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> { runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
GameView gameView = getGameView(player); GameView gameView = getGameView(player);
Assert.assertEquals("must have 1 card in hand", 1, gameView.getHand().values().size()); Assert.assertEquals("must have 1 card in hand", 1, gameView.getMyHand().values().size());
CardView cardView = gameView.getHand().values().stream().findFirst().get(); CardView cardView = gameView.getMyHand().values().stream().findFirst().get();
Assert.assertEquals("must have non x cost card icons in hand", 0, cardView.getCardIcons().size()); Assert.assertEquals("must have non x cost card icons in hand", 0, cardView.getCardIcons().size());
}); });
@ -224,8 +224,8 @@ public class CardIconsTest extends CardTestPlayerBase {
// hand (not visible) // hand (not visible)
runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> { runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
GameView gameView = getGameView(player); GameView gameView = getGameView(player);
Assert.assertEquals("must have 1 card in hand", 1, gameView.getHand().values().size()); Assert.assertEquals("must have 1 card in hand", 1, gameView.getMyHand().values().size());
CardView cardView = gameView.getHand().values().stream().findFirst().get(); CardView cardView = gameView.getMyHand().values().stream().findFirst().get();
Assert.assertEquals("must have non x cost card icons in hand", 0, cardView.getCardIcons().size()); Assert.assertEquals("must have non x cost card icons in hand", 0, cardView.getCardIcons().size());
}); });
@ -296,7 +296,7 @@ public class CardIconsTest extends CardTestPlayerBase {
// hand (not visible) // hand (not visible)
runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> { runCode("card icons in hand", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
GameView gameView = getGameView(player); GameView gameView = getGameView(player);
CardView cardView = gameView.getHand().values().stream() CardView cardView = gameView.getMyHand().values().stream()
.filter(c -> c.getName().equals("Agadeem's Awakening")) .filter(c -> c.getName().equals("Agadeem's Awakening"))
.findFirst() .findFirst()
.orElse(null); .orElse(null);

View file

@ -0,0 +1,68 @@
package org.mage.test.serverside;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import mage.view.GameView;
import org.junit.Assert;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
import java.util.UUID;
/**
* @author JayDi85
*/
public class GameViewTest extends CardTestPlayerBase {
@Test
public void test_GameViewForPlayersAndWatchers() {
addCard(Zone.HAND, playerA, "Forest", 1);
setStrictChooseMode(true);
setStopAt(1, PhaseStep.END_TURN);
execute();
UUID userA = UUID.randomUUID();
UUID userB = UUID.randomUUID();
UUID userWatcher = UUID.randomUUID();
// normal hand
GameView gameView = getGameView(playerA, userA);
Assert.assertNotNull(gameView);
Assert.assertNotNull(gameView.getMyHand());
Assert.assertEquals(1, gameView.getMyHand().size());
Assert.assertEquals("Forest", gameView.getMyHand().values().stream().findFirst().get().getName());
Assert.assertEquals(0, gameView.getLookedAt().size());
Assert.assertEquals(0, gameView.getRevealed().size());
Assert.assertEquals(0, gameView.getWatchedHands().size());
// empty hand
gameView = getGameView(playerB, userB);
Assert.assertNotNull(gameView);
Assert.assertNotNull(gameView.getMyHand());
Assert.assertEquals(0, gameView.getMyHand().size());
Assert.assertEquals(0, gameView.getLookedAt().size());
Assert.assertEquals(0, gameView.getRevealed().size());
Assert.assertEquals(0, gameView.getWatchedHands().size());
// watcher hand
gameView = getGameView(null, userWatcher);
Assert.assertNotNull(gameView);
Assert.assertNotNull(gameView.getMyHand());
Assert.assertEquals(0, gameView.getMyHand().size());
Assert.assertEquals(0, gameView.getLookedAt().size());
Assert.assertEquals(0, gameView.getRevealed().size());
Assert.assertEquals(0, gameView.getWatchedHands().size());
// A gives access to hand for B and watcher
playerA.addPermissionToShowHandCards(userB);
playerA.addPermissionToShowHandCards(userWatcher);
gameView = getGameView(playerA, userA);
Assert.assertEquals(0, gameView.getWatchedHands().size());
gameView = getGameView(playerB, userB);
Assert.assertEquals(1, gameView.getWatchedHands().size());
gameView = getGameView(null, userWatcher);
Assert.assertEquals(1, gameView.getWatchedHands().size());
}
}

View file

@ -2222,9 +2222,20 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
Assert.assertFalse(player.getName() + " has lost the game.", player.hasLost()); Assert.assertFalse(player.getName() + " has lost the game.", player.hasLost());
} }
/**
* Prepare GameView from current game (for client side code tests)
*
* @param player null for watcher mode
*/
public GameView getGameView(Player player) { public GameView getGameView(Player player) {
// prepare client-server data for tests // prepare client-server data for tests
return GameSessionPlayer.prepareGameView(currentGame, player.getId(), null); UUID playerId = player == null ? null : player.getId();
return GameSessionPlayer.prepareGameView(currentGame, playerId, playerId);
}
public GameView getGameView(Player player, UUID userId) {
// prepare client-server data for tests
return GameSessionPlayer.prepareGameView(currentGame, player == null ? null : player.getId(), userId);
} }
protected enum ExpectedType { protected enum ExpectedType {