From f3334c6e92a69dd099b8c99e130618dc87342e14 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Thu, 4 Aug 2011 21:51:00 +0400 Subject: [PATCH] Possibility to view other player's hands in UI. --- .../main/java/mage/client/game/GamePanel.java | 72 ++++++++++++++++++- Mage.Common/src/mage/view/GameView.java | 10 +++ .../java/mage/server/game/GameSession.java | 21 ++++-- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/game/GamePanel.java b/Mage.Client/src/main/java/mage/client/game/GamePanel.java index 32a4a96682..2eeb9172c3 100644 --- a/Mage.Client/src/main/java/mage/client/game/GamePanel.java +++ b/Mage.Client/src/main/java/mage/client/game/GamePanel.java @@ -34,6 +34,7 @@ package mage.client.game; +import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator; import mage.Constants; import mage.client.MageFrame; import mage.client.cards.Cards; @@ -48,6 +49,7 @@ import mage.remote.Session; import mage.view.*; import org.apache.log4j.Logger; +import javax.net.ssl.HandshakeCompletedEvent; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; @@ -69,6 +71,7 @@ import java.util.prefs.Preferences; public class GamePanel extends javax.swing.JPanel { private final static Logger logger = Logger.getLogger(GamePanel.class); + static final String YOUR_HAND = "Your hand"; private Map players = new HashMap(); private Map exiles = new HashMap(); @@ -80,6 +83,7 @@ public class GamePanel extends javax.swing.JPanel { private CombatDialog combat; private PickNumberDialog pickNumber; private JLayeredPane jLayeredPane; + private String chosenHandKey = "You"; private static final int HAND_CARD_WIDTH = 75; private static final Dimension handCardDimension = new Dimension(HAND_CARD_WIDTH, (int)(HAND_CARD_WIDTH * 3.5f / 2.5f)); @@ -174,6 +178,7 @@ public class GamePanel extends javax.swing.JPanel { this.feedbackPanel.clear(); this.abilityPicker.init(session, gameId); this.btnConcede.setVisible(true); + this.btnSwitchHands.setVisible(false); this.pnlReplay.setVisible(false); this.btnStopWatching.setVisible(false); this.gameChatPanel.clear(); @@ -190,6 +195,7 @@ public class GamePanel extends javax.swing.JPanel { this.feedbackPanel.init(gameId); this.feedbackPanel.clear(); this.btnConcede.setVisible(false); + this.btnSwitchHands.setVisible(false); this.btnStopWatching.setVisible(true); this.pnlReplay.setVisible(false); this.gameChatPanel.clear(); @@ -205,6 +211,7 @@ public class GamePanel extends javax.swing.JPanel { MageFrame.addGame(gameId, this); this.feedbackPanel.clear(); this.btnConcede.setVisible(false); + this.btnSwitchHands.setVisible(false); this.btnStopWatching.setVisible(false); this.pnlReplay.setVisible(true); this.gameChatPanel.clear(); @@ -298,9 +305,33 @@ public class GamePanel extends javax.swing.JPanel { if (playerId == null || game.getHand() == null) { this.hand.setVisible(false); } else { - this.hand.loadCards(game.getHand(), bigCard, gameId); - int count = game.getHand().size(); - hand.setPreferredSize(new java.awt.Dimension((getHandCardDimension().width + 5) * count + 5, getHandCardDimension().height + 20)); // for scroll + handCards.clear(); + handCards.put(YOUR_HAND, game.getHand()); + + // Get opponents hand cards if available + if (game.getOpponentHands() != null) { + for (Map.Entry hand: game.getOpponentHands().entrySet()) { + handCards.put(hand.getKey(), hand.getValue()); + } + } + + if (!handCards.containsKey(chosenHandKey)) { + chosenHandKey = YOUR_HAND; + } + this.hand.loadCards(handCards.get(chosenHandKey), bigCard, gameId); + hand.setPreferredSize(new java.awt.Dimension((getHandCardDimension().width + 5) * game.getHand().size() + 5, getHandCardDimension().height + 20)); // for scroll + + // set visible only if we have any other hand visible than ours + boolean previous = btnSwitchHands.isVisible(); + boolean visible = handCards.size() > 1; + if (previous != visible) { + btnSwitchHands.setVisible(visible); + if (visible) { + JOptionPane.showMessageDialog(null, "You control other player's turn. \nUse \"Switch Hand\" on the bottom to switch between cards in different hands."); + } else { + JOptionPane.showMessageDialog(null, "You lost control on other player's turn."); + } + } } if (game.getPhase() != null) this.txtPhase.setText(game.getPhase().toString()); @@ -525,6 +556,7 @@ public class GamePanel extends javax.swing.JPanel { lblPriority = new javax.swing.JLabel(); feedbackPanel = new mage.client.game.FeedbackPanel(); btnConcede = new javax.swing.JButton(); + btnSwitchHands = new javax.swing.JButton(); btnStopWatching = new javax.swing.JButton(); bigCard = new mage.client.cards.BigCard(); stack = new mage.client.cards.Cards(); @@ -544,6 +576,7 @@ public class GamePanel extends javax.swing.JPanel { jTabbedPane1 = new JTabbedPane(); hand.setCardDimension(getHandCardDimension()); + handCards = new HashMap(); jSplitPane1.setBorder(null); jSplitPane1.setDividerSize(7); @@ -603,6 +636,15 @@ public class GamePanel extends javax.swing.JPanel { } }); + btnSwitchHands.setText("Switch Hands"); + btnSwitchHands.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent evt) { + btnSwitchHandActionPerformed(null); + } + }); + btnSwitchHands.setBorder(BorderFactory.createLineBorder(Color.red)); + btnStopWatching.setText("Stop Watching"); btnStopWatching.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { @@ -676,6 +718,8 @@ public class GamePanel extends javax.swing.JPanel { .addGap(10, 10, 10) .addComponent(btnConcede) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(btnSwitchHands) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnStopWatching) .addContainerGap(62, Short.MAX_VALUE)) .addComponent(bigCard, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE) @@ -721,6 +765,7 @@ public class GamePanel extends javax.swing.JPanel { .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(pnlGameInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnConcede) + .addComponent(btnSwitchHands) .addComponent(btnStopWatching))) ); @@ -823,6 +868,25 @@ public class GamePanel extends javax.swing.JPanel { } }//GEN-LAST:event_btnConcedeActionPerformed + private void btnSwitchHandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConcedeActionPerformed + String[] choices = handCards.keySet().toArray(new String[0]); + + String chosenHandKey = (String) JOptionPane.showInputDialog( + this, + "Choose hand to display:", "Switch between hands", + JOptionPane.PLAIN_MESSAGE, + null, + choices, + YOUR_HAND); + + if (chosenHandKey != null && chosenHandKey.length() > 0) { + chosenHandKey = chosenHandKey; + CardsView cards = handCards.get(chosenHandKey); + this.hand.loadCards(cards, bigCard, gameId); + hand.setPreferredSize(new java.awt.Dimension((getHandCardDimension().width + 5) * cards.size() + 5, getHandCardDimension().height + 20)); // for scroll + } + } + private void btnStopWatchingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopWatchingActionPerformed if (modalQuestion("Are you sure you want to stop watching?", "Stop watching") == JOptionPane.YES_OPTION) { session.stopWatching(gameId); @@ -910,6 +974,7 @@ public class GamePanel extends javax.swing.JPanel { private mage.client.components.ability.AbilityPicker abilityPicker; private mage.client.cards.BigCard bigCard; private javax.swing.JButton btnConcede; + private javax.swing.JButton btnSwitchHands; private javax.swing.JButton btnNextPlay; private javax.swing.JButton btnPreviousPlay; private javax.swing.JButton btnStopReplay; @@ -918,6 +983,7 @@ public class GamePanel extends javax.swing.JPanel { private mage.client.game.FeedbackPanel feedbackPanel; private mage.client.chat.ChatPanel userChatPanel; private mage.client.cards.Cards hand; + private Map handCards; private javax.swing.JPanel jPanel3; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JLabel lblActivePlayer; diff --git a/Mage.Common/src/mage/view/GameView.java b/Mage.Common/src/mage/view/GameView.java index 53a2ea9997..59c9d12ec9 100644 --- a/Mage.Common/src/mage/view/GameView.java +++ b/Mage.Common/src/mage/view/GameView.java @@ -31,6 +31,7 @@ package mage.view; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.UUID; import mage.MageObject; @@ -56,6 +57,7 @@ public class GameView implements Serializable { private List players = new ArrayList(); private CardsView hand; + private Map opponentHands; private CardsView stack = new CardsView(); private List exiles = new ArrayList(); private List revealed = new ArrayList(); @@ -124,6 +126,14 @@ public class GameView implements Serializable { this.hand = hand; } + public Map getOpponentHands() { + return opponentHands; + } + + public void setOpponentHands(Map opponentHands) { + this.opponentHands = opponentHands; + } + public TurnPhase getPhase() { return phase; } diff --git a/Mage.Server/src/main/java/mage/server/game/GameSession.java b/Mage.Server/src/main/java/mage/server/game/GameSession.java index a386c66ac7..8b0051ab14 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameSession.java +++ b/Mage.Server/src/main/java/mage/server/game/GameSession.java @@ -29,18 +29,15 @@ package mage.server.game; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; -import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import mage.cards.Cards; import mage.game.Game; import mage.interfaces.callback.ClientCallback; +import mage.players.Player; import mage.server.User; import mage.server.UserManager; import mage.server.util.ConfigSettings; @@ -197,8 +194,20 @@ public class GameSession extends GameWatcher { @Override public GameView getGameView() { + Player player = game.getPlayer(playerId); GameView gameView = new GameView(game.getState(), game); - gameView.setHand(new CardsView(game.getPlayer(playerId).getHand().getCards(game))); + gameView.setHand(new CardsView(player.getHand().getCards(game))); + + if (player.getPlayersUnderYourControl().size() > 0) { + Map handCards = new HashMap(); + for (UUID playerId : player.getPlayersUnderYourControl()) { + Player opponent = game.getPlayer(playerId); + handCards.put(player.getName(), new CardsView(opponent.getHand().getCards(game))); + } + gameView.setOpponentHands(handCards); + } + + //TODO: should player be able to look at all these cards? List list = new ArrayList(); for (Entry entry : game.getState().getLookedAt(playerId).entrySet()) {