Skip to content

Commit

Permalink
fix: don't save empty winners data
Browse files Browse the repository at this point in the history
  • Loading branch information
RoinujNosde committed Oct 24, 2022
1 parent 29c9a86 commit 96cb5a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,14 @@ public void close() {

private void update(Winners winners) {
HashSet<GameConfiguration> 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) {
Expand All @@ -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();
}
Expand All @@ -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<GameConfiguration> updated = new HashSet<>();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/roinujnosde/titansbattle/types/Winners.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void setWinners(String game, List<UUID> 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);
Expand Down

0 comments on commit 96cb5a9

Please sign in to comment.