mirror of
https://github.com/magefree/mage.git
synced 2025-12-22 19:41:59 -08:00
Timers on client side. Refactored.
This commit is contained in:
parent
62ee197cda
commit
6532aaffdf
4 changed files with 81 additions and 16 deletions
|
|
@ -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.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,103 +0,0 @@
|
|||
package mage.server.game.timer;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
*/
|
||||
public class PriorityTimer extends TimerTask {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(PriorityTimer.class);
|
||||
|
||||
private int count;
|
||||
|
||||
private long delay;
|
||||
|
||||
private Runnable taskOnTimeout;
|
||||
|
||||
private States state = States.NONE;
|
||||
|
||||
enum States {
|
||||
NONE,
|
||||
INIT,
|
||||
RUNNING,
|
||||
PAUSED,
|
||||
FINISHED
|
||||
}
|
||||
|
||||
public PriorityTimer(int count, long delay, Runnable taskOnTimeout) {
|
||||
this.count = count;
|
||||
this.delay = delay;
|
||||
this.taskOnTimeout = taskOnTimeout;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
state = States.INIT;
|
||||
Timer timer = new Timer("Priority Timer", false);
|
||||
long delayMs = delay * (int) (1000L / delay);
|
||||
timer.scheduleAtFixedRate(this, delayMs, delayMs);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (state == States.NONE) {
|
||||
throw new IllegalStateException("Timer should have been initialized first");
|
||||
}
|
||||
if (state == States.FINISHED) {
|
||||
throw new IllegalStateException("Timer has already finished its work");
|
||||
}
|
||||
state = States.RUNNING;
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
state = States.PAUSED;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
state = States.FINISHED;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
if (state == States.FINISHED) {
|
||||
throw new IllegalStateException("Timer has already finished its work");
|
||||
}
|
||||
state = States.RUNNING;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (state == States.RUNNING) {
|
||||
count--;
|
||||
}
|
||||
if (logger.isDebugEnabled()) logger.debug("Count is: " + count);
|
||||
//System.out.println("Count is: " + count);
|
||||
if (count <= 0) {
|
||||
cancel();
|
||||
taskOnTimeout.run();
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
System.out.println("Exit");
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
timer.init();
|
||||
timer.start();
|
||||
Thread.sleep(2000);
|
||||
timer.pause();
|
||||
Thread.sleep(3000);
|
||||
timer.resume();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue