Provide access to player info via safer getter methods rather than giving

away references to our internal arrays.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1495 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-06-19 23:18:58 +00:00
parent 823bafb2f5
commit 5bfbc9f17a
3 changed files with 35 additions and 20 deletions
@@ -1,5 +1,5 @@
//
// $Id: GameManager.java,v 1.35 2002/06/19 23:06:08 shaper Exp $
// $Id: GameManager.java,v 1.36 2002/06/19 23:18:58 mdb Exp $
package com.threerings.parlor.game;
@@ -96,11 +96,27 @@ public class GameManager extends PlaceManager
}
/**
* Returns an array of the usernames of the players in this game.
* Returns the name of the player with the specified index.
*/
public String[] getPlayers ()
public String getPlayerName (int index)
{
return _players;
return _players[index];
}
/**
* Returns the user object oid of the player with the specified index.
*/
public int getPlayerOid (int index)
{
return _playerOids[index];
}
/**
* Returns the number of players in the game.
*/
public int getPlayerCount ()
{
return _players.length;
}
/**
@@ -1,5 +1,5 @@
//
// $Id: TurnGameManager.java,v 1.5 2002/02/12 06:57:30 mdb Exp $
// $Id: TurnGameManager.java,v 1.6 2002/06/19 23:18:58 mdb Exp $
package com.threerings.parlor.turn;
@@ -32,7 +32,13 @@ public interface TurnGameManager
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public String[] getPlayers ();
public String getPlayerName (int index);
/**
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public int getPlayerCount ();
/**
* Called when we are about to start the next turn. Implementations
@@ -1,5 +1,5 @@
//
// $Id: TurnGameManagerDelegate.java,v 1.4 2002/02/13 03:21:28 mdb Exp $
// $Id: TurnGameManagerDelegate.java,v 1.5 2002/06/19 23:18:58 mdb Exp $
package com.threerings.parlor.turn;
@@ -41,8 +41,8 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
public int getTurnHolderIndex ()
{
String holder = _turnGame.getTurnHolder();
for (int i = 0; i < _players.length; i++) {
if (_players[i].equals(holder)) {
for (int i = 0; i < _tgmgr.getPlayerCount(); i++) {
if (_tgmgr.getPlayerName(i).equals(holder)) {
return i;
}
}
@@ -62,7 +62,7 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
public void startTurn ()
{
// sanity check
if (_turnIdx < 0 || _turnIdx >= _players.length) {
if (_turnIdx < 0 || _turnIdx >= _tgmgr.getPlayerCount()) {
Log.warning("startTurn() called with invalid turn index " +
"[turnIdx=" + _turnIdx + "].");
// abort, abort
@@ -73,7 +73,7 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
_tgmgr.turnWillStart();
// and set the turn indicator accordingly
_turnGame.setTurnHolder(_players[_turnIdx]);
_turnGame.setTurnHolder(_tgmgr.getPlayerName(_turnIdx));
}
/**
@@ -117,9 +117,6 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
*/
public void gameDidStart ()
{
// grab the players array
_players = _tgmgr.getPlayers();
// figure out who will be first
setFirstTurnHolder();
@@ -137,7 +134,7 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
{
// TODO: sort out a better random number generator and make it
// available via the parlor services
_turnIdx = MathUtil.random(_players.length);
_turnIdx = MathUtil.random(_tgmgr.getPlayerCount());
}
/**
@@ -150,7 +147,7 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
protected void setNextTurnHolder ()
{
// next!
_turnIdx = (_turnIdx + 1) % _players.length;
_turnIdx = (_turnIdx + 1) % _tgmgr.getPlayerCount();
}
/** The game manager for which we are delegating. */
@@ -159,10 +156,6 @@ public class TurnGameManagerDelegate extends GameManagerDelegate
/** A reference to our game object. */
protected TurnGameObject _turnGame;
/** Our own happy copy of the game manager's players array. I *love*
* not having fucking multiple inheritance. */
protected String[] _players;
/** The offset into the _players array of the current turn holder or
* -1 if it's no one's turn. */
protected int _turnIdx = -1;