From da3893f3ff4f68610d8ccd2369f8b9cd75f69c34 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 15 May 2025 16:28:53 -0400 Subject: [PATCH] move server state --- docs/src/progress/renderertodo.md | 1 + .../auth/AuthenticationManager.java | 6 ++--- .../java/electrosphere/engine/Globals.java | 16 ++----------- .../loadingthreads/MainMenuLoading.java | 2 -- .../loadingthreads/ViewportLoading.java | 5 ++-- .../server/MainServerFunctions.java | 2 +- .../electrosphere/server/ServerState.java | 23 ++++++++++++++++++- .../electrosphere/server/datacell/Realm.java | 2 +- .../server/db/DatabaseUtils.java | 11 ++++----- .../server/player/BlockActions.java | 4 ++-- .../electrosphere/server/saves/SaveUtils.java | 2 +- .../server/service/CharacterService.java | 12 +++++----- .../entity/ServerEntityUtilsUnitTests.java | 2 -- 13 files changed, 45 insertions(+), 43 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 69a284c0..d9235c87 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1828,6 +1828,7 @@ Move microSimulation into serverState Move fluidCellManager into clientState Move engine flags under engineState Move rendering flags under renderingEngine +Move database connection into serverState diff --git a/src/main/java/electrosphere/auth/AuthenticationManager.java b/src/main/java/electrosphere/auth/AuthenticationManager.java index 40a48ce7..a049308a 100644 --- a/src/main/java/electrosphere/auth/AuthenticationManager.java +++ b/src/main/java/electrosphere/auth/AuthenticationManager.java @@ -61,7 +61,7 @@ public class AuthenticationManager { //first we hash the input password String hashedPassword = getHashedString(password); //then query the database for the username and hash for the input username - DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, username, pwdhash FROM accounts WHERE username=?;",username); + DatabaseResult result = Globals.serverState.dbController.executePreparedQuery("SELECT id, username, pwdhash FROM accounts WHERE username=?;",username); if(result.hasResult()){ boolean foundRow = false; //if we get a valid response from the database, check that it actually matches hashes @@ -76,10 +76,10 @@ public class AuthenticationManager { //If we didn't find a single account, go ahead and create it if(!foundRow){ LoggerInterface.loggerAuth.INFO("Created user " + username); - Globals.dbController.executePreparedStatement("INSERT INTO accounts (username, pwdhash) VALUES(?, ?);",username,hashedPassword); + Globals.serverState.dbController.executePreparedStatement("INSERT INTO accounts (username, pwdhash) VALUES(?, ?);",username,hashedPassword); //verify the account was created - result = Globals.dbController.executePreparedQuery("SELECT id, username, pwdhash FROM accounts WHERE username=?;",username); + result = Globals.serverState.dbController.executePreparedQuery("SELECT id, username, pwdhash FROM accounts WHERE username=?;",username); if(result.hasResult()){ foundRow = false; //if we get a valid response from the database, check that it actually matches hashes diff --git a/src/main/java/electrosphere/engine/Globals.java b/src/main/java/electrosphere/engine/Globals.java index b5b7d7df..79e5f7f0 100644 --- a/src/main/java/electrosphere/engine/Globals.java +++ b/src/main/java/electrosphere/engine/Globals.java @@ -35,10 +35,7 @@ import electrosphere.renderer.ui.elements.ImagePanel; import electrosphere.renderer.ui.font.FontManager; import electrosphere.script.ScriptEngine; import electrosphere.server.ServerState; -import electrosphere.server.db.DatabaseController; import electrosphere.server.entity.poseactor.PoseModel; -import electrosphere.server.service.CharacterService; -import electrosphere.server.service.StructureScanningService; import electrosphere.util.FileUtils; /** @@ -98,11 +95,6 @@ public class Globals { public static ScrollCallback scrollCallback = new ScrollCallback(); public static CursorState cursorState = new CursorState(); - // - // Database stuff - // - public static DatabaseController dbController = new DatabaseController(); - // @@ -163,7 +155,6 @@ public class Globals { //services public static FileWatcherService fileWatcherService; - public static StructureScanningService structureScanningService; @@ -194,7 +185,6 @@ public class Globals { //server state Globals.serverState = new ServerState(); - Globals.serverState.characterService = (CharacterService)Globals.engineState.serviceManager.registerService(new CharacterService()); //load in default texture map textureMapDefault = TextureMap.construct("Textures/default_texture_map.json"); @@ -224,7 +214,6 @@ public class Globals { Globals.scriptEngine = (ScriptEngine)Globals.engineState.serviceManager.registerService(new ScriptEngine()); Globals.mainThreadSignalService = (MainThreadSignalService)Globals.engineState.serviceManager.registerService(new MainThreadSignalService()); Globals.fileWatcherService = (FileWatcherService)Globals.engineState.serviceManager.registerService(new FileWatcherService()); - Globals.structureScanningService = (StructureScanningService)Globals.engineState.serviceManager.registerService(new StructureScanningService()); Globals.engineState.serviceManager.instantiate(); // //End service manager @@ -383,13 +372,12 @@ public class Globals { if(Globals.serverState != null){ Globals.serverState.aiManager.shutdown(); Globals.serverState.realmManager.reset(); + Globals.serverState.dbController.disconnect(); } - Globals.dbController.disconnect(); Globals.engineState.serviceManager.unloadScene(); Globals.clientState = new ClientState(); Globals.serverState = new ServerState(); - Globals.serverState.characterService = (CharacterService)Globals.engineState.serviceManager.registerService(new CharacterService()); } /** @@ -399,8 +387,8 @@ public class Globals { if(Globals.serverState != null){ Globals.serverState.aiManager.shutdown(); Globals.serverState.realmManager.reset(); + Globals.serverState.dbController.disconnect(); } - Globals.dbController.disconnect(); // //Actual globals to destroy Globals.assetManager = null; diff --git a/src/main/java/electrosphere/engine/loadingthreads/MainMenuLoading.java b/src/main/java/electrosphere/engine/loadingthreads/MainMenuLoading.java index ebcfbf20..e91f2c28 100644 --- a/src/main/java/electrosphere/engine/loadingthreads/MainMenuLoading.java +++ b/src/main/java/electrosphere/engine/loadingthreads/MainMenuLoading.java @@ -9,7 +9,6 @@ import electrosphere.engine.signal.Signal.SignalType; import electrosphere.engine.threads.LabeledThread.ThreadLabel; import electrosphere.renderer.ui.elements.Window; import electrosphere.server.ServerState; -import electrosphere.server.service.CharacterService; /** * Loading thread that returns the client to the main menu @@ -63,7 +62,6 @@ public class MainMenuLoading { private static void resetServerState(){ Globals.serverState.server.close(); Globals.serverState = new ServerState(); - Globals.serverState.characterService = (CharacterService)Globals.engineState.serviceManager.registerService(new CharacterService()); } /** diff --git a/src/main/java/electrosphere/engine/loadingthreads/ViewportLoading.java b/src/main/java/electrosphere/engine/loadingthreads/ViewportLoading.java index 7f884e5b..c2edd64e 100644 --- a/src/main/java/electrosphere/engine/loadingthreads/ViewportLoading.java +++ b/src/main/java/electrosphere/engine/loadingthreads/ViewportLoading.java @@ -84,9 +84,8 @@ public class ViewportLoading { * Initializes an in-memory db */ private static void initInMemoryDB(){ - Globals.dbController = new DatabaseController(); - Globals.dbController.connect(DatabaseController.IN_MEMORY_PATH); - DatabaseUtils.runScript(Globals.dbController,"createTables.sql"); + Globals.serverState.dbController.connect(DatabaseController.IN_MEMORY_PATH); + DatabaseUtils.runScript(Globals.serverState.dbController,"createTables.sql"); } } diff --git a/src/main/java/electrosphere/server/MainServerFunctions.java b/src/main/java/electrosphere/server/MainServerFunctions.java index 0921e628..f89e5dd1 100644 --- a/src/main/java/electrosphere/server/MainServerFunctions.java +++ b/src/main/java/electrosphere/server/MainServerFunctions.java @@ -60,7 +60,7 @@ public class MainServerFunctions { * Simulates server services */ private static void simulateServices(){ - Globals.structureScanningService.simulate(); + Globals.serverState.structureScanningService.simulate(); } } diff --git a/src/main/java/electrosphere/server/ServerState.java b/src/main/java/electrosphere/server/ServerState.java index 394964be..47863612 100644 --- a/src/main/java/electrosphere/server/ServerState.java +++ b/src/main/java/electrosphere/server/ServerState.java @@ -1,5 +1,6 @@ package electrosphere.server; +import electrosphere.engine.Globals; import electrosphere.net.server.Server; import electrosphere.net.server.player.PlayerManager; import electrosphere.net.synchronization.server.EntityValueTrackingService; @@ -7,8 +8,10 @@ import electrosphere.net.synchronization.server.ServerSynchronizationManager; import electrosphere.server.ai.AIManager; import electrosphere.server.datacell.EntityDataCellMapper; import electrosphere.server.datacell.RealmManager; +import electrosphere.server.db.DatabaseController; import electrosphere.server.saves.Save; import electrosphere.server.service.CharacterService; +import electrosphere.server.service.StructureScanningService; import electrosphere.server.simulation.MicroSimulation; /** @@ -49,7 +52,12 @@ public class ServerState { /** * Service for managing characters */ - public CharacterService characterService; + public final CharacterService characterService; + + /** + * Service for background scanning to detect when players create structures + */ + public final StructureScanningService structureScanningService; /** * behavior tree tracking service @@ -61,9 +69,22 @@ public class ServerState { */ public PlayerManager playerManager = new PlayerManager(); + /** + * Database controller + */ + public final DatabaseController dbController = new DatabaseController(); + /** * The micro simulation */ public final MicroSimulation microSimulation = new MicroSimulation(); + /** + * Constructor + */ + public ServerState(){ + this.characterService = (CharacterService)Globals.engineState.serviceManager.registerService(new CharacterService()); + this.structureScanningService = (StructureScanningService)Globals.engineState.serviceManager.registerService(new StructureScanningService()); + } + } diff --git a/src/main/java/electrosphere/server/datacell/Realm.java b/src/main/java/electrosphere/server/datacell/Realm.java index d00a4d80..2ba3252a 100644 --- a/src/main/java/electrosphere/server/datacell/Realm.java +++ b/src/main/java/electrosphere/server/datacell/Realm.java @@ -258,7 +258,7 @@ public class Realm { // //macro data simulation - if(this.macroData != null && Globals.dbController != null && Globals.dbController.isConnected()){ + if(this.macroData != null && Globals.serverState.dbController != null && Globals.serverState.dbController.isConnected()){ MacroSimulation.simulate(this); } diff --git a/src/main/java/electrosphere/server/db/DatabaseUtils.java b/src/main/java/electrosphere/server/db/DatabaseUtils.java index cb49374e..049cdfb1 100644 --- a/src/main/java/electrosphere/server/db/DatabaseUtils.java +++ b/src/main/java/electrosphere/server/db/DatabaseUtils.java @@ -22,15 +22,12 @@ public class DatabaseUtils { return false; } String dbFilePath = sanitizedPath + "/central" + DatabaseController.FILE_EXT; - if(Globals.dbController == null){ - Globals.dbController = new DatabaseController(); + if(!Globals.serverState.dbController.isConnected()){ + Globals.serverState.dbController.connect(dbFilePath); } - if(!Globals.dbController.isConnected()){ - Globals.dbController.connect(dbFilePath); - } - DatabaseUtils.runScript(Globals.dbController,"createTables.sql"); + DatabaseUtils.runScript(Globals.serverState.dbController,"createTables.sql"); //both of these are used for arena mode as well as main game - Globals.dbController.disconnect(); + Globals.serverState.dbController.disconnect(); return true; } diff --git a/src/main/java/electrosphere/server/player/BlockActions.java b/src/main/java/electrosphere/server/player/BlockActions.java index 2d8fac45..47125b2b 100644 --- a/src/main/java/electrosphere/server/player/BlockActions.java +++ b/src/main/java/electrosphere/server/player/BlockActions.java @@ -39,7 +39,7 @@ public class BlockActions { String goalBlockEntityId = Item.getBlockTypeId(blockTypeData); if(CreatureUtils.hasControllerPlayerId(creature)){ Player player = Globals.serverState.playerManager.getPlayerFromId(CreatureUtils.getControllerPlayerId(creature)); - Globals.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); + Globals.serverState.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); } if(equippedItemType.equals(goalBlockEntityId)){ @@ -64,7 +64,7 @@ public class BlockActions { ServerChargeState.attemptRemoveCharges(creature, 1); if(CreatureUtils.hasControllerPlayerId(creature)){ Player player = Globals.serverState.playerManager.getPlayerFromId(CreatureUtils.getControllerPlayerId(creature)); - Globals.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); + Globals.serverState.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); } } diff --git a/src/main/java/electrosphere/server/saves/SaveUtils.java b/src/main/java/electrosphere/server/saves/SaveUtils.java index 09dcc1f6..18dcfdf4 100644 --- a/src/main/java/electrosphere/server/saves/SaveUtils.java +++ b/src/main/java/electrosphere/server/saves/SaveUtils.java @@ -226,7 +226,7 @@ public class SaveUtils { //load db String dbFilePath = FileUtils.sanitizeFilePath(dirPath) + "/central" + DatabaseController.FILE_EXT; - Globals.dbController.connect(dbFilePath); + Globals.serverState.dbController.connect(dbFilePath); return true; } diff --git a/src/main/java/electrosphere/server/service/CharacterService.java b/src/main/java/electrosphere/server/service/CharacterService.java index dd909ea6..9d3b7ef3 100644 --- a/src/main/java/electrosphere/server/service/CharacterService.java +++ b/src/main/java/electrosphere/server/service/CharacterService.java @@ -69,7 +69,7 @@ public class CharacterService extends SignalServiceImpl { lock.lock(); Character toStore = new Character(template); toStore.setPlayerId(playerId); - DatabaseResult result = Globals.dbController.executePreparedQuery( + DatabaseResult result = Globals.serverState.dbController.executePreparedQuery( "INSERT INTO charaData (playerId,dataVal) VALUES (?,?) RETURNING id;", playerId, new Gson().toJson(toStore) @@ -98,7 +98,7 @@ public class CharacterService extends SignalServiceImpl { return rVal; } Character charData = null; - DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData WHERE id=?;", characterId); + DatabaseResult result = Globals.serverState.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData WHERE id=?;", characterId); if(!result.hasResult()){ LoggerInterface.loggerDB.WARNING("Failed to locate creature template for characterId=" + characterId); lock.unlock(); @@ -121,7 +121,7 @@ public class CharacterService extends SignalServiceImpl { */ public List getCharacters(int playerId){ lock.lock(); - DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData WHERE playerId=?;",playerId); + DatabaseResult result = Globals.serverState.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData WHERE playerId=?;",playerId); List rVal = new LinkedList(); if(result.hasResult()){ //if we get a valid response from the database, check that it actually matches hashes @@ -160,7 +160,7 @@ public class CharacterService extends SignalServiceImpl { String toStore = SerializationUtils.serialize(charaData); //store a serialization to associate with the character - Globals.dbController.executePreparedStatement( + Globals.serverState.dbController.executePreparedStatement( "UPDATE charaData SET dataVal=? WHERE id=?;", toStore, charaData.getId() @@ -174,7 +174,7 @@ public class CharacterService extends SignalServiceImpl { */ public List getAllCharacters(){ lock.lock(); - DatabaseResult result = Globals.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData"); + DatabaseResult result = Globals.serverState.dbController.executePreparedQuery("SELECT id, playerId, dataVal FROM charaData"); List rVal = new LinkedList(); if(result.hasResult()){ //if we get a valid response from the database, check that it actually matches hashes @@ -266,7 +266,7 @@ public class CharacterService extends SignalServiceImpl { String toStore = SerializationUtils.serialize(chara); //store a serialization to associate with the character - Globals.dbController.executePreparedStatement( + Globals.serverState.dbController.executePreparedStatement( "UPDATE charaData SET dataVal=? WHERE id=?;", toStore, chara.getId() diff --git a/src/test/java/electrosphere/entity/ServerEntityUtilsUnitTests.java b/src/test/java/electrosphere/entity/ServerEntityUtilsUnitTests.java index 14607a60..f04eceb8 100644 --- a/src/test/java/electrosphere/entity/ServerEntityUtilsUnitTests.java +++ b/src/test/java/electrosphere/entity/ServerEntityUtilsUnitTests.java @@ -7,7 +7,6 @@ import org.joml.Vector3d; import electrosphere.test.annotations.UnitTest; import electrosphere.engine.Globals; import electrosphere.server.datacell.Realm; -import electrosphere.server.service.CharacterService; /** * Unit tests for the server entity utils @@ -18,7 +17,6 @@ public class ServerEntityUtilsUnitTests { public void destroyEntity_ValidEntity_NoRealm(){ //setup Globals.initGlobals(); - Globals.serverState.characterService = (CharacterService)Globals.engineState.serviceManager.registerService(new CharacterService()); Realm realm = Globals.serverState.realmManager.createViewportRealm(new Vector3d(0,0,0), new Vector3d(1,1,1)); Entity entity = EntityCreationUtils.createServerEntity(realm, new Vector3d());