2024-08-30 14:24:46 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/8/29
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package cn.rtast.fancybot.util.file
|
|
|
|
|
|
2024-10-02 21:02:05 +08:00
|
|
|
import cn.rtast.fancybot.db.JrrpRecord
|
|
|
|
|
import cn.rtast.fancybot.db.JrrpTable
|
2024-08-30 14:24:46 +08:00
|
|
|
import cn.rtast.fancybot.util.isSameDay
|
2024-09-03 16:41:06 +08:00
|
|
|
import cn.rtast.fancybot.util.suspendedTransaction
|
2024-08-30 17:50:01 +08:00
|
|
|
import org.jetbrains.exposed.sql.insert
|
|
|
|
|
import org.jetbrains.exposed.sql.selectAll
|
|
|
|
|
import org.jetbrains.exposed.sql.update
|
2024-08-30 14:24:46 +08:00
|
|
|
import java.time.Instant
|
|
|
|
|
import kotlin.random.Random
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
class JrrpManager {
|
2024-08-30 14:24:46 +08:00
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
private suspend fun getRecord(id: Long): JrrpRecord? {
|
|
|
|
|
return suspendedTransaction {
|
|
|
|
|
JrrpTable.selectAll().where { JrrpTable.userId eq id }.map {
|
|
|
|
|
JrrpRecord(
|
|
|
|
|
it[JrrpTable.userId],
|
|
|
|
|
it[JrrpTable.timestamp],
|
|
|
|
|
it[JrrpTable.point],
|
|
|
|
|
)
|
|
|
|
|
}.singleOrNull()
|
|
|
|
|
}
|
2024-08-30 14:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
suspend fun isJrrped(id: Long): Boolean {
|
|
|
|
|
val record = getRecord(id)
|
2024-09-12 11:18:57 +08:00
|
|
|
return record?.timestamp?.isSameDay(Instant.now().epochSecond) ?: false
|
2024-08-30 17:50:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun jrrp(id: Long): Long {
|
|
|
|
|
val randomPoint = Random.nextLong(0, 101)
|
|
|
|
|
if (getRecord(id) == null) {
|
|
|
|
|
suspendedTransaction {
|
|
|
|
|
JrrpTable.insert {
|
|
|
|
|
it[userId] = id
|
|
|
|
|
it[timestamp] = Instant.now().epochSecond
|
|
|
|
|
it[point] = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val record = getRecord(id)
|
|
|
|
|
suspendedTransaction {
|
|
|
|
|
JrrpTable.update({ JrrpTable.userId eq id }) {
|
|
|
|
|
it[point] = record?.points!! + randomPoint
|
|
|
|
|
it[timestamp] = Instant.now().epochSecond
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return randomPoint
|
2024-08-30 14:24:46 +08:00
|
|
|
}
|
|
|
|
|
}
|