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;
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) {

View file

@ -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;
}

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 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);
}

View file

@ -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<UUID, GameSession> gameSessions = new ConcurrentHashMap<UUID, GameSession>();
private ConcurrentHashMap<UUID, GameWatcher> watchers = new ConcurrentHashMap<UUID, GameWatcher>();
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 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.");
}