[P2play-commit] SF.net SVN: p2play: [15] trunk/P2Play/src/org/p2play/scripting/pnuts/api
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-27 18:09:23
|
Revision: 15
http://p2play.svn.sourceforge.net/p2play/?rev=15&view=rev
Author: tisoft
Date: 2006-12-27 10:09:23 -0800 (Wed, 27 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Added Paths:
-----------
trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeGameAction.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionDelegate.java
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionAPI.java 2006-12-27 18:09:23 UTC (rev 15)
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.region;
+
+import java.util.Iterator;
+
+import org.p2play.net.region.Client;
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.Region;
+import org.p2play.net.region.RegionManager;
+import org.p2play.scripting.pnuts.api.object.ObjectAPI;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+import org.p2play.scripting.pnuts.object.GameActionClass;
+import org.p2play.scripting.pnuts.object.GameDescriptor;
+import org.p2play.scripting.pnuts.object.GameObject;
+import org.p2play.util.ListMap;
+
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+
+/**
+ * The almighty RegionAPI to make calls on the regionManager via Pnuts
+ * Here you can join and leave regions in our pastry network,
+ * send gameActions
+ * @author markus
+ *
+ */
+public class RegionAPI implements Client {
+
+ RegionManager regionManager;
+ private ListMap<Region, RegionDelegate> callbackMap;
+
+ public RegionAPI(RegionManager regionManager){
+ this.regionManager = regionManager;
+ this.callbackMap = new ListMap<Region, RegionDelegate>();
+ }
+
+ /**
+ * Joins the region with the name "regionName" and adds it to the local region map
+ * Additionally the ObjectAPI for the specified region is registered
+ * @param context the Pnuts context
+ * @param regionName the name of the region to join
+ * @param callback the Pnuts callback function
+ * @return
+ */
+ public RegionDelegate joinRegion(Context context, Region regionName, PnutsFunction receivedFunction, PnutsFunction changeFunction) {
+ RegionDelegate region = new RegionDelegate(this, context, regionName, receivedFunction, changeFunction);
+ joinRegion(context, region);
+ return region;
+ }
+
+ void joinRegion(Context context, RegionDelegate region) {
+ synchronized (callbackMap) {
+ callbackMap.add(region.getRegion(), region);
+ }
+ GameDescriptor gameDescriptor=GameDescriptor.getGameDescriptor(region.getRegion().getGame());
+
+ PackageHandler packageHandler=new PackageHandler();
+ packageHandler.putPackage("ROOT", context.getCurrentPackage());
+ ObjectAPI objectAPI=new ObjectAPI(gameDescriptor);
+ objectAPI.registerAPI(packageHandler);
+
+ //set region name
+ Iterator<GameActionClass> iterator=gameDescriptor.getGameActionMap().values().iterator();
+ while (iterator.hasNext()) {
+ GameActionClass element = iterator.next();
+ element.setRegion(region.getRegion());
+ }
+
+ regionManager.joinRegion(region.getRegion(),this);
+ }
+
+ /**
+ * Leaves the region if it is in the region map.
+ * If it is not in the local region map --> nothing to do.
+ * @param region the region to leave
+ */
+ public void leaveRegion(RegionDelegate region) {
+ callbackMap.remove(region.getRegion(),region);
+ if(callbackMap.isEmpty(region.getRegion()))
+ regionManager.leaveRegion(region.getRegion(),this);
+ }
+
+ /**
+ * Registers the RegionAPI functions in the root package of Pnuts.
+ * Only the joinRegion function is defined yet!
+ * @param scriptAPI the ScriptAPI from the scriptmanager
+ */
+ public void registerAPI(PackageHandler packageHandler) {
+ Package chatPackage = packageHandler.getRootPackage();
+ // Root package for now
+
+ PackageHandler.addFunction(chatPackage, "joinRegion",
+ new PnutsFunction() {
+
+ @Override
+ public boolean defined(int narg) {
+ return narg == 3;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return joinRegion(context, (Region) args[0], (PnutsFunction) args[1], (PnutsFunction) args[2]);
+
+ }
+ });
+
+ PackageHandler.addFunction(chatPackage, "loadGame",
+ new PnutsFunction() {
+
+ @Override
+ public boolean defined(int narg) {
+ return narg == 1;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ loadGame(context, (String)args[0]);
+ return null;
+ }
+ });
+
+ }
+
+ public void loadGame(Context context,String game){
+ GameDescriptor gameDescriptor=GameDescriptor.getGameDescriptor(game);
+
+ PackageHandler packageHandler=new PackageHandler(context.getCurrentPackage());
+ ObjectAPI objectAPI=new ObjectAPI(gameDescriptor);
+ objectAPI.registerAPI(packageHandler);
+
+ }
+
+ /**
+ * Executes the given gameAction
+ * @param gameAction what do you want to do today
+ */
+ public void sendAction(GameAction gameAction){
+ regionManager.scheduleInteraction(this,gameAction);
+ }
+
+ /* (non-Javadoc)
+ * @see de.upb.mmog.net.region.Client#handleGameStateChanged(java.lang.String, de.upb.mmog.net.region.GameState)
+ */
+ public void handleGameStateChanged(Region region, GameState gameState) {
+ Iterator<RegionDelegate> iterator=callbackMap.clonedIterator(region);
+ while (iterator.hasNext()) {
+ RegionDelegate region2 = iterator.next();
+
+ region2.gameStateReceived(gameState);
+ }
+ }
+
+ public void handleRegionChange(Region oldRegion, Region newRegion, Object gameObject) {
+ Iterator<RegionDelegate> iterator=callbackMap.clonedIterator(oldRegion);
+ while (iterator.hasNext()) {
+ RegionDelegate region2 = iterator.next();
+
+ region2.regionChangeReceived(newRegion, (GameObject)gameObject);
+ }
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeAPI.java 2006-12-27 18:09:23 UTC (rev 15)
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.region;
+
+import org.p2play.net.region.Region;
+import org.p2play.net.region.RegionManager;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+import org.p2play.scripting.pnuts.object.GameObject;
+
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+
+public class RegionChangeAPI {
+ private RegionManager regionManager;
+
+ public RegionChangeAPI(RegionManager regionManager) {
+ super();
+ this.regionManager = regionManager;
+ }
+
+ public RegionChangeAPI() {
+ this(null);
+ }
+
+ public void setRegionManager(RegionManager regionManager) {
+ this.regionManager = regionManager;
+ }
+
+ /**
+ * Registers the RegionAPI functions in the root package of Pnuts. Only the
+ * joinRegion function is defined yet!
+ *
+ * @param scriptAPI
+ * the ScriptAPI from the scriptmanager
+ */
+ public void registerAPI(final Region region, PackageHandler packageHandler) {
+ if (regionManager == null)
+ throw new IllegalStateException("No region manager was set!");
+
+ Package chatPackage = packageHandler.getRootPackage();
+ // Root package for now
+
+ PackageHandler.addFunction(chatPackage, "changeRegion",
+ new PnutsFunction() {
+
+ @Override
+ public boolean defined(int narg) {
+ return narg == 2;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ changeRegion(context, region, (Region) args[0],
+ (GameObject) args[1]);
+ return null;
+ }
+ });
+
+ }
+
+ protected void changeRegion(Context context, Region srcRegion,
+ Region trgRegion, GameObject object) {
+ regionManager.scheduleInteraction(null, new RegionChangeGameAction(
+ srcRegion, trgRegion, object));
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeGameAction.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeGameAction.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionChangeGameAction.java 2006-12-27 18:09:23 UTC (rev 15)
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.region;
+
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.Region;
+import org.p2play.scripting.pnuts.object.GameObject;
+
+
+public class RegionChangeGameAction implements GameAction {
+ private GameObject gameObject;
+
+ private Region sourceRegion;
+
+ private Region region;
+
+ public RegionChangeGameAction(Region sourceRegion, Region region,
+ GameObject gameObject) {
+ super();
+ this.sourceRegion = sourceRegion;
+ this.region = region;
+ this.gameObject = gameObject;
+ }
+
+ public Region getRegion() {
+ return region;
+ }
+
+ public GameObject getGameObject() {
+ return gameObject;
+ }
+
+ public Region getSourceRegion() {
+ return sourceRegion;
+ }
+
+ public long getGameTime() {
+ return 0;
+ }
+
+ public boolean sendToAll() {
+ return true;
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionDelegate.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionDelegate.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/region/RegionDelegate.java 2006-12-27 18:09:23 UTC (rev 15)
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.region;
+
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.Region;
+import org.p2play.scripting.pnuts.object.GameObject;
+
+import pnuts.lang.Context;
+import pnuts.lang.PnutsFunction;
+
+public class RegionDelegate {
+ private Region region;
+ private RegionAPI regionAPI;
+ private Context context;
+ private PnutsFunction receivedFunction;
+ private PnutsFunction changeFunction;
+
+ public RegionDelegate( RegionAPI regionAPI, Context context, Region regionName, PnutsFunction receivedFunction, PnutsFunction changeFunction) {
+ this.regionAPI=regionAPI;
+ this.region=regionName;
+ this.context=context;
+ this.receivedFunction=receivedFunction;
+ this.changeFunction=changeFunction;
+ }
+
+ public void sendGameAction(GameAction action){
+ regionAPI.sendAction(action);
+ }
+
+ public void leaveRegion(){
+ regionAPI.leaveRegion(this);
+ }
+
+ public Region getRegion() {
+ return region;
+ }
+
+ void gameStateReceived(GameState gameState){
+ receivedFunction.call(new Object[]{this,gameState},context);
+ }
+
+ void regionChangeReceived(Region newRegion, GameObject object){
+ RegionDelegate newRegionDelegate=new RegionDelegate(regionAPI,context,newRegion,receivedFunction,changeFunction);
+ if((Boolean)changeFunction.call(new Object[]{this,newRegionDelegate,object}, context))
+ regionAPI.joinRegion(context, newRegionDelegate);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|