diff --git a/Scripts/character/createCharacterTables.sql b/Scripts/character/createCharacterTables.sql index c775430e..f8527697 100644 --- a/Scripts/character/createCharacterTables.sql +++ b/Scripts/character/createCharacterTables.sql @@ -1,10 +1,10 @@ --characters --positions -CREATE TABLE charaWorldPositions (id INTEGER PRIMARY KEY, charID INTEGER, posX INTEGER, posY INTEGER); -CREATE INDEX charaWorldPositionsIDIndex ON charaWorldPositions (charID); +CREATE TABLE charaWorldPositions (playerId INTEGER PRIMARY KEY, id INTEGER, posX INTEGER, posY INTEGER); +CREATE INDEX charaWorldPositionsIDIndex ON charaWorldPositions (id); CREATE INDEX charaWorldPositionsPosIndex ON charaWorldPositions (posX, posY); --data -CREATE TABLE charaData (id INTEGER PRIMARY KEY, charID INTEGER, dataVal VARCHAR); -CREATE INDEX charaDataIDIndex ON charaData (charID); +CREATE TABLE charaData (playerId INTEGER PRIMARY KEY, id INTEGER, dataVal VARCHAR); +CREATE INDEX charaDataIDIndex ON charaData (id); diff --git a/src/main/java/electrosphere/engine/LoadingThread.java b/src/main/java/electrosphere/engine/LoadingThread.java index 897e7f77..a9e6a50a 100644 --- a/src/main/java/electrosphere/engine/LoadingThread.java +++ b/src/main/java/electrosphere/engine/LoadingThread.java @@ -69,6 +69,9 @@ import electrosphere.renderer.ui.Window; import electrosphere.util.FileUtils; import electrosphere.util.Utilities; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.LinkedList; import java.util.List; import java.util.Random; @@ -185,10 +188,12 @@ public class LoadingThread extends Thread { initServerArenaWorldData(); //init data cell manager initDataCellManager(); + //for testing purposes + FileUtils.recursivelyDelete("/home/satellite/temp/saves/arena"); //init database connection - // SaveUtils.initSave("arena"); + SaveUtils.initSave("arena"); //connect to database - // Globals.dbController.connect("./saves/arena/central.db"); + SaveUtils.loadSave("arena"); //init authentication initAuthenticationManager(); //initialize the server thread (server only) diff --git a/src/main/java/electrosphere/game/server/db/DatabaseController.java b/src/main/java/electrosphere/game/server/db/DatabaseController.java index 1e240704..cce3afd2 100644 --- a/src/main/java/electrosphere/game/server/db/DatabaseController.java +++ b/src/main/java/electrosphere/game/server/db/DatabaseController.java @@ -30,6 +30,7 @@ public class DatabaseController { conn = DriverManager.getConnection(fullAddress, connectionProps); } catch (SQLException ex) { LoggerInterface.loggerFileIO.ERROR("Failure to connect to db", ex); + ex.printStackTrace(); } } @@ -80,6 +81,9 @@ public class DatabaseController { public boolean isConnected(){ boolean rVal = false; + if(conn == null){ + return false; + } try { rVal = conn.isValid(100); } catch (SQLException ex) { diff --git a/src/main/java/electrosphere/game/server/db/DatabaseUtils.java b/src/main/java/electrosphere/game/server/db/DatabaseUtils.java index e5b051e2..8fd74256 100644 --- a/src/main/java/electrosphere/game/server/db/DatabaseUtils.java +++ b/src/main/java/electrosphere/game/server/db/DatabaseUtils.java @@ -1,6 +1,7 @@ package electrosphere.game.server.db; import electrosphere.logger.LoggerInterface; +import electrosphere.main.Globals; import electrosphere.util.FileUtils; import java.io.IOException; import java.util.logging.Level; @@ -19,19 +20,23 @@ public class DatabaseUtils { return false; } String dbFilePath = sanitizedPath + "/central.db"; - DatabaseController controller = new DatabaseController(); - controller.connect(dbFilePath); - runScript(controller,"createTables.sql"); + if(Globals.dbController == null){ + Globals.dbController = new DatabaseController(); + } + if(!Globals.dbController.isConnected()){ + Globals.dbController.connect(dbFilePath); + } + runScript(Globals.dbController,"createTables.sql"); //both of these are used for arena mode as well as main game - runScript(controller,"/auth/createAuthTables.sql"); - runScript(controller,"/character/createCharacterTables.sql"); + runScript(Globals.dbController,"/auth/createAuthTables.sql"); + runScript(Globals.dbController,"/character/createCharacterTables.sql"); //create adventure-only files if(!dbFilePath.equals("./saves/arena/central.db")){ //we only want to create these if we're not in arena mode - runScript(controller,"/towns/createTownsTables.sql"); - runScript(controller,"/structs/createStructsTables.sql"); + runScript(Globals.dbController,"/towns/createTownsTables.sql"); + runScript(Globals.dbController,"/structs/createStructsTables.sql"); } - controller.disconnect(); + Globals.dbController.disconnect(); return true; } diff --git a/src/main/java/electrosphere/game/server/saves/SaveUtils.java b/src/main/java/electrosphere/game/server/saves/SaveUtils.java index 29661e47..56bef734 100644 --- a/src/main/java/electrosphere/game/server/saves/SaveUtils.java +++ b/src/main/java/electrosphere/game/server/saves/SaveUtils.java @@ -16,7 +16,8 @@ import java.util.List; public class SaveUtils { static String deriveSaveDirectoryPath(String saveName){ - return "./saves/" + saveName; + String path = "/home/satellite/temp/saves/" + saveName; + return path; } /** @@ -30,7 +31,7 @@ public class SaveUtils { if(FileUtils.checkFileExists(dirPath)){ return false; } - //create dir + // create dir if(!FileUtils.createDirectory(dirPath)){ //we for some unknown reason, couldn't make the save dir return false; @@ -61,8 +62,10 @@ public class SaveUtils { String dirPath = deriveSaveDirectoryPath(saveName); String dbFilePath = FileUtils.sanitizeFilePath(dirPath) + "/central.db"; Globals.dbController.connect(dbFilePath); - Globals.serverTerrainManager.load(saveName); - Globals.serverWorldData = FileUtils.loadObjectFromSavePath(saveName, "world.json", ServerWorldData.class); + if(!saveName.equals("arena")){ + Globals.serverTerrainManager.load(saveName); + Globals.serverWorldData = FileUtils.loadObjectFromSavePath(saveName, "world.json", ServerWorldData.class); + } return true; } diff --git a/src/main/java/electrosphere/util/FileUtils.java b/src/main/java/electrosphere/util/FileUtils.java index 643ffd85..2c641024 100644 --- a/src/main/java/electrosphere/util/FileUtils.java +++ b/src/main/java/electrosphere/util/FileUtils.java @@ -242,6 +242,21 @@ public class FileUtils { } return rVal; } + + public static void recursivelyDelete(String path){ + File file = new File(path); + if(file.isDirectory()){ + for(File child : file.listFiles()){ + recursivelyDelete(child.getAbsolutePath()); + } + } + try { + Files.delete(file.toPath()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }