Revamped the player array in the game object to allow sparse player arrays

that may have one or more null player entries so that party games can
create their game with the maximum number of players they support and can
use the player index as the player's "seat".  Simplified some of the
jockeying we had to do when adding and removing players.  Complexified
some of the turn game management in order to deal properly with the sparse
player arrays.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1802 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2002-10-15 23:07:23 +00:00
parent 1d7f033586
commit 21c599cd0a
7 changed files with 257 additions and 108 deletions
@@ -1,5 +1,5 @@
//
// $Id: GameObject.java,v 1.9 2002/09/20 04:53:42 mdb Exp $
// $Id: GameObject.java,v 1.10 2002/10/15 23:07:23 shaper Exp $
package com.threerings.parlor.game;
@@ -35,6 +35,9 @@ public class GameObject extends PlaceObject
/** The field name of the <code>roundId</code> field. */
public static final String ROUND_ID = "roundId";
/** The field name of the <code>creator</code> field. */
public static final String CREATOR = "creator";
/** A game state constant indicating that the game has not yet started
* and is still awaiting the arrival of all of the players. */
public static final int AWAITING_PLAYERS = 0;
@@ -58,20 +61,43 @@ public class GameObject extends PlaceObject
/** Indicates whether or not this game is rated. */
public boolean isRated;
/** The username of the players involved in this game. */
/** The usernames of the players involved in this game. */
public String[] players;
/** The unique round identifier for the current round. */
public int roundId;
/** The player index of the creating player if this is a party game. */
public int creator;
/**
* Returns the number of players in the game.
*/
protected int getPlayerCount ()
{
int count = 0;
int size = players.length;
for (int ii = 0; ii < size; ii++) {
if (players[ii] != null) {
count++;
}
}
return count;
}
/**
* Returns the player index of the given user in the game, or
* <code>-1</code> if the player is not involved in the game.
*/
public int getPlayerIndex (String username)
{
return (players == null) ? -1 :
ListUtil.indexOfEqual(players, username);
int size = (players == null) ? 0 : players.length;
for (int ii = 0; ii < size; ii++) {
if (players[ii] != null && players[ii].equals(username)) {
return ii;
}
}
return -1;
}
/**
@@ -83,6 +109,14 @@ public class GameObject extends PlaceObject
return (state == IN_PLAY);
}
/**
* Returns whether the given player index in the game is occupied.
*/
public boolean isOccupiedPlayer (int index)
{
return (players[index] != null);
}
/**
* Requests that the <code>gameService</code> field be set to the specified
* value. The local value will be updated immediately and an event
@@ -166,4 +200,18 @@ public class GameObject extends PlaceObject
this.roundId = roundId;
requestAttributeChange(ROUND_ID, new Integer(roundId));
}
/**
* Requests that the <code>creator</code> field be set to the specified
* value. The local value will be updated immediately and an event
* will be propagated through the system to notify all listeners that
* the attribute did change. Proxied copies of this object (on
* clients) will apply the value change when they received the
* attribute changed notification.
*/
public void setCreator (int creator)
{
this.creator = creator;
requestAttributeChange(CREATOR, new Integer(creator));
}
}