feat: add some feature

This commit is contained in:
2024-08-30 14:24:46 +08:00
parent e48f899f9e
commit bf75cfad38
21 files changed
+333 -24

No files matched your search

@@ -0,0 +1,20 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/29
*/
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
}
@@ -0,0 +1,61 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/29
*/
package cn.rtast.fancybot.util
import cn.rtast.fancybot.ROOT_PATH
import cn.rtast.fancybot.gson
import java.io.File
abstract class JsonFileHandler<T> {
val file: File
constructor(filename: String) {
this.file = File(ROOT_PATH, filename)
initFile(null)
}
constructor(filename: String, defaultConfig: T) {
this.file = File(ROOT_PATH, filename)
initFile(defaultConfig)
}
private fun initFile(defaultConfig: T?) {
val configPath = File(ROOT_PATH)
configPath.mkdirs()
if (this.file.createNewFile()) {
if (defaultConfig != null) {
this.write(defaultConfig)
} else {
val resourceConfig = this::class.java.classLoader
.getResourceAsStream(file.name)
?.reader()?.readText()!!
this.write(resourceConfig)
}
}
}
private fun write(data: String) {
this.file.writeText(data)
}
fun write(data: T) {
this.write(gson.toJson(data))
}
inline fun <reified T> read(): T {
val content = this.file.readText()
return content.fromJson()
}
inline fun <reified T> readArray(): T {
val content = this.file.readText()
return content.fromArrayJson<T>()
}
}
@@ -0,0 +1,16 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/30
*/
package cn.rtast.fancybot.util.file
import cn.rtast.fancybot.entity.Config
import cn.rtast.fancybot.util.JsonFileHandler
class ConfigManager : JsonFileHandler<Config>("config.json") {
val ncmAPI get() = this.read<Config>().cnmApi
}
@@ -0,0 +1,31 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/29
*/
package cn.rtast.fancybot.util.file
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
import cn.rtast.fancybot.util.JsonFileHandler
import cn.rtast.fancybot.util.isSameDay
import java.time.Instant
import kotlin.random.Random
class JrrpManager : JsonFileHandler<List<JrrpRecord>>("jrrp.json", listOf()) {
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)
}
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)
}
}
@@ -0,0 +1,39 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/30
*/
package cn.rtast.fancybot.util.file
import cn.rtast.fancybot.entity.jrrp.JrrpRecord
import cn.rtast.fancybot.util.JsonFileHandler
import cn.rtast.fancybot.util.isSameDay
import java.time.Instant
import kotlin.random.Random
class SignManager : JsonFileHandler<List<JrrpRecord>>("sign.json", listOf()) {
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)
}
fun sign(id: Long): Long {
val allSign = this.readArray<MutableList<JrrpRecord>>()
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
}
fun getStatus(id: Long): JrrpRecord? {
val allSign = this.readArray<List<JrrpRecord>>()
return allSign.find { it.id == id }
}
}