diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java index c1ca1b82c5123122b74b577ccce6f2278fac0a9b..f2fcc0591e906e7642e85e8f01b4b808fae080c2 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Persistence.java @@ -58,6 +58,41 @@ public class Persistence { return communityMapsListRemote; } + public void loadPlayerAccount() throws DatabaseException { + try { + gsonHandler.loadAccount(); + log.info("Player account successfully loaded!"); + } + catch(Exception e){ + log.error(e); + throw new DatabaseException("Loading player account data from local storage failed!"); + } + } + + public void loadPlayerStatistics() throws DatabaseException { + try { + if (account.getAccountType() == AccountType.LOCAL) { + statistics = gsonHandler.loadStats(); + } else { + statistics = db.getStatistics(account.getPlayerName()); + } + log.info("Player statistics successfully loaded!"); + } + catch(SQLException e){ + log.error(e); + if(e.getMessage().contains("ORA-17868") | e.getMessage().contains("ORA-01017")){ + throw new DatabaseException("No connection to SQL server!"); + } + else{ + throw new DatabaseException("Unknown Database Error. Retrieving statistics from SQL server failed!"); + } + } + catch(GSONException e){ + log.error(e); + throw new DatabaseException(e.getMessage()); + } + } + public void loadCoreMaps() throws DatabaseException { try { coreMaps = gsonHandler.loadMaps(MapType.COREMAP); @@ -82,6 +117,17 @@ public class Persistence { } } + public void loadSettings() throws DatabaseException { + try { + settings = gsonHandler.loadSettings(); + log.info("Application settings successfully loaded from file!"); + } + catch(Exception e){ + log.error(e); + throw new DatabaseException("Loading settings from local storage failed!"); + } + } + public void updateCoreMaps() throws DatabaseException { try { coreMaps = db.getCoreMaps(); @@ -126,6 +172,39 @@ public class Persistence { } } + public String getGameMap(String mapSelected, boolean choseCoremaps) throws DatabaseException{ + + String mapID = mapSelected.substring(mapSelected.indexOf("(") + 1, mapSelected.length() - 1); + if (choseCoremaps) { + for (int index = 0; index < coreMaps.size(); index++) { + if (coreMaps.get(index).getMapID().equals(mapID)) { + return coreMaps.get(index).getMapID(); + } + } + } else { + for (int index = 0; index < communityMaps.size(); index++) { + if (communityMaps.get(index).getMapID().equals(mapID)) { + return coreMaps.get(index).getMapID(); + } + } + } + throw new DatabaseException("Database error - Map not found!"); + } + + public void savePlayerStatistics() throws DatabaseException{ //on shutdown of game + try { + if (account.getAccountType() == AccountType.LOCAL) { + gsonHandler.saveStats(statistics); + } else if (account.getAccountType() == AccountType.ONLINE) { + db.updatePlayerStats(statistics, account); + } + log.info("Statistics successfully saved!"); + } + catch(Exception e){ + log.error(e); + } + } + public void saveCreatedMapLocally(MapData map) throws DatabaseException { try { for(int i = 0; communityMaps.size() > i; i++){ @@ -168,15 +247,18 @@ public class Persistence { } } - public void loadSettings() throws DatabaseException { - try { - settings = gsonHandler.loadSettings(); - log.info("Application settings successfully loaded from file!"); + public void updatePlayerStatistics(int kills, int deaths, int gameTime, int blocksDestroyed, boolean gameWon) throws DatabaseException{ //after game round + statistics.addKills(kills); + statistics.addDeaths(deaths); + statistics.addGameTime(gameTime); + statistics.addBlocksDestroyed(blocksDestroyed); + if(gameWon){ + statistics.addGamesWon(); } - catch(Exception e){ - log.error(e); - throw new DatabaseException("Loading settings from local storage failed!"); + else{ + statistics.addGamesLost(); } + log.info("Statistics successfully updated!"); } public void createAccount(String playerName, String password, AccountType type) throws DatabaseException { @@ -219,17 +301,32 @@ public class Persistence { } } - public void loadPlayerAccount() throws DatabaseException { + public void createRemoteCommunityMapsList(){ //for Map-Browser try { - gsonHandler.loadAccount(); - log.info("Player account successfully loaded!"); + communityMapsListRemote = db.getCommunityMapsList(); + log.info("MapList successfully retrieved from server!"); + //log.info(communityMapsListRemote.get(0).getMapName()); //for testing purposes } catch(Exception e){ log.error(e); - throw new DatabaseException("Loading player account data from local storage failed!"); } } + //kommen beide eigentlich eher in die RuntimeInfo +/* public void createCoreMapsList(){ //for dropdown list in "create" scene + coreMapsListLocal = new ArrayList<String>(); + for(int i = 0; i < coreMaps.size(); i++){ + coreMapsListLocal.add(coreMaps.get(i).getMapName() + " (" + coreMaps.get(i).getMapID() + ")"); + } + } + + public void createLocalCommunityMapsList(){ //for dropdown list in "create" scene + communityMapsListLocal = new ArrayList<String>(); + for (int i = 0; i < communityMaps.size(); i++) { + communityMapsListLocal.add(communityMaps.get(i).getMapName() + " (" + communityMaps.get(i).getMapID() + ")"); + } + }*/ + public void verifyPlayerAccount() throws DatabaseException { try { if (account.getAccountType() == AccountType.NONE) { @@ -270,55 +367,39 @@ public class Persistence { } } - public void loadPlayerStatistics() throws DatabaseException { + public void verifyCoreMaps() throws DatabaseException{ try { - if (account.getAccountType() == AccountType.LOCAL) { - statistics = gsonHandler.loadStats(); - } else { - statistics = db.getStatistics(account.getPlayerName()); - } - log.info("Player statistics successfully loaded!"); - } - catch(SQLException e){ - log.error(e); - if(e.getMessage().contains("ORA-17868") | e.getMessage().contains("ORA-01017")){ - throw new DatabaseException("No connection to SQL server!"); - } - else{ - throw new DatabaseException("Unknown Database Error. Retrieving statistics from SQL server failed!"); + for (int i = 0; coreMaps.size() > i; i++) { + Parser.mapDataValid(coreMaps.get(i).getMapData()); + Parser.mapNameValid(coreMaps.get(i).getMapName()); + Parser.sha1HexHashValid(coreMaps.get(i).getMapID()); + if(!HashGenerator.hashAndHex(coreMaps.get(i).getMapData()).equals(coreMaps.get(i).getMapID())){ + throw new DatabaseException("mapData Hash does not match mapID!"); + } } + log.info("Core-Maps data is valid!"); } - catch(GSONException e){ + catch(Exception e){ log.error(e); throw new DatabaseException(e.getMessage()); } } - public void updatePlayerStatistics(int kills, int deaths, int gameTime, int blocksDestroyed, boolean gameWon) throws DatabaseException{ //after game round - statistics.addKills(kills); - statistics.addDeaths(deaths); - statistics.addGameTime(gameTime); - statistics.addBlocksDestroyed(blocksDestroyed); - if(gameWon){ - statistics.addGamesWon(); - } - else{ - statistics.addGamesLost(); - } - log.info("Statistics successfully updated!"); - } - - public void savePlayerStatistics() throws DatabaseException{ //on shutdown of game + public void verifyCommunityMaps() throws DatabaseException{ try { - if (account.getAccountType() == AccountType.LOCAL) { - gsonHandler.saveStats(statistics); - } else if (account.getAccountType() == AccountType.ONLINE) { - db.updatePlayerStats(statistics, account); + for (int i = 0; communityMaps.size() > i; i++) { + Parser.mapDataValid(communityMaps.get(i).getMapData()); + Parser.mapNameValid(communityMaps.get(i).getMapName()); + Parser.sha1HexHashValid(communityMaps.get(i).getMapID()); + if(!HashGenerator.hashAndHex(communityMaps.get(i).getMapData()).equals(communityMaps.get(i).getMapID())){ + throw new DatabaseException("mapData Hash does not match mapID!"); + } } - log.info("Statistics successfully saved!"); + log.info("Community-Maps data is valid!"); } catch(Exception e){ log.error(e); + throw new DatabaseException(e.getMessage()); } } @@ -369,85 +450,4 @@ public class Persistence { } } - public void verifyCoreMaps() throws DatabaseException{ - try { - for (int i = 0; coreMaps.size() > i; i++) { - Parser.mapDataValid(coreMaps.get(i).getMapData()); - Parser.mapNameValid(coreMaps.get(i).getMapName()); - Parser.sha1HexHashValid(coreMaps.get(i).getMapID()); - if(!HashGenerator.hashAndHex(coreMaps.get(i).getMapData()).equals(coreMaps.get(i).getMapID())){ - throw new DatabaseException("mapData Hash does not match mapID!"); - } - } - log.info("Core-Maps data is valid!"); - } - catch(Exception e){ - log.error(e); - throw new DatabaseException(e.getMessage()); - } - } - - public void verifyCommunityMaps() throws DatabaseException{ - try { - for (int i = 0; communityMaps.size() > i; i++) { - Parser.mapDataValid(communityMaps.get(i).getMapData()); - Parser.mapNameValid(communityMaps.get(i).getMapName()); - Parser.sha1HexHashValid(communityMaps.get(i).getMapID()); - if(!HashGenerator.hashAndHex(communityMaps.get(i).getMapData()).equals(communityMaps.get(i).getMapID())){ - throw new DatabaseException("mapData Hash does not match mapID!"); - } - } - log.info("Community-Maps data is valid!"); - } - catch(Exception e){ - log.error(e); - throw new DatabaseException(e.getMessage()); - } - } - - //kommen beide eigentlich eher in die RuntimeInfo -/* public void createCoreMapsList(){ //for dropdown list in "create" scene - coreMapsListLocal = new ArrayList<String>(); - for(int i = 0; i < coreMaps.size(); i++){ - coreMapsListLocal.add(coreMaps.get(i).getMapName() + " (" + coreMaps.get(i).getMapID() + ")"); - } - } - - public void createLocalCommunityMapsList(){ //for dropdown list in "create" scene - communityMapsListLocal = new ArrayList<String>(); - for (int i = 0; i < communityMaps.size(); i++) { - communityMapsListLocal.add(communityMaps.get(i).getMapName() + " (" + communityMaps.get(i).getMapID() + ")"); - } - }*/ - - public void createRemoteCommunityMapsList(){ //for Map-Browser - try { - communityMapsListRemote = db.getCommunityMapsList(); - log.info("MapList successfully retrieved from server!"); - //log.info(communityMapsListRemote.get(0).getMapName()); //for testing purposes - } - catch(Exception e){ - log.error(e); - } - } - - public String getGameMap(String mapSelected, boolean choseCoremaps) throws DatabaseException{ - - String mapID = mapSelected.substring(mapSelected.indexOf("(") + 1, mapSelected.length() - 1); - if (choseCoremaps) { - for (int index = 0; index < coreMaps.size(); index++) { - if (coreMaps.get(index).getMapID().equals(mapID)) { - return coreMaps.get(index).getMapID(); - } - } - } else { - for (int index = 0; index < communityMaps.size(); index++) { - if (communityMaps.get(index).getMapID().equals(mapID)) { - return coreMaps.get(index).getMapID(); - } - } - } - throw new DatabaseException("Database error - Map not found!"); - } - }