From a85a6f13942aabbc2e25e7d9f3622fdbd22f1029 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 13 Feb 2002 03:21:28 +0000 Subject: [PATCH] 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 --- .../crowd/client/PlaceController.java | 87 ++++++++++++++- .../crowd/client/PlaceControllerDelegate.java | 67 ++++++++++++ .../threerings/crowd/server/PlaceManager.java | 91 +++++++++++++++- .../crowd/server/PlaceManagerDelegate.java | 76 +++++++++++++ .../parlor/game/GameController.java | 25 ++++- .../parlor/game/GameControllerDelegate.java | 47 ++++++++ .../threerings/parlor/game/GameManager.java | 26 ++++- .../parlor/game/GameManagerDelegate.java | 43 ++++++++ .../threerings/parlor/game/GameObject.dobj | 8 +- .../threerings/parlor/game/GameObject.java | 8 +- .../parlor/turn/TurnGameController.java | 8 +- .../turn/TurnGameControllerDelegate.java | 50 ++++----- .../parlor/turn/TurnGameManagerDelegate.java | 101 ++++++++---------- 13 files changed, 531 insertions(+), 106 deletions(-) create mode 100644 src/java/com/threerings/crowd/client/PlaceControllerDelegate.java create mode 100644 src/java/com/threerings/crowd/server/PlaceManagerDelegate.java create mode 100644 src/java/com/threerings/parlor/game/GameControllerDelegate.java create mode 100644 src/java/com/threerings/parlor/game/GameManagerDelegate.java diff --git a/src/java/com/threerings/crowd/client/PlaceController.java b/src/java/com/threerings/crowd/client/PlaceController.java index e5594d25d..573ec3ca6 100644 --- a/src/java/com/threerings/crowd/client/PlaceController.java +++ b/src/java/com/threerings/crowd/client/PlaceController.java @@ -1,9 +1,10 @@ // -// $Id: PlaceController.java,v 1.5 2001/10/12 19:31:15 mdb Exp $ +// $Id: PlaceController.java,v 1.6 2002/02/13 03:21:28 mdb Exp $ package com.threerings.crowd.client; import java.awt.event.ActionEvent; +import java.util.ArrayList; import com.samskivert.swing.Controller; import com.threerings.crowd.data.PlaceConfig; @@ -41,6 +42,13 @@ public abstract class PlaceController // create our user interface _view = createPlaceView(); + // initialize our delegates + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + delegate.init(_ctx, _config); + } + }); + // let the derived classes do any initialization stuff didInit(); } @@ -54,6 +62,16 @@ public abstract class PlaceController { } + /** + * Returns a reference to the place view associated with this + * controller. This is only valid after a call has been made to {@link + * #init}. + */ + public PlaceView getPlaceView () + { + return _view; + } + /** * Creates the user interface that will be used to display this place. * The view instance returned will later be configured with the place @@ -68,7 +86,7 @@ public abstract class PlaceController * PlaceViewUtil#dispatchWillEnterPlace}. Derived classes can override * this and perform any other starting up that they need to do */ - public void willEnterPlace (PlaceObject plobj) + public void willEnterPlace (final PlaceObject plobj) { if (_view != null ) { // let the UI hierarchy know that we've got our place @@ -76,6 +94,13 @@ public abstract class PlaceController // and display the user interface _ctx.setPlaceView(_view); } + + // let our delegates know what's up + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + delegate.willEnterPlace(plobj); + } + }); } /** @@ -85,12 +110,19 @@ public abstract class PlaceController * super.didLeavePlace) and perform any necessary * cleanup. */ - public void didLeavePlace (PlaceObject plobj) + public void didLeavePlace (final PlaceObject plobj) { // let the UI hierarchy know that we're outta here if (_view != null ) { PlaceViewUtil.dispatchDidLeavePlace(_view, plobj); } + + // let our delegates know what's up + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + delegate.didLeavePlace(plobj); + } + }); } /** @@ -98,9 +130,51 @@ public abstract class PlaceController * should be sure to call super.handleAction for events * they don't specifically handle. */ - public boolean handleAction (ActionEvent action) + public boolean handleAction (final ActionEvent action) { - return false; + final boolean[] handled = new boolean[0]; + + // let our delegates have a crack at the action + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + // we take advantage of short-circuiting here + handled[0] = handled[0] || delegate.handleAction(action); + } + }); + + return handled[0]; + } + + /** + * Adds the supplied delegate to the list for this controller. + */ + protected void addDelegate (PlaceControllerDelegate delegate) + { + if (_delegates == null) { + _delegates = new ArrayList(); + } + _delegates.add(delegate); + } + + /** + * Used to call methods in delegates. + */ + protected static interface DelegateOp + { + public void apply (PlaceControllerDelegate delegate); + } + + /** + * Applies the supplied operation to the registered delegates. + */ + protected void applyToDelegates (DelegateOp op) + { + if (_delegates != null) { + int dcount = _delegates.size(); + for (int i = 0; i < dcount; i++) { + op.apply((PlaceControllerDelegate)_delegates.get(i)); + } + } } /** A reference to the active client context. */ @@ -115,4 +189,7 @@ public abstract class PlaceController /** A reference to the root user interface component. */ protected PlaceView _view; + + /** A list of the delegates in use by this controller. */ + protected ArrayList _delegates; } diff --git a/src/java/com/threerings/crowd/client/PlaceControllerDelegate.java b/src/java/com/threerings/crowd/client/PlaceControllerDelegate.java new file mode 100644 index 000000000..f556a97cb --- /dev/null +++ b/src/java/com/threerings/crowd/client/PlaceControllerDelegate.java @@ -0,0 +1,67 @@ +// +// $Id: PlaceControllerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $ + +package com.threerings.crowd.client; + +import java.awt.event.ActionEvent; + +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.crowd.data.PlaceObject; +import com.threerings.crowd.util.CrowdContext; + +/** + * Provides an extensible mechanism for encapsulating delegated + * functionality that works with the place services. + * + *

