Changed unreliable sequential integer object id to BSON ObjectId #162

This commit is contained in:
Simon 2018-06-04 14:53:20 -04:00
parent df9c466827
commit a9cc9662f4
30 changed files with 70 additions and 90 deletions

View File

@ -23,7 +23,7 @@ public class ChargeShieldCommandListener implements GameEventListener {
if (e.getName().equals("chargeShield")) {
GameObject cubot = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject cubot = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (cubot != null) {

View File

@ -22,7 +22,7 @@ public class PopItemCommandListener implements GameEventListener {
if (e.getName().equals("popItem")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -24,7 +24,7 @@ public class PutItemCommandListener implements GameEventListener {
if (e.getName().equals("putItem")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -22,7 +22,7 @@ public class SetInventoryPosition implements GameEventListener {
if (e.getName().equals("setInventoryPosition")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -13,6 +13,7 @@ import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.UserCreationEvent;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.Random;
@ -31,7 +32,7 @@ public class UserCreationListener implements GameEventListener {
User user = (User) event.getSource();
Cubot cubot = new Cubot();
cubot.addStatus(CubotStatus.FACTORY_NEW);
cubot.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
cubot.setObjectId(new ObjectId());
ServerConfiguration config = GameServer.INSTANCE.getConfig();
Point point = null;

View File

@ -4,6 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.game.objects.Structure;
import net.simon987.server.game.objects.Updatable;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.ArrayList;
@ -75,7 +76,7 @@ public class Factory extends Structure implements Updatable {
for (Object id : tmpNpcArray) {
NonPlayerCharacter npc = (NonPlayerCharacter) GameServer.INSTANCE.getGameUniverse().getObject((long) id);
NonPlayerCharacter npc = (NonPlayerCharacter) GameServer.INSTANCE.getGameUniverse().getObject((ObjectId) id);
if (npc != null) {
npc.setFactory(this);
@ -94,7 +95,7 @@ public class Factory extends Structure implements Updatable {
if (p != null) {
NonPlayerCharacter npc = new HarvesterNPC();
npc.setWorld(getWorld());
npc.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
npc.setObjectId(new ObjectId());
npc.setX(p.x);
npc.setY(p.y);
getWorld().addObject(npc);
@ -117,7 +118,7 @@ public class Factory extends Structure implements Updatable {
public Document mongoSerialise() {
Document dbObject = super.mongoSerialise();
List<Long> tmpNpcArray = new ArrayList<>(npcs.size());
List<ObjectId> tmpNpcArray = new ArrayList<>(npcs.size());
for (NonPlayerCharacter npc : npcs) {
tmpNpcArray.add(npc.getObjectId());

View File

@ -7,6 +7,7 @@ import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.ArrayList;
@ -148,6 +149,7 @@ public class VaultDimension {
exitPortal.setX(exitPortalPt.x);
exitPortal.setY(exitPortalPt.y);
exitPortal.setWorld(objectiveWorld);
exitPortal.setObjectId(new ObjectId());
objectiveWorld.addObject(exitPortal);
LogManager.LOGGER.severe("Objective: " + objectiveWorld.getId());
@ -165,6 +167,7 @@ public class VaultDimension {
homePortal.setX(homePortalPt.x);
homePortal.setY(homePortalPt.y);
homePortal.setWorld(homeWorld);
homePortal.setObjectId(new ObjectId());
homeWorld.addObject(homePortal);
Point entryCoords = homePortal.getAdjacentTile();

View File

@ -113,7 +113,7 @@ public class VaultDoor extends Structure implements MessageReceiver, Enterable,
@Override
public boolean enter(GameObject object) {
LogManager.LOGGER.fine("VAULT enter " + open);
// LogManager.LOGGER.fine("VAULT enter " + open);
if (open) {

View File

@ -1,8 +1,8 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.ArrayList;
@ -60,7 +60,7 @@ public class VaultWorldUtils {
}
ElectricBox box = new ElectricBox();
box.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
box.setObjectId(new ObjectId());
box.setX(p.x);
box.setY(p.y);
box.setWorld(world);

View File

@ -4,13 +4,13 @@ import net.simon987.npcplugin.Factory;
import net.simon987.npcplugin.NpcPlugin;
import net.simon987.npcplugin.RadioTower;
import net.simon987.npcplugin.VaultDoor;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldGenerationEvent;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.Random;
@ -50,7 +50,7 @@ public class WorldCreationListener implements GameEventListener {
Factory factory = new Factory();
factory.setWorld(world);
factory.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
factory.setObjectId(new ObjectId());
factory.setX(x);
factory.setY(y);
@ -84,7 +84,7 @@ public class WorldCreationListener implements GameEventListener {
RadioTower radioTower = new RadioTower();
radioTower.setWorld(world);
radioTower.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
radioTower.setObjectId(new ObjectId());
radioTower.setX(p.x);
radioTower.setY(p.y);
@ -128,7 +128,7 @@ public class WorldCreationListener implements GameEventListener {
}
}
vaultDoor.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
vaultDoor.setObjectId(new ObjectId());
world.addObject(vaultDoor);
world.incUpdatable(); //In case the Factory & Radio Tower couldn't be spawned.
vaultDoor.setWorld(world);

View File

@ -1,9 +1,9 @@
package net.simon987.biomassplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.ArrayList;
@ -63,7 +63,7 @@ public class WorldUtils {
}
BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
biomassBlob.setObjectId(new ObjectId());
biomassBlob.setBiomassCount(yield);
biomassBlob.setX(p.x);
biomassBlob.setY(p.y);

View File

@ -1,7 +1,6 @@
package net.simon987.biomassplugin.event;
import net.simon987.biomassplugin.BiomassBlob;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
@ -9,6 +8,7 @@ import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.bson.types.ObjectId;
/**
* Handles ObjectDeathEvent events
@ -58,7 +58,7 @@ public class ObjectDeathListener implements GameEventListener {
private BiomassBlob createBiomassBlobAt(int x, int y, World world) {
BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
biomassBlob.setObjectId(new ObjectId());
biomassBlob.setBiomassCount(biomassDropCount);
biomassBlob.setX(x);
biomassBlob.setY(y);

View File

@ -234,7 +234,6 @@ public class GameServer implements Runnable {
if (cursor.hasNext()) {
Document serverObj = cursor.next();
gameUniverse.setTime((long) serverObj.get("time"));
gameUniverse.setNextObjectId((long) serverObj.get("nextObjectId"));
}
LogManager.LOGGER.info("Done loading! W:" + GameServer.INSTANCE.getGameUniverse().getWorldCount() +
@ -276,7 +275,6 @@ public class GameServer implements Runnable {
Document serverObj = new Document();
serverObj.put("time", gameUniverse.getTime());
serverObj.put("nextObjectId", gameUniverse.getNextObjectId());
//A constant id ensures only one entry is kept and updated, instead of a new entry created every save.
server.replaceOne(new Document("_id", "serverinfo"), serverObj, updateOptions);

View File

@ -1,6 +1,7 @@
package net.simon987.server.event;
import net.simon987.server.websocket.OnlineUser;
import org.bson.types.ObjectId;
import org.json.simple.JSONObject;
import java.io.IOException;
@ -30,6 +31,10 @@ public class DebugCommandEvent extends GameEvent {
return (long) command.get(key);
}
public ObjectId getObjectId(String key) {
return (ObjectId) command.get(key);
}
/**
* Send back a response to the command issuer
*/

View File

@ -13,10 +13,10 @@ import net.simon987.server.game.world.WorldGenerator;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
public class GameUniverse {
@ -31,8 +31,6 @@ public class GameUniverse {
private long time;
private AtomicLong nextObjectId = new AtomicLong(0);
private int maxWidth = 0xFFFF;
public GameUniverse(ServerConfiguration config) {
@ -216,7 +214,7 @@ public class GameUniverse {
* @param id id of the game object
* @return GameObject, null if not found
*/
public GameObject getObject(long id) {
public GameObject getObject(ObjectId id) {
for (World world : getWorlds()) {
GameObject obj = world.findObject(id);
@ -251,10 +249,6 @@ public class GameUniverse {
return users.size();
}
public long getNextObjectId() {
return nextObjectId.getAndIncrement();
}
public String getGuestUsername() {
int i = 1;
@ -287,7 +281,4 @@ public class GameUniverse {
this.time = time;
}
public void setNextObjectId(long nextObjectId) {
this.nextObjectId.set(nextObjectId);
}
}

View File

@ -6,6 +6,7 @@ import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.MessageReceiver;
import org.bson.types.ObjectId;
public class ComPortMsgCommandListener implements GameEventListener {
@ -21,7 +22,7 @@ public class ComPortMsgCommandListener implements GameEventListener {
if (e.getName().equals("comPortMsg")) {
long objectId = e.getLong("objectId");
ObjectId objectId = e.getObjectId("objectId");
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(objectId);

View File

@ -22,7 +22,7 @@ public class DamageObjCommandListener implements GameEventListener {
if (e.getName().equals("damageObj")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -22,7 +22,7 @@ public class HealObjCommandListener implements GameEventListener {
if (e.getName().equals("healObj")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -20,7 +20,7 @@ public class MoveObjCommandListener implements GameEventListener {
if (e.getName().equals("moveObj")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -21,7 +21,7 @@ public class SetEnergyCommandListener implements GameEventListener {
if (e.getName().equals("setEnergy")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
if (object != null) {

View File

@ -7,6 +7,7 @@ import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.Arrays;
@ -28,7 +29,7 @@ public class SpawnObjCommandListener implements GameEventListener {
false, e.getString("dimension"));
Document dbObj = Document.parse(e.getString("data"));
dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId());
dbObj.put("id", new ObjectId());
GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject(dbObj);

View File

@ -23,7 +23,7 @@ public class TpObjectCommandListener implements GameEventListener {
if (e.getName().equals("tpObj")) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId"));
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getObjectId("objectId"));
World world = GameServer.INSTANCE.getGameUniverse().getWorld(e.getInt("worldX"), e.getInt("worldY"),
false, e.getString("dimension"));

View File

@ -5,12 +5,13 @@ import net.simon987.server.assembly.Memory;
import net.simon987.server.game.item.Item;
import net.simon987.server.game.world.World;
import net.simon987.server.user.User;
import org.bson.types.ObjectId;
import java.util.ArrayList;
public interface ControllableUnit {
long getObjectId();
ObjectId getObjectId();
void setKeyboardBuffer(ArrayList<Integer> kbBuffer);

View File

@ -5,6 +5,7 @@ import net.simon987.server.game.world.World;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.json.simple.JSONObject;
import java.awt.*;
@ -20,7 +21,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
/**
* Object's unique identifier
*/
private long objectId;
private ObjectId objectId;
/**
* X coordinate of the object in its World
@ -47,7 +48,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
}
public GameObject(Document document) {
objectId = document.getLong("id");
objectId = document.getObjectId("id");
x = document.getInteger("x");
y = document.getInteger("y");
}
@ -183,11 +184,11 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
return count;
}
public long getObjectId() {
public ObjectId getObjectId() {
return objectId;
}
public void setObjectId(long objectId) {
public void setObjectId(ObjectId objectId) {
this.objectId = objectId;
}
@ -226,7 +227,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
@Override
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
json.put("i", getObjectId());
json.put("i", getObjectId().toHexString());
json.put("t", getClass().getCanonicalName());
json.put("x", getX());
json.put("y", getY());

View File

@ -9,6 +9,7 @@ import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.awt.*;
import java.util.ArrayList;
@ -35,7 +36,7 @@ public class World implements MongoSerializable {
private String dimension;
private ConcurrentHashMap<Long, GameObject> gameObjects = new ConcurrentHashMap<>(8);
private ConcurrentHashMap<ObjectId, GameObject> gameObjects = new ConcurrentHashMap<>(8);
/**
* If this number is greater than 0, the World will be updated.
@ -152,12 +153,10 @@ public class World implements MongoSerializable {
gameObjects.remove(object.getObjectId());
}
public GameObject findObject(long objectId) {
public GameObject findObject(ObjectId objectId) {
return gameObjects.get(objectId);
}
/**
* Update this World and its GameObjects
* <br>

View File

@ -59,7 +59,7 @@ public class User implements MongoSerializable {
public static User deserialize(Document obj) throws CancelledException {
User user = new User((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("controlledUnit")));
User user = new User((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject(obj.getObjectId("controlledUnit")));
user.getControlledUnit().setParent(user);
user.username = (String) obj.get("username");
user.userCode = (String) obj.get("code");

View File

@ -291,7 +291,7 @@ var config = {
};
},
kbBufferX: 350,
kbBufferY: 20,
kbBufferY: 35,
arrowTextStyle: {
fontSize: 32,
fill: "#ffffff",
@ -735,24 +735,14 @@ var GameClient = (function () {
self.listeners.push(new DebugResponseListener());
self.socket.onmessage = function (received) {
var message;
try {
message = JSON.parse(received.data);
if (DEBUG) {
console.log("[MAR] Received: " + received.data);
}
for (var i = 0; i < self.listeners.length; i++) {
if (self.listeners[i].getListenedMessageType() === message.t) {
self.listeners[i].handle(message);
}
}
if (DEBUG) {
console.log("[MAR] Received: " + received.data);
}
catch (e) {
if (DEBUG) {
console.log("[MAR] Received invalid message, assuming floppy data");
message = JSON.parse(received.data);
for (var i = 0; i < self.listeners.length; i++) {
if (self.listeners[i].getListenedMessageType() === message.t) {
self.listeners[i].handle(message);
}
document.getElementById("floppyDown").innerHTML = "<i class=\"fa fa-long-arrow-down\" aria-hidden=\"true\"></i> <i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i>";
var blob = new Blob([received.data], { type: "application/octet-stream" });
saveAs(blob, "floppy.bin");
}
};
self.reloadCode();

View File

@ -84,7 +84,7 @@
</div>
<div class="col-sm-2">
<button class="btn btn-shadow btn-info text-mono regular-screen"
<button id="floppyDown" class="btn btn-shadow btn-info text-mono regular-screen"
onclick="window.location.assign('floppy_download')"><i class="mi">file_download</i>
Floppy
</button>

View File

@ -412,30 +412,18 @@ class GameClient {
let message;
try {
message = JSON.parse(received.data);
if (DEBUG) {
console.log("[MAR] Received: " + received.data)
}
for (let i = 0; i < self.listeners.length; i++) {
if (self.listeners[i].getListenedMessageType() === message.t) {
self.listeners[i].handle(message)
}
}
} catch (e) {
if (DEBUG) {
console.log("[MAR] Received invalid message, assuming floppy data");
}
document.getElementById("floppyDown").innerHTML = "<i class=\"fa fa-long-arrow-down\" aria-hidden=\"true\"></i> <i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i>";
let blob = new Blob([received.data], {type: "application/octet-stream"});
saveAs(blob, "floppy.bin");
if (DEBUG) {
console.log("[MAR] Received: " + received.data)
}
message = JSON.parse(received.data);
for (let i = 0; i < self.listeners.length; i++) {
if (self.listeners[i].getListenedMessageType() === message.t) {
self.listeners[i].handle(message)
}
}
};
self.reloadCode();

View File

@ -40,7 +40,7 @@ let config = {
}
},
kbBufferX: 350, ///Position of the keyboard buffer fill on screen
kbBufferY: 20,
kbBufferY: 35,
arrowTextStyle: {
fontSize: 32,
fill: "#ffffff",