From d46423ea7be13ea41769f4239fd523a0da2642ab Mon Sep 17 00:00:00 2001 From: Walter Korman Date: Thu, 24 Oct 2002 07:01:08 +0000 Subject: [PATCH] Added addPlayerAt(). Don't send gameReady notification to players when added since it's (at least currently) assumed that the puzzle has already been created and the player is fully present in the puzzle room. Sanity-check to disallow adding a player to a game more than once. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1823 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/parlor/game/GameManager.java | 83 +++++++++++++------ 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/java/com/threerings/parlor/game/GameManager.java b/src/java/com/threerings/parlor/game/GameManager.java index 93152f3df..83711703d 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.48 2002/10/15 23:07:23 shaper Exp $ +// $Id: GameManager.java,v 1.49 2002/10/24 07:01:08 shaper Exp $ package com.threerings.parlor.game; @@ -60,18 +60,9 @@ public class GameManager extends PlaceManager */ public int addPlayer (String player) { - // make sure we've space to add another player - int size = _gameobj.players.length; - if (_playerCount == size) { - Log.warning("Attempt to add player to full game " + - "[game=" + _gameobj.which() + ", player=" + player + - ", players=" + StringUtil.toString(_gameobj.players) + - "]."); - return -1; - } - // determine the first available player index int pidx = -1; + int size = _gameobj.players.length; for (int ii = 0; ii < size; ii++) { if (!_gameobj.isOccupiedPlayer(ii)) { pidx = ii; @@ -81,36 +72,76 @@ public class GameManager extends PlaceManager // sanity-check the player index if (pidx == -1) { - Log.warning("Couldn't find free player index for player who " + - "ought to be addable?! [game=" + _gameobj.which() + - ", player=" + player + + Log.warning("Couldn't find free player index for player " + + "[game=" + _gameobj.which() + ", player=" + player + ", players=" + StringUtil.toString(_gameobj.players) + "]."); return -1; } + // proceed with the rest of the adding business + return (!addPlayerAt(player, pidx)) ? -1 : pidx; + } + + /** + * Adds the given player to the game at the specified player index. + * This should only be called before the game is started, and is most + * likely to be used to add players to party games. + * + * @param player the username of the player to add to this game. + * @param pidx the player index at which the player is to be added. + * @return true if the player was added successfully, false if not. + */ + public boolean addPlayerAt (String player, int pidx) + { + // make sure the specified player index is valid + if (pidx < 0 || pidx >= _gameobj.players.length) { + Log.warning("Attempt to add player at an invalid index " + + "[game=" + _gameobj.which() + ", player=" + player + + ", pidx=" + pidx + "]."); + return false; + } + + // make sure the player index is available + if (_gameobj.players[pidx] != null) { + Log.warning("Attempt to add player at occupied index " + + "[game=" + _gameobj.which() + ", player=" + player + + ", pidx=" + pidx + "]."); + return false; + } + + // make sure the player isn't already somehow a part of the game + // to avoid any potential badness that might ensue if we added + // them more than once + if (_gameobj.getPlayerIndex(player) != -1) { + Log.warning("Attempt to add player to game that they're already " + + "playing [game=" + _gameobj.which() + + ", player=" + player + "]."); + return false; + } + + // get the player's body object + BodyObject bobj = CrowdServer.lookupBody(player); + if (bobj == null) { + Log.warning("Unable to get body object while adding player " + + "[game=" + _gameobj.which() + + ", player=" + player + "]."); + return false; + } + // fill in the player's information _gameobj.setPlayersAt(player, pidx); // increment the number of players in the game _playerCount++; - // deliver a game ready notification to the player - BodyObject bobj = CrowdServer.lookupBody(player); - if (bobj == null) { - Log.warning("Newly added player's body object went away before " + - "game ready notification could be sent " + - "[game=" + _gameobj.which() + ", player=" + player + - ", players=" + StringUtil.toString(_gameobj.players) + - "]."); - } else { - ParlorSender.gameIsReady(bobj, _gameobj.getOid()); - } + // save off their oid + _playerOids[pidx] = bobj.getOid(); // let derived classes do what they like playerWasAdded(player, pidx); - return pidx; + return true; } /**