summaryrefslogtreewikicommitdiff
path: root/core/src/db/Database.kt
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2026-03-28 17:04:05 -0400
committerFreya Murphy <freya@freyacat.org>2026-03-28 17:04:20 -0400
commit3a3e80a1793601293402bca5cafdfde664121b14 (patch)
tree94fe8d50762563d252b86ca92a10d3a1823b0853 /core/src/db/Database.kt
parentinitial (diff)
downloadkenshinshideandseek2-3a3e80a1793601293402bca5cafdfde664121b14.tar.gz
kenshinshideandseek2-3a3e80a1793601293402bca5cafdfde664121b14.tar.bz2
kenshinshideandseek2-3a3e80a1793601293402bca5cafdfde664121b14.zip
2.0.0-alpha1v2.0.0-alpha1
Diffstat (limited to 'core/src/db/Database.kt')
-rw-r--r--core/src/db/Database.kt34
1 files changed, 30 insertions, 4 deletions
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() =