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

@@ -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",