Updated mongodb driver to 3.7.0

This commit is contained in:
simon
2018-05-11 21:06:18 -04:00
parent e98575b23f
commit 854863ede9
57 changed files with 383 additions and 383 deletions

View File

@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
@@ -16,14 +21,14 @@
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:2.10.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.7.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:5.0.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.sparkjava:spark-core:2.7.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-server:9.4.8.v20171121" level="project" />

View File

@@ -107,12 +107,12 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.4.RELEASE</version>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>

View File

@@ -1,7 +1,11 @@
package net.simon987.server;
import com.mongodb.*;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import net.simon987.server.crypto.CryptoProvider;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher;
@@ -16,9 +20,9 @@ import net.simon987.server.user.User;
import net.simon987.server.user.UserManager;
import net.simon987.server.user.UserStatsHelper;
import net.simon987.server.websocket.SocketServer;
import org.bson.Document;
import java.io.File;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class GameServer implements Runnable {
@@ -49,17 +53,13 @@ public class GameServer implements Runnable {
this.config = new ServerConfiguration("config.properties");
try{
mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port"));
DB db = mongo.getDB(config.getString("mongo_dbname"));
mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port"));
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
DBCollection userCollection = db.getCollection("user");
MongoCollection<Document> userCollection = db.getCollection("user");
userManager = new UserManager(userCollection);
userStatsHelper = new UserStatsHelper(userCollection);
} catch (UnknownHostException e) {
e.printStackTrace();
}
userManager = new UserManager(userCollection);
userStatsHelper = new UserStatsHelper(userCollection);
gameUniverse = new GameUniverse(config);
gameUniverse.setMongo(mongo);
@@ -201,14 +201,14 @@ public class GameServer implements Runnable {
LogManager.LOGGER.info("Loading all data from MongoDB");
DB db = mongo.getDB(config.getString("mongo_dbname"));
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
DBCollection worlds = db.getCollection("world");
DBCollection server = db.getCollection("server");
MongoCollection<Document> worlds = db.getCollection("world");
MongoCollection<Document> server = db.getCollection("server");
BasicDBObject whereQuery = new BasicDBObject();
Document whereQuery = new Document();
whereQuery.put("shouldUpdate", true);
DBCursor cursor = worlds.find(whereQuery);
MongoCursor<Document> cursor = worlds.find(whereQuery).iterator();
GameUniverse universe = GameServer.INSTANCE.getGameUniverse();
while (cursor.hasNext()) {
World w = World.deserialize(cursor.next());
@@ -222,9 +222,9 @@ public class GameServer implements Runnable {
}
//Load misc server info
cursor = server.find();
cursor = server.find().iterator();
if (cursor.hasNext()) {
DBObject serverObj = cursor.next();
Document serverObj = cursor.next();
gameUniverse.setTime((long) serverObj.get("time"));
gameUniverse.setNextObjectId((long) serverObj.get("nextObjectId"));
}
@@ -237,19 +237,21 @@ public class GameServer implements Runnable {
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
try{
DB db = mongo.getDB(config.getString("mongo_dbname"));
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
UpdateOptions updateOptions = new UpdateOptions();
updateOptions.upsert(true);
int unloaded_worlds = 0;
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
MongoCollection<Document> worlds = db.getCollection("world");
MongoCollection<Document> users = db.getCollection("user");
MongoCollection<Document> server = db.getCollection("server");
int insertedWorlds = 0;
GameUniverse universe = GameServer.INSTANCE.getGameUniverse();
for (World w : universe.getWorlds()) {
insertedWorlds++;
worlds.save(w.mongoSerialise());
worlds.replaceOne(new Document("_id", w.getId()), w.mongoSerialise(), updateOptions);
//If the world should unload, it is removed from the Universe after having been saved.
if (w.shouldUnload()){
@@ -258,18 +260,18 @@ public class GameServer implements Runnable {
}
}
for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) {
for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) {
if (!u.isGuest()) {
users.save(u.mongoSerialise());
users.replaceOne(new Document("_id", u.getUsername()), u.mongoSerialise(), updateOptions);
}
}
BasicDBObject serverObj = new BasicDBObject();
serverObj.put("_id", "serverinfo"); //A constant id ensures only one entry is kept and updated, instead of a new entry created every save.
Document serverObj = new Document();
serverObj.put("time", gameUniverse.getTime());
serverObj.put("nextObjectId", gameUniverse.getNextObjectId());
server.save(serverObj);
//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);
LogManager.LOGGER.info("" + insertedWorlds + " worlds saved, " + unloaded_worlds + " unloaded");
} catch (Exception e) {

View File

@@ -3,7 +3,6 @@ package net.simon987.server;
import net.simon987.server.logging.LogManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

View File

@@ -1,26 +1,26 @@
package net.simon987.server.assembly;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.assembly.instruction.*;
import net.simon987.server.event.CpuInitialisationEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* CPU: Central Processing Unit. A CPU is capable of reading bytes from
* a Memory object and execute them. A CPU object holds registers objects and
* a Memory object.
*/
public class CPU implements MongoSerialisable {
public class CPU implements MongoSerializable {
/**
*
@@ -347,21 +347,21 @@ public class CPU implements MongoSerialisable {
}
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("memory", memory.mongoSerialise());
dbObject.put("registerSet", registerSet.mongoSerialise());
dbObject.put("codeSegmentOffset", codeSectionOffset);
BasicDBList hardwareList = new BasicDBList();
List<Document> hardwareList = new ArrayList<>();
for (Integer address : attachedHardware.keySet()) {
CpuHardware hardware = attachedHardware.get(address);
BasicDBObject serialisedHw = hardware.mongoSerialise();
Document serialisedHw = hardware.mongoSerialise();
serialisedHw.put("address", address);
hardwareList.add(serialisedHw);
}
@@ -372,22 +372,22 @@ public class CPU implements MongoSerialisable {
}
public static CPU deserialize(DBObject obj, User user) throws CancelledException {
public static CPU deserialize(Document obj, User user) throws CancelledException {
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user);
cpu.codeSectionOffset = (int) obj.get("codeSegmentOffset");
BasicDBList hardwareList = (BasicDBList) obj.get("hardware");
ArrayList hardwareList = (ArrayList) obj.get("hardware");
for (Object serialisedHw : hardwareList) {
CpuHardware hardware = CpuHardware.deserialize((DBObject) serialisedHw);
CpuHardware hardware = CpuHardware.deserialize((Document) serialisedHw);
hardware.setCpu(cpu);
cpu.attachHardware(hardware, (int) ((BasicDBObject) serialisedHw).get("address"));
cpu.attachHardware(hardware, (int) ((Document) serialisedHw).get("address"));
}
cpu.memory = Memory.deserialize((DBObject) obj.get("memory"));
cpu.registerSet = RegisterSet.deserialize((DBObject) obj.get("registerSet"));
cpu.memory = Memory.deserialize((Document) obj.get("memory"));
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
return cpu;

View File

@@ -1,13 +1,13 @@
package net.simon987.server.assembly;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public abstract class CpuHardware implements MongoSerialisable {
public abstract class CpuHardware implements MongoSerializable {
CPU cpu;
@@ -26,7 +26,7 @@ public abstract class CpuHardware implements MongoSerialisable {
public abstract char getId();
public static CpuHardware deserialize(DBObject obj) {
public static CpuHardware deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {

View File

@@ -1,11 +1,10 @@
package net.simon987.server.assembly;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -22,7 +21,7 @@ import java.util.zip.InflaterOutputStream;
/**
* Represents the available memory for a CPU in the game universe
*/
public class Memory implements Target, MongoSerialisable {
public class Memory implements Target, MongoSerializable {
/**
@@ -133,9 +132,9 @@ public class Memory implements Target, MongoSerialisable {
}
@Override
public BasicDBObject mongoSerialise() {
public Document mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
Document dbObject = new Document();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
@@ -154,7 +153,7 @@ public class Memory implements Target, MongoSerialisable {
return dbObject;
}
public static Memory deserialize(DBObject obj) {
public static Memory deserialize(Document obj) {
Memory memory = new Memory(0);

View File

@@ -1,21 +1,20 @@
package net.simon987.server.assembly;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A set of registers for a CPU
*/
public class RegisterSet implements Target, MongoSerialisable {
public class RegisterSet implements Target, MongoSerializable {
/**
* List of registers
@@ -145,10 +144,10 @@ public class RegisterSet implements Target, MongoSerialisable {
@Override
public BasicDBObject mongoSerialise() {
BasicDBList registers = new BasicDBList();
public Document mongoSerialise() {
List<Document> registers = new ArrayList<>();
for (Integer index : this.registers.keySet()) {
JSONObject register = new JSONObject();
Document register = new Document();
register.put("index", index);
register.put("name", getRegister(index).getName());
@@ -157,24 +156,24 @@ public class RegisterSet implements Target, MongoSerialisable {
registers.add(register);
}
BasicDBObject obj = new BasicDBObject();
Document obj = new Document();
obj.put("registers", registers);
return obj;
}
public static RegisterSet deserialize(DBObject obj) {
public static RegisterSet deserialize(Document obj) {
RegisterSet registerSet = new RegisterSet();
BasicDBList registers = (BasicDBList) obj.get("registers");
List registers = (ArrayList) obj.get("registers");
for (Object sRegister : registers) {
Register register = new Register((String) ((DBObject) sRegister).get("name"));
register.setValue((int) ((DBObject) sRegister).get("value"));
Register register = new Register((String) ((Document) sRegister).get("name"));
register.setValue((int) ((Document) sRegister).get("value"));
registerSet.registers.put((int) ((DBObject) sRegister).get("index"), register);
registerSet.registers.put((int) ((Document) sRegister).get("index"), register);
}

View File

@@ -1,11 +1,11 @@
package net.simon987.server.game;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.awt.*;
@@ -15,7 +15,7 @@ import java.util.ArrayList;
* An INSTANCE of an object (e.g. a Tree, a character ...) inside the
* game universe
*/
public abstract class GameObject implements JSONSerialisable, MongoSerialisable {
public abstract class GameObject implements JSONSerialisable, MongoSerializable {
private boolean dead;
/**
@@ -223,8 +223,8 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
return new JSONObject();
}
public static GameObject deserialize(DBObject obj) {
//
public static GameObject deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if (plugin instanceof GameObjectDeserializer) {

View File

@@ -1,6 +1,9 @@
package net.simon987.server.game;
import com.mongodb.*;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.Assembler;
@@ -9,6 +12,7 @@ import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
@@ -56,12 +60,12 @@ public class GameUniverse {
*/
private World loadWorld(int x, int y, String dimension) {
DB db = mongo.getDB(GameServer.INSTANCE.getConfig().getString("mongo_dbname"));
DBCollection worlds = db.getCollection("world");
MongoDatabase db = mongo.getDatabase(GameServer.INSTANCE.getConfig().getString("mongo_dbname"));
MongoCollection<Document> worlds = db.getCollection("world");
BasicDBObject whereQuery = new BasicDBObject();
Document whereQuery = new Document();
whereQuery.put("_id", World.idFromCoordinates(x, y, dimension));
DBCursor cursor = worlds.find(whereQuery);
MongoCursor<Document> cursor = worlds.find(whereQuery).iterator();
if (cursor.hasNext()) {
return World.deserialize(cursor.next());
}

View File

@@ -1,16 +1,15 @@
package net.simon987.server.game;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Random;
import java.util.zip.Deflater;
@@ -19,7 +18,7 @@ import java.util.zip.DeflaterOutputStream;
/**
* A 2D map of Tile objects of size width*height
*/
public class TileMap implements JSONSerialisable, MongoSerialisable {
public class TileMap implements JSONSerialisable, MongoSerializable {
public static final int VOID = -1;
public static final int PLAIN_TILE = 0;
@@ -141,25 +140,34 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
}
@Override
public BasicDBObject mongoSerialise() {
public Document mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
Document dbObject = new Document();
dbObject.put("tiles", tiles);
//Flatten multi-dimensional array
ArrayList<Integer> bsonTiles = new ArrayList<>();
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
bsonTiles.add(tiles[x][y]);
}
}
dbObject.put("tiles", bsonTiles);
return dbObject;
}
public static TileMap deserialize(DBObject object, int size) {
public static TileMap deserialize(Document object, int size) {
BasicDBList terrain = (BasicDBList) object.get("tiles");
ArrayList<Integer> terrain = (ArrayList<Integer>) object.get("tiles");
int[][] tiles = new int[size][size];
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
tiles[x][y] = (int) ((BasicDBList) terrain.get(x)).get(y);
tiles[x][y] = terrain.get(x * size + y);
}
}

View File

@@ -1,21 +1,20 @@
package net.simon987.server.game;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
public class World implements MongoSerialisable {
public class World implements MongoSerializable {
/**
* Size of the side of this world
@@ -171,17 +170,15 @@ public class World implements MongoSerialisable {
}
@Override
public BasicDBObject mongoSerialise() {
public Document mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
Document dbObject = new Document();
BasicDBList objects = new BasicDBList();
List<Document> objects = new ArrayList<>();
for (GameObject obj : gameObjects.values()) {
objects.add(obj.mongoSerialise());
}
dbObject.put("_id", getId());
dbObject.put("dimension", getDimension());
dbObject.put("objects", objects);
@@ -214,7 +211,7 @@ public class World implements MongoSerialisable {
}
public static World deserialize(DBObject dbObject) {
public static World deserialize(Document dbObject) {
World world = new World((int) dbObject.get("size"));
world.x = (int) dbObject.get("x");
@@ -222,13 +219,13 @@ public class World implements MongoSerialisable {
world.dimension = (String) dbObject.get("dimension");
world.updatable = (int) dbObject.get("updatable");
world.tileMap = TileMap.deserialize((BasicDBObject) dbObject.get("terrain"), world.getWorldSize());
world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize());
BasicDBList objects = (BasicDBList) dbObject.get("objects");
ArrayList<Document> objects = (ArrayList<Document>) dbObject.get("objects");
for (Object obj : objects) {
GameObject object = GameObject.deserialize((DBObject) obj);
GameObject object = GameObject.deserialize((Document) obj);
object.setWorld(world);
world.addObject(object);

View File

@@ -1,13 +1,12 @@
package net.simon987.server.game.debug;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import org.bson.Document;
import java.util.Arrays;
@@ -28,7 +27,7 @@ public class SpawnObjCommandListener implements GameEventListener {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(e.getInt("worldX"), e.getInt("worldY"),
false, e.getString("dimension"));
DBObject dbObj = (DBObject) JSON.parse(e.getString("data"));
Document dbObj = Document.parse(e.getString("data"));
dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId());
GameObject object = GameObject.deserialize(dbObj);

View File

@@ -1,10 +1,10 @@
package net.simon987.server.io;
import com.mongodb.DBObject;
import net.simon987.server.assembly.CpuHardware;
import org.bson.Document;
public interface CpuHardwareDeserializer {
CpuHardware deserializeHardware(DBObject hwJson);
CpuHardware deserializeHardware(Document hwJson);
}

View File

@@ -1,10 +1,10 @@
package net.simon987.server.io;
import com.mongodb.DBObject;
import net.simon987.server.game.GameObject;
import org.bson.Document;
public interface GameObjectDeserializer {
GameObject deserializeObject(DBObject object);
GameObject deserializeObject(Document object);
}

View File

@@ -1,9 +0,0 @@
package net.simon987.server.io;
import com.mongodb.BasicDBObject;
public interface MongoSerialisable {
BasicDBObject mongoSerialise();
}

View File

@@ -0,0 +1,9 @@
package net.simon987.server.io;
import org.bson.Document;
public interface MongoSerializable {
Document mongoSerialise();
}

View File

@@ -1,19 +1,18 @@
package net.simon987.server.user;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.UserCreationEvent;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
/**
* Represents a User (or player) of the game
*/
public class User implements MongoSerialisable {
public class User implements MongoSerializable {
private String username;
@@ -45,9 +44,9 @@ public class User implements MongoSerialisable {
}
@Override
public BasicDBObject mongoSerialise() {
public Document mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
Document dbObject = new Document();
dbObject.put("_id", username); // a constant id ensures only one entry per user is kept and updated, instead of a new entry created every save for every user.
dbObject.put("username", username);
@@ -62,18 +61,18 @@ public class User implements MongoSerialisable {
}
public static User deserialize(DBObject obj) throws CancelledException {
public static User deserialize(Document obj) throws CancelledException {
User user = new User((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("controlledUnit")));
user.username = (String) obj.get("username");
user.userCode = (String) obj.get("code");
user.password = (String) obj.get("password");
user.moderator = (boolean) obj.get("moderator");
user.stats = new UserStats((BasicDBObject) obj.get("stats"));
user.stats = new UserStats((Document) obj.get("stats"));
user.getControlledUnit().setParent(user);
user.cpu = CPU.deserialize((DBObject) obj.get("cpu"), user);
user.cpu = CPU.deserialize((Document) obj.get("cpu"), user);
return user;
}

View File

@@ -1,22 +1,21 @@
package net.simon987.server.user;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.crypto.RandomStringGenerator;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import org.springframework.security.crypto.bcrypt.BCrypt;
import java.util.ArrayList;
public class UserManager {
private DBCollection userCollection;
private MongoCollection<Document> userCollection;
public UserManager(DBCollection userCollection) {
public UserManager(MongoCollection<Document> userCollection) {
this.userCollection = userCollection;
}
@@ -30,7 +29,7 @@ public class UserManager {
ArrayList<User> userList = new ArrayList<>();
DBCursor cursor = userCollection.find();
MongoCursor<Document> cursor = userCollection.find().iterator();
while (cursor.hasNext()) {
try {
userList.add(User.deserialize(cursor.next()));
@@ -58,10 +57,10 @@ public class UserManager {
}
//Check if exists
DBObject where = new BasicDBObject();
Document where = new Document();
where.put("_id", username);
if (userCollection.findOne(where) != null) {
if (userCollection.find(where).first() != null) {
throw new RegistrationException("Username is already in use");
}
@@ -73,9 +72,9 @@ public class UserManager {
String hashedPassword = BCrypt.hashpw(password, salt);
user.setPassword(hashedPassword);
DBObject dbUser = user.mongoSerialise();
Document dbUser = user.mongoSerialise();
userCollection.save(dbUser);
userCollection.insertOne(dbUser);
} catch (Exception e) {
throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage());
}
@@ -89,10 +88,10 @@ public class UserManager {
*/
public boolean validateUser(String username, String password) {
DBObject where = new BasicDBObject();
Document where = new Document();
where.put("_id", username);
DBObject user = userCollection.findOne(where);
Document user = userCollection.find(where).first();
return user != null && BCrypt.checkpw(password, (String) user.get("password"));
}
@@ -114,7 +113,7 @@ public class UserManager {
String hashedPassword = BCrypt.hashpw(newPassword, salt);
user.setPassword(hashedPassword);
userCollection.save(user.mongoSerialise()); //Save new password immediately
userCollection.replaceOne(new Document("_id", username), user.mongoSerialise()); //Save new password immediately
}
/**

View File

@@ -1,28 +1,29 @@
package net.simon987.server.user;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
public class UserStats implements MongoSerialisable {
import java.util.ArrayList;
private BasicDBObject stats;
public class UserStats implements MongoSerializable {
private Document stats;
UserStats() {
this.stats = new BasicDBObject();
this.stats = new Document();
}
UserStats(BasicDBObject stats) {
UserStats(Document stats) {
if (stats != null) {
this.stats = stats;
} else {
this.stats = new BasicDBObject();
this.stats = new Document();
}
}
@Override
public BasicDBObject mongoSerialise() {
public Document mongoSerialise() {
return stats;
}
@@ -36,7 +37,7 @@ public class UserStats implements MongoSerialisable {
public void incrementStat(String name, int count) {
stats.putIfAbsent(name, 0);
stats.put(name, stats.getInt(name) + count);
stats.put(name, stats.getInteger(name) + count);
}
/**
@@ -57,8 +58,7 @@ public class UserStats implements MongoSerialisable {
* @return The value of the stat. Returns 0 if not found
*/
public int getInt(String name) {
return stats.getInt(name, 0);
return stats.getInteger(name, 0);
}
/**
@@ -69,10 +69,10 @@ public class UserStats implements MongoSerialisable {
*/
public void addToStringSet(String name, String value) {
stats.putIfAbsent(name, new BasicDBList());
stats.putIfAbsent(name, new ArrayList<>());
try {
((BasicDBList) stats.get(name)).add(value);
((ArrayList<String>) stats.get(name)).add(value);
} catch (ClassCastException e) {
LogManager.LOGGER.severe("UserStats: cannot add to list because stat already exists and is not a list");
}
@@ -87,16 +87,16 @@ public class UserStats implements MongoSerialisable {
*/
public boolean removeFromSet(String name, String value) {
if (stats.putIfAbsent(name, new BasicDBList()) != null) {
return ((BasicDBList) stats.get(name)).remove(value);
if (stats.putIfAbsent(name, new ArrayList()) != null) {
return ((ArrayList) stats.get(name)).remove(value);
}
return false;
}
public BasicDBList getSet(String name) {
stats.putIfAbsent(name, new BasicDBList());
public ArrayList getSet(String name) {
stats.putIfAbsent(name, new ArrayList());
return (BasicDBList) stats.get(name);
return (ArrayList) stats.get(name);
}
}

View File

@@ -1,11 +1,11 @@
package net.simon987.server.user;
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import net.simon987.server.GameServer;
import org.bson.Document;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.*;
/**
* Retrieve user stats in a structured fashion
@@ -15,12 +15,12 @@ public class UserStatsHelper {
/**
* Database collection of users
*/
private DBCollection users;
private MongoCollection<Document> users;
/**
* @param users Database collection of users
*/
public UserStatsHelper(DBCollection users) {
public UserStatsHelper(MongoCollection<Document> users) {
this.users = users;
}
@@ -35,11 +35,11 @@ public class UserStatsHelper {
ArrayList<Map.Entry<User, Integer>> rows = new ArrayList<>();
BasicDBObject orderBy = new BasicDBObject("$stats." + statName, -1);
DBCursor cursor = users.find().sort(orderBy).limit(n);
Document orderBy = new Document("$stats." + statName, -1);
MongoCursor<Document> cursor = users.find().sort(orderBy).limit(n).iterator();
while (cursor.hasNext()) {
DBObject dbUser = cursor.next();
Document dbUser = cursor.next();
User user = GameServer.INSTANCE.getGameUniverse().getUser((String) dbUser.get("username"));
rows.add(new AbstractMap.SimpleEntry<>(user, user.getStats().getInt(statName)));
}
@@ -54,26 +54,27 @@ public class UserStatsHelper {
* @param n Maximum number of players
* @return Top n players, in User,set format, in descending order
*/
public ArrayList<Map.Entry<User, BasicDBList>> getTopNSetLength(String statName, int n) {
public ArrayList<Map.Entry<User, ArrayList>> getTopNSetLength(String statName, int n) {
ArrayList<Map.Entry<User, BasicDBList>> rows = new ArrayList<>();
ArrayList<Map.Entry<User, ArrayList>> rows = new ArrayList<>();
BasicDBList ifNullList = new BasicDBList();
List<Object> ifNullList = new ArrayList<>(2);
ifNullList.add("$stats." + statName);
ifNullList.add(new BasicDBList());
ifNullList.add(new ArrayList());
BasicDBObject project = new BasicDBObject();
project.put("setLength", new BasicDBObject("$size", new BasicDBObject("$ifNull", ifNullList)));
Document project = new Document();
project.put("setLength", new Document("$size", new Document("$ifNull", ifNullList)));
project.put("username", 1);
Iterable<DBObject> results = users.aggregate(
new BasicDBObject("$project", project),
new BasicDBObject("$sort", new BasicDBObject("setLength", -1)),
new BasicDBObject("$limit", n)
).results();
for (DBObject dbUser : results) {
User user = GameServer.INSTANCE.getGameUniverse().getUser((String) dbUser.get("username"));
Iterator<Document> results = users.aggregate(Arrays.asList(
new Document("$project", project),
new Document("$sort", new Document("setLength", -1)),
new Document("$limit", n))
).iterator();
while (results.hasNext()) {
User user = GameServer.INSTANCE.getGameUniverse().getUser((String) results.next().get("username"));
rows.add(new AbstractMap.SimpleEntry<>(user, user.getStats().getSet(statName)));
}

View File

@@ -14,7 +14,7 @@ server_name=Official MAR server
#Database
mongo_dbname=mar_beta
#Change to 'mongodb' to use in docker
mongo_address=mongodb
mongo_address=localhost
mongo_port=27017
#Biomass
biomass_yield=2