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,116 @@
//
// $Id: ParlorDispatcher.java 4145 2006-05-24 01:24:24Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2006 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.server;
import com.threerings.parlor.client.ParlorService;
import com.threerings.parlor.data.ParlorMarshaller;
import com.threerings.parlor.data.TableConfig;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.server.InvocationDispatcher;
import com.threerings.presents.server.InvocationException;
import com.threerings.util.Name;
/**
* Dispatches requests to the {@link ParlorProvider}.
*/
public class ParlorDispatcher extends InvocationDispatcher
{
/**
* Creates a dispatcher that may be registered to dispatch invocation
* service requests for the specified provider.
*/
public ParlorDispatcher (ParlorProvider provider)
{
this.provider = provider;
}
// documentation inherited
public InvocationMarshaller createMarshaller ()
{
return new ParlorMarshaller();
}
// documentation inherited
public void dispatchRequest (
ClientObject source, int methodId, Object[] args)
throws InvocationException
{
switch (methodId) {
case ParlorMarshaller.CANCEL:
((ParlorProvider)provider).cancel(
source,
((Integer)args[0]).intValue(), (InvocationService.InvocationListener)args[1]
);
return;
case ParlorMarshaller.CREATE_TABLE:
((ParlorProvider)provider).createTable(
source,
((Integer)args[0]).intValue(), (TableConfig)args[1], (GameConfig)args[2], (ParlorService.TableListener)args[3]
);
return;
case ParlorMarshaller.INVITE:
((ParlorProvider)provider).invite(
source,
(Name)args[0], (GameConfig)args[1], (ParlorService.InviteListener)args[2]
);
return;
case ParlorMarshaller.JOIN_TABLE:
((ParlorProvider)provider).joinTable(
source,
((Integer)args[0]).intValue(), ((Integer)args[1]).intValue(), ((Integer)args[2]).intValue(), (InvocationService.InvocationListener)args[3]
);
return;
case ParlorMarshaller.LEAVE_TABLE:
((ParlorProvider)provider).leaveTable(
source,
((Integer)args[0]).intValue(), ((Integer)args[1]).intValue(), (InvocationService.InvocationListener)args[2]
);
return;
case ParlorMarshaller.RESPOND:
((ParlorProvider)provider).respond(
source,
((Integer)args[0]).intValue(), ((Integer)args[1]).intValue(), (Object)args[2], (InvocationService.InvocationListener)args[3]
);
return;
case ParlorMarshaller.START_SOLITAIRE:
((ParlorProvider)provider).startSolitaire(
source,
(GameConfig)args[0], (InvocationService.ConfirmListener)args[1]
);
return;
default:
super.dispatchRequest(source, methodId, args);
return;
}
}
}
@@ -0,0 +1,274 @@
//
// $Id: ParlorManager.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.server;
import com.samskivert.util.HashIntMap;
import com.threerings.util.Name;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationManager;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.server.PlaceRegistry;
import com.threerings.parlor.Log;
import com.threerings.parlor.data.ParlorCodes;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.parlor.game.server.GameManager;
/**
* The parlor manager is responsible for the parlor services in
* aggregate. This includes maintaining the registry of active games,
* handling the necessary coordination for the matchmaking services and
* anything else that falls outside the scope of an actual in-progress
* game.
*/
public class ParlorManager
implements ParlorCodes
{
/** Provides the server-side implementation of the parlor services. */
public ParlorProvider parprov;
/**
* Initializes the parlor manager. This should be called by the server
* that is making use of the parlor services on the single instance of
* parlor manager that it has created.
*
* @param invmgr a reference to the invocation manager in use by this
* server.
* @param plreg a reference to the place registry to be used by the
* parlor manager when creating game places.
*/
public void init (InvocationManager invmgr, PlaceRegistry plreg)
{
// create and register our invocation provider
parprov = new ParlorProvider(this);
invmgr.registerDispatcher(new ParlorDispatcher(parprov), true);
// keep this for later
_plreg = plreg;
}
/**
* Issues an invitation from the <code>inviter</code> to the
* <code>invitee</code> for a game as described by the supplied config
* object.
*
* @param inviter the player initiating the invitation.
* @param invitee the player being invited.
* @param config the configuration of the game being proposed.
*
* @return the invitation identifier for the newly created invitation
* record.
*
* @exception InvocationException thrown if the invitation was not
* able to be processed for some reason (like the invited player has
* requested not to be disturbed). The explanation will be provided in
* the message data of the exception.
*/
public int invite (BodyObject inviter, BodyObject invitee,
GameConfig config)
throws InvocationException
{
// Log.info("Received invitation request [inviter=" + inviter +
// ", invitee=" + invitee + ", config=" + config + "].");
// here we should check to make sure the invitee hasn't muted the
// inviter, and that the inviter isn't shunned and all that other
// access control type stuff
// create a new invitation record for this invitation
Invitation invite = new Invitation(inviter, invitee, config);
// stick it in the pending invites table
_invites.put(invite.inviteId, invite);
// deliver an invite notification to the invitee
ParlorSender.sendInvite(
invitee, invite.inviteId, inviter.getVisibleName(), config);
// and let the caller know the invite id we assigned
return invite.inviteId;
}
/**
* Effects a response to an invitation (accept, refuse or counter),
* made by the specified source user with the specified arguments.
*
* @param source the body object of the user that is issuing this
* response.
* @param inviteId the identifier of the invitation to which we are
* responding.
* @param code the response code (either {@link
* #INVITATION_ACCEPTED}, {@link #INVITATION_REFUSED} or {@link
* #INVITATION_COUNTERED}).
* @param arg the argument that goes along with the response: an
* explanatory message in the case of a refusal (the empty string, not
* null, if no message was provided) or the new game configuration in
* the case of a counter-invitation.
*/
public void respondToInvite (BodyObject source, int inviteId, int code,
Object arg)
{
// look up the invitation
Invitation invite = (Invitation)_invites.get(inviteId);
if (invite == null) {
Log.warning("Requested to respond to non-existent invitation " +
"[source=" + source + ", inviteId=" + inviteId +
", code=" + code + ", arg=" + arg + "].");
return;
}
// make sure this response came from the proper person
if (source != invite.invitee) {
Log.warning("Got response from non-invitee [source=" + source +
", invite=" + invite + ", code=" + code +
", arg=" + arg + "].");
return;
}
// let the other user know that a response was made to this
// invitation
ParlorSender.sendInviteResponse(
invite.inviter, invite.inviteId, code, arg);
switch (code) {
case INVITATION_ACCEPTED:
// the invitation was accepted, so we'll need to start up the
// game and get the necessary balls rolling
processAcceptedInvitation(invite);
// and remove the invitation from the pending table
_invites.remove(inviteId);
break;
case INVITATION_REFUSED:
// remove the invitation record from the pending table as it
// is no longer pending
_invites.remove(inviteId);
break;
case INVITATION_COUNTERED:
// swap control of the invitation to the invitee
invite.swapControl();
break;
default:
Log.warning("Requested to respond to invitation with " +
"unknown response code [source=" + source +
", invite=" + invite + ", code=" + code +
", arg=" + arg + "].");
break;
}
}
/**
* Requests that an outstanding invitation be cancelled.
*
* @param source the body object of the user that is making the
* request.
* @param inviteId the unique id of the invitation to be cancelled.
*/
public void cancelInvite (BodyObject source, int inviteId)
{
// TBD
}
/**
* Starts up and configures the game manager for an accepted
* invitation.
*/
protected void processAcceptedInvitation (Invitation invite)
{
try {
Log.info("Creating game manager [invite=" + invite + "].");
// configure the game config with the player info
invite.config.players = new Name[] {
invite.invitee.username, invite.inviter.username };
// create the game manager and begin it's initialization
// process. the game manager will take care of notifying the
// players that the game has been created once it has been
// started up (which is done by the place registry once the
// game object creation has completed)
GameManager gmgr = (GameManager)
_plreg.createPlace(invite.config, null);
} catch (Exception e) {
Log.warning("Unable to create game manager [invite=" + invite +
", error=" + e + "].");
Log.logStackTrace(e);
}
}
/**
* The invitation record is used by the parlor manager to keep track
* of pending invitations.
*/
protected static class Invitation
{
/** The unique identifier for this invitation. */
public int inviteId = _nextInviteId++;
/** The person proposing the invitation. */
public BodyObject inviter;
/** The person to whom the invitation is proposed. */
public BodyObject invitee;
/** The configuration of the game being proposed. */
public GameConfig config;
/**
* Constructs a new invitation with the specified participants and
* configuration.
*/
public Invitation (BodyObject inviter, BodyObject invitee,
GameConfig config)
{
this.inviter = inviter;
this.invitee = invitee;
this.config = config;
}
/**
* Swaps the inviter and invitee which is necessary when the
* invitee responds with a counter-invitation.
*/
public void swapControl ()
{
BodyObject tmp = inviter;
inviter = invitee;
invitee = tmp;
}
}
/** The place registry with which we operate. */
protected PlaceRegistry _plreg;
/** The table of pending invitations. */
protected HashIntMap _invites = new HashIntMap();
/** A counter used to generate unique identifiers for invitation
* records. */
protected static int _nextInviteId = 0;
}
@@ -0,0 +1,233 @@
//
// $Id: ParlorProvider.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.server;
import com.threerings.util.Name;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.client.InvocationService.InvocationListener;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationException;
import com.threerings.presents.server.InvocationProvider;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.PlaceManager;
import com.threerings.parlor.Log;
import com.threerings.parlor.client.ParlorService;
import com.threerings.parlor.data.ParlorCodes;
import com.threerings.parlor.data.TableConfig;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.parlor.game.server.GameManager;
/**
* The parlor provider handles the server side of the various Parlor
* services that are made available for direct invocation by the client.
* Primarily these are the matchmaking mechanisms.
*/
public class ParlorProvider
implements InvocationProvider, ParlorCodes
{
/**
* Constructs a parlor provider instance which will be used to handle
* all parlor-related invocation service requests. This is
* automatically taken care of by the parlor manager, so no other
* entity need instantiate and register a parlor provider.
*
* @param pmgr a reference to the parlor manager active in this
* server.
*/
public ParlorProvider (ParlorManager pmgr)
{
_pmgr = pmgr;
}
/**
* Processes a request from the client to invite another user to play
* a game.
*/
public void invite (ClientObject caller, Name invitee, GameConfig config,
ParlorService.InviteListener listener)
throws InvocationException
{
// Log.info("Handling invite request [source=" + source +
// ", invitee=" + invitee + ", config=" + config + "].");
BodyObject source = (BodyObject)caller;
String rsp = null;
// ensure that the invitee is online at present
BodyObject target = CrowdServer.lookupBody(invitee);
if (target == null) {
throw new InvocationException(INVITEE_NOT_ONLINE);
}
// submit the invite request to the parlor manager
int inviteId = _pmgr.invite(source, target, config);
listener.inviteReceived(inviteId);
}
/**
* Processes a request from the client to respond to an outstanding
* invitation by accepting, refusing, or countering it.
*/
public void respond (ClientObject caller, int inviteId, int code,
Object arg, InvocationListener listener)
{
// pass this on to the parlor manager
_pmgr.respondToInvite((BodyObject)caller, inviteId, code, arg);
}
/**
* Processes a request from the client to cancel an outstanding
* invitation.
*/
public void cancel (ClientObject caller, int inviteId,
InvocationListener listener)
{
// pass this on to the parlor manager
_pmgr.cancelInvite((BodyObject)caller, inviteId);
}
/**
* Processes a request from the client to create a new table.
*/
public void createTable (
ClientObject caller, int lobbyOid, TableConfig tableConfig,
GameConfig config, ParlorService.TableListener listener)
throws InvocationException
{
Log.info("Handling create table request [caller=" + caller.who() +
", lobbyOid=" + lobbyOid + ", config=" + config + "].");
// pass the creation request on to the table manager
TableManager tmgr = getTableManager(lobbyOid);
int tableId = tmgr.createTable((BodyObject)caller, tableConfig, config);
listener.tableCreated(tableId);
}
/**
* Processes a request from the client to join an existing table.
*/
public void joinTable (ClientObject caller, int lobbyOid, int tableId,
int position, InvocationListener listener)
throws InvocationException
{
Log.info("Handling join table request [caller=" + caller.who() +
", lobbyOid=" + lobbyOid + ", tableId=" + tableId +
", position=" + position + "].");
// pass the join request on to the table manager
TableManager tmgr = getTableManager(lobbyOid);
tmgr.joinTable((BodyObject)caller, tableId, position);
// there is normally no success response. the client will see
// themselves show up in the table that they joined
}
/**
* Processes a request from the client to leave an existing table.
*/
public void leaveTable (ClientObject caller, int lobbyOid, int tableId,
InvocationListener listener)
throws InvocationException
{
Log.info("Handling leave table request [caller=" + caller.who() +
", lobbyOid=" + lobbyOid + ", tableId=" + tableId + "].");
// pass the join request on to the table manager
TableManager tmgr = getTableManager(lobbyOid);
tmgr.leaveTable((BodyObject)caller, tableId);
// there is normally no success response. the client will see
// themselves removed from the table they just left
}
/**
* Handles a {@link ParlorService#startSolitaire} request.
*/
public void startSolitaire (ClientObject caller, GameConfig config,
InvocationService.ConfirmListener listener)
throws InvocationException
{
BodyObject user = (BodyObject)caller;
Log.debug("Processing start puzzle [caller=" + user.who() +
", config=" + config + "].");
try {
// just this fellow will be playing
if (config.players == null || config.players.length == 0) {
config.players = new Name[] { user.getVisibleName() };
}
// create the game manager and begin its initialization
// process
GameManager gmgr = (GameManager)
CrowdServer.plreg.createPlace(config, null);
// the game manager will take care of notifying the player
// that the game has been created once it has been started up;
// but we let the caller know that we processed their request
listener.requestProcessed();
} catch (InstantiationException ie) {
Log.warning("Error instantiating game manager " +
"[for=" + caller.who() + ", config=" + config + "].");
Log.logStackTrace(ie);
throw new InvocationException(INTERNAL_ERROR);
}
}
/**
* Looks up the place manager associated with the supplied lobby oid,
* casts it to a table lobby manager and obtains the associated table
* manager reference.
*
* @exception InvocationException thrown if something goes wrong
* along the way like no place manager exists or the place manager
* that does exist doesn't implement table lobby manager.
*/
protected TableManager getTableManager (int lobbyOid)
throws InvocationException
{
PlaceManager plmgr = CrowdServer.plreg.getPlaceManager(lobbyOid);
if (plmgr == null) {
Log.warning("No place manager exists from which to obtain " +
"table manager reference [ploid=" + lobbyOid + "].");
throw new InvocationException(INTERNAL_ERROR);
}
// sanity check
if (!(plmgr instanceof TableManagerProvider)) {
Log.warning("Place manager not a table lobby manager " +
"[plmgr=" + plmgr + "].");
throw new InvocationException(INTERNAL_ERROR);
}
return ((TableManagerProvider)plmgr).getTableManager();
}
/** A reference to the parlor manager we're working with. */
protected ParlorManager _pmgr;
}
@@ -0,0 +1,85 @@
//
// $Id: ParlorSender.java 4145 2006-05-24 01:24:24Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2006 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.server;
import com.threerings.parlor.client.ParlorDecoder;
import com.threerings.parlor.client.ParlorReceiver;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationSender;
import com.threerings.util.Name;
/**
* Used to issue notifications to a {@link ParlorReceiver} instance on a
* client.
*/
public class ParlorSender extends InvocationSender
{
/**
* Issues a notification that will result in a call to {@link
* ParlorReceiver#gameIsReady} on a client.
*/
public static void gameIsReady (
ClientObject target, int arg1)
{
sendNotification(
target, ParlorDecoder.RECEIVER_CODE, ParlorDecoder.GAME_IS_READY,
new Object[] { Integer.valueOf(arg1) });
}
/**
* Issues a notification that will result in a call to {@link
* ParlorReceiver#receivedInvite} on a client.
*/
public static void sendInvite (
ClientObject target, int arg1, Name arg2, GameConfig arg3)
{
sendNotification(
target, ParlorDecoder.RECEIVER_CODE, ParlorDecoder.RECEIVED_INVITE,
new Object[] { Integer.valueOf(arg1), arg2, arg3 });
}
/**
* Issues a notification that will result in a call to {@link
* ParlorReceiver#receivedInviteCancellation} on a client.
*/
public static void sendInviteCancellation (
ClientObject target, int arg1)
{
sendNotification(
target, ParlorDecoder.RECEIVER_CODE, ParlorDecoder.RECEIVED_INVITE_CANCELLATION,
new Object[] { Integer.valueOf(arg1) });
}
/**
* Issues a notification that will result in a call to {@link
* ParlorReceiver#receivedInviteResponse} on a client.
*/
public static void sendInviteResponse (
ClientObject target, int arg1, int arg2, Object arg3)
{
sendNotification(
target, ParlorDecoder.RECEIVER_CODE, ParlorDecoder.RECEIVED_INVITE_RESPONSE,
new Object[] { Integer.valueOf(arg1), Integer.valueOf(arg2), arg3 });
}
}
@@ -0,0 +1,379 @@
//
// $Id: TableManager.java 3804 2006-01-13 01:52:36Z ray $
//
// 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.server;
import java.util.HashMap;
import java.util.Iterator;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.StringUtil;
import com.threerings.util.Name;
import com.threerings.presents.dobj.ChangeListener;
import com.threerings.presents.dobj.ObjectAddedEvent;
import com.threerings.presents.dobj.ObjectDeathListener;
import com.threerings.presents.dobj.ObjectDestroyedEvent;
import com.threerings.presents.dobj.ObjectRemovedEvent;
import com.threerings.presents.dobj.OidListListener;
import com.threerings.presents.server.InvocationException;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.crowd.server.PlaceManager;
import com.threerings.crowd.server.PlaceRegistry;
import com.threerings.parlor.Log;
import com.threerings.parlor.data.ParlorCodes;
import com.threerings.parlor.data.Table;
import com.threerings.parlor.data.TableConfig;
import com.threerings.parlor.data.TableLobbyObject;
import com.threerings.parlor.game.data.GameConfig;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.game.server.GameManager;
/**
* A table manager can be used by a place manager to take care of the
* management of a table matchmaking service in the place managed by the
* place manager. The place manager need instantiate a table manager and
* implement the {@link TableManagerProvider} interface to provide access
* to the table manager for the associated invocation services.
*/
public class TableManager
implements ParlorCodes, OidListListener
{
/**
* Creates a table manager that will work in tandem with the specified
* place manager to manage a table matchmaking service in this place.
*/
public TableManager (PlaceManager plmgr)
{
// get a reference to our place object
_plobj = plmgr.getPlaceObject();
// add ourselves as an oidlist listener to this lobby so that we
// can tell if a user leaves the lobby without leaving their table
_plobj.addListener(this);
// make sure it implements table lobby object
_tlobj = (TableLobbyObject)_plobj;
}
/**
* Requests that a new table be created to matchmake the game
* described by the supplied game config instance. The config instance
* provided must implement the {@link TableConfig} interface so that
* the parlor services can determine how to configure the table that
* will be created.
*
* @param creator the body object that will own the new table.
* @param tableConfig the configuration parameters for the table.
* @param config the configuration of the game to be created.
*
* @return the id of the newly created table.
*
* @exception InvocationException thrown if the table creation was
* not able processed for some reason. The explanation will be
* provided in the message data of the exception.
*/
public int createTable (BodyObject creator, TableConfig tableConfig,
GameConfig config)
throws InvocationException
{
// make sure the creator is an occupant of the lobby in which
// they are requesting to create a table
if (!_plobj.occupants.contains(creator.getOid())) {
Log.warning("Requested to create a table in a lobby not " +
"occupied by the creator [creator=" + creator +
", loboid=" + _plobj.getOid() + "].");
throw new InvocationException(INTERNAL_ERROR);
}
// create a brand spanking new table
Table table = new Table(_plobj.getOid(), tableConfig, config);
// stick the creator into the first non-AI position
int cpos = (config.ais == null) ? 0 : config.ais.length;
String error = table.setOccupant(cpos, creator);
if (error != null) {
Log.warning("Unable to add creator to position zero of " +
"table!? [table=" + table + ", creator=" + creator +
", error=" + error + "].");
// bail out now and abort the table creation process
throw new InvocationException(error);
}
// stick the table into the table lobby object
_tlobj.addToTables(table);
// make a mapping from the creator to this table
_boidMap.put(creator.getOid(), table);
// also stick it into our tables tables
_tables.put(table.getTableId(), table);
// if the table has only one seat, start the game immediately
if (table.shouldBeStarted()) {
createGame(table);
}
// finally let the caller know what the new table id is
return table.getTableId();
}
/**
* Requests that the specified user be added to the specified table at
* the specified position. If the user successfully joins the table,
* the function will return normally. If they are not able to join for
* some reason (the slot is already full, etc.), a {@link
* InvocationException} will be thrown with a message code that
* describes the reason for failure. If the user does successfully
* join, they will be added to the table entry in the tables set in
* the place object that is hosting the table.
*
* @param joiner the body object of the user that wishes to join the
* table.
* @param tableId the id of the table to be joined.
* @param position the position at which to join the table.
*
* @exception InvocationException thrown if the joining was not able
* processed for some reason. The explanation will be provided in the
* message data of the exception.
*/
public void joinTable (BodyObject joiner, int tableId, int position)
throws InvocationException
{
// look the table up
Table table = (Table)_tables.get(tableId);
if (table == null) {
throw new InvocationException(NO_SUCH_TABLE);
}
// request that the user be added to the table at that position
String error = table.setOccupant(position, joiner);
// if that failed, report the error
if (error != null) {
throw new InvocationException(error);
}
// if the table is sufficiently full, start the game automatically
if (table.shouldBeStarted()) {
createGame(table);
} else {
// make a mapping from this occupant to this table
_boidMap.put(joiner.getOid(), table);
}
// update the table in the lobby
_tlobj.updateTables(table);
}
/**
* Requests that the specified user be removed from the specified
* table. If the user successfully leaves the table, the function will
* return normally. If they are not able to leave for some reason
* (they aren't sitting at the table, etc.), a {@link
* InvocationException} will be thrown with a message code that
* describes the reason for failure.
*
* @param leaver the body object of the user that wishes to leave the
* table.
* @param tableId the id of the table to be left.
*
* @exception InvocationException thrown if the leaving was not able
* processed for some reason. The explanation will be provided in the
* message data of the exception.
*/
public void leaveTable (BodyObject leaver, int tableId)
throws InvocationException
{
// look the table up
Table table = (Table)_tables.get(tableId);
if (table == null) {
throw new InvocationException(NO_SUCH_TABLE);
}
// request that the user be removed from the table
if (!table.clearOccupant(leaver.getVisibleName())) {
throw new InvocationException(NOT_AT_TABLE);
}
// remove the mapping from this user to the table
if (_boidMap.remove(leaver.getOid()) == null) {
Log.warning("No body to table mapping to clear? " +
"[leaver=" + leaver + ", table=" + table + "].");
}
// either update or delete the table depending on whether or not
// we just removed the last occupant
if (table.isEmpty()) {
purgeTable(table);
} else {
_tlobj.updateTables(table);
}
}
/**
* Removes the table from all of our internal tables and from its
* lobby's distributed object.
*/
protected void purgeTable (Table table)
{
// remove the table from our tables table
_tables.remove(table.getTableId());
// clear out all matching entries in the boid map
for (int i = 0; i < table.bodyOids.length; i++) {
_boidMap.remove(table.bodyOids[i]);
}
// remove the table from the lobby object
_tlobj.removeFromTables(table.tableId);
}
/**
* Called when we're ready to create a game (either an invitation has
* been accepted or a table is ready to start. If there is a problem
* creating the game manager, it should be reported in the logs.
*/
protected void createGame (final Table table)
throws InvocationException
{
// fill the players array into the game config
table.config.players = table.getPlayers();
PlaceRegistry.CreationObserver obs =
new PlaceRegistry.CreationObserver() {
public void placeCreated (PlaceObject plobj, PlaceManager pmgr) {
gameCreated(table, plobj);
}
};
try {
CrowdServer.plreg.createPlace(table.config, obs);
} catch (Throwable t) {
Log.warning("Failed to create manager for game " +
"[config=" + table.config + "]: " + t);
throw new InvocationException(INTERNAL_ERROR);
}
}
/**
* Called when our game has been created, we take this opportunity to
* clean up the table and transition it to "in play" mode.
*/
protected void gameCreated (Table table, PlaceObject plobj)
{
// update the table with the newly created game object
table.gameOid = plobj.getOid();
// configure the privacy of the game
((GameObject) plobj).setIsPrivate(table.tconfig.privateTable);
// clear the occupant to table mappings as this game is underway
for (int i = 0; i < table.bodyOids.length; i++) {
_boidMap.remove(table.bodyOids[i]);
}
// add an object death listener to unmap the table when the game
// finally goes away
plobj.addListener(_gameDeathListener);
// and then update the lobby object that contains the table
_tlobj.updateTables(table);
}
/**
* Called when a game created from a table managed by this table
* manager was destroyed. We remove the associated table.
*/
protected void unmapTable (int gameOid)
{
// look through our tables table for a table with a matching game
Iterator iter = _tables.values().iterator();
while (iter.hasNext()) {
Table table = (Table)iter.next();
if (table.gameOid == gameOid) {
purgeTable(table);
return; // all done
}
}
Log.warning("Requested to unmap table that wasn't mapped " +
"[gameOid=" + gameOid + "].");
}
// documentation inherited
public void objectAdded (ObjectAddedEvent event)
{
// nothing doing
}
// documentation inherited
public void objectRemoved (ObjectRemovedEvent event)
{
// if an occupant departed, see if they are in a pending table
if (!event.getName().equals(PlaceObject.OCCUPANTS)) {
return;
}
// look up the table to which this occupant is mapped
int bodyOid = event.getOid();
Table pender = (Table)_boidMap.remove(bodyOid);
if (pender == null) {
return;
}
// remove this occupant from the table
if (!pender.clearOccupant(bodyOid)) {
Log.warning("Attempt to remove body from mapped table failed " +
"[table=" + pender + ", bodyOid=" + bodyOid + "].");
return;
}
// either update or delete the table depending on whether or not
// we just removed the last occupant
if (pender.isEmpty()) {
purgeTable(pender);
} else {
_tlobj.updateTables(pender);
}
}
/** A reference to the place object in which we're managing tables. */
protected PlaceObject _plobj;
/** A reference to our place object casted to a table lobby object. */
protected TableLobbyObject _tlobj;
/** The table of pending tables. */
protected HashIntMap _tables = new HashIntMap();
/** A mapping from body oid to table. */
protected HashIntMap _boidMap = new HashIntMap();
/** A listener that prunes tables after the game dies. */
protected ChangeListener _gameDeathListener = new ObjectDeathListener() {
public void objectDestroyed (ObjectDestroyedEvent event) {
unmapTable(event.getTargetOid());
}
};
}
@@ -0,0 +1,37 @@
//
// $Id: TableManagerProvider.java 4191 2006-06-13 22:42:20Z ray $
//
// 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.server;
/**
* A place manager that wishes to provide table matchmaking services in
* its place needs to create a table manager and make it available by
* implementing this interface. The table invocation services and the
* table manager will take care of the rest.
*/
public interface TableManagerProvider
{
/**
* Returns a reference to the table manager that is responsible for
* table management in this lobby.
*/
public TableManager getTableManager ();
}