Yay for rabbit holes. Revamped the table services to decouple them from places.
While I was in there, I extracted the communication between the client and the TableManager into a new-style "embedded in the TableLobbyObject" service instead of wonkily routing everything through the global ParlorService and the ParlorProvider (which got merged into the ParlorManager as a part of this rabbit holery). The GG build will break... I will fix it. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@255 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -23,7 +23,6 @@ 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;
|
||||
@@ -66,13 +65,6 @@ public class ParlorDispatcher extends InvocationDispatcher
|
||||
);
|
||||
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,
|
||||
@@ -80,20 +72,6 @@ public class ParlorDispatcher extends InvocationDispatcher
|
||||
);
|
||||
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,
|
||||
@@ -108,13 +86,6 @@ public class ParlorDispatcher extends InvocationDispatcher
|
||||
);
|
||||
return;
|
||||
|
||||
case ParlorMarshaller.START_TABLE_NOW:
|
||||
((ParlorProvider)provider).startTableNow(
|
||||
source,
|
||||
((Integer)args[0]).intValue(), ((Integer)args[1]).intValue(), (InvocationService.InvocationListener)args[2]
|
||||
);
|
||||
return;
|
||||
|
||||
default:
|
||||
super.dispatchRequest(source, methodId, args);
|
||||
return;
|
||||
|
||||
@@ -24,14 +24,18 @@ package com.threerings.parlor.server;
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.threerings.util.Name;
|
||||
|
||||
import com.threerings.presents.data.ClientObject;
|
||||
import com.threerings.presents.server.InvocationException;
|
||||
import com.threerings.presents.server.InvocationManager;
|
||||
|
||||
import com.threerings.crowd.data.BodyObject;
|
||||
import com.threerings.crowd.server.CrowdServer;
|
||||
import com.threerings.crowd.server.PlaceRegistry;
|
||||
|
||||
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;
|
||||
|
||||
@@ -42,11 +46,8 @@ import com.threerings.parlor.game.server.GameManager;
|
||||
* game.
|
||||
*/
|
||||
public class ParlorManager
|
||||
implements ParlorCodes
|
||||
implements ParlorCodes, ParlorProvider
|
||||
{
|
||||
/** 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.
|
||||
@@ -58,13 +59,78 @@ public class ParlorManager
|
||||
public void init (InvocationManager invmgr, PlaceRegistry plreg)
|
||||
{
|
||||
// create and register our invocation provider
|
||||
parprov = new ParlorProvider(this);
|
||||
invmgr.registerDispatcher(new ParlorDispatcher(parprov), PARLOR_GROUP);
|
||||
invmgr.registerDispatcher(new ParlorDispatcher(this), PARLOR_GROUP);
|
||||
|
||||
// keep this for later
|
||||
_plreg = plreg;
|
||||
}
|
||||
|
||||
// from interface ParlorProvider
|
||||
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);
|
||||
}
|
||||
|
||||
int inviteId = invite(source, target, config);
|
||||
listener.inviteReceived(inviteId);
|
||||
}
|
||||
|
||||
// from interface ParlorProvider
|
||||
public void respond (ClientObject caller, int inviteId, int code, Object arg,
|
||||
ParlorService.InvocationListener listener)
|
||||
{
|
||||
// pass this on to the parlor manager
|
||||
respondToInvite((BodyObject)caller, inviteId, code, arg);
|
||||
}
|
||||
|
||||
// from interface ParlorProvider
|
||||
public void cancel (ClientObject caller, int inviteId,
|
||||
ParlorService.InvocationListener listener)
|
||||
{
|
||||
cancelInvite((BodyObject)caller, inviteId);
|
||||
}
|
||||
|
||||
// from interface ParlorProvider
|
||||
public void startSolitaire (ClientObject caller, GameConfig config,
|
||||
ParlorService.ConfirmListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
BodyObject user = (BodyObject)caller;
|
||||
|
||||
Log.debug("Processing start solitaire [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
|
||||
CrowdServer.plreg.createPlace(config);
|
||||
|
||||
// the game manager will notify the player that their game is
|
||||
// "ready", but tell the caller 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues an invitation from the <code>inviter</code> to the <code>invitee</code> for a game as
|
||||
* described by the supplied config object.
|
||||
@@ -79,8 +145,7 @@ public class ParlorManager
|
||||
* 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)
|
||||
public int invite (BodyObject inviter, BodyObject invitee, GameConfig config)
|
||||
throws InvocationException
|
||||
{
|
||||
// Log.info("Received invitation request [inviter=" + inviter +
|
||||
|
||||
@@ -21,221 +21,41 @@
|
||||
|
||||
package com.threerings.parlor.server;
|
||||
|
||||
import com.threerings.util.Name;
|
||||
|
||||
import com.threerings.parlor.client.ParlorService;
|
||||
import com.threerings.parlor.game.data.GameConfig;
|
||||
import com.threerings.presents.client.Client;
|
||||
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;
|
||||
import com.threerings.util.Name;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Defines the server-side of the {@link ParlorService}.
|
||||
*/
|
||||
public class ParlorProvider
|
||||
implements InvocationProvider, ParlorCodes
|
||||
public interface ParlorProvider extends InvocationProvider
|
||||
{
|
||||
/**
|
||||
* 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.
|
||||
* Handles a {@link ParlorService#cancel} request.
|
||||
*/
|
||||
public ParlorProvider (ParlorManager pmgr)
|
||||
{
|
||||
_pmgr = pmgr;
|
||||
}
|
||||
public void cancel (ClientObject caller, int arg1, InvocationService.InvocationListener arg2)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Processes a request from the client to invite another user to play
|
||||
* a game.
|
||||
* Handles a {@link ParlorService#invite} request.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
public void invite (ClientObject caller, Name arg1, GameConfig arg2, ParlorService.InviteListener arg3)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Processes a request from the client to respond to an outstanding
|
||||
* invitation by accepting, refusing, or countering it.
|
||||
* Handles a {@link ParlorService#respond} request.
|
||||
*/
|
||||
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
|
||||
}
|
||||
public void respond (ClientObject caller, int arg1, int arg2, Object arg3, InvocationService.InvocationListener arg4)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Handles a {@link ParlorService#startSolitaire} request.
|
||||
*/
|
||||
public void startTableNow (ClientObject caller, int lobbyOid, int tableId,
|
||||
InvocationService.InvocationListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
TableManager tmgr = getTableManager(lobbyOid);
|
||||
tmgr.startTableNow((BodyObject) caller, tableId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
CrowdServer.plreg.createPlace(config);
|
||||
|
||||
// the game manager will notify the player that their game is
|
||||
// "ready", but tell the caller 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;
|
||||
public void startSolitaire (ClientObject caller, GameConfig arg1, InvocationService.ConfirmListener arg2)
|
||||
throws InvocationException;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// $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.parlor.server;
|
||||
|
||||
import com.threerings.parlor.client.TableService;
|
||||
import com.threerings.parlor.data.TableConfig;
|
||||
import com.threerings.parlor.data.TableMarshaller;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Dispatches requests to the {@link TableProvider}.
|
||||
*/
|
||||
public class TableDispatcher extends InvocationDispatcher
|
||||
{
|
||||
/**
|
||||
* Creates a dispatcher that may be registered to dispatch invocation
|
||||
* service requests for the specified provider.
|
||||
*/
|
||||
public TableDispatcher (TableProvider provider)
|
||||
{
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
// from InvocationDispatcher
|
||||
public InvocationMarshaller createMarshaller ()
|
||||
{
|
||||
return new TableMarshaller();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // from InvocationDispatcher
|
||||
public void dispatchRequest (
|
||||
ClientObject source, int methodId, Object[] args)
|
||||
throws InvocationException
|
||||
{
|
||||
switch (methodId) {
|
||||
case TableMarshaller.CREATE_TABLE:
|
||||
((TableProvider)provider).createTable(
|
||||
source,
|
||||
(TableConfig)args[0], (GameConfig)args[1], (InvocationService.ResultListener)args[2]
|
||||
);
|
||||
return;
|
||||
|
||||
case TableMarshaller.JOIN_TABLE:
|
||||
((TableProvider)provider).joinTable(
|
||||
source,
|
||||
((Integer)args[0]).intValue(), ((Integer)args[1]).intValue(), (InvocationService.InvocationListener)args[2]
|
||||
);
|
||||
return;
|
||||
|
||||
case TableMarshaller.LEAVE_TABLE:
|
||||
((TableProvider)provider).leaveTable(
|
||||
source,
|
||||
((Integer)args[0]).intValue(), (InvocationService.InvocationListener)args[1]
|
||||
);
|
||||
return;
|
||||
|
||||
case TableMarshaller.START_TABLE_NOW:
|
||||
((TableProvider)provider).startTableNow(
|
||||
source,
|
||||
((Integer)args[0]).intValue(), (InvocationService.InvocationListener)args[1]
|
||||
);
|
||||
return;
|
||||
|
||||
default:
|
||||
super.dispatchRequest(source, methodId, args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,8 @@ 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.data.ClientObject;
|
||||
import com.threerings.presents.dobj.DObject;
|
||||
import com.threerings.presents.dobj.NamedEvent;
|
||||
import com.threerings.presents.dobj.ObjectAddedEvent;
|
||||
import com.threerings.presents.dobj.ObjectDeathListener;
|
||||
@@ -44,39 +45,43 @@ import com.threerings.crowd.server.CrowdServer;
|
||||
import com.threerings.crowd.server.PlaceManager;
|
||||
|
||||
import com.threerings.parlor.Log;
|
||||
import com.threerings.parlor.client.TableService;
|
||||
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.data.TableMarshaller;
|
||||
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.
|
||||
* A table manager can be used by a place manager (or other entity) to take care of the management
|
||||
* of a table matchmaking service in the place managed by the place manager (or on some other
|
||||
* distributed object). 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
|
||||
implements ParlorCodes, OidListListener, TableProvider
|
||||
{
|
||||
/**
|
||||
* Creates a table manager that will work in tandem with the specified
|
||||
* place manager to manage a table matchmaking service in this place.
|
||||
* Creates a table manager that will manage tables in the supplied distributed object (which
|
||||
* must implement {@link TableLobbyObject}.
|
||||
*/
|
||||
public TableManager (PlaceManager plmgr)
|
||||
public TableManager (DObject tableObject)
|
||||
{
|
||||
// get a reference to our place object
|
||||
_plobj = plmgr.getPlaceObject();
|
||||
// set up our object references
|
||||
_tlobj = (TableLobbyObject)tableObject;
|
||||
_tlobj.setTableService(
|
||||
(TableMarshaller)CrowdServer.invmgr.registerDispatcher(new TableDispatcher(this)));
|
||||
_dobj = tableObject;
|
||||
|
||||
// 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;
|
||||
// if our table is in a "place" add ourselves as a listener so that we can tell if a user
|
||||
// leaves the place without leaving their table
|
||||
if (_dobj instanceof PlaceObject) {
|
||||
_dobj.addListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,33 +92,19 @@ public class TableManager
|
||||
_tableClass = tableClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
// from interface ParlorProvider
|
||||
public void createTable (ClientObject caller, TableConfig tableConfig, GameConfig config,
|
||||
TableService.ResultListener listener)
|
||||
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() + "].");
|
||||
BodyObject creator = (BodyObject)caller;
|
||||
|
||||
// if we're managing tables in a place, make sure the creator is an occupant of the place
|
||||
// in which they are requesting to create a table
|
||||
if (_dobj instanceof PlaceObject &&
|
||||
!((PlaceObject)_dobj).occupants.contains(creator.getOid())) {
|
||||
Log.warning("Requested to create a table in a place not occupied by the creator " +
|
||||
"[creator=" + creator + ", ploid=" + _dobj.getOid() + "].");
|
||||
throw new InvocationException(INTERNAL_ERROR);
|
||||
}
|
||||
|
||||
@@ -122,26 +113,25 @@ public class TableManager
|
||||
try {
|
||||
table = _tableClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
Log.warning("Unable to create a new table instance! " +
|
||||
"[tableClass=" + _tableClass + ", error=" + e + "].");
|
||||
Log.warning("Unable to create a new table instance! [tableClass=" + _tableClass +
|
||||
", error=" + e + "].");
|
||||
throw new InvocationException(INTERNAL_ERROR);
|
||||
}
|
||||
table.init(_plobj.getOid(), tableConfig, config);
|
||||
table.init(_dobj.getOid(), tableConfig, config);
|
||||
|
||||
if (table.bodyOids != null && table.bodyOids.length > 0) {
|
||||
// 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 + "].");
|
||||
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);
|
||||
}
|
||||
|
||||
// make a mapping from the creator to this table
|
||||
_boidMap.put(creator.getOid(), table);
|
||||
notePlayerAdded(table, creator.getOid());
|
||||
}
|
||||
|
||||
// stick the table into the table lobby object
|
||||
@@ -155,32 +145,16 @@ public class TableManager
|
||||
int oid = createGame(table);
|
||||
}
|
||||
|
||||
// finally let the caller know what the new table id is
|
||||
return table.tableId;
|
||||
listener.requestProcessed(table.tableId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
// from interface ParlorProvider
|
||||
public void joinTable (ClientObject caller, int tableId, int position,
|
||||
TableService.InvocationListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
BodyObject joiner = (BodyObject)caller;
|
||||
|
||||
// look the table up
|
||||
Table table = _tables.get(tableId);
|
||||
if (table == null) {
|
||||
@@ -199,32 +173,23 @@ public class TableManager
|
||||
createGame(table);
|
||||
} else {
|
||||
// make a mapping from this occupant to this table
|
||||
_boidMap.put(joiner.getOid(), table);
|
||||
notePlayerAdded(table, joiner.getOid());
|
||||
}
|
||||
|
||||
// update the table in the lobby
|
||||
_tlobj.updateTables(table);
|
||||
|
||||
// there is normally no success response. the client will see
|
||||
// themselves show up in the table that they joined
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
// from interface ParlorProvider
|
||||
public void leaveTable (ClientObject caller, int tableId,
|
||||
TableService.InvocationListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
BodyObject leaver = (BodyObject)caller;
|
||||
|
||||
// look the table up
|
||||
Table table = _tables.get(tableId);
|
||||
if (table == null) {
|
||||
@@ -237,57 +202,42 @@ public class TableManager
|
||||
}
|
||||
|
||||
// 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 + "].");
|
||||
if (!notePlayerRemoved(table, leaver.getOid())) {
|
||||
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
|
||||
// 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);
|
||||
}
|
||||
|
||||
// there is normally no success response. the client will see
|
||||
// themselves removed from the table they just left
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests that the specified table be started now, even if there are
|
||||
* some vacant seats. This may only be done if the minimum number of
|
||||
* players are already present at the table. By convention, only the
|
||||
* player at seat 0 may request to have the game start early, as they
|
||||
* are usually the creator.
|
||||
*
|
||||
* @param requester the body object of the user that wishes to start
|
||||
* the table.
|
||||
* @param tableId the id of the table to be started.
|
||||
*
|
||||
* @exception InvocationException thrown if the starting was not able
|
||||
* processed for some reason. The explanation will be provided in the
|
||||
* message data of the exception.
|
||||
*/
|
||||
public void startTableNow (BodyObject requester, int tableId)
|
||||
// from interface ParlorProvider
|
||||
public void startTableNow (ClientObject caller, int tableId,
|
||||
TableService.InvocationListener listener)
|
||||
throws InvocationException
|
||||
{
|
||||
// look the table up
|
||||
BodyObject starter = (BodyObject)caller;
|
||||
Table table = _tables.get(tableId);
|
||||
if (table == null) {
|
||||
throw new InvocationException(NO_SUCH_TABLE);
|
||||
|
||||
} else if (requester.getOid() != table.bodyOids[0]) {
|
||||
} else if (starter.getOid() != table.bodyOids[0]) {
|
||||
throw new InvocationException(INVALID_TABLE_POSITION);
|
||||
|
||||
} else if (!table.mayBeStarted()) {
|
||||
throw new InvocationException(INTERNAL_ERROR);
|
||||
}
|
||||
|
||||
// I guess we're ready to go!
|
||||
createGame(table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the table from all of our internal tables and from its
|
||||
* lobby's distributed object.
|
||||
* Removes the table from all of our internal tables and from its lobby's distributed object.
|
||||
*/
|
||||
protected void purgeTable (Table table)
|
||||
{
|
||||
@@ -297,7 +247,7 @@ public class TableManager
|
||||
// clear out all matching entries in the boid map
|
||||
if (table.bodyOids != null) {
|
||||
for (int ii = 0; ii < table.bodyOids.length; ii++) {
|
||||
_boidMap.remove(table.bodyOids[ii]);
|
||||
notePlayerRemoved(table, table.bodyOids[ii]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,9 +259,30 @@ public class TableManager
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Called when a player is added to a table to set up our mappings.
|
||||
*/
|
||||
protected void notePlayerAdded (Table table, int playerOid)
|
||||
{
|
||||
_boidMap.put(playerOid, table);
|
||||
|
||||
// TODO: if we're not in a place, listen to the player's BodyObject for object death so
|
||||
// that we remove them from their table if they logoff
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player leaves a table to clear our mappings.
|
||||
*/
|
||||
protected boolean notePlayerRemoved (Table table, int playerOid)
|
||||
{
|
||||
boolean removed = (_boidMap.remove(playerOid) != null);
|
||||
// TODO: remove our BodyObject death listener
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return the oid of the newly-created game.
|
||||
*/
|
||||
@@ -325,8 +296,7 @@ public class TableManager
|
||||
return gobj.getOid();
|
||||
|
||||
} catch (Throwable t) {
|
||||
Log.warning("Failed to create manager for game " +
|
||||
"[config=" + table.config + "]: " + t);
|
||||
Log.warning("Failed to create manager for game [config=" + table.config + "]: " + t);
|
||||
throw new InvocationException(INTERNAL_ERROR);
|
||||
}
|
||||
}
|
||||
@@ -344,8 +314,8 @@ public class TableManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when our game has been created, we take this opportunity to clean
|
||||
* up the table and transition it to "in play" mode.
|
||||
* 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, GameObject gameobj)
|
||||
{
|
||||
@@ -365,8 +335,7 @@ public class TableManager
|
||||
}
|
||||
}
|
||||
|
||||
// add an object death listener to unmap the table when the game
|
||||
// finally goes away
|
||||
// add an object death listener to unmap the table when the game finally goes away
|
||||
gameobj.addListener(_gameListener);
|
||||
|
||||
// and then update the lobby object that contains the table
|
||||
@@ -374,18 +343,16 @@ public class TableManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a game created from a table managed by this table
|
||||
* manager was destroyed. We remove the associated 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)
|
||||
{
|
||||
Table table = _goidMap.get(gameOid);
|
||||
if (table != null) {
|
||||
purgeTable(table);
|
||||
|
||||
} else {
|
||||
Log.warning("Requested to unmap table that wasn't mapped " +
|
||||
"[gameOid=" + gameOid + "].");
|
||||
Log.warning("Requested to unmap table that wasn't mapped [gameOid=" + gameOid + "].");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,8 +363,7 @@ public class TableManager
|
||||
{
|
||||
Table table = _goidMap.get(gameOid);
|
||||
if (table == null) {
|
||||
Log.warning("Unable to find table for running game " +
|
||||
"[gameOid=" + gameOid + "].");
|
||||
Log.warning("Unable to find table for running game [gameOid=" + gameOid + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -437,13 +403,13 @@ public class TableManager
|
||||
|
||||
// remove this occupant from the table
|
||||
if (!pender.clearOccupantByOid(bodyOid)) {
|
||||
Log.warning("Attempt to remove body from mapped table failed " +
|
||||
"[table=" + pender + ", bodyOid=" + 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
|
||||
// either update or delete the table depending on whether or not we just removed the last
|
||||
// occupant
|
||||
if (pender.isEmpty()) {
|
||||
purgeTable(pender);
|
||||
} else {
|
||||
@@ -451,15 +417,15 @@ public class TableManager
|
||||
}
|
||||
}
|
||||
|
||||
/** A reference to the place object in which we're managing tables. */
|
||||
protected PlaceObject _plobj;
|
||||
/** A reference to the distributed object in which we're managing tables. */
|
||||
protected DObject _dobj;
|
||||
|
||||
/** A reference to our distributed object casted to a table lobby object. */
|
||||
protected TableLobbyObject _tlobj;
|
||||
|
||||
/** The class of table we instantiate. */
|
||||
protected Class<? extends Table> _tableClass = Table.class;
|
||||
|
||||
/** A reference to our place object casted to a table lobby object. */
|
||||
protected TableLobbyObject _tlobj;
|
||||
|
||||
/** The table of pending tables. */
|
||||
protected HashIntMap<Table> _tables = new HashIntMap<Table>();
|
||||
|
||||
@@ -488,9 +454,7 @@ public class TableManager
|
||||
maybeCheckOccupants(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the set event causes us to update the table.
|
||||
*/
|
||||
/** Check to see if the set event causes us to update the table. */
|
||||
protected void maybeCheckOccupants (NamedEvent event) {
|
||||
if (GameObject.OCCUPANTS.equals(event.getName())) {
|
||||
updateOccupants(event.getTargetOid());
|
||||
@@ -499,5 +463,5 @@ public class TableManager
|
||||
} // END: class GameDeathListener
|
||||
|
||||
/** A listener that prunes tables after the game dies. */
|
||||
protected ChangeListener _gameListener = new GameListener();
|
||||
protected GameListener _gameListener = new GameListener();
|
||||
}
|
||||
|
||||
@@ -22,16 +22,15 @@
|
||||
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.
|
||||
* 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.
|
||||
* Returns a reference to the table manager that is responsible for table management in this
|
||||
* lobby.
|
||||
*/
|
||||
public TableManager getTableManager ();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// $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.parlor.server;
|
||||
|
||||
import com.threerings.parlor.client.TableService;
|
||||
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.server.InvocationException;
|
||||
import com.threerings.presents.server.InvocationProvider;
|
||||
|
||||
/**
|
||||
* Defines the server-side of the {@link TableService}.
|
||||
*/
|
||||
public interface TableProvider extends InvocationProvider
|
||||
{
|
||||
/**
|
||||
* Handles a {@link TableService#createTable} request.
|
||||
*/
|
||||
public void createTable (ClientObject caller, TableConfig arg1, GameConfig arg2, InvocationService.ResultListener arg3)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Handles a {@link TableService#joinTable} request.
|
||||
*/
|
||||
public void joinTable (ClientObject caller, int arg1, int arg2, InvocationService.InvocationListener arg3)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Handles a {@link TableService#leaveTable} request.
|
||||
*/
|
||||
public void leaveTable (ClientObject caller, int arg1, InvocationService.InvocationListener arg2)
|
||||
throws InvocationException;
|
||||
|
||||
/**
|
||||
* Handles a {@link TableService#startTableNow} request.
|
||||
*/
|
||||
public void startTableNow (ClientObject caller, int arg1, InvocationService.InvocationListener arg2)
|
||||
throws InvocationException;
|
||||
}
|
||||
Reference in New Issue
Block a user