diff --git a/src/de/steamwar/sql/SteamwarUser.java b/src/de/steamwar/sql/SteamwarUser.java index 254857f..3a9cfa5 100644 --- a/src/de/steamwar/sql/SteamwarUser.java +++ b/src/de/steamwar/sql/SteamwarUser.java @@ -83,27 +83,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 List getAll() { @@ -129,11 +129,11 @@ public class SteamwarUser { } public static List getServerTeam() { - return loadPunishments(getServerTeam.listSelect()); + return getServerTeam.listSelect(); } public static List getTeam(int teamId) { - return loadPunishments(byTeam.listSelect(teamId)); + return byTeam.listSelect(teamId); } public static void batchCache(Set ids) { @@ -142,18 +142,10 @@ public class SteamwarUser { return; try (SelectStatement 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 loadPunishments(List users) { - return users; - } - - private static SteamwarUser loadPunishments(SteamwarUser user) { - return user; - } - @Getter @Field(keys = {Table.PRIMARY}, autoincrement = true) private final int id; @@ -162,6 +154,7 @@ public class SteamwarUser { @Getter @Field private String userName; + @Deprecated @Getter @Field(def = "'Member'") private final UserGroup userGroup; @@ -179,7 +172,9 @@ public class SteamwarUser { @Field(keys = {"discordId"}, nullable = true) private Long discordId; - private Map punishments; + private Map punishments = null; + private Set 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 +206,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 +225,21 @@ public class SteamwarUser { return true; } + public boolean hasPerm(UserPerm perm) { + initPerms(); + return permissions.contains(perm); + } + + public Set 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 +257,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 +290,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); + } } diff --git a/src/de/steamwar/sql/UserGroup.java b/src/de/steamwar/sql/UserGroup.java index aea2dba..6a2b1ad 100644 --- a/src/de/steamwar/sql/UserGroup.java +++ b/src/de/steamwar/sql/UserGroup.java @@ -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), diff --git a/src/de/steamwar/sql/UserPerm.java b/src/de/steamwar/sql/UserPerm.java new file mode 100644 index 0000000..386bfc5 --- /dev/null +++ b/src/de/steamwar/sql/UserPerm.java @@ -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 . + */ + +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, + TEAM, + BUILD, + CHECK, + MODERATION, + ADMINISTRATION; + + public static final Map prefixes; + public static final Prefix emptyPrefix; + static { + SqlTypeMapper.nameEnumMapper(UserPerm.class); + Map 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 table = new Table<>(UserPermTable.class, "UserPerm"); + private static final SelectStatement getPerms = table.selectFields("user"); + + public static Set 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; + } +}