feat: merge func
This commit is contained in:
11 files changed
+53
-251
No files matched your search
@@ -12,6 +12,7 @@ import cn.rtast.fancybot.entity.db.NiuziTable
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.length
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.timestamp
|
||||
import cn.rtast.fancybot.entity.db.NiuziTable.userId
|
||||
import cn.rtast.fancybot.entity.db.SignTable
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import cn.rtast.fancybot.util.suspendedTransaction
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
@@ -95,4 +96,14 @@ class NiuziManager {
|
||||
}
|
||||
return success to randomLength
|
||||
}
|
||||
|
||||
suspend fun redeemItem(id: Long, itemPrice: Double): Niuzi? {
|
||||
val current = getUser(id)
|
||||
suspendedTransaction {
|
||||
NiuziTable.update({ userId eq id }) {
|
||||
it[length] = current?.length!! - itemPrice
|
||||
}
|
||||
}
|
||||
return getUser(id)
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/8/30
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.util.file
|
||||
|
||||
import cn.rtast.fancybot.entity.db.JrrpRecord
|
||||
import cn.rtast.fancybot.entity.db.SignTable
|
||||
import cn.rtast.fancybot.entity.db.SignTable.point
|
||||
import cn.rtast.fancybot.entity.db.SignTable.timestamp
|
||||
import cn.rtast.fancybot.entity.db.SignTable.userId
|
||||
import cn.rtast.fancybot.util.isSameDay
|
||||
import cn.rtast.fancybot.util.suspendedTransaction
|
||||
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 {
|
||||
|
||||
private suspend fun getRecord(id: Long): JrrpRecord? {
|
||||
return suspendedTransaction {
|
||||
SignTable.selectAll().where { userId eq id }.map {
|
||||
JrrpRecord(
|
||||
it[userId],
|
||||
it[timestamp],
|
||||
it[point],
|
||||
)
|
||||
}.singleOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isSigned(id: Long): Boolean {
|
||||
val record = getRecord(id)
|
||||
return record?.timestamp?.isSameDay(Instant.now().epochSecond) ?: false
|
||||
}
|
||||
|
||||
suspend fun sign(id: Long): Long {
|
||||
val randomPoint = Random.nextLong(0, 101)
|
||||
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
|
||||
}
|
||||
|
||||
suspend fun getStatus(id: Long): JrrpRecord? {
|
||||
return getRecord(id)
|
||||
}
|
||||
|
||||
suspend fun redeemItem(id: Long, itemPrice: Int): JrrpRecord? {
|
||||
val current = getRecord(id)
|
||||
suspendedTransaction {
|
||||
SignTable.update({ userId eq id }) {
|
||||
it[point] = current?.points!! - itemPrice
|
||||
}
|
||||
}
|
||||
return getRecord(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user