Inject depends into Tournies and ToureyManager.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@646 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2008-06-30 13:30:17 +00:00
parent c3162107cf
commit 40ffa60f50
3 changed files with 61 additions and 59 deletions
@@ -21,12 +21,14 @@
package com.threerings.parlor.tourney.server; package com.threerings.parlor.tourney.server;
import com.google.inject.Inject;
import com.samskivert.util.Interval; import com.samskivert.util.Interval;
import com.samskivert.util.ResultListener; import com.samskivert.util.ResultListener;
import com.threerings.util.Name; import com.threerings.util.Name;
import com.threerings.crowd.data.BodyObject; import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.server.BodyLocator;
import com.threerings.parlor.tourney.data.Participant; import com.threerings.parlor.tourney.data.Participant;
import com.threerings.parlor.tourney.data.TourneyConfig; import com.threerings.parlor.tourney.data.TourneyConfig;
@@ -45,14 +47,14 @@ import com.threerings.presents.server.InvocationManager;
public abstract class TourneyManager public abstract class TourneyManager
implements TourneyProvider, TourneyCodes implements TourneyProvider, TourneyCodes
{ {
public TourneyManager (InvocationManager invmgr, RootDObjectManager omgr, TourneyConfig config, /**
TourniesManager tmgr, Comparable key, * Initializes this tourney manager and prepares it for operation.
InvocationService.ResultListener listener) *
* @return the oid of this manager's tourney object.
*/
public int init (TourneyConfig config, Comparable key)
{ {
_invmgr = invmgr;
_omgr = omgr;
_config = config; _config = config;
_tmgr = tmgr;
_key = key; _key = key;
// creare and configure our Tourney object // creare and configure our Tourney object
@@ -70,9 +72,7 @@ public abstract class TourneyManager
_startTime = System.currentTimeMillis() + (MINUTE * _config.startsIn); _startTime = System.currentTimeMillis() + (MINUTE * _config.startsIn);
} }
if (listener != null) { return _trobj.getOid();
listener.requestProcessed(Integer.valueOf(_trobj.getOid()));
}
} }
// documentation inherited from TourneyProvider // documentation inherited from TourneyProvider
@@ -185,7 +185,7 @@ public abstract class TourneyManager
// return the fees // return the fees
if (_trobj.config.entryFee != null) { if (_trobj.config.entryFee != null) {
for (Participant part : _trobj.participants) { for (Participant part : _trobj.participants) {
BodyObject body = lookupBody(part.username); BodyObject body = _locator.lookupBody(part.username);
if (body != null) { if (body != null) {
_trobj.config.entryFee.returnFee(body); _trobj.config.entryFee.returnFee(body);
} }
@@ -268,38 +268,30 @@ public abstract class TourneyManager
}.schedule(MINUTE * 2); }.schedule(MINUTE * 2);
} }
/**
* Looks up the BodyObject for a username.
*/
protected abstract BodyObject lookupBody (Name username);
/** /**
* Will throw an InvocationException if the user cannot join the tourney. * Will throw an InvocationException if the user cannot join the tourney.
*/ */
protected abstract void joinTourney (BodyObject body) protected abstract void joinTourney (BodyObject body)
throws InvocationException; throws InvocationException;
/** Used to register and clear invocation services. */
protected InvocationManager _invmgr;
/** Provides distributed object services. */
protected RootDObjectManager _omgr;
/** Our touney configuration. */ /** Our touney configuration. */
protected TourneyConfig _config; protected TourneyConfig _config;
/** Our distributed tourney object. */ /** Our distributed tourney object. */
protected TourneyObject _trobj; protected TourneyObject _trobj;
/** Reference to the tournies manager. */
protected TourniesManager _tmgr;
/** The time, in milliseconds, when the tourney starts. */ /** The time, in milliseconds, when the tourney starts. */
protected long _startTime; protected long _startTime;
/** The key this tourney is recorded under. */ /** The key this tourney is recorded under. */
protected Comparable _key; protected Comparable _key;
// services on which we depend
@Inject protected RootDObjectManager _omgr;
@Inject protected InvocationManager _invmgr;
@Inject protected BodyLocator _locator;
@Inject protected TourniesManager _tmgr;
/** One minute in milliseconds. */ /** One minute in milliseconds. */
protected static long MINUTE = 60 * 1000L; protected static long MINUTE = 60 * 1000L;
} }
@@ -22,41 +22,47 @@
package com.threerings.parlor.tourney.server; package com.threerings.parlor.tourney.server;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.samskivert.util.Interval; import com.google.common.collect.Maps;
import com.samskivert.util.RunQueue; import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.util.Interval;
import com.threerings.parlor.tourney.server.persist.TourneyRepository; import com.samskivert.util.RunQueue;
import com.threerings.parlor.tourney.data.Prize;
import com.threerings.parlor.tourney.data.TourneyConfig;
import com.threerings.presents.client.InvocationService; import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.dobj.RootDObjectManager;
import com.threerings.presents.server.InvocationException; import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.PresentsServer; import com.threerings.presents.server.PresentsServer;
import com.threerings.presents.server.ShutdownManager; import com.threerings.presents.server.ShutdownManager;
import com.threerings.presents.data.ClientObject;
import com.threerings.parlor.tourney.data.Prize;
import com.threerings.parlor.tourney.data.TourneyConfig;
import com.threerings.parlor.tourney.server.persist.TourneyRepository;
/** /**
* An extensible tournament manager. * An extensible tournament manager.
*/ */
@Singleton
public abstract class TourniesManager public abstract class TourniesManager
implements TourniesProvider, ShutdownManager.Shutdowner implements TourniesProvider, ShutdownManager.Shutdowner
{ {
/** /**
* Initializes the tournies manager and starts its periodic update task. * Initializes the tournies manager and starts its periodic update task.
*/ */
public void init (ConnectionProvider conprov) public void init (Injector injector)
throws PersistenceException throws PersistenceException
{ {
_tournrep = new TourneyRepository(conprov, getDBIdent());
loadTourneyConfigs(); loadTourneyConfigs();
_interval = new Interval(getRunQueue()) { _injector = injector;
_interval = new Interval(_omgr) {
public void expired () { public void expired () {
updateTournies(); updateTournies();
} }
@@ -90,8 +96,13 @@ public abstract class TourniesManager
protected void makeTourney (TourneyConfig config, InvocationService.ResultListener listener) protected void makeTourney (TourneyConfig config, InvocationService.ResultListener listener)
{ {
// create a new tourney manager which will run things // create a new tourney manager which will run things
Integer tourneyID = Integer.valueOf(_tourneyCount++); TourneyManager tmgr = _injector.getInstance(getTourneyManagerClass());
_tourneys.put(tourneyID, makeTourneyManager(config, tourneyID, listener)); int tournId = _tourneyCount++;
int tournOid = tmgr.init(config, tournId);
_tourneys.put(tournId, tmgr);
if (listener != null) {
listener.requestProcessed(tournOid);
}
} }
/** /**
@@ -112,7 +123,6 @@ public abstract class TourniesManager
for (TourneyConfig config : tournies) { for (TourneyConfig config : tournies) {
makeTourney(config, null); makeTourney(config, null);
} }
} }
/** /**
@@ -124,36 +134,28 @@ public abstract class TourniesManager
} }
/** /**
* Instantiates a new tourney manager. * Returns the derivation of {@link TourneyManager} to use.
*/ */
protected abstract TourneyManager makeTourneyManager( protected abstract Class<? extends TourneyManager> getTourneyManagerClass ();
TourneyConfig config, Comparable key, InvocationService.ResultListener listener);
/**
* Returns the database identifier for our repository.
*/
protected abstract String getDBIdent ();
/**
* Returns the RunQueue to use for our tourney interval.
*/
protected abstract RunQueue getRunQueue ();
/** /**
* Returns the tourney interval delay in milliseconds. * Returns the tourney interval delay in milliseconds.
*/ */
protected abstract long getIntervalDelay (); protected abstract long getIntervalDelay ();
/** Holds all the current tournies in the game. */ /** Used to resolve dependencies in the {@link TourneyManager}s that we create. */
protected HashMap<Comparable, TourneyManager> _tourneys = protected Injector _injector;
new HashMap<Comparable, TourneyManager>();
/** Reference to our tourney repository. */
protected TourneyRepository _tournrep;
/** The interval which updates loaded tournies. */ /** The interval which updates loaded tournies. */
protected Interval _interval; protected Interval _interval;
/** Count to provide a unique key to tournies as they're created. */ /** Count to provide a unique key to tournies as they're created. */
protected int _tourneyCount; protected int _tourneyCount;
/** Holds all the current tournies in the game. */
protected Map<Comparable, TourneyManager> _tourneys = Maps.newHashMap();
// our dependencies
@Inject protected RootDObjectManager _omgr;
@Inject protected TourneyRepository _tournrep;
} }
@@ -30,6 +30,9 @@ import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
@@ -49,6 +52,7 @@ import static com.threerings.parlor.Log.log;
/** /**
* The persistent store for tourney related information. * The persistent store for tourney related information.
*/ */
@Singleton
public class TourneyRepository extends JORARepository public class TourneyRepository extends JORARepository
{ {
/** Contains tourney information loaded from the database. */ /** Contains tourney information loaded from the database. */
@@ -100,9 +104,13 @@ public class TourneyRepository extends JORARepository
} }
} }
public TourneyRepository (ConnectionProvider conprov, String dbident) /** The database identifier used when establishing a database connection. This value being
* <code>tourneydb</code>. */
public static final String TOURNEY_DB_IDENT = "tourneydb";
@Inject public TourneyRepository (ConnectionProvider conprov)
{ {
super(conprov, dbident); super(conprov, TOURNEY_DB_IDENT);
} }
/** /**