From dc470659363d63bb64c9cd8f29b2e61841dd40d7 Mon Sep 17 00:00:00 2001 From: Martin <ms618@hdm-stuttgart.de> Date: Mon, 8 Jan 2024 12:23:31 +0100 Subject: [PATCH] Update: Parser.java (added "localhost" to ip address parser) Update: Persistence.java (new method to update and save statistics) --- .../DataStorage/Classes/Persistence.java | 38 ++++++++++++++----- .../DataStorage/Classes/PlayerAccount.java | 2 +- .../DataStorage/Classes/RuntimeInfo.java | 3 +- .../DataStorage/Classes/Utilities/Parser.java | 4 ++ .../Classes/Utilities/ParserTest.java | 3 +- 5 files changed, 38 insertions(+), 12 deletions(-) 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 f2af47b5..d8dd5bf0 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 db701301..a028edd3 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 6f9c89fe..641449a5 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 a061c289..4ccec6fc 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 e6810e41..62f0b705 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)); } -- GitLab