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,7 @@
package net.simon987.cubotplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.Memory;
import net.simon987.server.game.*;
@@ -114,16 +116,41 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
return json;
}
public static Cubot deserialize(JSONObject json) {
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("i", getObjectId());
dbObject.put("t", ID);
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("direction", getDirection().ordinal());
dbObject.put("heldItem", heldItem);
dbObject.put("hp", hp);
dbObject.put("action", lastAction.ordinal());
dbObject.put("holo", hologram);
dbObject.put("holoStr", hologramString);
dbObject.put("holoMode", lastHologramMode.ordinal());
dbObject.put("holoC", hologramColor);
dbObject.put("energy", energy);
if (parent != null) {
dbObject.put("parent", parent.getUsername()); //Only used client-side for now
}
return dbObject;
}
public static Cubot deserialize(DBObject obj) {
Cubot cubot = new Cubot();
cubot.setObjectId((long) json.get("i"));
cubot.setX((int) (long) json.get("x"));
cubot.setY((int) (long) json.get("y"));
cubot.hp = (int) (long) json.get("hp");
cubot.setDirection(Direction.getDirection((int) (long) json.get("direction")));
cubot.heldItem = (int) (long) json.get("heldItem");
cubot.energy = (int) (long) json.get("energy");
cubot.setObjectId((long) obj.get("i"));
cubot.setX((int) obj.get("x"));
cubot.setY((int) obj.get("y"));
cubot.hp = (int) obj.get("hp");
cubot.setDirection(Direction.getDirection((int) obj.get("direction")));
cubot.heldItem = (int) obj.get("heldItem");
cubot.energy = (int) obj.get("energy");
cubot.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
return cubot;

View File

@@ -1,9 +1,10 @@
package net.simon987.cubotplugin;
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 org.json.simple.JSONObject;
public class CubotBattery extends CpuHardware {
@@ -44,16 +45,19 @@ public class CubotBattery extends CpuHardware {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
public BasicDBObject mongoSerialise() {
return json;
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotBattery deserialize(JSONObject hwJSON) {
return new CubotBattery((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotBattery deserialize(DBObject obj) {
return new CubotBattery((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,11 +1,12 @@
package net.simon987.cubotplugin;
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.game.GameObject;
import net.simon987.server.game.Programmable;
import org.json.simple.JSONObject;
import java.awt.*;
import java.util.ArrayList;
@@ -123,15 +124,17 @@ public class CubotComPort extends CpuHardware {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
public BasicDBObject mongoSerialise() {
return json;
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotComPort deserialize(JSONObject json) {
return new CubotComPort((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) json.get("cubot")));
public static CubotComPort deserialize(DBObject obj) {
return new CubotComPort((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,11 +1,12 @@
package net.simon987.cubotplugin;
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.game.Action;
import net.simon987.server.game.TileMap;
import org.json.simple.JSONObject;
public class CubotDrill extends CpuHardware {
@@ -62,15 +63,17 @@ public class CubotDrill extends CpuHardware {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
public BasicDBObject mongoSerialise() {
return json;
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotDrill deserialize(JSONObject hwJSON) {
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotDrill deserialize(DBObject obj) {
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,9 +1,10 @@
package net.simon987.cubotplugin;
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 org.json.simple.JSONObject;
public class CubotFloppyDrive extends CpuHardware {
@@ -78,24 +79,26 @@ public class CubotFloppyDrive extends CpuHardware {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
if (floppyDisk != null) {
json.put("floppy", floppyDisk.serialise());
dbObject.put("floppy", floppyDisk.mongoSerialise());
}
return json;
return dbObject;
}
public static CubotFloppyDrive deserialize(JSONObject hwJSON) {
public static CubotFloppyDrive deserialize(DBObject obj) {
CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
if (hwJSON.containsKey("floppy")) {
drive.floppyDisk = FloppyDisk.deserialise((JSONObject) hwJSON.get("floppy"));
if (obj.containsField("floppy")) {
drive.floppyDisk = FloppyDisk.deserialise((DBObject) obj.get("floppy"));
}
return drive;

View File

@@ -1,9 +1,10 @@
package net.simon987.cubotplugin;
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 org.json.simple.JSONObject;
public class CubotHologram extends CpuHardware {
@@ -82,16 +83,19 @@ public class CubotHologram extends CpuHardware {
return HWID;
}
public static CubotHologram deserialize(JSONObject hwJSON) {
return new CubotHologram((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotHologram deserialize(DBObject obj) {
return new CubotHologram((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
public BasicDBObject mongoSerialise() {
return json;
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
}

View File

@@ -1,9 +1,10 @@
package net.simon987.cubotplugin;
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 org.json.simple.JSONObject;
public class CubotInventory extends CpuHardware {
@@ -49,17 +50,19 @@ public class CubotInventory extends CpuHardware {
}
@Override
public JSONObject serialise() {
public BasicDBObject mongoSerialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
BasicDBObject dbObject = new BasicDBObject();
return json;
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotInventory deserialize(JSONObject hwJSON) {
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotInventory deserialize(DBObject obj) {
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,9 +1,10 @@
package net.simon987.cubotplugin;
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 org.json.simple.JSONObject;
public class CubotKeyboard extends CpuHardware {
@@ -52,16 +53,17 @@ public class CubotKeyboard extends CpuHardware {
}
@Override
public JSONObject serialise() {
public BasicDBObject mongoSerialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
BasicDBObject dbObject = new BasicDBObject();
return json;
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotKeyboard deserialize(JSONObject hwJSON) {
return new CubotKeyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotKeyboard deserialize(DBObject obj) {
return new CubotKeyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,5 +1,7 @@
package net.simon987.cubotplugin;
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;
@@ -7,7 +9,6 @@ import net.simon987.server.game.Action;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import org.json.simple.JSONObject;
import java.awt.*;
import java.util.ArrayList;
@@ -89,16 +90,17 @@ public class CubotLaser extends CpuHardware {
}
@Override
public JSONObject serialise() {
public BasicDBObject mongoSerialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
BasicDBObject dbObject = new BasicDBObject();
return json;
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLaser deserialize(JSONObject hwJSON) {
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
public static CubotLaser deserialize(DBObject obj) {
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,5 +1,7 @@
package net.simon987.cubotplugin;
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;
@@ -80,8 +82,19 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
return json;
}
public static CubotLeg deserialize(JSONObject hwJSON) {
return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLeg deserialize(DBObject obj) {
return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}

View File

@@ -1,5 +1,7 @@
package net.simon987.cubotplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Memory;
@@ -141,7 +143,18 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
return json;
}
public static CubotLidar deserialize(JSONObject hwJSON) {
return new CubotLidar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLidar deserialize(DBObject obj) {
return new CubotLidar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@@ -1,5 +1,6 @@
package net.simon987.cubotplugin;
import com.mongodb.DBObject;
import net.simon987.cubotplugin.event.CpuInitialisationListener;
import net.simon987.cubotplugin.event.UserCreationListener;
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;
public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer {
@@ -23,9 +23,9 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
}
@Override
public GameObject deserializeObject(JSONObject object) {
public GameObject deserializeObject(DBObject object) {
int objType = (int) (long) object.get("t");
int objType = (int) object.get("t");
if (objType == Cubot.ID) {
@@ -36,30 +36,30 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
}
@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 CubotLeg.HWID:
return CubotLeg.deserialize(hwJson);
return CubotLeg.deserialize(obj);
case CubotLaser.HWID:
return CubotLaser.deserialize(hwJson);
return CubotLaser.deserialize(obj);
case CubotLidar.HWID:
return CubotLidar.deserialize(hwJson);
return CubotLidar.deserialize(obj);
case CubotDrill.HWID:
return CubotDrill.deserialize(hwJson);
return CubotDrill.deserialize(obj);
case CubotInventory.HWID:
return CubotInventory.deserialize(hwJson);
return CubotInventory.deserialize(obj);
case CubotKeyboard.HWID:
return CubotKeyboard.deserialize(hwJson);
return CubotKeyboard.deserialize(obj);
case CubotHologram.HWID:
return CubotHologram.deserialize(hwJson);
return CubotHologram.deserialize(obj);
case CubotBattery.HWID:
return CubotBattery.deserialize(hwJson);
return CubotBattery.deserialize(obj);
case CubotFloppyDrive.HWID:
return CubotFloppyDrive.deserialize(hwJson);
return CubotFloppyDrive.deserialize(obj);
case CubotComPort.HWID:
return CubotComPort.deserialize(hwJson);
return CubotComPort.deserialize(obj);
}
return null;

View File

@@ -1,16 +1,17 @@
package net.simon987.cubotplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.assembly.Memory;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
import net.simon987.server.io.MongoSerialisable;
/**
* Represents a floppy disk that is inside a floppy drive.
* Floppies contains 80 tracks with 18 sectors per track.
* That's 1440 sectors of 512 words. (total 1,474,560 bytes / 737,280 words / 1.44Mb)
*/
public class FloppyDisk implements JSONSerialisable {
public class FloppyDisk implements MongoSerialisable {
/**
* Contents of the disk
@@ -82,23 +83,22 @@ public class FloppyDisk implements JSONSerialisable {
}
}
@Override
public JSONObject serialise() {
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
JSONObject json = new JSONObject();
json.put("rwHeadTrack", rwHeadTrack);
json.put("memory", memory.serialise());
dbObject.put("rwHeadTrack", rwHeadTrack);
dbObject.put("memory", memory.mongoSerialise());
return json;
return dbObject;
}
public static FloppyDisk deserialise(JSONObject json) {
public static FloppyDisk deserialise(DBObject obj) {
FloppyDisk floppyDisk = new FloppyDisk();
floppyDisk.rwHeadTrack = (int) (long) json.get("rwHeadTrack");
floppyDisk.memory = Memory.deserialize((JSONObject) json.get("memory"));
floppyDisk.rwHeadTrack = (int) obj.get("rwHeadTrack");
floppyDisk.memory = Memory.deserialize((DBObject) obj.get("memory"));
return floppyDisk;
}

View File

@@ -27,7 +27,7 @@ public class UserCreationListener implements GameEventListener {
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(
GameServer.INSTANCE.getConfig().getInt("new_user_worldX"),
GameServer.INSTANCE.getConfig().getInt("new_user_worldY")));
GameServer.INSTANCE.getConfig().getInt("new_user_worldY"), true));
cubot.getWorld().getGameObjects().add(cubot);
cubot.getWorld().incUpdatable();