77 lines
2.4 KiB
Kotlin
77 lines
2.4 KiB
Kotlin
package cc.maxmc.invite.data
|
|
|
|
import cc.maxmc.invite.PluginScope
|
|
import kotlinx.coroutines.launch
|
|
import org.bukkit.Bukkit
|
|
import org.jetbrains.exposed.dao.*
|
|
import org.jetbrains.exposed.dao.id.EntityID
|
|
import org.jetbrains.exposed.dao.id.IdTable
|
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
|
import org.jetbrains.exposed.sql.Column
|
|
import org.jetbrains.exposed.sql.Database
|
|
import org.jetbrains.exposed.sql.SchemaUtils
|
|
import org.jetbrains.exposed.sql.javatime.date
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
|
import taboolib.common.platform.function.getDataFolder
|
|
import taboolib.common.platform.function.info
|
|
import taboolib.common.platform.function.warning
|
|
import taboolib.platform.BukkitPlugin
|
|
import java.io.File
|
|
import java.util.*
|
|
import kotlin.collections.HashMap
|
|
|
|
fun initDatabase() {
|
|
try {
|
|
Database.connect("jdbc:h2:file:./${File(getDataFolder(), "data").toPath()}")
|
|
info("§a| §7成功连接至内存数据库.")
|
|
} catch (e: Exception) {
|
|
warning("§c| §7连接至内存数据库时出现异常: ${e.message}")
|
|
e.printStackTrace()
|
|
Bukkit.getPluginManager().disablePlugin(BukkitPlugin.getInstance())
|
|
}
|
|
PluginScope.launch {
|
|
transaction {
|
|
info("§b| §7正在读取数据库信息...")
|
|
SchemaUtils.create(InviteCodes, TimeTable)
|
|
info("§a| §7成功读取数据库信息.")
|
|
}
|
|
convert()
|
|
}
|
|
}
|
|
|
|
object InviteCodes : IntIdTable() {
|
|
val inviteCode = varchar("invite_code", 24)
|
|
val name = varchar("name", 100).nullable()
|
|
val uid = uuid("player_uid").nullable()
|
|
val qq = varchar("qq", 10).nullable()
|
|
}
|
|
|
|
class InviteCode(id: EntityID<Int>) : IntEntity(id) {
|
|
companion object : IntEntityClass<InviteCode>(InviteCodes)
|
|
|
|
var inviteCode by InviteCodes.inviteCode
|
|
var uid by InviteCodes.uid
|
|
var name by InviteCodes.name
|
|
var qq by InviteCodes.qq
|
|
}
|
|
|
|
object TimeTable : IdTable<UUID>() {
|
|
override val id = uuid("uid").entityId()
|
|
val lastLogout = date("last_login")
|
|
override val primaryKey = PrimaryKey(id)
|
|
}
|
|
|
|
class LastLogout(id: EntityID<UUID>): UUIDEntity(id) {
|
|
companion object : UUIDEntityClass<LastLogout>(TimeTable)
|
|
|
|
val uid by TimeTable.id
|
|
var last by TimeTable.lastLogout
|
|
}
|
|
|
|
val userTrials = HashMap<UUID, UserTrial>()
|
|
|
|
data class UserTrial(val uid: UUID, private var attempts: Int = 0) {
|
|
fun tryOnce() = attempts++
|
|
|
|
fun isLimited() = attempts == 3
|
|
} |