Loading & Saving support for MongoDB

This commit is contained in:
simon
2018-01-02 17:45:58 -05:00
parent e2763faeee
commit 4e76d57ef9
43 changed files with 702 additions and 412 deletions

View File

@@ -1,5 +1,8 @@
package net.simon987.npcplugin;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
@@ -117,14 +120,34 @@ public class Factory extends GameObject implements Updatable {
return json;
}
public static Factory deserialise(JSONObject json) {
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
BasicDBList tmpNpcArray = new BasicDBList();
for (NonPlayerCharacter npc : npcs) {
tmpNpcArray.add(npc.getObjectId());
}
dbObject.put("n", tmpNpcArray);
return dbObject;
}
public static Factory deserialise(DBObject obj) {
Factory factory = new Factory();
factory.setObjectId((long) json.get("i"));
factory.setX((int) (long) json.get("x"));
factory.setY((int) (long) json.get("y"));
factory.setObjectId((long) obj.get("i"));
factory.setX((int) obj.get("x"));
factory.setY((int) obj.get("y"));
factory.tmpNpcArray = ((JSONArray) json.get("n")).toArray();
factory.tmpNpcArray = ((BasicDBList) obj.get("n")).toArray();
return factory;
}

View File

@@ -1,5 +1,7 @@
package net.simon987.npcplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.game.Direction;
import org.json.simple.JSONObject;
@@ -58,16 +60,32 @@ public class HarvesterNPC extends NonPlayerCharacter {
return json;
}
public static HarvesterNPC deserialize(JSONObject json) {
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("direction", getDirection().ordinal());
dbObject.put("hp", getHp());
// dbObject.put("energy", energy);
dbObject.put("action", getAction().ordinal());
dbObject.put("t", ID);
return dbObject;
}
public static HarvesterNPC deserialize(DBObject obj) {
HarvesterNPC npc = new HarvesterNPC();
npc.setObjectId((long) json.get("i"));
npc.setX((int) (long) json.get("x"));
npc.setY((int) (long) json.get("y"));
npc.setHp((int) (long) json.get("hp"));
npc.setDirection(Direction.getDirection((int) (long) json.get("direction")));
npc.energy = (int) (long) json.get("energy");
npc.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
npc.setObjectId((long) obj.get("i"));
npc.setX((int) obj.get("x"));
npc.setY((int) obj.get("y"));
npc.setHp((int) obj.get("hp"));
npc.setDirection(Direction.getDirection((int) obj.get("direction")));
// npc.energy = (int) obj.get("energy");
// npc.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
return npc;

View File

@@ -1,5 +1,6 @@
package net.simon987.npcplugin;
import com.mongodb.DBObject;
import net.simon987.npcplugin.event.CpuInitialisationListener;
import net.simon987.npcplugin.event.WorldCreationListener;
import net.simon987.server.ServerConfiguration;
@@ -9,7 +10,6 @@ import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.json.simple.JSONObject;
import java.util.ArrayList;
@@ -32,28 +32,28 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C
}
@Override
public GameObject deserializeObject(JSONObject json) {
public GameObject deserializeObject(DBObject obj) {
int objType = (int) (long) json.get("t");
int objType = (int) obj.get("t");
if (objType == HarvesterNPC.ID) {
return HarvesterNPC.deserialize(json);
return HarvesterNPC.deserialize(obj);
} else if (objType == Factory.ID) {
return Factory.deserialise(json);
return Factory.deserialise(obj);
} else if (objType == RadioTower.ID) {
return RadioTower.deserialize(json);
return RadioTower.deserialize(obj);
}
return null;
}
@Override
public CpuHardware deserializeHardware(JSONObject hwJson) {
int hwid = (int) (long) hwJson.get("hwid");
public CpuHardware deserializeHardware(DBObject obj) {
int hwid = (int) obj.get("hwid");
switch (hwid) {
case RadioReceiverHardware.HWID:
return RadioReceiverHardware.deserialize(hwJson);
return RadioReceiverHardware.deserialize(obj);
}
return null;

View File

@@ -1,12 +1,13 @@
package net.simon987.npcplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.Action;
import net.simon987.server.game.ControllableUnit;
import org.json.simple.JSONObject;
import java.util.ArrayList;
@@ -64,16 +65,19 @@ public class RadioReceiverHardware extends CpuHardware {
return HWID;
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
return json;
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static RadioReceiverHardware deserialize(JSONObject json) {
return new RadioReceiverHardware((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) json.get("cubot")));
public static RadioReceiverHardware deserialize(DBObject obj) {
return new RadioReceiverHardware((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,5 +1,7 @@
package net.simon987.npcplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Programmable;
import net.simon987.server.game.Updatable;
@@ -65,12 +67,24 @@ public class RadioTower extends GameObject implements Programmable, Updatable {
}
public static RadioTower deserialize(JSONObject json) {
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
return dbObject;
}
public static RadioTower deserialize(DBObject obj) {
RadioTower tower = new RadioTower();
tower.setObjectId((long) json.get("i"));
tower.setX((int) (long) json.get("x"));
tower.setY((int) (long) json.get("y"));
tower.setObjectId((long) obj.get("i"));
tower.setX((int) obj.get("x"));
tower.setY((int) obj.get("y"));
NpcPlugin.getRadioTowers().add(tower);