Refactor: changed the way game objects and cpu hardware are saved/loaded from the database #151

This commit is contained in:
simon
2018-05-12 15:32:41 -04:00
parent 8d029cf621
commit 4cd58c86a5
96 changed files with 628 additions and 1221 deletions

View File

@@ -5,15 +5,16 @@ 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 com.mongodb.client.model.ReplaceOptions;
import net.simon987.server.crypto.CryptoProvider;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher;
import net.simon987.server.event.TickEvent;
import net.simon987.server.game.DayNightCycle;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.World;
import net.simon987.server.game.debug.*;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.game.world.DayNightCycle;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.PluginManager;
import net.simon987.server.user.User;
@@ -43,14 +44,15 @@ public class GameServer implements Runnable {
private CryptoProvider cryptoProvider;
private MongoClient mongo = null;
private MongoClient mongo;
private UserManager userManager;
private UserStatsHelper userStatsHelper;
public GameServer() {
private GameRegistry gameRegistry;
public GameServer() {
this.config = new ServerConfiguration("config.properties");
mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port"));
@@ -64,6 +66,7 @@ public class GameServer implements Runnable {
gameUniverse = new GameUniverse(config);
gameUniverse.setMongo(mongo);
pluginManager = new PluginManager();
gameRegistry = new GameRegistry();
maxExecutionTime = config.getInt("user_timeout");
@@ -79,7 +82,7 @@ public class GameServer implements Runnable {
for (File pluginFile : pluginDirListing) {
if (pluginFile.getName().endsWith(".jar")) {
pluginManager.load(pluginFile, config);
pluginManager.load(pluginFile, config, gameRegistry);
}
}
} else {
@@ -238,7 +241,7 @@ public class GameServer implements Runnable {
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
try{
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
UpdateOptions updateOptions = new UpdateOptions();
ReplaceOptions updateOptions = new ReplaceOptions();
updateOptions.upsert(true);
int unloaded_worlds = 0;
@@ -260,7 +263,6 @@ public class GameServer implements Runnable {
}
}
for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) {
if (!u.isGuest()) {
users.replaceOne(new Document("_id", u.getUsername()), u.mongoSerialise(), updateOptions);
@@ -303,4 +305,8 @@ public class GameServer implements Runnable {
public UserStatsHelper getUserStatsHelper() {
return userStatsHelper;
}
public GameRegistry getRegistry() {
return gameRegistry;
}
}

View File

@@ -381,12 +381,12 @@ public class CPU implements MongoSerializable {
ArrayList hardwareList = (ArrayList) obj.get("hardware");
for (Object serialisedHw : hardwareList) {
CpuHardware hardware = CpuHardware.deserialize((Document) serialisedHw);
CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw);
hardware.setCpu(cpu);
cpu.attachHardware(hardware, (int) ((Document) serialisedHw).get("address"));
}
cpu.memory = Memory.deserialize((Document) obj.get("memory"));
cpu.memory = new Memory((Document) obj.get("memory"));
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
return cpu;

View File

@@ -1,15 +1,21 @@
package net.simon987.server.assembly;
import net.simon987.server.GameServer;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public abstract class CpuHardware implements MongoSerializable {
CPU cpu;
private CPU cpu;
public CpuHardware() {
}
public CpuHardware(Document document) {
}
/**
* Handle an HWI instruction
@@ -26,22 +32,6 @@ public abstract class CpuHardware implements MongoSerializable {
public abstract char getId();
public static CpuHardware deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if (plugin instanceof CpuHardwareDeserializer) {
CpuHardware hw = ((CpuHardwareDeserializer) plugin).deserializeHardware(obj);
if (hw != null) {
return hw;
}
}
}
return null;
}
@Override
public String toString() {
return String.format("<%04X>", (int) getId());

View File

@@ -38,6 +38,31 @@ public class Memory implements Target, MongoSerializable {
words = new char[size];
}
public Memory(Document document) {
String zipBytesStr = (String) document.get("zipBytes");
if (zipBytesStr != null) {
byte[] compressedBytes = Base64.getDecoder().decode((String) document.get("zipBytes"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater(true);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
inflaterOutputStream.write(compressedBytes);
inflaterOutputStream.close();
setBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} else {
LogManager.LOGGER.severe("Memory was manually deleted");
words = new char[GameServer.INSTANCE.getConfig().getInt("memory_size")];
}
}
/**
* Get the value at an address
*
@@ -153,35 +178,6 @@ public class Memory implements Target, MongoSerializable {
return dbObject;
}
public static Memory deserialize(Document obj) {
Memory memory = new Memory(0);
String zipBytesStr = (String) obj.get("zipBytes");
if (zipBytesStr != null) {
byte[] compressedBytes = Base64.getDecoder().decode((String) obj.get("zipBytes"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater(true);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
inflaterOutputStream.write(compressedBytes);
inflaterOutputStream.close();
memory.setBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} else {
LogManager.LOGGER.severe("Memory was manually deleted");
memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size"));
}
return memory;
}
public void setBytes(byte[] bytes) {
this.words = new char[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(this.words);

View File

@@ -4,17 +4,8 @@ package net.simon987.server.event;
* Event dispatched by a GameObject who has needed callbacks on death
*/
public class ObjectDeathEvent extends GameEvent {
/**
* The GameObject type ID of object that initialize this event
*/
private long sourceObjectId;
public ObjectDeathEvent(Object source, int sourceObjectId) {
public ObjectDeathEvent(Object source) {
setSource(source);
this.sourceObjectId = sourceObjectId;
}
public long getSourceObjectId() {
return sourceObjectId;
}
}

View File

@@ -1,6 +1,6 @@
package net.simon987.server.event;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class WorldGenerationEvent extends GameEvent {

View File

@@ -1,6 +1,6 @@
package net.simon987.server.event;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class WorldUpdateEvent extends GameEvent {

View File

@@ -10,6 +10,9 @@ import net.simon987.server.assembly.Assembler;
import net.simon987.server.assembly.AssemblyResult;
import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.game.world.WorldGenerator;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
@@ -115,7 +118,7 @@ public class GameUniverse {
}
}
World getLoadedWorld(int x, int y, String dimension) {
public World getLoadedWorld(int x, int y, String dimension) {
// Wrapping coordinates around cyclically
x %= maxWidth;
y %= maxWidth;

View File

@@ -4,8 +4,8 @@ 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.Programmable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable;
public class ComPortMsgCommandListener implements GameEventListener {

View File

@@ -4,7 +4,7 @@ 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.World;
import net.simon987.server.game.world.World;
public class CreateWorldCommandListener implements GameEventListener {

View File

@@ -4,8 +4,8 @@ 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.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
public class DamageObjCommandListener implements GameEventListener {

View File

@@ -4,8 +4,8 @@ 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.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
public class HealObjCommandListener implements GameEventListener {

View File

@@ -4,8 +4,8 @@ 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 net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.Arrays;

View File

@@ -4,7 +4,7 @@ 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.objects.GameObject;
public class MoveObjCommandListener implements GameEventListener {

View File

@@ -4,8 +4,8 @@ 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 net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import java.util.Arrays;
import java.util.Collection;
@@ -37,7 +37,7 @@ public class ObjInfoCommandListener implements GameEventListener {
if (obj.isAt(e.getInt("x"), e.getInt("y")) || (obj.getX() == e.getInt("x") &&
obj.getY() == e.getInt("y"))) {
str += "Mongo:" + obj.mongoSerialise() + "\n";
str += "JSON :" + obj.serialise().toJSONString() + "\n\n";
str += "JSON :" + obj.jsonSerialise().toJSONString() + "\n\n";
}
}

View File

@@ -4,8 +4,8 @@ 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.Rechargeable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Rechargeable;
public class SetEnergyCommandListener implements GameEventListener {

View File

@@ -4,7 +4,7 @@ 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.World;
import net.simon987.server.game.world.World;
public class SetTileAtCommandListener implements GameEventListener {

View File

@@ -4,8 +4,8 @@ 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 net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import org.bson.Document;
import java.util.Arrays;
@@ -30,7 +30,7 @@ public class SpawnObjCommandListener implements GameEventListener {
Document dbObj = Document.parse(e.getString("data"));
dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId());
GameObject object = GameObject.deserialize(dbObj);
GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject(dbObj);
if (object != null) {
world.addObject(object);

View File

@@ -4,9 +4,9 @@ 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.Updatable;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.world.World;
public class TpObjectCommandListener implements GameEventListener {

View File

@@ -4,7 +4,7 @@ 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.ControllableUnit;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.user.User;
public class UserInfoCommandListener implements GameEventListener {

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public enum Action {
IDLE,

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Objects that can be attacked or healed
@@ -8,12 +8,15 @@ public interface Attackable {
void setHealRate(int hp);
int getHp();
void setHp(int hp);
int getMaxHp();
void setMaxHp(int hp);
void heal(int amount);
void damage(int amount);
}

View File

@@ -1,6 +1,7 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
import net.simon987.server.assembly.Memory;
import net.simon987.server.game.world.World;
import net.simon987.server.user.User;
import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Direction of a game object in a 4-direction grid-based

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Enterable {

View File

@@ -1,10 +1,9 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
import net.simon987.server.GameServer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.world.World;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
import org.json.simple.JSONObject;
@@ -12,7 +11,7 @@ import java.awt.*;
import java.util.ArrayList;
/**
* An INSTANCE of an object (e.g. a Tree, a character ...) inside the
* An instance of an object (e.g. a Cubot, a NPC...) inside the
* game universe
*/
public abstract class GameObject implements JSONSerialisable, MongoSerializable {
@@ -43,6 +42,15 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
*/
private World world;
public GameObject() {
}
public GameObject(Document document) {
objectId = document.getLong("id");
x = document.getInteger("x");
y = document.getInteger("y");
}
/**
* Increment the location of the game object by 1 tile
@@ -101,9 +109,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
} else {
return false;
}
}
}
public abstract char getMapInfo();
@@ -119,7 +125,6 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
} else {
return new Point(x - 1, y);
}
}
/**
@@ -219,24 +224,13 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
}
@Override
public JSONObject serialise() {
return new JSONObject();
}
public static GameObject deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if (plugin instanceof GameObjectDeserializer) {
GameObject object = ((GameObjectDeserializer) plugin).deserializeObject(obj);
if (object != null) {
return object;
}
}
}
return null;
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
json.put("i", getObjectId());
json.put("t", getClass().getCanonicalName());
json.put("x", getX());
json.put("y", getY());
return json;
}
@@ -254,6 +248,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
/**
* Called before this GameObject is removed from the world - defaults to doing nothing
*
* @return true if cancelled
*/
public boolean onDeadCallback() {
@@ -263,4 +258,17 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
public void initialize() {
}
@Override
public Document mongoSerialise() {
Document document = new Document();
document.put("id", getObjectId());
document.put("type", getClass().getCanonicalName());
document.put("x", getX());
document.put("y", getY());
return document;
}
}

View File

@@ -0,0 +1,70 @@
package net.simon987.server.game.objects;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
public class GameRegistry {
private HashMap<String, Class<? extends GameObject>> gameObjects;
private HashMap<String, Class<? extends CpuHardware>> hardware;
public GameRegistry() {
gameObjects = new HashMap<>();
hardware = new HashMap<>();
}
public void registerGameObject(Class<? extends GameObject> clazz) {
gameObjects.put(clazz.getCanonicalName(), clazz);
}
public void registerHardware(Class<? extends CpuHardware> clazz) {
hardware.put(clazz.getCanonicalName(), clazz);
}
public CpuHardware deserializeHardware(Document document) {
String type = document.getString("type");
if (hardware.containsKey(type)) {
try {
return hardware.get(type).getConstructor(Document.class).newInstance(document);
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
return null;
}
} else {
LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type);
return null;
}
}
public GameObject deserializeGameObject(Document document) {
String type = document.getString("type");
if (gameObjects.containsKey(type)) {
try {
return gameObjects.get(type).getConstructor(Document.class).newInstance(document);
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
return null;
}
} else {
LogManager.LOGGER.severe("Trying to deserialize unknown GameObject type: " + type);
return null;
}
}
public boolean isGameObjectRegistered(String type) {
return gameObjects.containsKey(type);
}
}

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface InventoryHolder {

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Programmable {

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
//Alpha: ±5cm
//Beta: 10-20 feet

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Rechargeable {

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Updatable objects needs to be updated each tick

View File

@@ -1,7 +1,7 @@
package net.simon987.server.game.pathfinding;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.Collections;

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
/**
* Represents a location in the game universe

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.io.JSONSerialisable;
@@ -110,7 +110,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
}
@Override
public JSONObject serialise() {
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
byte[] terrain = new byte[width * width];
@@ -164,7 +164,6 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
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] = terrain.get(x * size + y);

View File

@@ -1,8 +1,11 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
@@ -221,11 +224,11 @@ public class World implements MongoSerializable {
world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize());
ArrayList<Document> objects = (ArrayList<Document>) dbObject.get("objects");
ArrayList objects = (ArrayList) dbObject.get("objects");
for (Object obj : objects) {
GameObject object = GameObject.deserialize((Document) obj);
GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject((Document) obj);
object.setWorld(world);
world.addObject(object);

View File

@@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
@@ -43,7 +43,7 @@ public class WorldGenerator {
private HashMap<Point, Integer> centerPointsMap;
WorldGenerator(ServerConfiguration config) {
public WorldGenerator(ServerConfiguration config) {
centerPointCountMin = config.getInt("wg_centerPointCountMin");
centerPointCountMax = config.getInt("wg_centerPointCountMax");

View File

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

View File

@@ -1,45 +0,0 @@
package net.simon987.server.io;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.logging.LogManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Manages the database: this class manages the interactions with the database
*/
public abstract class DatabaseManager {
/**
* SQL connection url
*/
private String url;
/**
* SQL username
*/
private String username;
/**
* SQL password
*/
private String password;
public DatabaseManager(ServerConfiguration config) {
this.url = config.getString("mysql_url");
this.username = config.getString("mysql_user");
this.password = config.getString("mysql_pass");
}
protected Connection getConnection() {
try {
return DriverManager.getConnection(url, this.username, password);
} catch (SQLException e) {
LogManager.LOGGER.severe("Couldn't connect to MySQL server state:" + e.getSQLState() + " error:" + e.getErrorCode());
return null;
}
}
}

View File

@@ -1,22 +0,0 @@
package net.simon987.server.io;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileUtils {
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
/**
* Creates a new stamp containing the current date and time
*
* @return date and time stamp
*/
private static String getDateTimeStamp() {
Date millisToDate = new Date(System.currentTimeMillis());
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
return f.format(millisToDate);
}
}

View File

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

View File

@@ -4,6 +4,6 @@ import org.json.simple.JSONObject;
public interface JSONSerialisable {
JSONObject serialise();
JSONObject jsonSerialise();
}

View File

@@ -1,6 +1,7 @@
package net.simon987.server.plugin;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import java.io.File;
@@ -21,7 +22,7 @@ public class PluginManager {
this.plugins = new ArrayList<>(10);
}
public void load(File pluginFile, ServerConfiguration config) {
public void load(File pluginFile, ServerConfiguration config, GameRegistry gameRegistry) {
LogManager.LOGGER.info("Loading plugin file " + pluginFile.getName());
@@ -53,7 +54,7 @@ public class PluginManager {
plugins.add(plugin);
//Init the plugin
plugin.init(config);
plugin.init(config, gameRegistry);
} else {
LogManager.LOGGER.severe("Couldn't find plugin.properties in " + pluginFile.getName());

View File

@@ -2,6 +2,7 @@ package net.simon987.server.plugin;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
@@ -27,7 +28,7 @@ public abstract class ServerPlugin implements JSONSerialisable {
/**
* Called when the plugin is loaded
*/
public abstract void init(ServerConfiguration config);
public abstract void init(ServerConfiguration config, GameRegistry gameRegistry);
public String getName() {
return name;
@@ -50,7 +51,7 @@ public abstract class ServerPlugin implements JSONSerialisable {
}
@Override
public JSONObject serialise() {
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();

View File

@@ -5,7 +5,7 @@ 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.game.objects.ControllableUnit;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;

View File

@@ -1,8 +1,8 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -37,7 +37,7 @@ public class ObjectsRequestHandler implements MessageHandler {
for (GameObject object : world.getGameObjects()) {
objects.add(object.serialise());
objects.add(object.jsonSerialise());
}
response.put("t", "object");

View File

@@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.eclipse.jetty.websocket.api.Session;

View File

@@ -1,109 +0,0 @@
package net.simon987.server.websocket;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.io.DatabaseManager;
import net.simon987.server.logging.LogManager;
import java.sql.*;
class SocketServerDatabase extends DatabaseManager {
public SocketServerDatabase(ServerConfiguration config) {
super(config);
}
String validateAuthToken(String token) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT username FROM mar_user WHERE authToken=?");
p.setString(1, token);
ResultSet rs = p.executeQuery();
if (rs.next()) {
return rs.getString("username");
} else {
return null;
}
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
boolean isModerator(String username) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT moderator FROM mar_user WHERE username=?");
p.setString(1, username);
ResultSet rs = p.executeQuery();
return rs.next() && rs.getBoolean("moderator");
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
byte[] getFloppy(String username) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT floppyData FROM mar_user WHERE username=?");
p.setString(1, username);
ResultSet rs = p.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("floppyData");
if (blob != null) {
return blob.getBytes(1, (int) blob.length() - 1);
}
}
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}

View File

@@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

View File

@@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;

View File

@@ -785,15 +785,15 @@ var GameClient = (function () {
}());
var ObjectType;
(function (ObjectType) {
ObjectType[ObjectType["CUBOT"] = 1] = "CUBOT";
ObjectType[ObjectType["BIOMASS"] = 2] = "BIOMASS";
ObjectType[ObjectType["HARVESTER_NPC"] = 10] = "HARVESTER_NPC";
ObjectType[ObjectType["FACTORY"] = 3] = "FACTORY";
ObjectType[ObjectType["RADIO_TOWER"] = 4] = "RADIO_TOWER";
ObjectType[ObjectType["VAULT_DOOR"] = 5] = "VAULT_DOOR";
ObjectType[ObjectType["OBSTACLE"] = 6] = "OBSTACLE";
ObjectType[ObjectType["ELECTRIC_BOX"] = 7] = "ELECTRIC_BOX";
ObjectType[ObjectType["PORTAL"] = 8] = "PORTAL";
ObjectType["CUBOT"] = "net.simon987.cubotplugin.Cubot";
ObjectType["BIOMASS"] = "net.simon987.biomassplugin.BiomassBlob";
ObjectType["HARVESTER_NPC"] = "net.simon987.npcplugin.HarvesterNPC";
ObjectType["FACTORY"] = "net.simon987.npcplugin.Factory";
ObjectType["RADIO_TOWER"] = "net.simon987.npcplugin.RadioTower";
ObjectType["VAULT_DOOR"] = "net.simon987.npcplugin.VaultDoor";
ObjectType["OBSTACLE"] = "net.simon987.npcplugin.Obstacle";
ObjectType["ELECTRIC_BOX"] = "net.simon987.npcplugin.ElectricBox";
ObjectType["PORTAL"] = "net.simon987.npcplugin.Portal";
})(ObjectType || (ObjectType = {}));
var ItemType;
(function (ItemType) {

View File

@@ -1,13 +1,13 @@
enum ObjectType {
CUBOT = 1,
BIOMASS = 2,
HARVESTER_NPC = 10,
FACTORY = 3,
RADIO_TOWER = 4,
VAULT_DOOR = 5,
OBSTACLE = 6,
ELECTRIC_BOX = 7,
PORTAL = 8
CUBOT = "net.simon987.cubotplugin.Cubot",
BIOMASS = "net.simon987.biomassplugin.BiomassBlob",
HARVESTER_NPC = "net.simon987.npcplugin.HarvesterNPC",
FACTORY = "net.simon987.npcplugin.Factory",
RADIO_TOWER = "net.simon987.npcplugin.RadioTower",
VAULT_DOOR = "net.simon987.npcplugin.VaultDoor",
OBSTACLE = "net.simon987.npcplugin.Obstacle",
ELECTRIC_BOX = "net.simon987.npcplugin.ElectricBox",
PORTAL = "net.simon987.npcplugin.Portal"
}
enum ItemType {