Minor refactoring of DatabaseManager

This commit is contained in:
Melledy
2023-11-25 17:41:40 -08:00
parent 3b53caf874
commit 265ba8895a
3 changed files with 40 additions and 36 deletions

View File

@@ -81,9 +81,8 @@ public class LunarCore {
break; break;
case "-database": case "-database":
// Database only // Database only
DatabaseManager databaseManager = new DatabaseManager(); DatabaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer());
databaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer()); LunarCore.getLogger().info("Running local mongo server at " + DatabaseManager.getServer().getConnectionString());
LunarCore.getLogger().info("Running local mongo server at " + databaseManager.getServer().getConnectionString());
// Console // Console
LunarCore.startConsole(); LunarCore.startConsole();
return; return;
@@ -114,6 +113,9 @@ public class LunarCore {
gameServer.start(); gameServer.start();
} }
// Hook into shutdown event
Runtime.getRuntime().addShutdownHook(new Thread(LunarCore::onShutdown));
// Start console // Start console
LunarCore.startConsole(); LunarCore.startConsole();
} }
@@ -203,6 +205,14 @@ public class LunarCore {
} }
} }
// Shutdown event
private static void onShutdown() {
if (gameServer != null) {
gameServer.onShutdown();
}
}
// Server enums // Server enums
public enum ServerType { public enum ServerType {

View File

@@ -34,14 +34,11 @@ import lombok.Getter;
@Getter @Getter
public final class DatabaseManager { public final class DatabaseManager {
private MongoServer server; @Getter private static MongoServer server;
private Datastore datastore; private Datastore datastore;
private final DeleteOptions DELETE_MANY = new DeleteOptions().multi(true); private final DeleteOptions DELETE_MANY = new DeleteOptions().multi(true);
// Empty constructor for when we want to start an internal server
public DatabaseManager() {}
public DatabaseManager(DatabaseInfo info, ServerType type) { public DatabaseManager(DatabaseInfo info, ServerType type) {
// Variables // Variables
String connectionString = info.getUri(); String connectionString = info.getUri();
@@ -123,31 +120,6 @@ public final class DatabaseManager {
} }
} }
// Internal mongo server
public String startInternalMongoServer(InternalMongoInfo internalMongo) {
// Get backend
MongoBackend backend = null;
if (internalMongo.filePath != null && internalMongo.filePath.length() > 0) {
backend = new H2Backend(internalMongo.filePath);
} else {
backend = new MemoryBackend();
}
// Create the local mongo server and replace the connection string
server = new MongoServer(backend);
// Bind to address of it exists
if (internalMongo.getAddress() != null && internalMongo.getPort() != 0) {
server.bind(internalMongo.getAddress(), internalMongo.getPort());
} else {
server.bind(); // Binds to random port
}
return server.getConnectionString();
}
// Database Functions // Database Functions
public boolean checkIfObjectExists(Class<?> cls, long uid) { public boolean checkIfObjectExists(Class<?> cls, long uid) {
@@ -199,4 +171,29 @@ public final class DatabaseManager {
getDatastore().save(counter); getDatastore().save(counter);
} }
} }
// Internal MongoDB server
public static String startInternalMongoServer(InternalMongoInfo internalMongo) {
// Get backend
MongoBackend backend = null;
if (internalMongo.filePath != null && internalMongo.filePath.length() > 0) {
backend = new H2Backend(internalMongo.filePath);
} else {
backend = new MemoryBackend();
}
// Create the local mongo server and replace the connection string
server = new MongoServer(backend);
// Bind to address of it exists
if (internalMongo.getAddress() != null && internalMongo.getPort() != 0) {
server.bind(internalMongo.getAddress(), internalMongo.getPort());
} else {
server.bind(); // Binds to random port
}
return server.getConnectionString();
}
} }

View File

@@ -63,9 +63,6 @@ public class GameServer extends KcpServer {
onTick(); onTick();
} }
}, 0, 1000); }, 0, 1000);
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
} }
public GameServerConfig getServerConfig() { public GameServerConfig getServerConfig() {
@@ -166,7 +163,7 @@ public class GameServer extends KcpServer {
} }
} }
private void onShutdown() { public void onShutdown() {
// Close server socket // Close server socket
this.stop(); this.stop();