From 3a3e80a1793601293402bca5cafdfde664121b14 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sat, 28 Mar 2026 17:04:05 -0400 Subject: 2.0.0-alpha1 --- core/src/db/Database.kt | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'core/src/db/Database.kt') diff --git a/core/src/db/Database.kt b/core/src/db/Database.kt index 27864b3..efcb92f 100644 --- a/core/src/db/Database.kt +++ b/core/src/db/Database.kt @@ -49,14 +49,40 @@ class Database(plugin: Khs) { .filterNotNull() } - fun upsertPlayer(player: Player) = transaction(db) { Players.upsert { it.fromPlayer(player) } } + fun upsertPlayer(player: Player) = + transaction(db) { + val id = player.uuid.toString() + val exists = Players.selectAll().where { Players.uuid eq id }.any() + + if (exists) { + Players.update({ Players.uuid eq id }) { it.fromPlayer(player) } + } else { + Players.insert { + it[uuid] = id + it.fromPlayer(player) + } + } + } fun upsertName(u: UUID, n: String) = transaction(db) { - Players.upsert { - it[uuid] = u.toString() - it[name] = n + val id = u.toString() + + val current = + Players.selectAll() + .where { Players.uuid eq id } + .map { it.toPlayer() } + .singleOrNull() + + if (current == null) { + Players.insert { + it[uuid] = id + it[name] = n + } + return@transaction } + + Players.update({ Players.uuid eq id }) { it[name] = n } } fun migrateLegacy() = -- cgit v1.2.3-freya