Delegation! Since it's clear that extracting services into delegation

classes is only going to become more common, I've gone and created a
comprehensive facility for creating and using delegates in the place
controller and manager as well as the game controller and manager. With
the pattern nicely set, it is also easy to extend to controller/managers
further up the hierarchy that might need to delegate special methods of
their own. Whee!


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@994 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-02-13 03:21:28 +00:00
parent 8399c9a9cf
commit a85a6f1394
13 changed files with 531 additions and 106 deletions
@@ -1,31 +1,24 @@
//
// $Id: TurnGameManagerDelegate.java,v 1.3 2002/02/12 07:17:33 mdb Exp $
// $Id: TurnGameManagerDelegate.java,v 1.4 2002/02/13 03:21:28 mdb Exp $
package com.threerings.parlor.turn;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.parlor.Log;
import com.threerings.parlor.game.GameManager;
import com.threerings.parlor.game.GameManagerDelegate;
import com.threerings.parlor.util.MathUtil;
/**
* Performs the server-side turn-based game processing for a turn based
* game. Games which wish to make use of the turn services must call the
* following methods on the delegate at the appropriate times:
*
* <pre>
* init()
* gameDidStart()
* startTurn()
* endTurn()
* </pre>
*
* They must also implement the {@link TurnGameManager} interface. If the
* game requires specialized behavior from its delegate, it can extend the
* delegate and override the many methods that have been provided for just
* such a purpose (e.g. {@link #setFirstTurnHolder}, {@link
* #setNextTurnHolder}).
* game. Game managers which wish to make use of the turn services must
* implement {@link TurnGameManager} either create an instance of this
* class, or an instance of a derivation which customizes the behavior,
* either of which would be passed to {@link GameManager#addDelegate} to
* be activated.
*/
public class TurnGameManagerDelegate
public class TurnGameManagerDelegate extends GameManagerDelegate
{
/**
* Constructs a delegate that will manage the turn game state and call
@@ -34,36 +27,10 @@ public class TurnGameManagerDelegate
*/
public TurnGameManagerDelegate (TurnGameManager tgmgr)
{
super((GameManager)tgmgr);
_tgmgr = tgmgr;
}
/**
* This should be called from {@link GameManager#didStartup} to
* initialize the delegate.
*/
public void init (TurnGameObject turnGame)
{
_turnGame = turnGame;
}
/**
* This should be called from {@link GameManager#gameDidStart} to let
* the turn delegate perform start of game processing.
*/
public void gameDidStart ()
{
// grab the players array
_players = _tgmgr.getPlayers();
// figure out who will be first
setFirstTurnHolder();
// and start the first turn if we should apparently do so
if (_turnIdx != -1) {
startTurn();
}
}
/**
* Returns the index of the current turn holder as configured in the
* game object.
@@ -82,17 +49,6 @@ public class TurnGameManagerDelegate
return -1;
}
/**
* This is called to determine whichi player will take the first
* turn. The default implementation chooses a player at random.
*/
protected void setFirstTurnHolder ()
{
// TODO: sort out a better random number generator and make it
// available via the parlor services
_turnIdx = MathUtil.random(_players.length);
}
/**
* Called to start the next turn. It calls {@link
* TurnGameManager#turnWillStart} to allow our owning manager to
@@ -149,6 +105,41 @@ public class TurnGameManagerDelegate
}
}
// documentation inherited
public void didStartup (PlaceObject plobj)
{
_turnGame = (TurnGameObject)plobj;
}
/**
* This should be called from {@link GameManager#gameDidStart} to let
* the turn delegate perform start of game processing.
*/
public void gameDidStart ()
{
// grab the players array
_players = _tgmgr.getPlayers();
// figure out who will be first
setFirstTurnHolder();
// and start the first turn if we should apparently do so
if (_turnIdx != -1) {
startTurn();
}
}
/**
* This is called to determine whichi player will take the first
* turn. The default implementation chooses a player at random.
*/
protected void setFirstTurnHolder ()
{
// TODO: sort out a better random number generator and make it
// available via the parlor services
_turnIdx = MathUtil.random(_players.length);
}
/**
* This is called to determine which player will next hold the turn.
* The default implementation simply rotates through the players in