From 3bb6c7e3f68d715629c69d9538a82d7ae5346f7f Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 23 Oct 2007 01:36:20 +0000 Subject: [PATCH] - In TurnGameManagerDelegate, pulled out random-turn picking into its own method, so that subclasses can get that behavior without assuming their their superclass implementation of setFirstTurnHolder does that. - Use that in EZ's code, and also fall back to it if there's some problem with the user-specified next-turn value and it's nobody's turn. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@464 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../ezgame/server/EZGameTurnDelegate.java | 11 ++++--- .../turn/server/TurnGameManagerDelegate.java | 32 ++++++++++++------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java b/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java index 010e8064..8d9bd57e 100644 --- a/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java +++ b/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java @@ -69,11 +69,12 @@ public class EZGameTurnDelegate extends TurnGameManagerDelegate return; } - } else if (_turnIdx == -1) { - // If it's nobody's turn, and the user does not specify a next-turn, randomize. - // We set turnIdx to the player we want - 1, so that when it gets inc'd it'll - // be right. - _turnIdx = RandomUtil.getInt(_turnGame.getPlayers().length) - 1; + } + + // Otherwise, if it's nobody's turn randomly pick a turn holder + if (_turnIdx == -1) { + assignTurnRandomly(); + return; } // otherwise, do the default behavior diff --git a/src/java/com/threerings/parlor/turn/server/TurnGameManagerDelegate.java b/src/java/com/threerings/parlor/turn/server/TurnGameManagerDelegate.java index 77b96a2d..920219f5 100644 --- a/src/java/com/threerings/parlor/turn/server/TurnGameManagerDelegate.java +++ b/src/java/com/threerings/parlor/turn/server/TurnGameManagerDelegate.java @@ -194,18 +194,7 @@ public class TurnGameManagerDelegate extends GameManagerDelegate */ protected void setFirstTurnHolder () { - int size = _turnGame.getPlayers().length; - if (size > 0) { - int firstPick = _turnIdx = RandomUtil.getInt(size); - while (!_tgmgr.isActivePlayer(_turnIdx)) { - _turnIdx = (_turnIdx + 1) % size; - if (_turnIdx == firstPick) { - Log.warning("No players eligible for first turn. Choking. " + - "[game=" + where() + "]."); - return; - } - } - } + assignTurnRandomly(); } /** @@ -237,6 +226,25 @@ public class TurnGameManagerDelegate extends GameManagerDelegate } while (!_tgmgr.isActivePlayer(_turnIdx)); } + /** + * Convenience function to randomly assign the turn. + */ + protected void assignTurnRandomly () + { + int size = _turnGame.getPlayers().length; + if (size > 0) { + int firstPick = _turnIdx = RandomUtil.getInt(size); + while (!_tgmgr.isActivePlayer(_turnIdx)) { + _turnIdx = (_turnIdx + 1) % size; + if (_turnIdx == firstPick) { + Log.warning("No players eligible for randomly-assigned turn. Choking. " + + "[game=" + where() + "]."); + return; + } + } + } + } + /** The game manager for which we are delegating. */ protected TurnGameManager _tgmgr;