Allow a player to be replaced in an in-play game; properly handle the case

in a turn-based game where the turn holder is replaced and update the
turnHolder field in a way that doesn't cause the client to think that the
turn changed.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3667 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-08-03 07:46:54 +00:00
parent a3ca9ba149
commit 738cf657cb
5 changed files with 72 additions and 1 deletions
@@ -270,6 +270,36 @@ public class GameManager extends PlaceManager
{
}
/**
* Replaces the player at the specified index and calls {@link
* #playerWasReplaced} to let derived classes and delegates know
* what's going on.
*/
public void replacePlayer (final int pidx, final Name player)
{
final Name oplayer = _gameobj.players[pidx];
_gameobj.setPlayersAt(player, pidx);
// allow derived classes to respond
playerWasReplaced(pidx, oplayer, player);
// notify our delegates
applyToDelegates(new DelegateOp() {
public void apply (PlaceManagerDelegate delegate) {
((GameManagerDelegate)delegate).playerWasReplaced(
pidx, oplayer, player);
}
});
}
/**
* Called when a player has been replaced via a call to {@link
* #replacePlayer}.
*/
protected void playerWasReplaced (int pidx, Name oldPlayer, Name newPlayer)
{
}
/**
* Returns the user object for the player with the specified index or
* null if the player at that index is not online.
@@ -21,6 +21,8 @@
package com.threerings.parlor.game.server;
import com.threerings.util.Name;
import com.threerings.crowd.server.PlaceManagerDelegate;
import com.threerings.parlor.game.data.GameAI;
@@ -54,6 +56,14 @@ public class GameManagerDelegate extends PlaceManagerDelegate
{
}
/**
* Called when a player in the game has been replaced by a call to
* {@link GameManager#replacePlayer}.
*/
public void playerWasReplaced (int pidx, Name oldPlayer, Name newPlayer)
{
}
/**
* Called by the manager when we should do some AI. Only called while
* the game is IN_PLAY.
@@ -117,7 +117,18 @@ public class TurnGameControllerDelegate extends GameControllerDelegate
{
// handle turn changes
if (event.getName().equals(_thfield)) {
_tgctrl.turnDidChange((Name)event.getValue());
Name name = (Name)event.getValue();
Name oname = (Name)event.getOldValue();
if (TurnGameObject.TURN_HOLDER_REPLACED.equals(name) ||
TurnGameObject.TURN_HOLDER_REPLACED.equals(oname)) {
// small hackery: ignore the turn holder being set to
// TURN_HOLDER_REPLACED as it means that we're replacing
// the current turn holder rather than switching turns;
// also ignore the new turn holder when we switch from THR
// to a real name again
} else {
_tgctrl.turnDidChange(name);
}
}
}
@@ -31,6 +31,12 @@ import com.threerings.parlor.game.data.GameObject;
*/
public interface TurnGameObject
{
/** A special value used to communicate to the client that the current
* turn holder was replaced (perhaps due to disconnection or departure
* and being replaced by an AI). */
public static final Name TURN_HOLDER_REPLACED =
new Name("__TURN_HOLDER_REPLACED__");
/**
* Returns the distributed object field name of the
* <code>turnHolder</code> field in the object that implements this
@@ -158,6 +158,20 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
_turnGame = (TurnGameObject)plobj;
}
// documentation inherited
public void playerWasReplaced (int pidx, Name oplayer, Name nplayer)
{
// we need to update the turn holder if the current turn holder
// was the player that was replaced and we need to do so in a way
// that doesn't make everyone think that the turn just changed
if (oplayer != null && oplayer.equals(_turnGame.getTurnHolder())) {
// small hackery: this will indicate to the client that we are
// replacing the turn holder rather than changing the turn
_turnGame.setTurnHolder(TurnGameObject.TURN_HOLDER_REPLACED);
_turnGame.setTurnHolder(nplayer);
}
}
/**
* This should be called from {@link GameManager#gameDidStart} to let
* the turn delegate perform start of game processing.