Skip to content

Commit 791f946

Browse files
committed
⚡️(wayzer/user/ban) 使用Dao代替entity,提高性能和兼容性
1 parent 3d58342 commit 791f946

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

scripts/wayzer/user/ban.dao.kt

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
package wayzer.user
22

3-
import org.jetbrains.exposed.dao.IntEntity
4-
import org.jetbrains.exposed.dao.IntEntityClass
5-
import org.jetbrains.exposed.dao.id.EntityID
63
import org.jetbrains.exposed.dao.id.IntIdTable
7-
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
8-
import org.jetbrains.exposed.sql.and
4+
import org.jetbrains.exposed.sql.*
95
import org.jetbrains.exposed.sql.javatime.CurrentTimestamp
106
import org.jetbrains.exposed.sql.javatime.timestamp
7+
import org.jetbrains.exposed.sql.transactions.transaction
118
import wayzer.lib.PlayerData
129
import java.time.Duration
1310
import java.time.Instant
1411

15-
class PlayerBan(id: EntityID<Int>) : IntEntity(id) {
16-
var ids by T.ids
17-
var reason by T.reason
18-
var operator by T.operator
19-
val createTime by T.createTime
20-
var endTime by T.endTime
12+
data class PlayerBan(
13+
val id: Int,
14+
val ids: String,
15+
val reason: String,
16+
val operator: String?,
17+
val createTime: Instant,
18+
val endTime: Instant
19+
) {
20+
constructor(row: ResultRow) : this(
21+
row[T.id].value,
22+
row[T.ids],
23+
row[T.reason],
24+
row[T.operator],
25+
row[T.createTime],
26+
row[T.endTime]
27+
)
2128

2229
object T : IntIdTable("PlayerBanV2") {
2330
val ids = text("ids", eagerLoading = true)
@@ -27,21 +34,30 @@ class PlayerBan(id: EntityID<Int>) : IntEntity(id) {
2734
val endTime = timestamp("endTime").defaultExpression(CurrentTimestamp)
2835
}
2936

30-
companion object : IntEntityClass<PlayerBan>(T) {
31-
fun create(ids: PlayerData, time: Duration, reason: String, operator: String?): PlayerBan {
32-
return new {
33-
this.ids = ids.idsInDB
34-
endTime = Instant.now() + time
35-
this.operator = operator
36-
this.reason = reason
37+
companion object {
38+
fun create(ids: PlayerData, time: Duration, reason: String, operator: String?): PlayerBan = transaction {
39+
val ban = T.insertReturning {
40+
it[T.ids] = ids.idsInDB
41+
it[T.endTime] = Instant.now() + time
42+
it[T.operator] = operator
43+
it[T.reason] = reason
3744
}
45+
PlayerBan(ban.first())
3846
}
3947

40-
fun allNotEnd() = find(T.endTime.greater(CurrentTimestamp))
48+
fun allNotEnd() = transaction {
49+
T.selectAll().where { T.endTime.greater(CurrentTimestamp) }
50+
.map { PlayerBan(it) }
51+
}
52+
53+
fun findNotEnd(id: String): PlayerBan? = transaction {
54+
T.selectAll().where { (T.ids like "%$${id}$%") and T.endTime.greater(CurrentTimestamp) }.firstOrNull()
55+
?.let { PlayerBan(it) }
56+
}
4157

42-
fun findNotEnd(id: String): PlayerBan? {
43-
return find { (T.ids like "%$${id}$%") and (T.endTime.greater(CurrentTimestamp)) }
44-
.firstOrNull()
58+
fun delete(id: Int): PlayerBan? = transaction {
59+
T.deleteReturning { T.id eq id }.firstOrNull()
60+
?.let { PlayerBan(it) }
4561
}
4662
}
4763
}

scripts/wayzer/user/ban.kts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package wayzer.user
44

55
import coreLibrary.DBApi.DB.registerTable
6-
import org.jetbrains.exposed.sql.transactions.transaction
76
import java.text.DateFormat
87
import java.time.Duration
98
import java.time.Instant
@@ -27,7 +26,7 @@ fun Player.kick(ban: PlayerBan) {
2726

2827
listen<EventType.PlayerConnect> {
2928
launch(Dispatchers.IO) {
30-
val ban = transaction { PlayerBan.findNotEnd(PlayerData[it.player].id) } ?: return@launch
29+
val ban = PlayerBan.findNotEnd(PlayerData[it.player].id) ?: return@launch
3130
withContext(Dispatchers.game) {
3231
it.player.kick(ban)
3332
}
@@ -36,12 +35,10 @@ listen<EventType.PlayerConnect> {
3635

3736
suspend fun ban(player: PlayerData, time: Int, reason: String, operate: Player?) {
3837
val ban = withContext(Dispatchers.IO) {
39-
transaction {
40-
PlayerBan.create(
41-
player, Duration.ofMinutes(time.toLong()), reason,
42-
operate?.let { PlayerData[it].id }
43-
).also { it.flush() }
44-
}
38+
PlayerBan.create(
39+
player, Duration.ofMinutes(time.toLong()), reason,
40+
operate?.let { PlayerData[it].id }
41+
)
4542
}
4643
Groups.player.filter { PlayerData[it].id in player.ids }.forEach {
4744
it.kick(ban)
@@ -72,9 +69,7 @@ command("unbanX", "管理指令: 解禁") {
7269
body {
7370
if (arg.isEmpty()) replyUsage()
7471
val id = arg[0].toIntOrNull() ?: replyUsage()
75-
val ban = transaction {
76-
PlayerBan.findById(id)?.also { it.delete() }
77-
} ?: returnReply("[red]找不到封禁记录,检查ID是否正确".with())
72+
val ban = PlayerBan.delete(id) ?: returnReply("[red]找不到封禁记录,检查ID是否正确".with())
7873
logger.info("unban ${ban.ids} ${ban.endTime} ${ban.reason}")
7974
reply("[green]解禁成功, 禁封原因: {reason}".with("reason" to ban.reason))
8075
}

0 commit comments

Comments
 (0)