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
@@ -83,89 +83,37 @@ public class SpotProvider
return;
}
// avoid cluttering up the method declaration with final keywords
final BodyObject fsource = (BodyObject)caller;
final int fportalId = portalId;
final int fsceneVer = destSceneVer;
final SceneMoveListener flistener = listener;
// obtain the source scene
final SpotSceneManager srcmgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
BodyObject body = (BodyObject)caller;
SpotSceneManager srcmgr = (SpotSceneManager)_screg.getSceneManager(sceneId);
if (srcmgr == null) {
Log.warning("Traverse portal missing source scene " +
"[user=" + fsource.who() + ", sceneId=" + sceneId +
"[user=" + body.who() + ", sceneId=" + sceneId +
", portalId=" + portalId + "].");
throw new InvocationException(INTERNAL_ERROR);
}
// obtain the destination scene and location id
SpotScene rss = (SpotScene)srcmgr.getScene();
final Portal fdest = rss.getPortal(portalId);
Portal dest = rss.getPortal(portalId);
// give the source scene manager a chance to do access control
String errmsg = srcmgr.mayTraversePortal(fsource, fdest);
String errmsg = srcmgr.mayTraversePortal(body, dest);
if (errmsg != null) {
throw new InvocationException(errmsg);
}
// make sure this portal has valid info
if (fdest == null || !fdest.isValid()) {
Log.warning("Traverse portal with invalid portal [user=" + fsource.who() +
", scene=" + srcmgr.where() + ", pid=" + portalId + ", portal=" + fdest +
if (dest == null || !dest.isValid()) {
Log.warning("Traverse portal with invalid portal [user=" + body.who() +
", scene=" + srcmgr.where() + ", pid=" + portalId + ", portal=" + dest +
", portals=" + StringUtil.toString(rss.getPortals()) + "].");
throw new InvocationException(NO_SUCH_PORTAL);
}
// resolve their destination scene
SceneRegistry.ResolutionListener 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 (!fsource.isActive()) {
Log.info("Abandoning portal traversal, client gone [who=" + fsource.who() +
", dest=" + scmgr.where() + "].");
InvocationMarshaller.setNoResponse(flistener);
return;
}
// let the source manager know that this guy is departing via the specified portal
srcmgr.willTraversePortal(fsource, fdest);
SpotSceneManager sscmgr = (SpotSceneManager)scmgr;
finishTraversePortalRequest(fsource, sscmgr, fsceneVer, fdest, flistener);
}
public void sceneFailedToResolve (
int rsceneId, Exception reason) {
Log.warning("Unable to resolve target scene [sceneId=" + rsceneId +
", reason=" + reason + "].");
// pretend like the scene doesn't exist to the client
flistener.requestFailed(NO_SUCH_PLACE);
}
};
_screg.resolveScene(fdest.targetSceneId, rl);
}
/**
* This is called after the scene to which we are moving is guaranteed to have been loaded into
* the server.
*/
protected void finishTraversePortalRequest (
BodyObject source, SpotSceneManager destmgr, int sceneVer, Portal dest,
SceneMoveListener listener)
{
// let the destination scene manager know that we're coming in
destmgr.mapEnteringBody(source, dest);
try {
// move to the place object associated with this scene
_screg.effectSceneMove(source, destmgr, sceneVer, listener);
} catch (InvocationException sfe) {
listener.requestFailed(sfe.getMessage());
// and let the destination scene manager know that we're no
// longer coming in
destmgr.clearEnteringBody(source);
}
_screg.resolveScene(dest.targetSceneId, new SpotSceneMoveHandler(
srcmgr, body, sceneId, destSceneVer, dest, listener));
}
/**
@@ -0,0 +1,69 @@
//
// $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.spot.server;
import com.threerings.presents.server.InvocationException;
import com.threerings.crowd.data.BodyObject;
import com.threerings.whirled.client.SceneService;
import com.threerings.whirled.server.SceneManager;
import com.threerings.whirled.server.SceneMoveHandler;
import com.threerings.whirled.spot.data.Portal;
/**
* Moves a player between scenes, accounting for their exit and entry via portals.
*/
public class SpotSceneMoveHandler extends SceneMoveHandler
{
public SpotSceneMoveHandler (SpotSceneManager srcmgr, BodyObject body, int sceneId,
int sceneVer, Portal dest, SceneService.SceneMoveListener listener)
{
super(body, sceneId, sceneVer, listener);
_srcmgr = srcmgr;
_dest = dest;
}
@Override // from AbstractSceneMoveHandler
protected void effectSceneMove (SceneManager scmgr)
throws InvocationException
{
// let the source manager know that this guy is departing via the specified portal
_srcmgr.willTraversePortal(_body, _dest);
// let the destination scene manager know that we're coming in
SpotSceneManager destmgr = (SpotSceneManager)scmgr;
destmgr.mapEnteringBody(_body, _dest);
try {
super.effectSceneMove(destmgr);
} catch (InvocationException ie) {
// if anything goes haywire, clear out our entering status
destmgr.clearEnteringBody(_body);
throw ie;
}
}
protected SpotSceneManager _srcmgr;
protected Portal _dest;
}