Behold Vilya, Ring of Air and repository for our game and virtual worldly

extensions to the distributed environment provided by Narya.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@1 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2006-06-23 17:58:11 +00:00
commit a4df87e52f
317 changed files with 45818 additions and 0 deletions
@@ -0,0 +1,217 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.client;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.HashMap;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Icon;
import com.samskivert.swing.GroupLayout;
import com.samskivert.swing.VGroupLayout;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.util.Name;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.turn.data.TurnGameObject;
import com.threerings.presents.dobj.AttributeChangeListener;
import com.threerings.presents.dobj.AttributeChangedEvent;
import com.threerings.presents.dobj.ElementUpdateListener;
import com.threerings.presents.dobj.ElementUpdatedEvent;
import com.threerings.presents.dobj.DObject;
/**
* Automatically display a list of players and turn change information
* in a turn-based game.
*/
// TODO
// - adapt this to be able to display scores in some generic way as well.
// - allow configuring of turn / winner labels from prototypes,
// rather than forcing one to be an icon, the other a string, and
// examine the prototype to determine how to highlight the turnholder.
public class TurnDisplay extends JPanel
implements PlaceView, AttributeChangeListener, ElementUpdateListener
{
/**
* Create a TurnDisplay.
*/
public TurnDisplay ()
{
}
/**
* Create a TurnDisplay for a game using the specified Icon to denote
* whose turn it is.
*/
public TurnDisplay (Icon turnIcon)
{
setTurnIcon(turnIcon);
}
/**
* Set the icon to use.
*/
public void setTurnIcon (Icon turnIcon)
{
_turnIcon = turnIcon;
if (_turnObj != null) {
createList();
}
}
/**
* Set the text to be displayed next to the winner's name.
*/
public void setWinnerText (String winnerText)
{
_winnerText = winnerText;
}
/**
* Set optional icons to use for identifying each player in the game.
*/
public void setPlayerIcons (Icon[] icons)
{
_playerIcons = icons;
if (_turnObj != null) {
createList();
}
}
/**
* Create the list of names and highlight as appropriate.
*/
protected void createList ()
{
removeAll();
_labels.clear();
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints iconC = new GridBagConstraints();
GridBagConstraints labelC = new GridBagConstraints();
iconC.fill = labelC.fill = GridBagConstraints.BOTH;
labelC.weightx = 1.0;
labelC.insets.left = 10;
labelC.gridwidth = GridBagConstraints.REMAINDER;
Name[] names = _turnObj.getPlayers();
boolean[] winners = ((GameObject) _turnObj).winners;
Name holder = _turnObj.getTurnHolder();
for (int ii=0, jj=0; ii < names.length; ii++, jj++) {
if (names[ii] == null) continue;
JLabel iconLabel = new JLabel();
if (winners == null) {
if (names[ii].equals(holder)) {
iconLabel.setIcon(_turnIcon);
}
} else if (winners[ii]) {
iconLabel.setText(_winnerText);
iconLabel.setForeground(Color.GREEN);
}
_labels.put(names[ii], iconLabel);
add(iconLabel, iconC);
JLabel label = new JLabel(names[ii].toString());
if (_playerIcons != null) {
label.setIcon(_playerIcons[jj]);
}
add(label, labelC);
}
SwingUtil.refresh(this);
}
// documentation inherited from interface PlaceView
public void willEnterPlace (PlaceObject plobj)
{
_turnObj = (TurnGameObject) plobj;
plobj.addListener(this);
createList();
}
// documentation inherited from interface PlaceView
public void didLeavePlace (PlaceObject plobj)
{
plobj.removeListener(this);
_turnObj = null;
removeAll();
}
// documentation inherited from interface AttributeChangeListener
public void attributeChanged (AttributeChangedEvent event)
{
String name = event.getName();
if (name.equals(_turnObj.getTurnHolderFieldName())) {
JLabel oldLabel = (JLabel) _labels.get((Name) event.getOldValue());
if (oldLabel != null) {
oldLabel.setIcon(null);
}
JLabel newLabel = (JLabel) _labels.get((Name) event.getValue());
if (newLabel != null) {
newLabel.setIcon(_turnIcon);
}
} else if (name.equals(GameObject.PLAYERS)) {
createList();
} else if (name.equals(GameObject.WINNERS)) {
createList();
}
}
// documentation inherited from interface ElementUpdateListener
public void elementUpdated (ElementUpdatedEvent event)
{
String name = event.getName();
if (name.equals(GameObject.PLAYERS)) {
createList();
}
}
/** The TurnGameObject we're displaying. */
protected TurnGameObject _turnObj;
/** A mapping of the labels currently associated with each player. */
protected HashMap _labels = new HashMap();
/** The game-specified player icons. */
protected Icon[] _playerIcons;
/** The text to display next to a winner's name. */
protected String _winnerText;
/** The Icon we use for indicating the turn. */
protected Icon _turnIcon;
}
@@ -0,0 +1,44 @@
//
// $Id: TurnGameController.java 3381 2005-03-03 19:36:34Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.client;
import com.threerings.util.Name;
import com.threerings.parlor.game.client.GameController;
/**
* Games that wish to make use of the turn game services should have their
* controller implement this interface and create an instance of {@link
* TurnGameControllerDelegate} which should be passed to {@link
* GameController#addDelegate}.
*/
public interface TurnGameController
{
/**
* Called when the turn changed. This indicates the start of a turn
* and the user interface should adjust itself accordingly (activating
* controls if it is our turn and deactivating them if it is not).
*
* @param turnHolder the username of the new holder of the turn.
*/
public void turnDidChange (Name turnHolder);
}
@@ -0,0 +1,149 @@
//
// $Id: TurnGameControllerDelegate.java 3758 2005-11-10 23:18:58Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.client;
import com.threerings.util.Name;
import com.threerings.presents.dobj.AttributeChangedEvent;
import com.threerings.presents.dobj.AttributeChangeListener;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.parlor.game.client.GameController;
import com.threerings.parlor.game.client.GameControllerDelegate;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.turn.data.TurnGameObject;
/**
* Performs the client-side processing for a turn-based game. Games which
* wish to make use of these services must construct a delegate and call
* out to it at the appropriate times (see the method documentation for
* which methods should be called when). The game's controller must also
* implement the {@link TurnGameController} interface so that it can be
* notified when turn-based game events take place.
*/
public class TurnGameControllerDelegate extends GameControllerDelegate
implements AttributeChangeListener
{
/**
* Constructs a delegate which will call back to the supplied {@link
* TurnGameController} implementation wen turn-based game related
* things happen.
*/
public TurnGameControllerDelegate (TurnGameController tgctrl)
{
super((GameController)tgctrl);
// keep this around for later
_tgctrl = tgctrl;
}
/**
* Returns true if the game is in progress and it is our turn; false
* otherwise.
*/
public boolean isOurTurn ()
{
BodyObject self = (BodyObject)_ctx.getClient().getClientObject();
return (_gameObj.state == GameObject.IN_PLAY &&
self.getVisibleName().equals(_turnGame.getTurnHolder()));
}
/**
* Returns the index of the current turn holder as configured in the
* game object.
*
* @return the index into the players array of the current turn holder
* or -1 if there is no current turn holder.
*/
public int getTurnHolderIndex ()
{
return _gameObj.getPlayerIndex(_turnGame.getTurnHolder());
}
// documentation inherited
public void init (CrowdContext ctx, PlaceConfig config)
{
_ctx = ctx;
}
// documentation inherited
public void willEnterPlace (PlaceObject plobj)
{
// get a casted reference to the object
_gameObj = (GameObject)plobj;
_turnGame = (TurnGameObject)plobj;
_thfield = _turnGame.getTurnHolderFieldName();
// and add ourselves as a listener
plobj.addListener(this);
}
// documentation inherited
public void didLeavePlace (PlaceObject plobj)
{
// remove our listenership
plobj.removeListener(this);
// clean up
_turnGame = null;
}
// documentation inherited
public void attributeChanged (AttributeChangedEvent event)
{
// handle turn changes
if (event.getName().equals(_thfield)) {
Name name = (Name)event.getValue();
Name oname = (Name)event.getOldValue();
if (TurnGameObject.TURN_HOLDER_REPLACED.equals(name) ||
TurnGameObject.TURN_HOLDER_REPLACED.equals(oname)) {
// small hackery: ignore the turn holder being set to
// TURN_HOLDER_REPLACED as it means that we're replacing
// the current turn holder rather than switching turns;
// also ignore the new turn holder when we switch from THR
// to a real name again
} else {
_tgctrl.turnDidChange(name);
}
}
}
/** The turn game controller for whom we are delegating. */
protected TurnGameController _tgctrl;
/** A reference to our client context. */
protected CrowdContext _ctx;
/** A reference to our game object. */
protected GameObject _gameObj;
/** A casted reference to our game object as a turn game. */
protected TurnGameObject _turnGame;
/** The name of the turn holder field. */
protected String _thfield;
}
@@ -0,0 +1,64 @@
//
// $Id: TurnGameObject.java 3667 2005-08-03 07:46:54Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.data;
import com.threerings.util.Name;
import com.threerings.parlor.game.data.GameObject;
/**
* Games that wish to support turn-based play must implement this
* interface with their {@link GameObject}.
*/
public interface TurnGameObject
{
/** A special value used to communicate to the client that the current
* turn holder was replaced (perhaps due to disconnection or departure
* and being replaced by an AI). */
public static final Name TURN_HOLDER_REPLACED =
new Name("__TURN_HOLDER_REPLACED__");
/**
* Returns the distributed object field name of the
* <code>turnHolder</code> field in the object that implements this
* interface.
*/
public String getTurnHolderFieldName ();
/**
* Returns the username of the player who is currently taking their
* turn in this turn-based game or <code>null</code> if no user
* currently holds the turn.
*/
public Name getTurnHolder ();
/**
* Requests that the <code>turnHolder</code> field be set to the specified
* value.
*/
public void setTurnHolder (Name turnHolder);
/**
* Returns the array of player names involved in the game.
*/
public Name[] getPlayers ();
}
@@ -0,0 +1,92 @@
//
// $Id: TurnGameManager.java 3390 2005-03-10 00:13:25Z tedv $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.server;
import com.threerings.util.Name;
import com.threerings.parlor.game.server.GameManager;
/**
* A game manager that wishes to make use of the turn game services should
* implement this interface and create a {@link TurnGameManagerDelegate}
* which will perform the basic turn game processing and call back to the
* main manager via this interface.
*
* <p> The basic flow of a turn-based game is as follows:
* <pre>
* GameManager.gameWillStart()
* GameManager.gameDidStart()
* TurnGameManagerDelegate.setFirstTurnHolder()
* TurnGameManagerDelegate.startTurn()
* TurnGameManager.turnWillStart()
* TurnGameManagerDelegate.endTurn()
* TurnGameManager.turnDidEnd()
* TurnGameManagerDelegate.setNextTurnHolder()
* TurnGameManagerDelegate.startTurn()
* ...
* GameManager.endGame()
* </pre>
*/
public interface TurnGameManager
{
/**
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public Name getPlayerName (int index);
/**
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public int getPlayerIndex (Name username);
/**
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public int getPlayerCount ();
/**
* Extending {@link GameManager} should automatically handle
* implementing this method.
*/
public boolean isActivePlayer (int pidx);
/**
* Called when we are about to start the next turn. Implementations
* can do whatever pre-start turn activities need to be done.
*/
public void turnWillStart ();
/**
* Called when we have started the next turn. Implementations can do
* whatever post-start turn activities need to be done.
*/
public void turnDidStart ();
/**
* Called when the turn was ended. Implementations can perform any
* post-turn processing (like updating scores, etc.).
*/
public void turnDidEnd ();
}
@@ -0,0 +1,246 @@
//
// $Id: TurnGameManagerDelegate.java 4188 2006-06-13 18:03:48Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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.parlor.turn.server;
import com.samskivert.util.RandomUtil;
import com.threerings.util.Name;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.parlor.Log;
import com.threerings.parlor.game.server.GameManager;
import com.threerings.parlor.game.server.GameManagerDelegate;
import com.threerings.parlor.turn.data.TurnGameObject;
/**
* Performs the server-side turn-based game processing for a turn based
* game. Game managers which wish to make use of the turn services must
* implement {@link TurnGameManager} and either create an instance of this
* class, or an instance of a derivation which customizes the behavior,
* either of which would be passed to {@link GameManager#addDelegate} to
* be activated.
*/
public class TurnGameManagerDelegate extends GameManagerDelegate
{
/**
* Constructs a delegate that will manage the turn game state and call
* back to the supplied {@link TurnGameManager} implementation to let
* it in on the progression of the game.
*/
public TurnGameManagerDelegate (TurnGameManager tgmgr)
{
super((GameManager)tgmgr);
_tgmgr = tgmgr;
}
/**
* Returns the index of the current turn holder as configured in the
* game object.
*
* @return the index into the players array of the current turn holder
* or <code>-1</code> if there is no current turn holder.
*/
public int getTurnHolderIndex ()
{
return _tgmgr.getPlayerIndex(_turnGame.getTurnHolder());
}
/** Test if it's the inputted player's turn. */
public boolean isPlayersTurn (int playerIndex)
{
// Don't accidently match a visitor's id of -1 with the "no one's
// turn" state of turn -1.
int turnHolder = getTurnHolderIndex();
if (turnHolder < 0) {
return false;
}
// It's this player's turn if the ids match
return (turnHolder == playerIndex);
}
/**
* Called to start the next turn. It calls {@link
* TurnGameManager#turnWillStart} to allow our owning manager to
* perform any pre-start turn processing, sets the turn holder that
* was configured either when the game started or when finishing up
* the last turn, and then calls {@link TurnGameManager#turnDidStart}
* to allow the manager to perform any post-start turn
* processing. This assumes that a valid turn holder has been
* assigned. If some pre-game preparation needs to take place in a
* non-turn-based manner, this function should not be called until it
* is time to start the first turn.
*/
public void startTurn ()
{
// sanity check
if (_turnIdx < 0 || _turnIdx >= _turnGame.getPlayers().length) {
Log.warning("startTurn() called with invalid turn index " +
"[game=" + where() + ", turnIdx=" + _turnIdx + "].");
// abort, abort
return;
}
// get the player name and sanity-check again
Name name = _tgmgr.getPlayerName(_turnIdx);
if (name == null) {
Log.warning("startTurn() called with invalid player " +
"[game=" + where() + ", turnIdx=" + _turnIdx + "].");
return;
}
// do pre-start processing
_tgmgr.turnWillStart();
// and set the turn indicator accordingly
_turnGame.setTurnHolder(name);
// do post-start processing
_tgmgr.turnDidStart();
}
/**
* Called to end the turn. Whatever indication a game manager has that
* the turn has ended (probably the submission of a valid move of some
* sort by the turn holding player), it should call this function to
* cause this turn to end and the next to begin.
*
* <p> If the next turn should not be started immediately after this
* turn, the game manager should arrange for {@link
* #setNextTurnHolder} to set the {@link #_turnIdx} field to
* <code>-1</code> which will cause us not to start the next turn. It
* can then call {@link GameManager#endGame} if the game is over or do
* whatever else it needs to do outside the context of the turn flow.
* To start things back up again it would set {@link #_turnIdx} to the
* next turn holder and call {@link #startTurn} itself.
*/
public void endTurn ()
{
// let the manager know that the turn is over
_tgmgr.turnDidEnd();
// figure out who's up next
setNextTurnHolder();
// and start the next turn if desired
if (_turnIdx != -1) {
startTurn();
} else {
// otherwise, clear out the turn holder
_turnGame.setTurnHolder(null);
}
}
// documentation inherited
public void didStartup (PlaceObject plobj)
{
_turnGame = (TurnGameObject)plobj;
}
// documentation inherited
public void playerWasReplaced (int pidx, Name oplayer, Name nplayer)
{
// we need to update the turn holder if the current turn holder
// was the player that was replaced and we need to do so in a way
// that doesn't make everyone think that the turn just changed
if (oplayer != null && oplayer.equals(_turnGame.getTurnHolder())) {
// small hackery: this will indicate to the client that we are
// replacing the turn holder rather than changing the turn
_turnGame.setTurnHolder(TurnGameObject.TURN_HOLDER_REPLACED);
_turnGame.setTurnHolder(nplayer);
}
}
/**
* This should be called from {@link GameManager#gameDidStart} to let
* the turn delegate perform start of game processing.
*/
public void gameDidStart ()
{
// figure out who will be first
setFirstTurnHolder();
// and start the first turn if we should apparently do so
if (_turnIdx != -1) {
startTurn();
}
}
/**
* This is called to determine which player will take the first
* turn. The default implementation chooses a player at random.
*/
protected void setFirstTurnHolder ()
{
int size = _turnGame.getPlayers().length;
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;
}
}
}
/**
* This is called to determine which player will next hold the turn.
* The default implementation simply rotates through the players in
* order, but some games may need to mess with the turn from time to
* time. This should update the <code>_turnIdx</code> field, not set
* the turn holder field in the game object directly.
*/
protected void setNextTurnHolder ()
{
// stick with the current player if they're the only participant
if (_tgmgr.getPlayerCount() == 1) {
return;
}
// find the next occupied active player slot
int size = _turnGame.getPlayers().length;
int oturnIdx = _turnIdx;
do {
_turnIdx = (_turnIdx + 1) % size;
if (_turnIdx == oturnIdx) {
// if we've wrapped all the way around, stop where we are
// even if the current player is not active.
Log.warning("1 or less active players. Unable to properly " +
"change turn. [game=" + where() + "].");
break;
}
} while (!_tgmgr.isActivePlayer(_turnIdx));
}
/** The game manager for which we are delegating. */
protected TurnGameManager _tgmgr;
/** A reference to our game object. */
protected TurnGameObject _turnGame;
/** The player index of the current turn holder or <code>-1</code> if
* it's no one's turn. */
protected int _turnIdx = -1;
}