Thanks to Java's lack of multiple inheritance, it will likely + * become necessary to factor certain services that might be used by a + * variety of {@link PlaceController} derived classes into delegate + * classes because they do not fit into the single inheritance hierarchy + * that makes sense for a particular application. To facilitate this + * process, this delegate class is provided which the standard place + * controller can be made to call out to for all of the standard methods. + */ +public class PlaceControllerDelegate +{ + /** + * Constructs the delegate with the controller for which it is + * delegating. + */ + public PlaceControllerDelegate (PlaceController controller) + { + _controller = controller; + } + + /** + * Called to initialize the delegate. + */ + public void init (CrowdContext ctx, PlaceConfig config) + { + } + + /** + * Called to let the delegate know that we're entering a place. + */ + public void willEnterPlace (PlaceObject plobj) + { + } + + /** + * Called to let the delegate know that we've left the place. + */ + public void didLeavePlace (PlaceObject plobj) + { + } + + /** + * Called to give the delegate a chance to handle controller actions + * that weren't handled by the main controller. + */ + public boolean handleAction (ActionEvent action) + { + return false; + } + + /** A reference to the controller for which we are delegating. */ + protected PlaceController _controller; +} diff --git a/src/java/com/threerings/crowd/server/PlaceManager.java b/src/java/com/threerings/crowd/server/PlaceManager.java index 1cfc0d77a..9ab7a8ab5 100644 --- a/src/java/com/threerings/crowd/server/PlaceManager.java +++ b/src/java/com/threerings/crowd/server/PlaceManager.java @@ -1,8 +1,9 @@ // -// $Id: PlaceManager.java,v 1.25 2001/12/14 00:11:17 mdb Exp $ +// $Id: PlaceManager.java,v 1.26 2002/02/13 03:21:28 mdb Exp $ package com.threerings.crowd.server; +import java.util.ArrayList; import java.util.HashMap; import java.util.Properties; @@ -16,7 +17,10 @@ import com.threerings.presents.dobj.ObjectRemovedEvent; import com.threerings.presents.dobj.OidListListener; import com.threerings.crowd.Log; -import com.threerings.crowd.data.*; +import com.threerings.crowd.data.BodyObject; +import com.threerings.crowd.data.OccupantInfo; +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.crowd.data.PlaceObject; /** * The place manager is the server-side entity that handles all @@ -101,6 +105,13 @@ public class PlaceManager _config = config; _omgr = omgr; + // initialize our delegates + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.didInit(_config); + } + }); + // let derived classes do initialization stuff didInit(); } @@ -131,6 +142,13 @@ public class PlaceManager // we'll need to hear about place object events plobj.addListener(this); + // let our delegates know that we've started up + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.didStartup(_plobj); + } + }); + // let our derived classes do their thang didStartup(); } @@ -153,6 +171,16 @@ public class PlaceManager { // destroy the object and everything will follow from that CrowdServer.omgr.destroyObject(_plobj.getOid()); + + // let our delegates know that we've shut down + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.didShutdown(); + } + }); + + // let our derived classes do their thang + didShutdown(); } /** @@ -217,16 +245,23 @@ public class PlaceManager /** * Called when a body object enters this place. */ - protected void bodyEntered (int bodyOid) + protected void bodyEntered (final int bodyOid) { Log.info("Body entered [ploid=" + _plobj.getOid() + ", oid=" + bodyOid + "]."); + + // let our delegates know what's up + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.bodyEntered(bodyOid); + } + }); } /** * Called when a body object leaves this place. */ - protected void bodyLeft (int bodyOid) + protected void bodyLeft (final int bodyOid) { Log.info("Body left [ploid=" + _plobj.getOid() + ", oid=" + bodyOid + "]."); @@ -239,6 +274,13 @@ public class PlaceManager _plobj.removeFromOccupantInfo(key); } + // let our delegates know what's up + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.bodyLeft(bodyOid); + } + }); + // if that leaves us with zero occupants, maybe do something if (_plobj.occupants.size() == 0) { placeBecameEmpty(); @@ -253,6 +295,12 @@ public class PlaceManager */ protected void placeBecameEmpty () { + // let our delegates know what's up + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + delegate.placeBecameEmpty(); + } + }); } /** @@ -356,6 +404,38 @@ public class PlaceManager buf.append(", config=").append(_config); } + /** + * Adds the supplied delegate to the list for this manager. + */ + protected void addDelegate (PlaceManagerDelegate delegate) + { + if (_delegates == null) { + _delegates = new ArrayList(); + } + _delegates.add(delegate); + } + + /** + * Used to call methods in delegates. + */ + protected static interface DelegateOp + { + public void apply (PlaceManagerDelegate delegate); + } + + /** + * Applies the supplied operation to the registered delegates. + */ + protected void applyToDelegates (DelegateOp op) + { + if (_delegates != null) { + int dcount = _delegates.size(); + for (int i = 0; i < dcount; i++) { + op.apply((PlaceManagerDelegate)_delegates.get(i)); + } + } + } + /** A distributed object manager for doing dobj stuff. */ protected DObjectManager _omgr; @@ -370,4 +450,7 @@ public class PlaceManager /** Message handlers are used to process message events. */ protected HashMap _msghandlers; + + /** A list of the delegates in use by this manager. */ + protected ArrayList _delegates; } diff --git a/src/java/com/threerings/crowd/server/PlaceManagerDelegate.java b/src/java/com/threerings/crowd/server/PlaceManagerDelegate.java new file mode 100644 index 000000000..5d0970401 --- /dev/null +++ b/src/java/com/threerings/crowd/server/PlaceManagerDelegate.java @@ -0,0 +1,76 @@ +// +// $Id: PlaceManagerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $ + +package com.threerings.crowd.server; + +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.crowd.data.PlaceObject; + +/** + * Provides an extensible mechanism for encapsulating delegated + * functionality that works with the place services. + * + *

