Added tick services to EZGame.
git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@82 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.HashMap;
|
||||
|
||||
import com.samskivert.util.ArrayUtil;
|
||||
import com.samskivert.util.CollectionUtil;
|
||||
import com.samskivert.util.Interval;
|
||||
import com.samskivert.util.RandomUtil;
|
||||
|
||||
import com.threerings.util.Name;
|
||||
@@ -15,6 +16,8 @@ import com.threerings.util.Name;
|
||||
import com.threerings.presents.data.ClientObject;
|
||||
import com.threerings.presents.data.InvocationCodes;
|
||||
import com.threerings.presents.dobj.AccessController;
|
||||
import com.threerings.presents.dobj.DObjectManager;
|
||||
import com.threerings.presents.dobj.MessageEvent;
|
||||
import com.threerings.presents.client.InvocationService;
|
||||
import com.threerings.presents.server.InvocationException;
|
||||
|
||||
@@ -196,6 +199,45 @@ public class EZGameManager extends GameManager
|
||||
}
|
||||
}
|
||||
|
||||
// from EZGameProvider
|
||||
public void setTicker (
|
||||
ClientObject caller, String tickerName, int msOfDelay,
|
||||
InvocationService.InvocationListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
validateUser(caller);
|
||||
|
||||
Ticker t;
|
||||
if (msOfDelay >= MIN_TICKER_DELAY) {
|
||||
if (_tickers != null) {
|
||||
t = _tickers.get(tickerName);
|
||||
|
||||
} else {
|
||||
_tickers = new HashMap<String, Ticker>();
|
||||
t = null;
|
||||
}
|
||||
if (t == null) {
|
||||
if (_tickers.size() >= MAX_TICKERS) {
|
||||
throw new InvocationException(ACCESS_DENIED);
|
||||
}
|
||||
t = new Ticker(tickerName, _gameObj);
|
||||
_tickers.put(tickerName, t);
|
||||
}
|
||||
t.start(msOfDelay);
|
||||
|
||||
} else if (msOfDelay <= 0) {
|
||||
if (_tickers != null) {
|
||||
t = _tickers.remove(tickerName);
|
||||
if (t != null) {
|
||||
t.stop();
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new InvocationException(ACCESS_DENIED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to send a private message to the specified player
|
||||
* index (must already be verified).
|
||||
@@ -273,10 +315,19 @@ public class EZGameManager extends GameManager
|
||||
protected void didShutdown ()
|
||||
{
|
||||
CrowdServer.invmgr.clearDispatcher(_gameObj.ezGameService);
|
||||
stopTickers();
|
||||
|
||||
super.didShutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void gameDidEnd ()
|
||||
{
|
||||
stopTickers();
|
||||
|
||||
super.gameDidEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assignWinners (boolean[] winners)
|
||||
{
|
||||
@@ -290,6 +341,70 @@ public class EZGameManager extends GameManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop and clear all tickers.
|
||||
*/
|
||||
protected void stopTickers ()
|
||||
{
|
||||
if (_tickers != null) {
|
||||
for (Ticker ticker : _tickers.values()) {
|
||||
ticker.stop();
|
||||
}
|
||||
_tickers = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A timer that fires message events to a game.
|
||||
*/
|
||||
protected static class Ticker
|
||||
{
|
||||
/**
|
||||
* Create a Ticker.
|
||||
*/
|
||||
public Ticker (String name, EZGameObject gameObj)
|
||||
{
|
||||
_name = name;
|
||||
// once we are constructed, we want to avoid calling
|
||||
// methods on dobjs.
|
||||
_oid = gameObj.getOid();
|
||||
_omgr = gameObj.getManager();
|
||||
}
|
||||
|
||||
public void start (int msOfDelay)
|
||||
{
|
||||
_value = 0;
|
||||
_interval.schedule(0, msOfDelay);
|
||||
}
|
||||
|
||||
public void stop ()
|
||||
{
|
||||
_interval.cancel();
|
||||
}
|
||||
|
||||
/** The interval that does our work. Note well that this is
|
||||
* not a 'safe' interval that operates using a RunQueue.
|
||||
* This interval instead does something that we happen to know
|
||||
* is safe for any thread: posting an event to the dobj manager.
|
||||
* If we were using a RunQueue it would be the same event queue
|
||||
* and we would be posted there, wait our turn, and then do the same
|
||||
* thing: post this event. We just expedite the process.
|
||||
*/
|
||||
protected Interval _interval = new Interval() {
|
||||
public void expired ()
|
||||
{
|
||||
_omgr.postEvent(
|
||||
new MessageEvent(_oid, EZGameObject.USER_MESSAGE,
|
||||
new Object[] { _name, _value++ }));
|
||||
}
|
||||
};
|
||||
|
||||
protected int _oid;
|
||||
protected DObjectManager _omgr;
|
||||
protected String _name;
|
||||
protected int _value;
|
||||
} // End: static class Ticker
|
||||
|
||||
/** A nice casted reference to the game object. */
|
||||
protected EZGameObject _gameObj;
|
||||
|
||||
@@ -301,4 +416,13 @@ public class EZGameManager extends GameManager
|
||||
|
||||
/** The map of collections, lazy-initialized. */
|
||||
protected HashMap<String, ArrayList<byte[]>> _collections;
|
||||
|
||||
/** The map of tickers, lazy-initialized. */
|
||||
protected HashMap<String, Ticker> _tickers;
|
||||
|
||||
/** The minimum delay a ticker can have. */
|
||||
protected static final int MIN_TICKER_DELAY = 50;
|
||||
|
||||
/** The maximum number of tickers allowed at one time. */
|
||||
protected static final int MAX_TICKERS = 3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user