diff --git a/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java b/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java index 23925db7..eb83c727 100644 --- a/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java +++ b/src/main/java/me/roinujnosde/titansbattle/managers/DatabaseManager.java @@ -142,28 +142,14 @@ public void close() { private void update(Winners winners) { HashSet updated = new HashSet<>(); - String date = new SimpleDateFormat("dd/MM/yyyy").format(winners.getDate()); String update = "UPDATE tb_winners SET killer=?, player_winners=?, winner_group=? WHERE date=? AND game=?;"; try (PreparedStatement statement = getConnection().prepareStatement(update)) { for (GameConfiguration game : getGames()) { - String killer = null; - if (winners.getKiller(game.getName()) != null) { - killer = winners.getKiller(game.getName()).toString(); - } - String player_winners; - JsonArray ja = new JsonArray(); - if (winners.getPlayerWinners(game.getName()) != null) { - winners.getPlayerWinners(game.getName()).stream().map(UUID::toString).forEach(ja::add); + if (winners.isEmpty(game.getName())) { + continue; } - player_winners = new Gson().toJson(ja); - String winner_group = winners.getWinnerGroup(game.getName()); - - statement.setString(1, killer); - statement.setString(2, player_winners); - statement.setString(3, winner_group); - statement.setString(4, date); - statement.setString(5, game.getName()); + setValues(winners, statement, game); int count = statement.executeUpdate(); if (count != 0) { @@ -177,25 +163,8 @@ private void update(Winners winners) { String insert = "INSERT INTO tb_winners (killer, player_winners, winner_group, date, game) VALUES (?, ?, ?, ?, ?);"; try (PreparedStatement statement = getConnection().prepareStatement(insert)) { for (GameConfiguration game : getGames()) { - if (!updated.contains(game)) { - String gameName = game.getName(); - String killer = null; - if (winners.getKiller(gameName) != null) { - killer = winners.getKiller(gameName).toString(); - } - String player_winners; - JsonArray ja = new JsonArray(); - if (winners.getPlayerWinners(gameName) != null) { - winners.getPlayerWinners(gameName).stream().map(UUID::toString).forEach(ja::add); - } - player_winners = new Gson().toJson(ja); - String winner_group = winners.getWinnerGroup(gameName); - - statement.setString(1, killer); - statement.setString(2, player_winners); - statement.setString(3, winner_group); - statement.setString(4, date); - statement.setString(5, gameName); + if (!updated.contains(game) && !winners.isEmpty(game.getName())) { + setValues(winners, statement, game); statement.execute(); } @@ -205,6 +174,29 @@ private void update(Winners winners) { } } + private void setValues(Winners winners, PreparedStatement statement, GameConfiguration game) throws SQLException { + String date = new SimpleDateFormat("dd/MM/yyyy").format(winners.getDate()); + + String name = game.getName(); + String killer = null; + if (winners.getKiller(name) != null) { + killer = winners.getKiller(name).toString(); + } + String player_winners; + JsonArray ja = new JsonArray(); + if (winners.getPlayerWinners(name) != null) { + winners.getPlayerWinners(name).stream().map(UUID::toString).forEach(ja::add); + } + player_winners = new Gson().toJson(ja); + String winner_group = winners.getWinnerGroup(name); + + statement.setString(1, killer); + statement.setString(2, player_winners); + statement.setString(3, winner_group); + statement.setString(4, date); + statement.setString(5, name); + } + private void update(@NotNull String id, @NotNull GroupData data) { HashSet updated = new HashSet<>(); diff --git a/src/main/java/me/roinujnosde/titansbattle/types/Winners.java b/src/main/java/me/roinujnosde/titansbattle/types/Winners.java index 0a73b648..f7920261 100644 --- a/src/main/java/me/roinujnosde/titansbattle/types/Winners.java +++ b/src/main/java/me/roinujnosde/titansbattle/types/Winners.java @@ -86,6 +86,10 @@ public void setWinners(String game, List winners) { playerWinners.put(game, winners); } + public boolean isEmpty(String game) { + return killer.get(game) == null && playerWinners.get(game) == null && winnerGroup.get(game) == null; + } + @Override public int compareTo(@NotNull Winners o) { return this.date.compareTo(o.date);