Timers on client side. Refactored.

This commit is contained in:
magenoxx 2013-06-20 00:17:06 +04:00
parent 62ee197cda
commit 6532aaffdf
4 changed files with 81 additions and 16 deletions

View file

@ -34,6 +34,7 @@
package mage.client.game; package mage.client.game;
import mage.MageException;
import mage.cards.MageCard; import mage.cards.MageCard;
import mage.cards.action.ActionCallback; import mage.cards.action.ActionCallback;
import mage.cards.decks.importer.DckDeckImporter; import mage.cards.decks.importer.DckDeckImporter;
@ -51,7 +52,9 @@ import mage.client.util.Command;
import mage.client.util.ImageHelper; import mage.client.util.ImageHelper;
import mage.client.util.gui.BufferedImageBuilder; import mage.client.util.gui.BufferedImageBuilder;
import mage.components.ImagePanel; import mage.components.ImagePanel;
import mage.constants.Constants;
import mage.remote.Session; import mage.remote.Session;
import mage.utils.timer.PriorityTimer;
import mage.view.CardView; import mage.view.CardView;
import mage.view.ManaPoolView; import mage.view.ManaPoolView;
import mage.view.PlayerView; import mage.view.PlayerView;
@ -101,6 +104,8 @@ public class PlayerPanelExt extends javax.swing.JPanel {
private int avatarId = -1; private int avatarId = -1;
private PriorityTimer timer;
/** Creates new form PlayerPanel */ /** Creates new form PlayerPanel */
public PlayerPanelExt() { public PlayerPanelExt() {
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
@ -114,6 +119,25 @@ public class PlayerPanelExt extends javax.swing.JPanel {
session = MageFrame.getSession(); session = MageFrame.getSession();
cheat.setVisible(session.isTestMode()); cheat.setVisible(session.isTestMode());
cheat.setFocusable(false); 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) { public void update(PlayerView player) {
@ -183,17 +207,21 @@ public class PlayerPanelExt extends javax.swing.JPanel {
} }
this.avatar.setText(player.getName()); this.avatar.setText(player.getName());
String priorityTimeValue = getPriorityTimeLeftString(player); String priorityTimeValue = getPriorityTimeLeftString(player);
this.timer.setCount(player.getPriorityTimeLeft());
this.avatar.setTopText(priorityTimeValue); this.avatar.setTopText(priorityTimeValue);
this.btnPlayer.setText(player.getName()); this.btnPlayer.setText(player.getName());
if (player.isActive()) { if (player.isActive()) {
this.avatar.setBorder(greenBorder); this.avatar.setBorder(greenBorder);
this.btnPlayer.setBorder(greenBorder); this.btnPlayer.setBorder(greenBorder);
this.timer.resume();
} else if (player.hasLeft()) { } else if (player.hasLeft()) {
this.avatar.setBorder(redBorder); this.avatar.setBorder(redBorder);
this.btnPlayer.setBorder(redBorder); this.btnPlayer.setBorder(redBorder);
this.timer.pause();
} else { } else {
this.avatar.setBorder(emptyBorder); this.avatar.setBorder(emptyBorder);
this.btnPlayer.setBorder(emptyBorder); this.btnPlayer.setBorder(emptyBorder);
this.timer.pause();
} }
synchronized (this) { synchronized (this) {
@ -220,7 +248,14 @@ public class PlayerPanelExt extends javax.swing.JPanel {
private String getPriorityTimeLeftString(PlayerView player) { private String getPriorityTimeLeftString(PlayerView player) {
int priorityTimeLeft = player.getPriorityTimeLeft(); 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) { protected void update(ManaPoolView pool) {

View file

@ -68,6 +68,11 @@ public final class Constants {
public static final double SCALE_FACTOR = 0.5; 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 { public enum SessionState {
DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_UNAVAILABLE, SERVER_STARTING; DISCONNECTED, CONNECTED, CONNECTING, DISCONNECTING, SERVER_UNAVAILABLE, SERVER_STARTING;
} }

View file

@ -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 org.apache.log4j.Logger;
import java.util.Timer; import java.util.Timer;
@ -16,7 +18,9 @@ public class PriorityTimer extends TimerTask {
private long delay; private long delay;
private Runnable taskOnTimeout; private Action taskOnTimeout;
private Action taskOnTick;
private States state = States.NONE; private States state = States.NONE;
@ -28,7 +32,7 @@ public class PriorityTimer extends TimerTask {
FINISHED FINISHED
} }
public PriorityTimer(int count, long delay, Runnable taskOnTimeout) { public PriorityTimer(int count, long delay, Action taskOnTimeout) {
this.count = count; this.count = count;
this.delay = delay; this.delay = delay;
this.taskOnTimeout = taskOnTimeout; this.taskOnTimeout = taskOnTimeout;
@ -71,24 +75,44 @@ public class PriorityTimer extends TimerTask {
return count; return count;
} }
public void setCount(int count) {
this.count = count;
}
public void setTaskOnTick(Action taskOnTick) {
this.taskOnTick = taskOnTick;
}
@Override @Override
public void run() { public void run() {
if (state == States.RUNNING) { if (state == States.RUNNING) {
count--; count--;
if (taskOnTick != null) {
try {
taskOnTick.execute();
} catch (MageException e) {
throw new RuntimeException(e);
}
}
} }
if (logger.isDebugEnabled()) logger.debug("Count is: " + count); if (logger.isDebugEnabled()) logger.debug("Count is: " + count);
//System.out.println("Count is: " + count); //System.out.println("Count is: " + count);
if (count <= 0) { if (count <= 0) {
cancel(); cancel();
taskOnTimeout.run(); try {
taskOnTimeout.execute();
} catch (MageException e) {
throw new RuntimeException(e);
}
} }
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
long delay = 250L; long delay = 250L;
int count = 5; int count = 5;
PriorityTimer timer = new PriorityTimer(count, delay, new Runnable() { PriorityTimer timer = new PriorityTimer(count, delay, new Action() {
public void run() { @Override
public void execute() throws MageException {
System.out.println("Exit"); System.out.println("Exit");
System.exit(0); System.exit(0);
} }

View file

@ -36,6 +36,7 @@ import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo; import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository; import mage.cards.repository.CardRepository;
import mage.constants.Constants;
import mage.constants.Zone; import mage.constants.Zone;
import mage.game.Game; import mage.game.Game;
import mage.game.GameException; import mage.game.GameException;
@ -43,14 +44,18 @@ import mage.game.events.Listener;
import mage.game.events.PlayerQueryEvent; import mage.game.events.PlayerQueryEvent;
import mage.game.events.TableEvent; import mage.game.events.TableEvent;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.interfaces.Action;
import mage.players.Player; import mage.players.Player;
import mage.server.*; import mage.server.*;
import mage.server.game.timer.PriorityTimer;
import mage.server.util.Splitter; import mage.server.util.Splitter;
import mage.server.util.SystemUtil; import mage.server.util.SystemUtil;
import mage.server.util.ThreadExecutor; 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.ChatMessage.MessageColor;
import mage.view.GameView;
import mage.view.PermanentView;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.*; import java.io.*;
@ -74,11 +79,6 @@ public class GameController implements GameCallback {
private ConcurrentHashMap<UUID, GameSession> gameSessions = new ConcurrentHashMap<UUID, GameSession>(); private ConcurrentHashMap<UUID, GameSession> gameSessions = new ConcurrentHashMap<UUID, GameSession>();
private ConcurrentHashMap<UUID, GameWatcher> watchers = new ConcurrentHashMap<UUID, GameWatcher>(); private ConcurrentHashMap<UUID, GameWatcher> watchers = new ConcurrentHashMap<UUID, GameWatcher>();
private ConcurrentHashMap<UUID, PriorityTimer> timers = new ConcurrentHashMap<UUID, PriorityTimer>(); private ConcurrentHashMap<UUID, PriorityTimer> timers = new ConcurrentHashMap<UUID, PriorityTimer>();
/**
* Time each player has during the game to play using his\her priority.
*/
private static final int PRIORITY_TIME_SEC = 62;
private ConcurrentHashMap<UUID, UUID> userPlayerMap; private ConcurrentHashMap<UUID, UUID> userPlayerMap;
private UUID gameSessionId; private UUID gameSessionId;
@ -137,8 +137,9 @@ public class GameController implements GameCallback {
throw new IllegalStateException("INIT_TIMER: playerId can't be null"); throw new IllegalStateException("INIT_TIMER: playerId can't be null");
} }
long delay = 250L; // run each 250 ms long delay = 250L; // run each 250 ms
timer = new PriorityTimer(PRIORITY_TIME_SEC, delay, new Runnable() { timer = new PriorityTimer(Constants.PRIORITY_TIME_SEC, delay, new Action() {
public void run() { @Override
public void execute() throws MageException {
game.concede(initPlayerId); game.concede(initPlayerId);
logger.info("Game timeout for player: " + initPlayerId + ". Conceding."); logger.info("Game timeout for player: " + initPlayerId + ". Conceding.");
} }