From 6532aaffdf2483e49a2d0cbd384ed38e7666e012 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Thu, 20 Jun 2013 00:17:06 +0400 Subject: [PATCH] Timers on client side. Refactored. --- .../java/mage/client/game/PlayerPanelExt.java | 37 ++++++++++++++++++- Mage.Common/src/mage/constants/Constants.java | 5 +++ .../src/mage/utils}/timer/PriorityTimer.java | 36 +++++++++++++++--- .../java/mage/server/game/GameController.java | 19 +++++----- 4 files changed, 81 insertions(+), 16 deletions(-) rename {Mage.Server/src/main/java/mage/server/game => Mage.Common/src/mage/utils}/timer/PriorityTimer.java (72%) diff --git a/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java b/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java index a67210205df..0890cde733e 100644 --- a/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java +++ b/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java @@ -34,6 +34,7 @@ package mage.client.game; +import mage.MageException; import mage.cards.MageCard; import mage.cards.action.ActionCallback; import mage.cards.decks.importer.DckDeckImporter; @@ -51,7 +52,9 @@ import mage.client.util.Command; import mage.client.util.ImageHelper; import mage.client.util.gui.BufferedImageBuilder; import mage.components.ImagePanel; +import mage.constants.Constants; import mage.remote.Session; +import mage.utils.timer.PriorityTimer; import mage.view.CardView; import mage.view.ManaPoolView; import mage.view.PlayerView; @@ -101,6 +104,8 @@ public class PlayerPanelExt extends javax.swing.JPanel { private int avatarId = -1; + private PriorityTimer timer; + /** Creates new form PlayerPanel */ public PlayerPanelExt() { setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); @@ -114,6 +119,25 @@ public class PlayerPanelExt extends javax.swing.JPanel { session = MageFrame.getSession(); cheat.setVisible(session.isTestMode()); cheat.setFocusable(false); + + long delay = 1000L; + timer = new PriorityTimer(Constants.PRIORITY_TIME_SEC, delay, new mage.interfaces.Action() { + @Override + public void execute() throws MageException { + // do nothing + } + }); + final PriorityTimer pt = timer; + timer.setTaskOnTick(new mage.interfaces.Action() { + @Override + public void execute() throws MageException { + int priorityTimeValue = pt.getCount(); + String text = getPriorityTimeLeftString(priorityTimeValue); + PlayerPanelExt.this.avatar.setTopText(text); + PlayerPanelExt.this.avatar.repaint(); + } + }); + timer.init(); } public void update(PlayerView player) { @@ -183,17 +207,21 @@ public class PlayerPanelExt extends javax.swing.JPanel { } this.avatar.setText(player.getName()); String priorityTimeValue = getPriorityTimeLeftString(player); + this.timer.setCount(player.getPriorityTimeLeft()); this.avatar.setTopText(priorityTimeValue); this.btnPlayer.setText(player.getName()); if (player.isActive()) { this.avatar.setBorder(greenBorder); this.btnPlayer.setBorder(greenBorder); + this.timer.resume(); } else if (player.hasLeft()) { this.avatar.setBorder(redBorder); this.btnPlayer.setBorder(redBorder); + this.timer.pause(); } else { this.avatar.setBorder(emptyBorder); this.btnPlayer.setBorder(emptyBorder); + this.timer.pause(); } synchronized (this) { @@ -220,7 +248,14 @@ public class PlayerPanelExt extends javax.swing.JPanel { private String getPriorityTimeLeftString(PlayerView player) { int priorityTimeLeft = player.getPriorityTimeLeft(); - return priorityTimeLeft / 3600 + ":" + (priorityTimeLeft % 3600) / 60 + ":" + priorityTimeLeft % 60; + return getPriorityTimeLeftString(priorityTimeLeft); + } + + private String getPriorityTimeLeftString(int priorityTimeLeft) { + int h = priorityTimeLeft / 3600; + int m = (priorityTimeLeft % 3600) / 60; + int s = priorityTimeLeft % 60; + return (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s; } protected void update(ManaPoolView pool) { diff --git a/Mage.Common/src/mage/constants/Constants.java b/Mage.Common/src/mage/constants/Constants.java index a2e837dd0bf..c6b76fcb3b6 100644 --- a/Mage.Common/src/mage/constants/Constants.java +++ b/Mage.Common/src/mage/constants/Constants.java @@ -68,6 +68,11 @@ public final class Constants { public static final double SCALE_FACTOR = 0.5; + /** + * Time each player has during the game to play using his\her priority. + */ + public static final int PRIORITY_TIME_SEC = 1200; + public enum SessionState { DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_UNAVAILABLE, SERVER_STARTING; } diff --git a/Mage.Server/src/main/java/mage/server/game/timer/PriorityTimer.java b/Mage.Common/src/mage/utils/timer/PriorityTimer.java similarity index 72% rename from Mage.Server/src/main/java/mage/server/game/timer/PriorityTimer.java rename to Mage.Common/src/mage/utils/timer/PriorityTimer.java index 7398e74ab0b..90911c8ddd2 100644 --- a/Mage.Server/src/main/java/mage/server/game/timer/PriorityTimer.java +++ b/Mage.Common/src/mage/utils/timer/PriorityTimer.java @@ -1,5 +1,7 @@ -package mage.server.game.timer; +package mage.utils.timer; +import mage.MageException; +import mage.interfaces.Action; import org.apache.log4j.Logger; import java.util.Timer; @@ -16,7 +18,9 @@ public class PriorityTimer extends TimerTask { private long delay; - private Runnable taskOnTimeout; + private Action taskOnTimeout; + + private Action taskOnTick; private States state = States.NONE; @@ -28,7 +32,7 @@ public class PriorityTimer extends TimerTask { FINISHED } - public PriorityTimer(int count, long delay, Runnable taskOnTimeout) { + public PriorityTimer(int count, long delay, Action taskOnTimeout) { this.count = count; this.delay = delay; this.taskOnTimeout = taskOnTimeout; @@ -71,24 +75,44 @@ public class PriorityTimer extends TimerTask { return count; } + public void setCount(int count) { + this.count = count; + } + + public void setTaskOnTick(Action taskOnTick) { + this.taskOnTick = taskOnTick; + } + @Override public void run() { if (state == States.RUNNING) { count--; + if (taskOnTick != null) { + try { + taskOnTick.execute(); + } catch (MageException e) { + throw new RuntimeException(e); + } + } } if (logger.isDebugEnabled()) logger.debug("Count is: " + count); //System.out.println("Count is: " + count); if (count <= 0) { cancel(); - taskOnTimeout.run(); + try { + taskOnTimeout.execute(); + } catch (MageException e) { + throw new RuntimeException(e); + } } } public static void main(String[] args) throws Exception { long delay = 250L; int count = 5; - PriorityTimer timer = new PriorityTimer(count, delay, new Runnable() { - public void run() { + PriorityTimer timer = new PriorityTimer(count, delay, new Action() { + @Override + public void execute() throws MageException { System.out.println("Exit"); System.exit(0); } diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index cc0e26fedb7..fe29d7fb06a 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -36,6 +36,7 @@ import mage.cards.decks.Deck; import mage.cards.decks.DeckCardLists; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; +import mage.constants.Constants; import mage.constants.Zone; import mage.game.Game; import mage.game.GameException; @@ -43,14 +44,18 @@ import mage.game.events.Listener; import mage.game.events.PlayerQueryEvent; import mage.game.events.TableEvent; import mage.game.permanent.Permanent; +import mage.interfaces.Action; import mage.players.Player; import mage.server.*; -import mage.server.game.timer.PriorityTimer; import mage.server.util.Splitter; import mage.server.util.SystemUtil; import mage.server.util.ThreadExecutor; -import mage.view.*; +import mage.utils.timer.PriorityTimer; +import mage.view.AbilityPickerView; +import mage.view.CardsView; import mage.view.ChatMessage.MessageColor; +import mage.view.GameView; +import mage.view.PermanentView; import org.apache.log4j.Logger; import java.io.*; @@ -74,11 +79,6 @@ public class GameController implements GameCallback { private ConcurrentHashMap gameSessions = new ConcurrentHashMap(); private ConcurrentHashMap watchers = new ConcurrentHashMap(); private ConcurrentHashMap timers = new ConcurrentHashMap(); - - /** - * Time each player has during the game to play using his\her priority. - */ - private static final int PRIORITY_TIME_SEC = 62; private ConcurrentHashMap userPlayerMap; private UUID gameSessionId; @@ -137,8 +137,9 @@ public class GameController implements GameCallback { throw new IllegalStateException("INIT_TIMER: playerId can't be null"); } long delay = 250L; // run each 250 ms - timer = new PriorityTimer(PRIORITY_TIME_SEC, delay, new Runnable() { - public void run() { + timer = new PriorityTimer(Constants.PRIORITY_TIME_SEC, delay, new Action() { + @Override + public void execute() throws MageException { game.concede(initPlayerId); logger.info("Game timeout for player: " + initPlayerId + ". Conceding."); }