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
This commit is contained in:
Ray Greenwell
2008-01-05 01:05:52 +00:00
parent 5d693ec6bd
commit 8d55a105a9
4 changed files with 298 additions and 86 deletions
@@ -76,7 +76,7 @@ public class EZGameManager extends GameManager
{ {
public EZGameManager () public EZGameManager ()
{ {
addDelegate(_turnDelegate = new EZGameTurnDelegate()); // addDelegate(_turnDelegate = new EZGameTurnDelegate());
} }
/** /**
@@ -139,21 +139,21 @@ public class EZGameManager extends GameManager
{ {
validateUser(caller); validateUser(caller);
// make sure this player is the turn holder // // make sure this player is the turn holder
Name holder = _ezObj.turnHolder; // Name holder = _ezObj.turnHolder;
if (holder != null && !holder.equals(((BodyObject) caller).getVisibleName())) { // if (holder != null && !holder.equals(((BodyObject) caller).getVisibleName())) {
throw new InvocationException(InvocationCodes.ACCESS_DENIED); // throw new InvocationException(InvocationCodes.ACCESS_DENIED);
} // }
Name nextTurnHolder = null; // Name nextTurnHolder = null;
if (nextPlayerId != 0) { // if (nextPlayerId != 0) {
BodyObject target = getPlayerByOid(nextPlayerId); // BodyObject target = getPlayerByOid(nextPlayerId);
if (target != null) { // if (target != null) {
nextTurnHolder = target.getVisibleName(); // nextTurnHolder = target.getVisibleName();
} // }
} // }
_turnDelegate.endTurn(nextTurnHolder); _turnDelegate.endTurn(nextPlayerId);
} }
// from EZGameProvider // from EZGameProvider
@@ -556,6 +556,26 @@ public class EZGameManager extends GameManager
return new EZGameObject(); 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 @Override
protected void didStartup () protected void didStartup ()
{ {
@@ -1,81 +1,12 @@
// //
// $Id$ // $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; package com.threerings.ezgame.server;
import com.samskivert.util.ListUtil; public interface EZGameTurnDelegate
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
{ {
/** /**
* 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) public void endTurn (int nextTurnHolderId);
{
_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;
} }
@@ -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<Integer> 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<Integer> list = new ArrayList<Integer>(_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<Integer>(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<Integer> _ordering;
/** The oid of the current turn holder. */
protected int _currentHolderId;
}
@@ -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;
}