From 4dd593243e8181bd230ea2c1b37d43d51563cce1 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 15 Nov 2020 12:23:44 +0100 Subject: [PATCH] Fixing Recursive SQL Select --- .../steamwar/bungeecore/sql/Punishment.java | 59 ++++++++++++++----- .../steamwar/bungeecore/sql/SteamwarUser.java | 8 +++ 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/Punishment.java b/src/de/steamwar/bungeecore/sql/Punishment.java index 87d50dd..f6f1c2c 100644 --- a/src/de/steamwar/bungeecore/sql/Punishment.java +++ b/src/de/steamwar/bungeecore/sql/Punishment.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore.sql; +import de.steamwar.bungeecore.BungeeCore; + import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; @@ -27,7 +29,7 @@ import java.util.*; public class Punishment { public static Punishment getPunishmentOfPlayer(int user, PunishmentType type) { - ResultSet set = SQL.select("SELECT * FROM `Punishments` WHERE EndTime < NOW() AND UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1", user, type.name()); + ResultSet set = SQL.select("SELECT * FROM Punishments WHERE UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1", user, type.name()); try { if(!set.next()) return null; @@ -38,17 +40,14 @@ public class Punishment { } public static Map getPunishmentsOfPlayer(int user) { - ResultSet set = SQL.select("SELECT * FROM Punishments WHERE PunishmentId IN (SELECT MAX(PunishmentId) FROM Punishments WHERE UserId = ? GROUP BY Type)", user); - try { - Map punishmentMap = new HashMap<>(); - while (set.next()) { - Punishment punishment = new Punishment(set); - punishmentMap.put(punishment.getType(), punishment); - } - return punishmentMap; - } catch (SQLException e) { - throw new SecurityException("Could not Load Punishments", e); + Map punishmentMap = new HashMap<>(); + for (PunishmentType type : PunishmentType.values()) { + Punishment punishment = getPunishmentOfPlayer(user, type); + if(punishment == null) + continue; + punishmentMap.put(type, punishment); } + return punishmentMap; } public static List getAllPunishmentsOfPlayer(int user) { @@ -71,12 +70,13 @@ public class Punishment { } private final Timestamp startTime; - private final Timestamp endTime; + private Timestamp endTime; private final PunishmentType type; private final int user; - private final String reason; + private final int id; + private String reason; private final int punisher; - private final boolean perma; + private boolean perma; private Punishment(ResultSet set) throws SQLException { user = set.getInt("UserId"); @@ -86,6 +86,7 @@ public class Punishment { endTime = set.getTimestamp("EndTime"); punisher = set.getInt("Punisher"); perma = set.getBoolean("Perma"); + id = set.getInt("PunishmentId"); } public Timestamp getStartTime() { @@ -116,6 +117,36 @@ public class Punishment { return perma; } + public void updateEndTime(int from, String reason, Timestamp newUpdate, boolean perma) { + StringBuilder newReason = new StringBuilder(reason); + if(newUpdate.before(endTime)) + newReason.append(" verkürtz vom ") + .append(getBantime()) + .append(" von ") + .append(SteamwarUser.get(from).getUserName()) + .append(" Grund: ") + .append(reason); + else + newReason.append(" verlängert vom ") + .append(getBantime()) + .append(" von ") + .append(SteamwarUser.get(from).getUserName()) + .append(" Grund: ") + .append(reason); + + SQL.update("UPDATE Punishments SET EndTime = ?, Reason = ?, Perma = ? WHERE PunishmentId = ?", newUpdate, newReason.toString(), perma, id); + this.reason = newReason.toString(); + this.perma = perma; + this.endTime = newUpdate; + } + + public String getBantime() { + if(perma) + return "Permanent"; + else + return endTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT); + } + public enum PunishmentType { Ban, Mute; diff --git a/src/de/steamwar/bungeecore/sql/SteamwarUser.java b/src/de/steamwar/bungeecore/sql/SteamwarUser.java index d092b9e..b1e723f 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -189,6 +189,10 @@ public class SteamwarUser { } public void ban(Timestamp time, String banReason, int from, boolean perma){ + if(isBanned()) { + punishments.get(Punishment.PunishmentType.Ban).updateEndTime(from, banReason, time, perma); + return; + } punishments.remove(Punishment.PunishmentType.Ban); punishments.put(Punishment.PunishmentType.Ban, Punishment.createPunishment(id, from, Punishment.PunishmentType.Ban, banReason, time, perma)); @@ -201,6 +205,10 @@ public class SteamwarUser { } public void mute(Timestamp time, String muteReason, int from, boolean perma){ + if(isMuted()) { + punishments.get(Punishment.PunishmentType.Ban).updateEndTime(from, muteReason, time, perma); + return; + } punishments.remove(Punishment.PunishmentType.Mute); punishments.put(Punishment.PunishmentType.Mute, Punishment.createPunishment(id, from, Punishment.PunishmentType.Mute, muteReason, time, perma)); }