From b51ea484e414808dd180ce3fb4b79b1262fdeba1 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 28 Oct 2025 20:38:02 +0100 Subject: [PATCH] Refactor UserConfig Signed-off-by: Chaoscaot --- .../SQL/src/de/steamwar/sql/UserConfig.kt | 106 +++++++++--------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/CommonCore/SQL/src/de/steamwar/sql/UserConfig.kt b/CommonCore/SQL/src/de/steamwar/sql/UserConfig.kt index c2525a57..7d8b4ff4 100644 --- a/CommonCore/SQL/src/de/steamwar/sql/UserConfig.kt +++ b/CommonCore/SQL/src/de/steamwar/sql/UserConfig.kt @@ -17,57 +17,63 @@ * along with this program. If not, see . */ -package de.steamwar.sql; +package de.steamwar.sql -import de.steamwar.sql.internal.Field; -import de.steamwar.sql.internal.SelectStatement; -import de.steamwar.sql.internal.Statement; -import de.steamwar.sql.internal.Table; -import lombok.AllArgsConstructor; +import de.steamwar.sql.internal.useDb +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.dao.id.CompositeID +import org.jetbrains.exposed.v1.core.dao.id.CompositeIdTable +import org.jetbrains.exposed.v1.core.dao.id.EntityID +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.dao.CompositeEntity +import org.jetbrains.exposed.v1.dao.CompositeEntityClass +import org.jetbrains.exposed.v1.jdbc.upsert +import java.util.UUID -import java.util.UUID; +object UserConfigTable: CompositeIdTable("UserConfig") { + val userId = reference("userId", SteamwarUserTable) + val key = varchar("Key", 32) + val value = text("Value") -@AllArgsConstructor -public class UserConfig { - - private static final Table table = new Table<>(UserConfig.class); - private static final SelectStatement select = table.select(Table.PRIMARY); - private static final Statement insert = table.insertAll(); - private static final Statement delete = table.delete(Table.PRIMARY); - - @Field(keys = {Table.PRIMARY}) - private final int user; - @Field(keys = {Table.PRIMARY}) - private final String config; - @Field - private final String value; - - public static String getConfig(UUID player, String config) { - return getConfig(SteamwarUser.get(player).getId(), config); - } - - public static String getConfig(int player, String config) { - UserConfig value = select.select(player, config); - return value != null ? value.value : null; - } - - public static void updatePlayerConfig(UUID uuid, String config, String value) { - updatePlayerConfig(SteamwarUser.get(uuid).getId(), config, value); - } - - public static void updatePlayerConfig(int id, String config, String value) { - if (value == null) { - removePlayerConfig(id, config); - return; - } - insert.update(id, config, value); - } - - public static void removePlayerConfig(UUID uuid, String config) { - removePlayerConfig(SteamwarUser.get(uuid).getId(), config); - } - - public static void removePlayerConfig(int id, String config) { - delete.update(id, config); - } + override val primaryKey = PrimaryKey(userId, key) } + +class UserConfig(id: EntityID): CompositeEntity(id) { + val userId by UserConfigTable.userId + val key by UserConfigTable.key + var value by UserConfigTable.value + + companion object: CompositeEntityClass(UserConfigTable) { + @JvmStatic + fun getConfig(userId: Int, config: String) = useDb { + find { (UserConfigTable.userId eq userId) and (UserConfigTable.key eq config) }.firstOrNull()?.value + } + + @JvmStatic + fun getConfig(user: UUID, config: String) = getConfig(SteamwarUser.get(user)!!.id.value, config) + + @JvmStatic + fun updatePlayerConfig(id: Int, config: String, value: String?) = useDb { + if (value == null) { + removePlayerConfig(id, config) + } else { + UserConfigTable.upsert { + it[UserConfigTable.userId] = id + it[UserConfigTable.key] = config + it[UserConfigTable.value] = value + } + } + } + + @JvmStatic + fun updatePlayerConfig(user: UUID, config: String, value: String?) = updatePlayerConfig(SteamwarUser.get(user)!!.id.value, config, value) + + @JvmStatic + fun removePlayerConfig(id: Int, config: String) = useDb { + find { (UserConfigTable.userId eq id) and (UserConfigTable.key eq config) }.firstOrNull()?.delete() + } + + @JvmStatic + fun removePlayerConfig(user: UUID, config: String) = removePlayerConfig(SteamwarUser.get(user)!!.id.value, config) + } +} \ No newline at end of file