Move UserIdentifier up to GameManager where it can be used by other services.

Revamped the way the GameCookieManager does its business.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@346 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-07-05 19:50:33 +00:00
parent 63adadcd24
commit 4628b8fdff
3 changed files with 75 additions and 88 deletions
@@ -336,8 +336,7 @@ public class EZGameManager extends GameManager
InvocationService.InvocationListener listener) InvocationService.InvocationListener listener)
throws InvocationException throws InvocationException
{ {
GameCookieManager gcm = getCookieManager(); if (_gameObj.userCookies != null && _gameObj.userCookies.containsKey(playerOid)) {
if (_gameObj.userCookies.containsKey(playerOid)) {
// already loaded: we do nothing // already loaded: we do nothing
return; return;
} }
@@ -356,7 +355,8 @@ public class EZGameManager extends GameManager
// indicate that we're looking up a cookie // indicate that we're looking up a cookie
_cookieLookups.add(playerOid); _cookieLookups.add(playerOid);
gcm.getCookie(_gameconfig.getGameId(), body, new ResultListener<byte[]>() { int ppId = getPlayerPersistentId(body);
getCookieManager().getCookie(_gameconfig.getGameId(), ppId, new ResultListener<byte[]>() {
public void requestCompleted (byte[] result) { public void requestCompleted (byte[] result) {
// note that we're done with this lookup // note that we're done with this lookup
_cookieLookups.remove(playerOid); _cookieLookups.remove(playerOid);
@@ -381,34 +381,17 @@ public class EZGameManager extends GameManager
{ {
validateUser(caller); validateUser(caller);
GameCookieManager gcm = getCookieManager(); // persist this new cookie
getCookieManager().setCookie(
_gameconfig.getGameId(), getPlayerPersistentId((BodyObject)caller), value);
// and update the distributed object
UserCookie cookie = new UserCookie(caller.getOid(), value); UserCookie cookie = new UserCookie(caller.getOid(), value);
if (_gameObj.userCookies.containsKey(cookie.getKey())) { if (_gameObj.userCookies.containsKey(cookie.getKey())) {
_gameObj.updateUserCookies(cookie); _gameObj.updateUserCookies(cookie);
} else { } else {
_gameObj.addToUserCookies(cookie); _gameObj.addToUserCookies(cookie);
} }
gcm.setCookie(_gameconfig.getGameId(), (BodyObject)caller, value);
}
/**
* Get the cookie manager, and do a bit of other setup.
*/
protected GameCookieManager getCookieManager ()
throws InvocationException
{
GameCookieManager gcm = GameCookieManager.getInstance();
if (gcm == null) {
log.warning("GameCookieManager not initialized.");
throw new InvocationException(INTERNAL_ERROR);
}
if (_gameObj.userCookies == null) {
// lazy-init this
_gameObj.setUserCookies(new DSet<UserCookie>());
}
return gcm;
} }
/** /**
@@ -630,6 +613,26 @@ public class EZGameManager extends GameManager
return 0; return 0;
} }
/**
* Get the cookie manager, and do a bit of other setup.
*/
protected GameCookieManager getCookieManager ()
{
if (_cookMgr == null) {
_cookMgr = createCookieManager();
_gameObj.setUserCookies(new DSet<UserCookie>());
}
return _cookMgr;
}
/**
* Creates the cookie manager we'll use to store user cookies.
*/
protected GameCookieManager createCookieManager ()
{
return new GameCookieManager();
}
/** /**
* A timer that fires message events to a game. * A timer that fires message events to a game.
*/ */
@@ -692,12 +695,12 @@ public class EZGameManager extends GameManager
/** Tracks which cookies are currently being retrieved from the db. */ /** Tracks which cookies are currently being retrieved from the db. */
protected ArrayIntSet _cookieLookups = new ArrayIntSet(); protected ArrayIntSet _cookieLookups = new ArrayIntSet();
// /** User tokens, lazy-initialized. */
// protected HashIntMap<HashSet<String>> _tokens;
/** The array of winner oids, after the user has filled it in. */ /** The array of winner oids, after the user has filled it in. */
protected int[] _winnerIds; protected int[] _winnerIds;
/** Handles the storage of our user cookies; lazily initialized. */
protected GameCookieManager _cookMgr;
/** The minimum delay a ticker can have. */ /** The minimum delay a ticker can have. */
protected static final int MIN_TICKER_DELAY = 50; protected static final int MIN_TICKER_DELAY = 50;
@@ -24,7 +24,6 @@ package com.threerings.ezgame.server;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.RepositoryListenerUnit; import com.samskivert.jdbc.RepositoryListenerUnit;
import com.samskivert.util.Invoker; import com.samskivert.util.Invoker;
import com.samskivert.util.ResultListener; import com.samskivert.util.ResultListener;
@@ -41,49 +40,28 @@ import static com.threerings.ezgame.server.Log.log;
public class GameCookieManager public class GameCookieManager
{ {
/** /**
* An interface for identifying users. * Creates a game cookie manager that stores cookies in Java preferences on the local machine.
* This should only be used for developer testing.
*/ */
public interface UserIdentifier public GameCookieManager ()
{ {
/** Return the persistent user id for the specified player, or 0 if they're not a valid _prefs = Preferences.userRoot().node("gameCookieManager");
* user, or a guest, or something like that (they'll have no cookies). */
public int getUserId (BodyObject bodyObj);
} }
/** /**
* Sets up cookie manager that will store cookie information on the local filesystem using the * Creates a game cookie manager that stores cookies in the supplied repository.
* Java preferences system. This should only be used during testing and not in a production
* system.
*/ */
public static void init (UserIdentifier ider) public GameCookieManager (GameCookieRepository repo)
{
_singleton = new GameCookieManager(ider);
}
/**
* Sets up a cookie manager that will store cookie information in a database table accessed via
* the supplied connection provider.
*/
public static void init (ConnectionProvider conprov, UserIdentifier ider)
throws PersistenceException throws PersistenceException
{ {
_singleton = new GameCookieManager(conprov, ider); _repo = repo;
}
/**
* Get an instance of the GameCookieManager.
*/
public static GameCookieManager getInstance ()
{
return _singleton;
} }
/** /**
* Get the specified user's cookie. * Get the specified user's cookie.
*/ */
public void getCookie (final int gameId, BodyObject bobj, ResultListener<byte[]> rl) public void getCookie (final int gameId, final int userId, ResultListener<byte[]> rl)
{ {
final int userId = _identifier.getUserId(bobj);
if (userId == 0) { if (userId == 0) {
rl.requestCompleted(null); rl.requestCompleted(null);
return; return;
@@ -105,9 +83,8 @@ public class GameCookieManager
/** /**
* Set the specified user's cookie. * Set the specified user's cookie.
*/ */
public void setCookie (final int gameId, BodyObject bobj, final byte[] cookie) public void setCookie (final int gameId, final int userId, final byte[] cookie)
{ {
final int userId = _identifier.getUserId(bobj);
if (userId == 0) { if (userId == 0) {
// fail to save, silently // fail to save, silently
return; return;
@@ -131,37 +108,9 @@ public class GameCookieManager
}); });
} }
/**
* Protected constructor.
*/
protected GameCookieManager (UserIdentifier identifier)
{
_identifier = identifier;
if (_identifier == null) {
throw new IllegalArgumentException("UserIdentifier must be non-null");
}
_prefs = Preferences.userRoot().node("gameCookieManager");
}
/**
* Protected constructor.
*/
protected GameCookieManager (ConnectionProvider conprov, UserIdentifier identifier)
throws PersistenceException
{
this(identifier);
_repo = new GameCookieRepository(conprov);
}
/** The entity we ask to identify users. */
protected UserIdentifier _identifier;
/** Our database repository, which is used in real operation. */ /** Our database repository, which is used in real operation. */
protected GameCookieRepository _repo; protected GameCookieRepository _repo;
/** Our local store, which is used when testing. */ /** Our local store, which is used when testing. */
protected Preferences _prefs; protected Preferences _prefs;
/** A reference to the single GameCookieManager instantiated. */
protected static GameCookieManager _singleton;
} }
@@ -67,6 +67,26 @@ import com.threerings.util.MessageBundle;
public class GameManager extends PlaceManager public class GameManager extends PlaceManager
implements ParlorCodes, GameCodes implements ParlorCodes, GameCodes
{ {
/**
* An interface for identifying users. A larger system using the Parlor game services can
* enable user identification by passing a UserIdentifier to {@link #setUserIdentifier}.
*/
public interface UserIdentifier
{
/** Returns the persistent user id for the specified player, or 0 if they're not a valid
* user, or a guest, or something like that (for whom we'll track no persistent data). */
public int getUserId (BodyObject bodyObj);
}
/**
* Configures a means by which the Parlor game services can map users to a persistent user id
* for things like per-game cookies and ratings.
*/
public static void setUserIdentifier (UserIdentifier userIder)
{
_userIder = userIder;
}
/** /**
* Returns the configuration object for the game being managed by this manager. * Returns the configuration object for the game being managed by this manager.
*/ */
@@ -310,6 +330,14 @@ public class GameManager extends PlaceManager
return (_playerOids == null) ? -1 : _playerOids[index]; return (_playerOids == null) ? -1 : _playerOids[index];
} }
/**
* Returns the persistent user id for the supplied body.
*/
public int getPlayerPersistentId (BodyObject bobj)
{
return (bobj == null) ? 0 : _userIder.getUserId(bobj);
}
/** /**
* Returns the number of players in the game. * Returns the number of players in the game.
*/ */
@@ -1279,6 +1307,13 @@ public class GameManager extends PlaceManager
/** The interval for the game manager tick. */ /** The interval for the game manager tick. */
protected static Interval _tickInterval; protected static Interval _tickInterval;
/** Used to map users to persistent integer identifiers. */
protected static UserIdentifier _userIder = new UserIdentifier() {
public int getUserId (BodyObject bodyObj) {
return 0; // by default no one has persistent info
}
};
/** We give players 30 seconds to turn up in a game; after that, they're considered a no /** We give players 30 seconds to turn up in a game; after that, they're considered a no
* show. */ * show. */
protected static final long NOSHOW_DELAY = 30 * 1000L; protected static final long NOSHOW_DELAY = 30 * 1000L;