feat: fix bug and useing sqlite to store data
This commit is contained in:
13 files changed
+185
-89
No files matched your search
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import cn.rtast.fancybot.ROOT_PATH
|
||||
import cn.rtast.fancybot.entity.JrrpTable
|
||||
import cn.rtast.fancybot.entity.SignTable
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||
|
||||
suspend fun initDatabase() {
|
||||
Database.connect("jdbc:sqlite:file:$ROOT_PATH/data.sqlite", "org.sqlite.JDBC")
|
||||
suspendedTransaction {
|
||||
SchemaUtils.createMissingTablesAndColumns(JrrpTable)
|
||||
SchemaUtils.createMissingTablesAndColumns(SignTable)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T> suspendedTransaction(block: suspend () -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
@@ -8,13 +8,10 @@
|
||||
package cn.rtast.fancybot.util
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
fun Long.isSameDay(other: Long): Boolean {
|
||||
val currentDate = LocalDate.now()
|
||||
val dateToCompare = Instant.ofEpochSecond(other)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate()
|
||||
return currentDate == dateToCompare
|
||||
fun Long.isSameDay(otherTimestamp: Long, zoneId: ZoneId = ZoneId.systemDefault()): Boolean {
|
||||
val thisDate = Instant.ofEpochMilli(this).atZone(zoneId).toLocalDate()
|
||||
val otherDate = Instant.ofEpochMilli(otherTimestamp).atZone(zoneId).toLocalDate()
|
||||
return thisDate == otherDate
|
||||
}
|
||||
@@ -7,25 +7,57 @@
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
import cn.rtast.fancybot.entity.JrrpRecord
|
||||
import cn.rtast.fancybot.entity.JrrpTable
|
||||
import cn.rtast.fancybot.util.suspendedTransaction
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import java.time.Instant
|
||||
import kotlin.random.Random
|
||||
|
||||
class JrrpManager : JsonFileHandler<List<JrrpRecord>>("jrrp.json", listOf()) {
|
||||
class JrrpManager {
|
||||
|
||||
fun isJrrped(id: Long): Boolean {
|
||||
val allJrrp = this.readArray<List<JrrpRecord>>()
|
||||
val user = allJrrp.find { it.id == id } ?: return false
|
||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
fun jrrp(id: Long): Int {
|
||||
val allJrrp = this.readArray<MutableList<JrrpRecord>>()
|
||||
allJrrp.removeAll { it.id == id }
|
||||
allJrrp.add(JrrpRecord(id, Instant.now().epochSecond, 0))
|
||||
this.write(allJrrp)
|
||||
return Random.nextInt(0, 101)
|
||||
suspend fun isJrrped(id: Long): Boolean {
|
||||
val record = getRecord(id)
|
||||
return if (record != null) {
|
||||
record.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -7,33 +7,64 @@
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
|
||||
import cn.rtast.fancybot.util.JsonFileHandler
|
||||
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
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import java.time.Instant
|
||||
import kotlin.random.Random
|
||||
|
||||
class SignManager : JsonFileHandler<List<JrrpRecord>>("sign.json", listOf()) {
|
||||
class SignManager {
|
||||
|
||||
fun isSigned(id: Long): Boolean {
|
||||
val allSign = this.readArray<List<JrrpRecord>>()
|
||||
val user = allSign.find { it.id == id } ?: return false
|
||||
return user.timestamp.isSameDay(Instant.now().epochSecond)
|
||||
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||
return suspendedTransaction {
|
||||
SignTable.selectAll().where { userId eq id }.map {
|
||||
JrrpRecord(
|
||||
it[userId],
|
||||
it[timestamp],
|
||||
it[point],
|
||||
)
|
||||
}.singleOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
fun sign(id: Long): Long {
|
||||
val allSign = this.readArray<MutableList<JrrpRecord>>()
|
||||
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 {
|
||||
val randomPoint = Random.nextLong(0, 101)
|
||||
val current = allSign.find { it.id == id } ?: JrrpRecord(id, Instant.now().epochSecond, 0)
|
||||
current.points = randomPoint + current.points
|
||||
allSign.remove(current)
|
||||
allSign.add(current)
|
||||
this.write(allSign)
|
||||
return randomPoint + current.points
|
||||
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
|
||||
}
|
||||
|
||||
fun getStatus(id: Long): JrrpRecord? {
|
||||
val allSign = this.readArray<List<JrrpRecord>>()
|
||||
return allSign.find { it.id == id }
|
||||
suspend fun getStatus(id: Long): JrrpRecord? {
|
||||
return getRecord(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user