2024-08-30 14:24:46 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/8/30
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package cn.rtast.fancybot.util.file
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
import cn.rtast.fancybot.entity.JrrpRecord
|
|
|
|
|
import cn.rtast.fancybot.entity.SignTable
|
|
|
|
|
import cn.rtast.fancybot.entity.SignTable.point
|
|
|
|
|
import cn.rtast.fancybot.entity.SignTable.timestamp
|
|
|
|
|
import cn.rtast.fancybot.entity.SignTable.userId
|
|
|
|
|
import cn.rtast.fancybot.util.suspendedTransaction
|
2024-08-30 14:24:46 +08:00
|
|
|
import cn.rtast.fancybot.util.isSameDay
|
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 SignManager {
|
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 {
|
|
|
|
|
SignTable.selectAll().where { userId eq id }.map {
|
|
|
|
|
JrrpRecord(
|
|
|
|
|
it[userId],
|
|
|
|
|
it[timestamp],
|
|
|
|
|
it[point],
|
|
|
|
|
)
|
|
|
|
|
}.singleOrNull()
|
|
|
|
|
}
|
2024-08-30 14:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
suspend fun isSigned(id: Long): Boolean {
|
|
|
|
|
val record = getRecord(id)
|
|
|
|
|
return if (record != null) {
|
|
|
|
|
record.timestamp.isSameDay(Instant.now().epochSecond)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun sign(id: Long): Long {
|
2024-08-30 14:24:46 +08:00
|
|
|
val randomPoint = Random.nextLong(0, 101)
|
2024-08-30 17:50:01 +08:00
|
|
|
if (getRecord(id) == null) {
|
|
|
|
|
suspendedTransaction {
|
|
|
|
|
SignTable.insert {
|
|
|
|
|
it[userId] = id
|
|
|
|
|
it[timestamp] = Instant.now().epochSecond
|
|
|
|
|
it[point] = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val record = getRecord(id)
|
|
|
|
|
suspendedTransaction {
|
|
|
|
|
SignTable.update({ userId eq id }) {
|
|
|
|
|
it[point] = record?.points!! + randomPoint
|
|
|
|
|
it[timestamp] = Instant.now().epochSecond
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return randomPoint
|
2024-08-30 14:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
suspend fun getStatus(id: Long): JrrpRecord? {
|
|
|
|
|
return getRecord(id)
|
2024-08-30 14:24:46 +08:00
|
|
|
}
|
|
|
|
|
}
|