From 8d55a105a9e66d57929b31ad5bbd092f84435e53 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Sat, 5 Jan 2008 01:05:52 +0000 Subject: [PATCH] Made turns work in party games as well as seated games. In a party game, if a next player is unspecified, the player that has been around the longest without getting a turn will get the turn. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@539 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../ezgame/server/EZGameManager.java | 48 +++-- .../ezgame/server/EZGameTurnDelegate.java | 75 +------- .../ezgame/server/EZPartyTurnDelegate.java | 177 ++++++++++++++++++ .../ezgame/server/EZSeatedTurnDelegate.java | 84 +++++++++ 4 files changed, 298 insertions(+), 86 deletions(-) create mode 100644 src/java/com/threerings/ezgame/server/EZPartyTurnDelegate.java create mode 100644 src/java/com/threerings/ezgame/server/EZSeatedTurnDelegate.java diff --git a/src/java/com/threerings/ezgame/server/EZGameManager.java b/src/java/com/threerings/ezgame/server/EZGameManager.java index 80f08cd1..c64ead0d 100644 --- a/src/java/com/threerings/ezgame/server/EZGameManager.java +++ b/src/java/com/threerings/ezgame/server/EZGameManager.java @@ -76,7 +76,7 @@ public class EZGameManager extends GameManager { public EZGameManager () { - addDelegate(_turnDelegate = new EZGameTurnDelegate()); +// addDelegate(_turnDelegate = new EZGameTurnDelegate()); } /** @@ -139,21 +139,21 @@ public class EZGameManager extends GameManager { validateUser(caller); - // make sure this player is the turn holder - Name holder = _ezObj.turnHolder; - if (holder != null && !holder.equals(((BodyObject) caller).getVisibleName())) { - throw new InvocationException(InvocationCodes.ACCESS_DENIED); - } +// // make sure this player is the turn holder +// Name holder = _ezObj.turnHolder; +// if (holder != null && !holder.equals(((BodyObject) caller).getVisibleName())) { +// throw new InvocationException(InvocationCodes.ACCESS_DENIED); +// } - Name nextTurnHolder = null; - if (nextPlayerId != 0) { - BodyObject target = getPlayerByOid(nextPlayerId); - if (target != null) { - nextTurnHolder = target.getVisibleName(); - } - } +// Name nextTurnHolder = null; +// if (nextPlayerId != 0) { +// BodyObject target = getPlayerByOid(nextPlayerId); +// if (target != null) { +// nextTurnHolder = target.getVisibleName(); +// } +// } - _turnDelegate.endTurn(nextTurnHolder); + _turnDelegate.endTurn(nextPlayerId); } // from EZGameProvider @@ -556,6 +556,26 @@ public class EZGameManager extends GameManager return new EZGameObject(); } + @Override + protected void didInit () + { + super.didInit(); + + // initialize the appropriate turn delegate + if (getMatchType() == GameConfig.PARTY) { + EZPartyTurnDelegate del = new EZPartyTurnDelegate(); + _turnDelegate = del; + addDelegate(del); + del.didInit(_config); + + } else { + EZSeatedTurnDelegate del = new EZSeatedTurnDelegate(); + _turnDelegate = del; + addDelegate(del); + del.didInit(_config); + } + } + @Override protected void didStartup () { diff --git a/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java b/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java index 33d916f4..ee0e35d9 100644 --- a/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java +++ b/src/java/com/threerings/ezgame/server/EZGameTurnDelegate.java @@ -1,81 +1,12 @@ // // $Id$ -// -// Vilya library - tools for developing networked games -// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved -// http://www.threerings.net/code/vilya/ -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package com.threerings.ezgame.server; -import com.samskivert.util.ListUtil; -import com.samskivert.util.RandomUtil; - -import com.threerings.util.Name; - -import com.threerings.parlor.turn.server.TurnGameManagerDelegate; - -/** - * A special turn delegate for ez games. - */ -public class EZGameTurnDelegate extends TurnGameManagerDelegate +public interface EZGameTurnDelegate { /** - * A form of endTurn where you can specify the next turn holder oid. + * Start the next turn, specifying the id of the next turn holder or 0. */ - public void endTurn (Name nextPlayer) - { - _nextPlayer = nextPlayer; - endTurn(); - } - - @Override - protected void setFirstTurnHolder () - { - // make it nobody's turn - _turnIdx = -1; - } - - @Override - protected void setNextTurnHolder () - { - // if the user-supplied value seems to make sense, use it! - if (_nextPlayer != null) { - // copy the value, clear out _nextPlayer. - Name nextPlayer = _nextPlayer; - _nextPlayer = null; - - int index = ListUtil.indexOf(_turnGame.getPlayers(), nextPlayer); - if (index != -1) { - _turnIdx = index; - return; - } - - } - - // Otherwise, if it's nobody's turn randomly pick a turn holder - if (_turnIdx == -1) { - assignTurnRandomly(); - return; - } - - // otherwise, do the default behavior - super.setNextTurnHolder(); - } - - /** An override next turn holder, or null. */ - protected Name _nextPlayer; + public void endTurn (int nextTurnHolderId); } diff --git a/src/java/com/threerings/ezgame/server/EZPartyTurnDelegate.java b/src/java/com/threerings/ezgame/server/EZPartyTurnDelegate.java new file mode 100644 index 00000000..15f0d7e0 --- /dev/null +++ b/src/java/com/threerings/ezgame/server/EZPartyTurnDelegate.java @@ -0,0 +1,177 @@ +// +// $Id: EZGameTurnDelegate.java 523 2007-12-07 21:41:22Z ray $ +// +// Vilya library - tools for developing networked games +// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/vilya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.ezgame.server; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashSet; + +import com.samskivert.util.RandomUtil; + +import com.threerings.util.Name; + +import com.threerings.crowd.data.BodyObject; +import com.threerings.crowd.data.PlaceObject; +import com.threerings.crowd.server.PlaceManager; + +import com.threerings.parlor.game.server.GameManagerDelegate; + +import com.threerings.parlor.turn.data.TurnGameObject; +import com.threerings.parlor.turn.server.TurnGameManager; + +/** + * A special turn delegate for seated ez games. + */ +public class EZPartyTurnDelegate extends GameManagerDelegate + implements EZGameTurnDelegate +{ + @Override + public void setPlaceManager (PlaceManager plmgr) + { + super.setPlaceManager(plmgr); + _tgmgr = (TurnGameManager) plmgr; + } + + @Override + public void didStartup (PlaceObject plobj) + { + super.didStartup(plobj); + _plobj = plobj; + _turnGame = (TurnGameObject) plobj; + } + + @Override + public void bodyEntered (int bodyOid) + { + super.bodyEntered(bodyOid); + + if (_ordering != null) { + _ordering.add(bodyOid); + } + } + + @Override + public void bodyLeft (int bodyOid) + { + super.bodyLeft(bodyOid); + + if (_ordering != null) { + _ordering.remove(bodyOid); + } + } + + @Override + public void gameDidEnd () + { + super.gameDidEnd(); + + // we can forget about any ordering now + _ordering = null; + } + + // from EZGameTurnDelegate + public void endTurn (int nextPlayerId) + { + _tgmgr.turnDidEnd(); + if (!_turnGame.isInPlay()) { + _turnGame.setTurnHolder(null); + _currentHolderId = 0; + return; + } + + // set up the ordering if we haven't done so already + if (_ordering == null) { + createOrdering(); + } + + // try using the player-specified value + if (nextPlayerId != 0 && setNextTurn(nextPlayerId)) { + return; + } + + Iterator itr = _ordering.iterator(); + while (itr.hasNext()) { + nextPlayerId = itr.next(); + if (nextPlayerId != _currentHolderId) { + setNextTurn(nextPlayerId); // should always work + return; + } + } + + // we may get to the end without finding a new turn holder. Oh well! + } + + /** + * Set the next turn holder to the specified id, returning true on success. + */ + protected boolean setNextTurn (int nextPlayerId) + { + BodyObject nextPlayer = ((EZGameManager) _plmgr).getOccupantByOid(nextPlayerId); + if (nextPlayer == null) { + return false; + } + + // if the last turn holder was still in there, move them to the end of the list now + if (_ordering.remove(_currentHolderId)) { + _ordering.add(_currentHolderId); + } + + _tgmgr.turnWillStart(); + _turnGame.setTurnHolder(nextPlayer.getVisibleName()); + _tgmgr.turnDidStart(); + _currentHolderId = nextPlayerId; + return true; + } + + /** + * Add all the occupant body oids to the _ordering list in a random order. + */ + protected void createOrdering () + { + ArrayList list = new ArrayList(_plobj.occupants.size()); + for (int ii = _plobj.occupants.size() - 1; ii >= 0; ii--) { + list.add(_plobj.occupants.get(ii)); + } + + // randomize the list + Collections.shuffle(list, RandomUtil.rand); + + // and start out the ordering using that random order + _ordering = new LinkedHashSet(list); + } + + /** The place object. */ + protected PlaceObject _plobj; + + /** The game manager for which we are delegating. */ + protected TurnGameManager _tgmgr; + + /** A reference to our game object. */ + protected TurnGameObject _turnGame; + + /** Tracks the turn ordering for occupants. */ + protected LinkedHashSet _ordering; + + /** The oid of the current turn holder. */ + protected int _currentHolderId; +} diff --git a/src/java/com/threerings/ezgame/server/EZSeatedTurnDelegate.java b/src/java/com/threerings/ezgame/server/EZSeatedTurnDelegate.java new file mode 100644 index 00000000..395fe77c --- /dev/null +++ b/src/java/com/threerings/ezgame/server/EZSeatedTurnDelegate.java @@ -0,0 +1,84 @@ +// +// $Id: EZGameTurnDelegate.java 523 2007-12-07 21:41:22Z ray $ +// +// Vilya library - tools for developing networked games +// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/vilya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.ezgame.server; + +import com.samskivert.util.ListUtil; +import com.samskivert.util.RandomUtil; + +import com.threerings.util.Name; + +import com.threerings.crowd.data.BodyObject; + +import com.threerings.parlor.turn.server.TurnGameManagerDelegate; + +/** + * A special turn delegate for seated ez games. + */ +public class EZSeatedTurnDelegate extends TurnGameManagerDelegate + implements EZGameTurnDelegate +{ + // from EZGameTurnDelegate + public void endTurn (int nextPlayerId) + { + _nextPlayerId = nextPlayerId; + endTurn(); + } + + @Override + protected void setFirstTurnHolder () + { + // make it nobody's turn + _turnIdx = -1; + } + + @Override + protected void setNextTurnHolder () + { + // if the user-supplied value seems to make sense, use it! + if (_nextPlayerId != 0) { + // clear out _nextPlayerId. + int nextId = _nextPlayerId; + _nextPlayerId = 0; + + BodyObject nextPlayer = ((EZGameManager) _plmgr).getPlayerByOid(nextId); + if (nextPlayer != null) { + int index = ListUtil.indexOf(_turnGame.getPlayers(), nextPlayer.getVisibleName()); + if (index != -1) { + _turnIdx = index; + return; + } + } + } + + // Otherwise, if it's nobody's turn- randomly pick a turn holder + if (_turnIdx == -1) { + assignTurnRandomly(); + return; + } + + // otherwise, do the default behavior + super.setNextTurnHolder(); + } + + /** An override next turn holder, or 0. */ + protected int _nextPlayerId; +}