Factored scene movement out into separate helper classes so that code can be a

bit more cleanly shared and the process of resolving zones and scenes and the
various fiddling done during a scene move can be more easily grokked.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@363 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-07-17 00:25:18 +00:00
parent 755b835c93
commit cb05d5f56b
8 changed files with 404 additions and 325 deletions
@@ -0,0 +1,90 @@
//
// $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.whirled.server;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.server.InvocationException;
import com.threerings.crowd.data.BodyObject;
import com.threerings.whirled.client.SceneService;
import com.threerings.whirled.data.SceneCodes;
import com.threerings.whirled.Log;
/**
* Handles the basics of moving a client into a new scene, which may involve resolution. Takes care
* of annoying edge cases like the client logging off before their target scene is resolved and can
* be extended to handle extra fun stuff.
*/
public abstract class AbstractSceneMoveHandler
implements SceneRegistry.ResolutionListener
{
public AbstractSceneMoveHandler (BodyObject body, int sceneId, int version,
InvocationService.InvocationListener listener)
{
_body = body;
_sceneId = sceneId;
_version = version;
_listener = listener;
}
// from interface SceneRegistry.ResolutionListener
public void sceneWasResolved (SceneManager scmgr)
{
// make sure our caller is still around; under heavy load, clients might end their session
// while the scene is resolving
if (!_body.isActive()) {
Log.info("Abandoning scene move, client gone [who=" + _body.who() +
", dest=" + scmgr.where() + "].");
InvocationMarshaller.setNoResponse(_listener);
return;
}
try {
effectSceneMove(scmgr);
} catch (InvocationException sfe) {
_listener.requestFailed(sfe.getMessage());
} catch (RuntimeException re) {
Log.logStackTrace(re);
_listener.requestFailed(SceneCodes.INTERNAL_ERROR);
}
}
// from interface SceneRegistry.ResolutionListener
public void sceneFailedToResolve (int sceneId, Exception reason)
{
Log.warning("Unable to resolve scene [sceneid=" + sceneId + ", reason=" + reason + "].");
_listener.requestFailed(SceneCodes.NO_SUCH_PLACE);
}
protected abstract void effectSceneMove (SceneManager scmgr)
throws InvocationException;
protected BodyObject _body;
protected int _sceneId;
protected int _version;
protected InvocationService.InvocationListener _listener;
}
@@ -0,0 +1,76 @@
//
// $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.whirled.server;
import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.server.InvocationException;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.server.CrowdServer;
import com.threerings.whirled.client.SceneService;
import com.threerings.whirled.data.SceneCodes;
import com.threerings.whirled.data.SceneModel;
import com.threerings.whirled.data.SceneUpdate;
import com.threerings.whirled.data.ScenedBodyObject;
/**
* Handles a simple scene to scene move.
*/
public class SceneMoveHandler extends AbstractSceneMoveHandler
{
public SceneMoveHandler (BodyObject body, int sceneId, int sceneVer,
SceneService.SceneMoveListener listener)
{
super(body, sceneId, sceneVer, listener);
}
@Override // from AbstractSceneMoveHandler
protected void effectSceneMove (SceneManager scmgr)
throws InvocationException
{
// move to the place object associated with this scene
int ploid = scmgr.getPlaceObject().getOid();
PlaceConfig config = CrowdServer.plreg.locprov.moveTo(_body, ploid);
// now that we've finally moved, we can update the user object with the new scene id
((ScenedBodyObject)_body).setSceneId(scmgr.getScene().getId());
// check to see if they need a newer version of the scene data
SceneService.SceneMoveListener listener = (SceneService.SceneMoveListener)_listener;
SceneModel model = scmgr.getScene().getSceneModel();
if (_version != model.version) {
SceneUpdate[] updates = null;
if (_version < model.version) {
updates = scmgr.getUpdates(_version);
}
if (updates != null) {
listener.moveSucceededWithUpdates(ploid, config, updates);
} else {
listener.moveSucceededWithScene(ploid, config, model);
}
} else {
listener.moveSucceeded(ploid, config);
}
}
}
@@ -221,72 +221,11 @@ public class SceneRegistry
}
// from interface SceneService
public void moveTo (ClientObject caller, int sceneId, final int sceneVer,
final SceneService.SceneMoveListener listener)
public void moveTo (ClientObject caller, int sceneId, int sceneVer,
SceneService.SceneMoveListener listener)
{
final BodyObject source = (BodyObject)caller;
// create a callback object to handle the resolution or failed resolution of the scene
SceneRegistry.ResolutionListener rl = null;
rl = new SceneRegistry.ResolutionListener() {
public void sceneWasResolved (SceneManager scmgr) {
// make sure our caller is still around; under heavy load, clients might end their
// session while the scene is resolving
if (!source.isActive()) {
Log.info("Abandoning scene move, client gone [who=" + source.who() +
", dest=" + scmgr.where() + "].");
InvocationMarshaller.setNoResponse(listener);
return;
}
finishMoveToRequest(source, scmgr, sceneVer, listener);
}
public void sceneFailedToResolve (int rsceneId, Exception reason) {
Log.warning("Unable to resolve scene [sceneid=" + rsceneId +
", reason=" + reason + "].");
// pretend like the scene doesn't exist to the client
listener.requestFailed(NO_SUCH_PLACE);
}
};
// make sure the scene they are headed to is actually loaded into the server
resolveScene(sceneId, rl);
}
/**
* Moves the supplied body into the supplied (already resolved) scene and informs the supplied
* listener if the move is successful.
*
* @exception InvocationException thrown if a failure occurs attempting to move the user into
* the place associated with the scene.
*/
public void effectSceneMove (BodyObject source, SceneManager scmgr,
int sceneVersion, SceneService.SceneMoveListener listener)
throws InvocationException
{
// move to the place object associated with this scene
int ploid = scmgr.getPlaceObject().getOid();
PlaceConfig config = CrowdServer.plreg.locprov.moveTo(source, ploid);
// now that we've finally moved, we can update the user object with the new scene id
((ScenedBodyObject)source).setSceneId(scmgr.getScene().getId());
// check to see if they need a newer version of the scene data
SceneModel model = scmgr.getScene().getSceneModel();
if (sceneVersion != model.version) {
SceneUpdate[] updates = null;
if (sceneVersion < model.version) {
// try getting updates to bring the client to the right version
updates = scmgr.getUpdates(sceneVersion);
}
if (updates != null) {
listener.moveSucceededWithUpdates(ploid, config, updates);
} else {
listener.moveSucceededWithScene(ploid, config, model);
}
} else {
listener.moveSucceeded(ploid, config);
}
BodyObject body = (BodyObject)caller;
resolveScene(sceneId, new SceneMoveHandler(body, sceneId, sceneVer, listener));
}
/**
@@ -315,25 +254,6 @@ public class SceneRegistry
source.setSceneId(0);
}
/**
* This is called after the scene to which we are moving is guaranteed to have been loaded into
* the server.
*/
protected void finishMoveToRequest (BodyObject source, SceneManager scmgr, int sceneVersion,
SceneService.SceneMoveListener listener)
{
try {
effectSceneMove(source, scmgr, sceneVersion, listener);
} catch (InvocationException sfe) {
listener.requestFailed(sfe.getMessage());
} catch (RuntimeException re) {
Log.logStackTrace(re);
listener.requestFailed(INTERNAL_ERROR);
}
}
/**
* Called when the scene resolution has completed successfully.
*/