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;
case "-database":
// Database only
DatabaseManager databaseManager = new DatabaseManager();
databaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer());
LunarCore.getLogger().info("Running local mongo server at " + databaseManager.getServer().getConnectionString());
DatabaseManager.startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer());
LunarCore.getLogger().info("Running local mongo server at " + DatabaseManager.getServer().getConnectionString());
// Console
LunarCore.startConsole();
return;
@ -113,6 +112,9 @@ public class LunarCore {
gameServer = new GameServer(getConfig().getGameServer());
gameServer.start();
}
// Hook into shutdown event
Runtime.getRuntime().addShutdownHook(new Thread(LunarCore::onShutdown));
// Start console
LunarCore.startConsole();
@ -202,6 +204,14 @@ public class LunarCore {
LunarCore.getLogger().error("Terminal error: ", e);
}
}
// Shutdown event
private static void onShutdown() {
if (gameServer != null) {
gameServer.onShutdown();
}
}
// Server enums

View File

@ -34,14 +34,11 @@ import lombok.Getter;
@Getter
public final class DatabaseManager {
private MongoServer server;
@Getter private static MongoServer server;
private Datastore datastore;
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) {
// Variables
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
public boolean checkIfObjectExists(Class<?> cls, long uid) {
@ -199,4 +171,29 @@ public final class DatabaseManager {
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();
}
}, 0, 1000);
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
}
public GameServerConfig getServerConfig() {
@ -166,7 +163,7 @@ public class GameServer extends KcpServer {
}
}
private void onShutdown() {
public void onShutdown() {
// Close server socket
this.stop();