UserPerm model, lazy punishment loading #57
@ -82,27 +82,27 @@ public class SteamwarUser {
|
||||
SteamwarUser user = usersByName.get(userName.toLowerCase());
|
||||
if(user != null)
|
||||
return user;
|
||||
return loadPunishments(byName.select(userName));
|
||||
return byName.select(userName);
|
||||
}
|
||||
|
||||
public static SteamwarUser get(UUID uuid){
|
||||
SteamwarUser user = usersByUUID.get(uuid);
|
||||
if(user != null)
|
||||
return user;
|
||||
return loadPunishments(byUUID.select(uuid));
|
||||
return byUUID.select(uuid);
|
||||
}
|
||||
|
||||
public static SteamwarUser get(int id) {
|
||||
SteamwarUser user = usersById.get(id);
|
||||
if(user != null)
|
||||
return user;
|
||||
return loadPunishments(byID.select(id));
|
||||
return byID.select(id);
|
||||
}
|
||||
|
||||
public static SteamwarUser get(Long discordId) {
|
||||
if(usersByDiscord.containsKey(discordId))
|
||||
return usersByDiscord.get(discordId);
|
||||
return loadPunishments(byDiscord.select(discordId));
|
||||
return byDiscord.select(discordId);
|
||||
}
|
||||
|
||||
public static SteamwarUser getOrCreate(UUID uuid, String name, Consumer<UUID> newPlayer, BiConsumer<String, String> nameUpdate) {
|
||||
@ -124,11 +124,11 @@ public class SteamwarUser {
|
||||
}
|
||||
|
||||
public static List<SteamwarUser> getServerTeam() {
|
||||
return loadPunishments(getServerTeam.listSelect());
|
||||
return getServerTeam.listSelect();
|
||||
}
|
||||
|
||||
public static List<SteamwarUser> getTeam(int teamId) {
|
||||
return loadPunishments(byTeam.listSelect(teamId));
|
||||
return byTeam.listSelect(teamId);
|
||||
}
|
||||
|
||||
public static void batchCache(Set<Integer> ids) {
|
||||
@ -137,23 +137,10 @@ public class SteamwarUser {
|
||||
return;
|
||||
|
||||
try (SelectStatement<SteamwarUser> batch = new SelectStatement<>(table, "SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")")) {
|
||||
loadPunishments(batch.listSelect());
|
||||
batch.listSelect();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<SteamwarUser> loadPunishments(List<SteamwarUser> users) {
|
||||
users.forEach(user -> user.punishments = Punishment.getPunishmentsOfPlayer(user.getId()));
|
||||
return users;
|
||||
}
|
||||
|
||||
private static SteamwarUser loadPunishments(SteamwarUser user) {
|
||||
if(user == null)
|
||||
return null;
|
||||
|
||||
user.punishments = Punishment.getPunishmentsOfPlayer(user.getId());
|
||||
return user;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Field(keys = {Table.PRIMARY}, autoincrement = true)
|
||||
private final int id;
|
||||
@ -162,6 +149,7 @@ public class SteamwarUser {
|
||||
@Getter
|
||||
@Field
|
||||
private String userName;
|
||||
@Deprecated
|
||||
@Getter
|
||||
@Field(def = "'Member'")
|
||||
private final UserGroup userGroup;
|
||||
@ -179,7 +167,9 @@ public class SteamwarUser {
|
||||
@Field(keys = {"discordId"}, nullable = true)
|
||||
private Long discordId;
|
||||
|
||||
private Map<Punishment.PunishmentType, Punishment> punishments;
|
||||
private Map<Punishment.PunishmentType, Punishment> punishments = null;
|
||||
private Set<UserPerm> permissions = null;
|
||||
private UserPerm.Prefix prefix = null;
|
||||
|
||||
public SteamwarUser(int id, UUID uuid, String userName, UserGroup userGroup, int team, boolean leader, Locale locale, boolean manualLocale, Long discordId) {
|
||||
this.id = id;
|
||||
@ -211,10 +201,12 @@ public class SteamwarUser {
|
||||
}
|
||||
|
||||
public Punishment getPunishment(Punishment.PunishmentType type) {
|
||||
initPunishments();
|
||||
return punishments.getOrDefault(type, null);
|
||||
}
|
||||
|
||||
public boolean isPunished(Punishment.PunishmentType punishment) {
|
||||
initPunishments();
|
||||
if (!punishments.containsKey(punishment)) {
|
||||
return false;
|
||||
}
|
||||
@ -228,6 +220,21 @@ public class SteamwarUser {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean hasPerm(UserPerm perm) {
|
||||
initPerms();
|
||||
return permissions.contains(perm);
|
||||
}
|
||||
|
||||
public Set<UserPerm> perms() {
|
||||
initPerms();
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public UserPerm.Prefix prefix() {
|
||||
initPerms();
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public double getOnlinetime() {
|
||||
return getPlaytime.select(rs -> {
|
||||
if (rs.next() && rs.getBigDecimal("Playtime") != null)
|
||||
@ -245,6 +252,7 @@ public class SteamwarUser {
|
||||
}
|
||||
|
||||
public void punish(Punishment.PunishmentType punishment, Timestamp time, String banReason, int from, boolean perma) {
|
||||
initPunishments();
|
||||
punishments.remove(punishment);
|
||||
punishments.put(punishment, Punishment.createPunishment(id, from, punishment, banReason, time, perma));
|
||||
}
|
||||
@ -277,4 +285,19 @@ public class SteamwarUser {
|
||||
usersByDiscord.put(discordId, this);
|
||||
}
|
||||
}
|
||||
|
||||
private void initPunishments() {
|
||||
if(punishments != null)
|
||||
return;
|
||||
|
||||
punishments = Punishment.getPunishmentsOfPlayer(id);
|
||||
}
|
||||
|
||||
private void initPerms() {
|
||||
if(permissions != null)
|
||||
return;
|
||||
|
||||
permissions = UserPerm.getPerms(id);
|
||||
prefix = permissions.stream().filter(UserPerm.prefixes::containsKey).findAny().map(UserPerm.prefixes::get).orElse(UserPerm.emptyPrefix);
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ package de.steamwar.sql;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Deprecated
|
||||
@AllArgsConstructor
|
||||
public enum UserGroup {
|
||||
Admin("§4", "§e", "Admin", true, true, true, true),
|
||||
|
89
src/de/steamwar/sql/UserPerm.java
Normale Datei
89
src/de/steamwar/sql/UserPerm.java
Normale Datei
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.sql;
|
||||
|
||||
import de.steamwar.sql.internal.Field;
|
||||
import de.steamwar.sql.internal.SelectStatement;
|
||||
import de.steamwar.sql.internal.SqlTypeMapper;
|
||||
import de.steamwar.sql.internal.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum UserPerm {
|
||||
PREFIX_NONE, // special value, not stored in database
|
||||
PREFIX_YOUTUBER,
|
||||
PREFIX_GUIDE,
|
||||
PREFIX_BUILDER,
|
||||
PREFIX_SUPPORTER,
|
||||
PREFIX_MODERATOR,
|
||||
PREFIX_DEVELOPER,
|
||||
PREFIX_ADMIN,
|
||||
|
||||
RESTRICTED_MODS,
|
||||
COLOR_CHAT,
|
||||
Chaoscaot markierte diese Unterhaltung als gelöst
Veraltet
|
||||
TEAM,
|
||||
BUILD,
|
||||
CHECK,
|
||||
MODERATION,
|
||||
ADMINISTRATION;
|
||||
|
||||
public static final Map<UserPerm, Prefix> prefixes;
|
||||
public static final Prefix emptyPrefix;
|
||||
static {
|
||||
SqlTypeMapper.nameEnumMapper(UserPerm.class);
|
||||
Map<UserPerm, Prefix> p = new EnumMap<>(UserPerm.class);
|
||||
emptyPrefix = new Prefix("§7", "");
|
||||
p.put(PREFIX_NONE, emptyPrefix);
|
||||
p.put(PREFIX_YOUTUBER, new Prefix("§5", "YT"));
|
||||
p.put(PREFIX_GUIDE, new Prefix("§f", "Guide"));
|
||||
|
||||
p.put(PREFIX_BUILDER, new Prefix("§2", "Arch"));
|
||||
p.put(PREFIX_SUPPORTER, new Prefix("§9", "Sup"));
|
||||
p.put(PREFIX_MODERATOR, new Prefix("§6", "Mod"));
|
||||
p.put(PREFIX_DEVELOPER, new Prefix("§b", "Dev"));
|
||||
p.put(PREFIX_ADMIN, new Prefix("§4", "Admin"));
|
||||
prefixes = Collections.unmodifiableMap(p);
|
||||
}
|
||||
|
||||
private static final Table<UserPermTable> table = new Table<>(UserPermTable.class, "UserPerm");
|
||||
private static final SelectStatement<UserPermTable> getPerms = table.selectFields("user");
|
||||
|
||||
public static Set<UserPerm> getPerms(int user) {
|
||||
return getPerms.listSelect(user).stream().map(up -> up.perm).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public static class Prefix {
|
||||
private final String colorCode;
|
||||
private final String chatPrefix;
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
private static class UserPermTable {
|
||||
@Field(keys = {Table.PRIMARY})
|
||||
private final int user;
|
||||
@Field(keys = {Table.PRIMARY})
|
||||
private final UserPerm perm;
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren
Permission vorschlag: