Signed-off-by: Chaoscaot <max@maxsp.de>
This commit is contained in:
2025-10-28 21:31:57 +01:00
parent b51ea484e4
commit a8eaf3daa7
3 changed files with 21 additions and 23 deletions
@@ -59,7 +59,7 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
@JvmStatic @JvmStatic
fun getAll(user: SteamwarUser) = fromSql( fun getAll(user: SteamwarUser) = fromSql(
"WITH RECURSIVE Nodes AS (SELECT NodeId, ParentId as ParentNode FROM NodeMember WHERE UserId = ?} UNION SELECT NodeId, ParentNode FROM SchematicNode WHERE NodeOwner = ?), RSN AS ( SELECT NodeId, ParentNode FROM Nodes UNION SELECT SN.NodeId, SN.ParentNode FROM SchematicNode SN, RSN WHERE SN.ParentNode = RSN.NodeId ) SELECT SN.* FROM RSN INNER JOIN SchematicNode SN ON RSN.NodeId = SN.NodeId", "WITH RECURSIVE Nodes AS (SELECT NodeId, ParentId as ParentNode FROM NodeMember WHERE UserId = ? UNION SELECT NodeId, ParentNode FROM SchematicNode WHERE NodeOwner = ?), RSN AS ( SELECT NodeId, ParentNode FROM Nodes UNION SELECT SN.NodeId, SN.ParentNode FROM SchematicNode SN, RSN WHERE SN.ParentNode = RSN.NodeId ) SELECT SN.* FROM RSN INNER JOIN SchematicNode SN ON RSN.NodeId = SN.NodeId",
listOf( listOf(
IntegerColumnType() to user.getId(), IntegerColumnType() to user.getId(),
IntegerColumnType() to user.getId() IntegerColumnType() to user.getId()
@@ -72,11 +72,10 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
@JvmStatic @JvmStatic
fun list(user: SteamwarUser, schematicId: Int?) = fromSql( fun list(user: SteamwarUser, schematicId: Int?) = fromSql(
"SELECT SchematicNode.NodeId, NodeOwner, NodeName, NM.ParentId AS ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode INNER JOIN NodeMember NM on SchematicNode.NodeId = NM.NodeId WHERE NM.ParentId <=> ? AND NM.UserId = ? UNION ALL SELECT SchematicNode.NodeId, NodeOwner, ? AS EffectiveOwner, NodeName, ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode WHERE (? IS NULL AND ParentNode IS NULL AND NodeOwner = ?) OR (? IS NOT NULL AND ParentNode = ?) ORDER BY NodeName", "SELECT SchematicNode.NodeId, NodeOwner, NodeName, NM.ParentId AS ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode INNER JOIN NodeMember NM on SchematicNode.NodeId = NM.NodeId WHERE NM.ParentId <=> ? AND NM.UserId = ? UNION ALL SELECT SchematicNode.NodeId, NodeOwner, NodeName, ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode WHERE (? IS NULL AND ParentNode IS NULL AND NodeOwner = ?) OR (? IS NOT NULL AND ParentNode = ?) ORDER BY NodeName",
listOf( listOf(
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
IntegerColumnType() to user.getId(), IntegerColumnType() to user.getId(),
IntegerColumnType() to user.getId(),
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
IntegerColumnType() to user.getId(), IntegerColumnType() to user.getId(),
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
@@ -86,12 +85,11 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
@JvmStatic @JvmStatic
fun byParentName(user: SteamwarUser, schematicId: Int?, name: String) = fromSql( fun byParentName(user: SteamwarUser, schematicId: Int?, name: String) = fromSql(
"SELECT SchematicNode.NodeId, NodeOwner, NodeName, NM.ParentId AS ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode INNER JOIN NodeMember NM on SchematicNode.NodeId = NM.NodeId WHERE NM.ParentId <=> ? AND NM.UserId = ? AND SchematicNode.NodeName = ? UNION ALL SELECT SchematicNode.NodeId, NodeOwner, ? AS EffectiveOwner, NodeName, ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode WHERE ((? IS NULL AND ParentNode IS NULL AND NodeOwner = ?) OR (? IS NOT NULL AND ParentNode = ?)) AND NodeName = ?", "SELECT SchematicNode.NodeId, NodeOwner, NodeName, NM.ParentId AS ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode INNER JOIN NodeMember NM on SchematicNode.NodeId = NM.NodeId WHERE NM.ParentId <=> ? AND NM.UserId = ? AND SchematicNode.NodeName = ? UNION ALL SELECT SchematicNode.NodeId, NodeOwner, NodeName, ParentNode, LastUpdate, NodeItem, NodeType, NodeRank, Config FROM SchematicNode WHERE ((? IS NULL AND ParentNode IS NULL AND NodeOwner = ?) OR (? IS NOT NULL AND ParentNode = ?)) AND NodeName = ?",
listOf( listOf(
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
IntegerColumnType() to user.getId(), IntegerColumnType() to user.getId(),
VarCharColumnType() to name, VarCharColumnType() to name,
IntegerColumnType() to user.getId(),
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
IntegerColumnType() to user.getId(), IntegerColumnType() to user.getId(),
IntegerColumnType() to schematicId, IntegerColumnType() to schematicId,
@@ -342,8 +340,8 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
fun isDir() = nodeType == null fun isDir() = nodeType == null
private fun <T> checkDir(block: () -> T): T { private fun <T> checkDir(block: () -> T): T {
if (!isDir()) { if (isDir()) {
throw IllegalStateException("Node is not a directory") throw IllegalStateException("Node is a directory")
} }
return block() return block()
@@ -387,12 +385,12 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
return breadcrumbs return breadcrumbs
} }
fun generateBreadcrumbs(split: String, user: SteamwarUser): String { fun generateBreadcrumbs(split: String, user: SteamwarUser): String = useDb {
val builder: StringBuilder = StringBuilder(name) val builder: StringBuilder = StringBuilder(name)
if (isDir()) { if (isDir()) {
builder.append(split) builder.append(split)
} }
var currentNode: SchematicNode? = this var currentNode: SchematicNode? = this@SchematicNode
while (currentNode != null) { while (currentNode != null) {
currentNode = currentNode currentNode = currentNode
.let { .let {
@@ -404,12 +402,12 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
builder.insert(0, split).insert(0, it.name) builder.insert(0, split).insert(0, it.name)
} }
} }
return builder.toString() return@useDb builder.toString()
} }
fun generateBreadcrumbsMap(user: SteamwarUser): List<Pair<String, Int>> { fun generateBreadcrumbsMap(user: SteamwarUser): List<Pair<String, Int>> = useDb {
val map = mutableListOf<Pair<String, Int>>() val map = mutableListOf<Pair<String, Int>>()
var currentNode: SchematicNode? = this var currentNode: SchematicNode? = this@SchematicNode
while (currentNode != null) { while (currentNode != null) {
currentNode = currentNode currentNode = currentNode
.also { .also {
@@ -421,7 +419,7 @@ class SchematicNode(id: EntityID<Int>) : IntEntity(id) {
} }
?.let { findById(it) } ?.let { findById(it) }
} }
return map return@useDb map
} }
fun accessibleByUser(user: SteamwarUser) = schematicAccessibleForUser(user, nodeId) fun accessibleByUser(user: SteamwarUser) = schematicAccessibleForUser(user, nodeId)
@@ -42,7 +42,7 @@ object SteamwarUserTable : IntIdTable("UserData", "id") {
val team = integer("Team") val team = integer("Team")
val leader = bool("Leader") val leader = bool("Leader")
val locale = varchar("Locale", 16).nullable() val locale = varchar("Locale", 16).nullable()
val manualeLocale = bool("ManualeLocale") val manualLocale = bool("ManualLocale")
val bedrock = bool("Bedrock") val bedrock = bool("Bedrock")
val password = text("Password").nullable() val password = text("Password").nullable()
val discordId = long("DiscordId").nullable() val discordId = long("DiscordId").nullable()
@@ -171,7 +171,7 @@ class SteamwarUser(id: EntityID<Int>): IntEntity(id) {
var locale: Locale by SteamwarUserTable.locale var locale: Locale by SteamwarUserTable.locale
.transform({ it.toLanguageTag() }, { it?.let { Locale.forLanguageTag(it) } ?: Locale.getDefault()}) .transform({ it.toLanguageTag() }, { it?.let { Locale.forLanguageTag(it) } ?: Locale.getDefault()})
var manualLocale by SteamwarUserTable.manualeLocale var manualLocale by SteamwarUserTable.manualLocale
var bedrock by SteamwarUserTable.bedrock var bedrock by SteamwarUserTable.bedrock
private var passwordInternal by SteamwarUserTable.password private var passwordInternal by SteamwarUserTable.password
var password: String? var password: String?
@@ -31,22 +31,22 @@ import org.jetbrains.exposed.v1.jdbc.upsert
import java.util.UUID import java.util.UUID
object UserConfigTable: CompositeIdTable("UserConfig") { object UserConfigTable: CompositeIdTable("UserConfig") {
val userId = reference("userId", SteamwarUserTable) val userId = reference("User", SteamwarUserTable)
val key = varchar("Key", 32) val config = varchar("Config", 32)
val value = text("Value") val value = text("Value", eagerLoading = true)
override val primaryKey = PrimaryKey(userId, key) override val primaryKey = PrimaryKey(userId, config)
} }
class UserConfig(id: EntityID<CompositeID>): CompositeEntity(id) { class UserConfig(id: EntityID<CompositeID>): CompositeEntity(id) {
val userId by UserConfigTable.userId val userId by UserConfigTable.userId
val key by UserConfigTable.key val config by UserConfigTable.config
var value by UserConfigTable.value var value by UserConfigTable.value
companion object: CompositeEntityClass<UserConfig>(UserConfigTable) { companion object: CompositeEntityClass<UserConfig>(UserConfigTable) {
@JvmStatic @JvmStatic
fun getConfig(userId: Int, config: String) = useDb { fun getConfig(userId: Int, config: String) = useDb {
find { (UserConfigTable.userId eq userId) and (UserConfigTable.key eq config) }.firstOrNull()?.value find { (UserConfigTable.userId eq userId) and (UserConfigTable.config eq config) }.firstOrNull()?.value
} }
@JvmStatic @JvmStatic
@@ -59,7 +59,7 @@ class UserConfig(id: EntityID<CompositeID>): CompositeEntity(id) {
} else { } else {
UserConfigTable.upsert { UserConfigTable.upsert {
it[UserConfigTable.userId] = id it[UserConfigTable.userId] = id
it[UserConfigTable.key] = config it[UserConfigTable.config] = config
it[UserConfigTable.value] = value it[UserConfigTable.value] = value
} }
} }
@@ -70,7 +70,7 @@ class UserConfig(id: EntityID<CompositeID>): CompositeEntity(id) {
@JvmStatic @JvmStatic
fun removePlayerConfig(id: Int, config: String) = useDb { fun removePlayerConfig(id: Int, config: String) = useDb {
find { (UserConfigTable.userId eq id) and (UserConfigTable.key eq config) }.firstOrNull()?.delete() find { (UserConfigTable.userId eq id) and (UserConfigTable.config eq config) }.firstOrNull()?.delete()
} }
@JvmStatic @JvmStatic