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 f2af47b5a30458e587911537c200a5a1253fb83f..d8dd5bf0303003f3ab525b04a8b2ea6db4588403 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 @@ -162,8 +162,7 @@ public class Persistence { else{ throw new DatabaseException("Corrupted Account-Type. Please restart game."); } - GsonHandler gson = new GsonHandler(); - gson.saveAccount(account); + gsonHandler.saveAccount(account); } catch(ParserException e){ log.error(e); @@ -199,15 +198,15 @@ public class Persistence { public void verifyPlayerAccount() throws DatabaseException { try { - if (account.getAccountState() == AccountType.NONE) { + if (account.getAccountType() == AccountType.NONE) { throw new DatabaseException("Must create playerAccount first"); - } else if (account.getAccountState() == AccountType.LOCAL) { + } else if (account.getAccountType() == AccountType.LOCAL) { Parser.usernameValid(account.getPlayerName()); - } else if (account.getAccountState() == AccountType.ONLINE) { + } else if (account.getAccountType() == AccountType.ONLINE) { Parser.usernameValid(account.getPlayerName()); Parser.sha1HashValid(account.getAccountPassword()); if (!(account.getAccountPassword().equals(db.checkCredentials(account.getPlayerName())))){ - throw new DatabaseException("Locally stored password does not match online password. Please renter credentials!"); + throw new DatabaseException("Locally stored password does not match online password. Please reenter credentials!"); } } else{ @@ -238,7 +237,7 @@ public class Persistence { public void loadPlayerStatistics() throws DatabaseException { try { - if (account.getAccountState() == AccountType.LOCAL) { + if (account.getAccountType() == AccountType.LOCAL) { statistics = gsonHandler.loadStats(); } else { statistics = db.getStatistics(account.getPlayerName()); @@ -259,8 +258,29 @@ public class Persistence { } } - public void updatePlayerStatistics(){ - //check for account type and update statistics in RAM and persistence + public void updatePlayerStatistics(int kills, int deaths, int gameTime, boolean gameWon){ //after game round + statistics.setKills(statistics.getKills() + kills); + statistics.setDeaths(statistics.getDeaths() + deaths); + statistics.setGameTime(statistics.getGameTime() + gameTime); + if(gameWon){ + statistics.setGamesWon(statistics.getGamesWon() + 1); + } + else{ + statistics.setGamesWon(statistics.getGamesLost() + 1); + } + } + + public void savePlayerStatistics(){ //on shutdown of game + try { + if (account.getAccountType() == AccountType.LOCAL) { + gsonHandler.saveStats(statistics); + } else if (account.getAccountType() == AccountType.ONLINE) { + db.updatePlayerStats(statistics, account); + } + } + catch(Exception e){ + log.error(e); + } } } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PlayerAccount.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PlayerAccount.java index db701301210accb77174f28d2f1e182cebbb144a..a028edd30e66fc1116615efb7764503e3746469a 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PlayerAccount.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/PlayerAccount.java @@ -21,7 +21,7 @@ public class PlayerAccount { return accountPassword; } - public AccountType getAccountState() { + public AccountType getAccountType() { return accountType; } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java index 6f9c89fef44d6c0d152a41558cc0b294cc5b9fef..641449a5164e3aaa72e5aef97324569ec1c7b3c2 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/RuntimeInfo.java @@ -17,6 +17,7 @@ public class RuntimeInfo { protected ArrayList<String> communityMapsListLocal; //for drop-down list protected ArrayList<MapInfo> communityMapsListRemote; //for community map browser protected String mapDataGame; + protected boolean offlineMode; //if Account-Type is online but no SQL connection: start game without stats tracking private RuntimeInfo(){}; @@ -33,7 +34,7 @@ public class RuntimeInfo { try { Parser.mapDataValid(mapData); Parser.mapNameValid(mapName); - return new MapData(HashGenerator.hashAndHex(mapData), mapName, 19, 19, mapData); + return new MapData(HashGenerator.hashAndHex(mapData), mapName, 18, 18, mapData); } catch(Exception e){ log.error(e); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java index a061c289cd76873cc9d66277da08a9207b53862e..4ccec6fc0d9eb8956792cdcc5290cbc1455dae10 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java @@ -83,6 +83,10 @@ public class Parser { public static void ipAddressValid(String address) throws ParserException { + if(address.equals("localhost")){ + return; + } + if(address.length() > 15){ throw new ParserException("IP-Address too long. Must have length of 15 characters including octet dividers (.) !"); } diff --git a/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java index e6810e41bf920eb34c28eda70f93ab2c823f6376..62f0b705af1053613a39adfe2a296fc69099dd98 100644 --- a/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java +++ b/src/test/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/ParserTest.java @@ -90,7 +90,8 @@ class ParserTest { @ParameterizedTest @ValueSource(strings = {"255.255.255.255", "0.0.0.0", - "12.12.12.1"}) + "12.12.12.1", + "localhost"}) void ipAddressValid(String test){ assertDoesNotThrow(() -> Parser.ipAddressValid(test)); }