[P2play-commit] SF.net SVN: p2play: [16] trunk/P2Play/src/org/p2play/scripting/pnuts
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-27 18:19:50
|
Revision: 16
http://p2play.svn.sourceforge.net/p2play/?rev=16&view=rev
Author: tisoft
Date: 2006-12-27 10:19:51 -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/PnutsSerializer.java
trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServer.java
trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServerFactory.java
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsSerializer.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsSerializer.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsSerializer.java 2006-12-27 18:19:51 UTC (rev 16)
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+import java.io.IOException;
+
+import org.p2play.io.Serializer;
+import org.p2play.scripting.pnuts.object.GameObject;
+
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+
+public class PnutsSerializer implements Serializer {
+
+ public Object deserialize(InputBuffer inputBuffer) throws IOException {
+ return GameObject.deserializeObject(inputBuffer);
+ }
+
+ public void serialize(Object object, OutputBuffer outputBuffer)
+ throws IOException {
+ GameObject.serializeObject(outputBuffer, object);
+ }
+
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServer.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServer.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServer.java 2006-12-27 18:19:51 UTC (rev 16)
@@ -0,0 +1,156 @@
+/*
+ * 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;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+
+import org.p2play.net.region.GameAction;
+import org.p2play.net.region.GameState;
+import org.p2play.net.region.Region;
+import org.p2play.net.region.Server;
+import org.p2play.resource.ResourceClassLoader;
+import org.p2play.scripting.pnuts.api.object.ObjectAPI;
+import org.p2play.scripting.pnuts.api.region.RegionChangeAPI;
+import org.p2play.scripting.pnuts.api.region.RegionChangeGameAction;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+import org.p2play.scripting.pnuts.object.GameDescriptor;
+import org.p2play.scripting.pnuts.object.GameObject;
+import org.p2play.scripting.pnuts.object.GameStateObject;
+
+import pnuts.lang.Context;
+import pnuts.lang.Pnuts;
+import pnuts.lang.PnutsFunction;
+
+public class PnutsServer implements Server {
+
+ private Pnuts pnuts;
+
+ private Region region;
+
+ private PnutsHandler pnutsHandler;
+
+ private GameDescriptor gameDescriptor;
+
+ private Random random;
+
+ public PnutsServer(RegionChangeAPI regionChangeAPI, Region region) {
+ this.region = region;
+
+ System.out.println(region);
+
+ gameDescriptor = GameDescriptor.getGameDescriptor(region.getGame());
+
+ PackageHandler packageHandler = new PackageHandler();
+
+ regionChangeAPI.registerAPI(region, packageHandler);
+
+ ObjectAPI objectAPI = new ObjectAPI(gameDescriptor);
+ objectAPI.registerAPI(packageHandler);
+
+ // TODO: make script api
+ packageHandler.getRootPackage().set("random", new PnutsFunction(){@Override
+ public boolean defined(int narg) {
+ return narg==1;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return getRandom().nextInt((Integer)args[0]);
+ }});
+
+ // NutCracker.debug=true;
+
+ pnutsHandler = new PnutsHandler(packageHandler);
+
+ pnuts = pnutsHandler.getPnuts(ResourceClassLoader
+ .getResourceClassLoader().getResourceAsStream(
+ gameDescriptor.getServerScript()));
+
+ pnutsHandler.execute(pnuts);
+ }
+
+ public Random getRandom() {
+ return random;
+ }
+
+ public void assignGameObject(GameState gamestate, GameObject object,
+ Region region) {
+ synchronized (gamestate) {
+ pnutsHandler.callFunction("assignGameObject", gamestate, object,
+ region);
+ }
+ }
+
+ public Region getRegion() {
+ return region;
+ }
+
+ public List<Region> getNeighbourRegionIdentifiers() {
+ return gameDescriptor.getNeighbourRegionIdentifiers(region);
+ }
+
+ public boolean isEmpty(GameState gameState) {
+ return (Boolean) pnutsHandler.callFunction(gameDescriptor
+ .getEmptyFunction(), gameState);
+ }
+
+ public final GameState tick(GameState abstractGameState,
+ List<GameAction> interactions) {
+ synchronized (abstractGameState) {
+
+ random=abstractGameState.getRandom();
+
+ if (interactions == null) {
+ interactions = new LinkedList<GameAction>();
+ }
+ abstractGameState.tick();
+
+ Iterator iterator = interactions.iterator();
+ while (iterator.hasNext()) {
+ GameAction element = (GameAction) iterator.next();
+
+ if (element instanceof RegionChangeGameAction) {// TODO: darf
+ // die
+ // gameaction
+ // \x9Fberhaupt
+ // verarbeitet
+ // werden?
+ RegionChangeGameAction rcga = (RegionChangeGameAction) element;
+
+ assignGameObject(abstractGameState, rcga.getGameObject(),
+ rcga.getSourceRegion());
+
+ iterator.remove();
+ }
+ }
+
+ return (GameState) pnutsHandler.callFunction("tick",
+ abstractGameState, interactions);
+ }
+ }
+
+ public GameState init(long gameTime, long randomSeed) {
+ GameStateObject gameState = gameDescriptor.getGameStateClass()
+ .createInstance(region, gameTime);
+ return (GameState) pnutsHandler.callFunction("init", gameState);
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServerFactory.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServerFactory.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/PnutsServerFactory.java 2006-12-27 18:19:51 UTC (rev 16)
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+import org.p2play.net.region.Region;
+import org.p2play.net.region.Server;
+import org.p2play.net.region.ServerFactory;
+import org.p2play.scripting.pnuts.api.region.RegionChangeAPI;
+
+
+public class PnutsServerFactory implements ServerFactory {
+ private RegionChangeAPI regionChangeAPI;
+
+ public PnutsServerFactory(RegionChangeAPI regionChangeAPI) {
+ super();
+ this.regionChangeAPI = regionChangeAPI;
+ }
+
+ public Server getServer(Region region) {
+ System.out.println(region);
+ return new PnutsServer(regionChangeAPI, region);
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|