Archiviert
1
0

Fixing Recursive SQL Select

Dieser Commit ist enthalten in:
Chaoscaot 2020-11-15 12:23:44 +01:00
Ursprung 6607b736ee
Commit 4dd593243e
2 geänderte Dateien mit 53 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.bungeecore.sql; package de.steamwar.bungeecore.sql;
import de.steamwar.bungeecore.BungeeCore;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -27,7 +29,7 @@ import java.util.*;
public class Punishment { public class Punishment {
public static Punishment getPunishmentOfPlayer(int user, PunishmentType type) { 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 { try {
if(!set.next()) if(!set.next())
return null; return null;
@ -38,17 +40,14 @@ public class Punishment {
} }
public static Map<PunishmentType, Punishment> getPunishmentsOfPlayer(int user) { public static Map<PunishmentType, Punishment> 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<PunishmentType, Punishment> punishmentMap = new HashMap<>(); Map<PunishmentType, Punishment> punishmentMap = new HashMap<>();
while (set.next()) { for (PunishmentType type : PunishmentType.values()) {
Punishment punishment = new Punishment(set); Punishment punishment = getPunishmentOfPlayer(user, type);
punishmentMap.put(punishment.getType(), punishment); if(punishment == null)
continue;
punishmentMap.put(type, punishment);
} }
return punishmentMap; return punishmentMap;
} catch (SQLException e) {
throw new SecurityException("Could not Load Punishments", e);
}
} }
public static List<Punishment> getAllPunishmentsOfPlayer(int user) { public static List<Punishment> getAllPunishmentsOfPlayer(int user) {
@ -71,12 +70,13 @@ public class Punishment {
} }
private final Timestamp startTime; private final Timestamp startTime;
private final Timestamp endTime; private Timestamp endTime;
private final PunishmentType type; private final PunishmentType type;
private final int user; private final int user;
private final String reason; private final int id;
private String reason;
private final int punisher; private final int punisher;
private final boolean perma; private boolean perma;
private Punishment(ResultSet set) throws SQLException { private Punishment(ResultSet set) throws SQLException {
user = set.getInt("UserId"); user = set.getInt("UserId");
@ -86,6 +86,7 @@ public class Punishment {
endTime = set.getTimestamp("EndTime"); endTime = set.getTimestamp("EndTime");
punisher = set.getInt("Punisher"); punisher = set.getInt("Punisher");
perma = set.getBoolean("Perma"); perma = set.getBoolean("Perma");
id = set.getInt("PunishmentId");
} }
public Timestamp getStartTime() { public Timestamp getStartTime() {
@ -116,6 +117,36 @@ public class Punishment {
return perma; 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 { public enum PunishmentType {
Ban, Ban,
Mute; Mute;

Datei anzeigen

@ -189,6 +189,10 @@ public class SteamwarUser {
} }
public void ban(Timestamp time, String banReason, int from, boolean perma){ 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.remove(Punishment.PunishmentType.Ban);
punishments.put(Punishment.PunishmentType.Ban, Punishment.createPunishment(id, from, Punishment.PunishmentType.Ban, banReason, time, perma)); 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){ 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.remove(Punishment.PunishmentType.Mute);
punishments.put(Punishment.PunishmentType.Mute, Punishment.createPunishment(id, from, Punishment.PunishmentType.Mute, muteReason, time, perma)); punishments.put(Punishment.PunishmentType.Mute, Punishment.createPunishment(id, from, Punishment.PunishmentType.Mute, muteReason, time, perma));
} }