Thanks to Java's lack of multiple inheritance, it will likely + * become necessary to factor certain services that might be used by a + * variety of {@link PlaceManager} derived classes into delegate classes + * because they do not fit into the single inheritance hierarchy that + * makes sense for a particular application. To facilitate this process, + * this delegate class is provided which the standard place manager can be + * made to call out to for all of the standard methods. + */ +public class PlaceManagerDelegate +{ + /** + * Provides the delegate with a reference to the manager for which it + * is delegating. + */ + public PlaceManagerDelegate (PlaceManager plmgr) + { + _plmgr = plmgr; + } + + /** + * Called when the place manager is initialized. + */ + public void didInit (PlaceConfig config) + { + } + + /** + * Called when the place manager is started up. + */ + public void didStartup (PlaceObject plobj) + { + } + + /** + * Called when the place manager is shut down. + */ + public void didShutdown () + { + } + + /** + * Called when a body enters the place. + */ + public void bodyEntered (int bodyOid) + { + } + + /** + * Called when a body leaves the place. + */ + public void bodyLeft (int bodyOid) + { + } + + /** + * Called when the last body leaves the place. + */ + public void placeBecameEmpty () + { + } + + /** A reference to the manager for which we are delegating. */ + protected PlaceManager _plmgr; +} diff --git a/src/java/com/threerings/parlor/game/GameController.java b/src/java/com/threerings/parlor/game/GameController.java index b2fe34dce..d7db4ce4c 100644 --- a/src/java/com/threerings/parlor/game/GameController.java +++ b/src/java/com/threerings/parlor/game/GameController.java @@ -1,5 +1,5 @@ // -// $Id: GameController.java,v 1.8 2001/10/12 00:03:03 mdb Exp $ +// $Id: GameController.java,v 1.9 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.game; @@ -8,7 +8,9 @@ import java.awt.event.ActionEvent; import com.samskivert.swing.Controller; import com.threerings.presents.dobj.*; + import com.threerings.crowd.client.PlaceController; +import com.threerings.crowd.client.PlaceControllerDelegate; import com.threerings.crowd.data.PlaceConfig; import com.threerings.crowd.data.PlaceObject; import com.threerings.crowd.util.CrowdContext; @@ -26,8 +28,7 @@ import com.threerings.parlor.util.ParlorContext; * subscription to the game object and dispatch of commands and * distributed object events. */ -public abstract class GameController - extends PlaceController +public abstract class GameController extends PlaceController implements AttributeChangeListener, GameCodes { /** @@ -127,6 +128,12 @@ public abstract class GameController */ protected void gameDidStart () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + ((GameControllerDelegate)delegate).gameDidStart(); + } + }); } /** @@ -136,6 +143,12 @@ public abstract class GameController */ protected void gameDidEnd () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + ((GameControllerDelegate)delegate).gameDidEnd(); + } + }); } /** @@ -143,6 +156,12 @@ public abstract class GameController */ protected void gameWasCancelled () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceControllerDelegate delegate) { + ((GameControllerDelegate)delegate).gameWasCancelled(); + } + }); } /** A reference to the active parlor context. */ diff --git a/src/java/com/threerings/parlor/game/GameControllerDelegate.java b/src/java/com/threerings/parlor/game/GameControllerDelegate.java new file mode 100644 index 000000000..60c004ef6 --- /dev/null +++ b/src/java/com/threerings/parlor/game/GameControllerDelegate.java @@ -0,0 +1,47 @@ +// +// $Id: GameControllerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $ + +package com.threerings.parlor.game; + +import com.threerings.crowd.client.PlaceControllerDelegate; + +/** + * Extends the {@link PlaceControllerDelegate} mechanism with game + * controller specific methods. + */ +public class GameControllerDelegate extends PlaceControllerDelegate +{ + /** + * Provides the delegate with a reference to the game controller for + * which it is delegating. + */ + public GameControllerDelegate (GameController ctrl) + { + super(ctrl); + } + + /** + * Called when the game transitions to the IN_PLAY + * state. This happens when all of the players have arrived and the + * server starts the game. + */ + public void gameDidStart () + { + } + + /** + * Called when the game transitions to the GAME_OVER + * state. This happens when the game reaches some end condition by + * normal means (is not cancelled or aborted). + */ + public void gameDidEnd () + { + } + + /** + * Called when the game was cancelled for some reason. + */ + public void gameWasCancelled () + { + } +} diff --git a/src/java/com/threerings/parlor/game/GameManager.java b/src/java/com/threerings/parlor/game/GameManager.java index 227951203..47dd91b6a 100644 --- a/src/java/com/threerings/parlor/game/GameManager.java +++ b/src/java/com/threerings/parlor/game/GameManager.java @@ -1,5 +1,5 @@ // -// $Id: GameManager.java,v 1.18 2002/02/12 06:57:29 mdb Exp $ +// $Id: GameManager.java,v 1.19 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.game; @@ -9,9 +9,11 @@ import com.threerings.presents.dobj.MessageEvent; import com.threerings.crowd.data.BodyObject; import com.threerings.crowd.data.PlaceObject; + import com.threerings.crowd.server.PlaceManager; import com.threerings.crowd.server.CrowdServer; import com.threerings.crowd.server.CrowdServer; +import com.threerings.crowd.server.PlaceManagerDelegate; import com.threerings.parlor.Log; import com.threerings.parlor.client.ParlorCodes; @@ -25,8 +27,7 @@ import com.threerings.parlor.client.ParlorCodes; * implicitly played in a location, the players of the game implicitly * bodies in that location. */ -public class GameManager - extends PlaceManager +public class GameManager extends PlaceManager implements ParlorCodes, GameCodes, AttributeChangeListener { // documentation inherited @@ -158,6 +159,12 @@ public class GameManager */ protected void gameWillStart () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + ((GameManagerDelegate)delegate).gameWillStart(); + } + }); } /** @@ -168,6 +175,12 @@ public class GameManager */ protected void gameDidStart () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + ((GameManagerDelegate)delegate).gameDidStart(); + } + }); } /** @@ -195,6 +208,13 @@ public class GameManager */ protected void gameDidEnd () { + // let our delegates do their business + applyToDelegates(new DelegateOp() { + public void apply (PlaceManagerDelegate delegate) { + ((GameManagerDelegate)delegate).gameDidEnd(); + } + }); + // calculate ratings and all that... } diff --git a/src/java/com/threerings/parlor/game/GameManagerDelegate.java b/src/java/com/threerings/parlor/game/GameManagerDelegate.java new file mode 100644 index 000000000..5059b1bf3 --- /dev/null +++ b/src/java/com/threerings/parlor/game/GameManagerDelegate.java @@ -0,0 +1,43 @@ +// +// $Id: GameManagerDelegate.java,v 1.1 2002/02/13 03:21:28 mdb Exp $ + +package com.threerings.parlor.game; + +import com.threerings.crowd.server.PlaceManagerDelegate; + +/** + * Extends the {@link PlaceManagerDelegate} mechanism with game manager + * specific methods. + */ +public class GameManagerDelegate extends PlaceManagerDelegate +{ + /** + * Provides the delegate with a reference to the game manager for + * which it is delegating. + */ + public GameManagerDelegate (GameManager gmgr) + { + super(gmgr); + } + + /** + * Called by the game manager when the game is about to start. + */ + public void gameWillStart () + { + } + + /** + * Called by the game manager after the game was started. + */ + public void gameDidStart () + { + } + + /** + * Called by the game manager after the game ended. + */ + public void gameDidEnd () + { + } +} diff --git a/src/java/com/threerings/parlor/game/GameObject.dobj b/src/java/com/threerings/parlor/game/GameObject.dobj index ab2d19516..c002c760d 100644 --- a/src/java/com/threerings/parlor/game/GameObject.dobj +++ b/src/java/com/threerings/parlor/game/GameObject.dobj @@ -1,5 +1,5 @@ // -// $Id: GameObject.dobj,v 1.7 2002/02/08 23:55:25 mdb Exp $ +// $Id: GameObject.dobj,v 1.8 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.game; @@ -23,13 +23,13 @@ public class GameObject extends PlaceObject public static final int AWAITING_PLAYERS = 0; /** A game state constant indicating that the game is in play. */ - public static final int IN_PLAY = 1; + public static final int IN_PLAY = AWAITING_PLAYERS+1; /** A game state constant indicating that the game ended normally. */ - public static final int GAME_OVER = 2; + public static final int GAME_OVER = IN_PLAY+2; /** A game state constant indicating that the game was cancelled. */ - public static final int CANCELLED = 3; + public static final int CANCELLED = GAME_OVER+3; /** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY}, * {@link #GAME_OVER}, or {@link #CANCELLED}. */ diff --git a/src/java/com/threerings/parlor/game/GameObject.java b/src/java/com/threerings/parlor/game/GameObject.java index 4b3da6988..9cda19cd7 100644 --- a/src/java/com/threerings/parlor/game/GameObject.java +++ b/src/java/com/threerings/parlor/game/GameObject.java @@ -1,5 +1,5 @@ // -// $Id: GameObject.java,v 1.1 2002/02/08 23:55:25 mdb Exp $ +// $Id: GameObject.java,v 1.2 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.game; @@ -32,13 +32,13 @@ public class GameObject extends PlaceObject public static final int AWAITING_PLAYERS = 0; /** A game state constant indicating that the game is in play. */ - public static final int IN_PLAY = 1; + public static final int IN_PLAY = AWAITING_PLAYERS+1; /** A game state constant indicating that the game ended normally. */ - public static final int GAME_OVER = 2; + public static final int GAME_OVER = IN_PLAY+2; /** A game state constant indicating that the game was cancelled. */ - public static final int CANCELLED = 3; + public static final int CANCELLED = GAME_OVER+3; /** The game state, one of {@link #AWAITING_PLAYERS}, {@link #IN_PLAY}, * {@link #GAME_OVER}, or {@link #CANCELLED}. */ diff --git a/src/java/com/threerings/parlor/turn/TurnGameController.java b/src/java/com/threerings/parlor/turn/TurnGameController.java index 75af4b903..c175967cf 100644 --- a/src/java/com/threerings/parlor/turn/TurnGameController.java +++ b/src/java/com/threerings/parlor/turn/TurnGameController.java @@ -1,13 +1,15 @@ // -// $Id: TurnGameController.java,v 1.3 2002/02/12 06:57:30 mdb Exp $ +// $Id: TurnGameController.java,v 1.4 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.turn; +import com.threerings.parlor.game.GameController; + /** * Games that wish to make use of the turn game services should have their * controller implement this interface and create an instance of {@link - * TurnGameControllerDelegate}, calling out to it at the appropriate - * times. + * TurnGameControllerDelegate} which should be passed to {@link + * GameController#addDelegate}. */ public interface TurnGameController { diff --git a/src/java/com/threerings/parlor/turn/TurnGameControllerDelegate.java b/src/java/com/threerings/parlor/turn/TurnGameControllerDelegate.java index e8b2fa74c..429b88b84 100644 --- a/src/java/com/threerings/parlor/turn/TurnGameControllerDelegate.java +++ b/src/java/com/threerings/parlor/turn/TurnGameControllerDelegate.java @@ -1,5 +1,5 @@ // -// $Id: TurnGameControllerDelegate.java,v 1.1 2002/02/12 06:57:30 mdb Exp $ +// $Id: TurnGameControllerDelegate.java,v 1.2 2002/02/13 03:21:28 mdb Exp $ package com.threerings.parlor.turn; @@ -7,9 +7,12 @@ import com.threerings.presents.dobj.AttributeChangedEvent; import com.threerings.presents.dobj.AttributeChangeListener; import com.threerings.crowd.data.BodyObject; +import com.threerings.crowd.data.PlaceConfig; +import com.threerings.crowd.data.PlaceObject; import com.threerings.crowd.util.CrowdContext; import com.threerings.parlor.game.GameController; +import com.threerings.parlor.game.GameControllerDelegate; import com.threerings.parlor.game.GameObject; /** @@ -20,7 +23,7 @@ import com.threerings.parlor.game.GameObject; * implement the {@link TurnGameController} interface so that it can be * notified when turn-based game events take place. */ -public class TurnGameControllerDelegate +public class TurnGameControllerDelegate extends GameControllerDelegate implements AttributeChangeListener { /** @@ -30,15 +33,10 @@ public class TurnGameControllerDelegate */ public TurnGameControllerDelegate (TurnGameController tgctrl) { - _tgctrl = tgctrl; - } + super((GameController)tgctrl); - /** - * This must be called from {@link GameController#init}. - */ - public void init (CrowdContext ctx) - { - _ctx = ctx; + // keep this around for later + _tgctrl = tgctrl; } /** @@ -52,27 +50,29 @@ public class TurnGameControllerDelegate _turnGame.getTurnHolder().equals(self.username)); } - /** - * This must be called from {@link GameController#willEnterPlace}. - */ - public void willEnterPlace (GameObject gobj) + // documentation inherited + public void init (CrowdContext ctx, PlaceConfig config) + { + _ctx = ctx; + } + + // documentation inherited + public void willEnterPlace (PlaceObject plobj) { // get a casted reference to the object - _gameObj = gobj; - _turnGame = (TurnGameObject)gobj; + _gameObj = (GameObject)plobj; + _turnGame = (TurnGameObject)plobj; _thfield = _turnGame.getTurnHolderFieldName(); // and add ourselves as a listener - gobj.addListener(this); + plobj.addListener(this); } - /** - * This must be called from {@link GameController#didLeavePlace}. - */ - public void didLeavePlace (GameObject gobj) + // documentation inherited + public void didLeavePlace (PlaceObject plobj) { // remove our listenership - gobj.removeListener(this); + plobj.removeListener(this); // clean up _turnGame = null; @@ -87,12 +87,12 @@ public class TurnGameControllerDelegate } } - /** A reference to our client context. */ - protected CrowdContext _ctx; - /** The turn game controller for whom we are delegating. */ protected TurnGameController _tgctrl; + /** A reference to our client context. */ + protected CrowdContext _ctx; + /** A reference to our game object. */ protected GameObject _gameObj; diff --git a/src/java/com/threerings/parlor/turn/TurnGameManagerDelegate.java b/src/java/com/threerings/parlor/turn/TurnGameManagerDelegate.java index 191039f18..768898238 100644 --- a/src/java/com/threerings/parlor/turn/TurnGameManagerDelegate.java +++ b/src/java/com/threerings/parlor/turn/TurnGameManagerDelegate.java @@ -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: - * - *

- * init()
- * gameDidStart()
- * startTurn()
- * endTurn()
- * 
- * - * 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