Skip to content

Commit

Permalink
Merge pull request #435 from FxRayHughes/master
Browse files Browse the repository at this point in the history
[player-database]
  • Loading branch information
Bkm016 authored Aug 11, 2024
2 parents 74c3507 + 9b7a0c5 commit 3fd7433
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class DataContainer(val user: String, val database: Database) {
save(key)
}

// 穿透缓存的写数据库方法
operator fun set(targetUser: String, key: String, value: Any) {
database[targetUser, key] = value.toString()
}

fun setDelayed(key: String, value: Any, delay: Long = 3L, timeUnit: TimeUnit = TimeUnit.SECONDS) {
source[key] = value.toString()
updateMap[key] = System.currentTimeMillis() - timeUnit.toMillis(delay)
Expand Down Expand Up @@ -67,4 +72,4 @@ class DataContainer(val user: String, val database: Database) {
playerDataContainer.values.forEach { it.checkUpdate() }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa
}

operator fun set(user: String, name: String, data: String) {
if (data.isEmpty()) {
remove(user, name)
return
}
if (get(user, name) == null) {
type.tableVar().insert(dataSource, "user", "key", "value") { value(user, name, data) }
} else {
Expand All @@ -38,4 +42,42 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa
}
}
}
}

// 查询某个User的Key对应的Value
fun select(user: String, key: String): String? {
return type.tableVar().select(dataSource) {
rows("value")
where("user" eq user and ("key" eq key))
}.firstOrNull {
getString("value")
}
}

// 根据Key获取所有数据
// return: Map<user, value>
fun getList(name: String): MutableMap<String, String> {
return type.tableVar().select(dataSource) {
rows("user", "value")
where("key" eq name)
}.map {
getString("user") to getString("value")
}.toMap(ConcurrentHashMap())
}

fun getList(name: String, value: String): List<String> {
return type.tableVar().select(dataSource) {
rows("user")
where("key" eq name and ("value" eq value))
}.map {
getString("user")
}
}

fun remove(user: String, name: String) {
type.tableVar().delete(dataSource) {
where("user" eq user and ("key" eq name))
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var playerDatabase: Database? = null

val playerDataContainer = ConcurrentHashMap<UUID, DataContainer>()

val playerReadOnlyDataContainer = ConcurrentHashMap<UUID, ReadOnlyDataContainer>()

fun setupPlayerDatabase(
conf: ConfigurationSection,
table: String = conf.getString("table", "")!!,
Expand Down Expand Up @@ -49,10 +51,37 @@ fun setupPlayerDatabase(
setupPlayerDatabase(conf, table, flags, clearFlags, ssl)
}

fun buildPlayerDatabase(
conf: ConfigurationSection,
table: String = conf.getString("table", "")!!,
flags: List<String> = emptyList(),
clearFlags: Boolean = false,
ssl: String? = null,
): Database {
val hostSQL = HostSQL(conf)
if (clearFlags) {
hostSQL.flags.clear()
}
hostSQL.flags.addAll(flags)
if (ssl != null) {
hostSQL.flags -= "useSSL=false"
hostSQL.flags += "sslMode=$ssl"
}
return Database(TypeSQL(hostSQL, table))
}

fun setupPlayerDatabase(file: File = newFile(getDataFolder(), "data.db")) {
playerDatabase = Database(TypeSQLite(file))
}

fun setupPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), tableName: String) {
playerDatabase = Database(TypeSQLite(file, tableName))
}

fun buildPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), table: String): Database {
return Database(TypeSQLite(file, table))
}

fun ProxyPlayer.getDataContainer(): DataContainer {
return playerDataContainer[uniqueId] ?: error("unavailable")
}
Expand All @@ -76,4 +105,16 @@ fun UUID.releasePlayerDataContainer() {

fun ProxyPlayer.releaseDataContainer() {
playerDataContainer.remove(uniqueId)
}
}

// 获取只读容器
fun UUID.getReadOnlyDataContainer(): ReadOnlyDataContainer {
return playerReadOnlyDataContainer.computeIfAbsent(this) {
ReadOnlyDataContainer(this.toString(), playerDatabase!!)
}
}

// 释放只读容器
fun UUID.releaseReadOnlyDataContainer() {
playerReadOnlyDataContainer.remove(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ fun Player.setupDataContainer(usernameMode: Boolean = false) {

fun Player.releaseDataContainer() {
adaptPlayer(this).releaseDataContainer()
}
}

fun Player.getReadOnlyDataContainer(): ReadOnlyDataContainer {
return this.uniqueId.getReadOnlyDataContainer()
}

fun Player.releaseReadOnlyDataContainer() {
this.uniqueId.releaseReadOnlyDataContainer()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package taboolib.expansion

import taboolib.common.Inject
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.function.submitAsync

// 只读数据容器
class ReadOnlyDataContainer(val user: String, val database: Database) {

var source = database[user]

operator fun get(key: String): String? {
return source[key]
}

fun keys(): Set<String> {
return source.keys
}

fun values(): Map<String, String> {
return source
}

fun size(): Int {
return source.size
}

override fun toString(): String {
return "ReadOnlyDataContainer(user='$user', source=$source)"
}

fun update() {
source = database[user]
}


@Inject
internal companion object {

// 延迟更新时间
var syncTick = 80L

@Awake(LifeCycle.ACTIVE)
private fun update() {
submitAsync(period = syncTick) {
playerReadOnlyDataContainer.values.forEach {
runCatching { it.update() }
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TypeSQL(val host: Host<SQL>, table: String) : Type() {
val tableVar = Table(table, host) {
add { id() }
add("user") {
type(ColumnTypeSQL.VARCHAR, 36) {
type(ColumnTypeSQL.VARCHAR, 64) {
options(ColumnOptionSQL.KEY)
}
}
Expand All @@ -17,7 +17,7 @@ class TypeSQL(val host: Host<SQL>, table: String) : Type() {
}
}
add("value") {
type(ColumnTypeSQL.VARCHAR, 128)
type(ColumnTypeSQL.LONGTEXT)
}
}

Expand All @@ -28,4 +28,4 @@ class TypeSQL(val host: Host<SQL>, table: String) : Type() {
override fun tableVar(): Table<*, *> {
return tableVar
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import taboolib.module.database.Table
import taboolib.module.database.getHost
import java.io.File

class TypeSQLite(file: File) : Type() {
class TypeSQLite(file: File, tableName: String? = null) : Type() {

val host = newFile(file).getHost()

val tableVar = Table(pluginId, host) {
val tableVar = Table(tableName ?: pluginId, host) {
add("user") {
type(ColumnTypeSQLite.TEXT, 36)
type(ColumnTypeSQLite.TEXT, 64)
}
add("key") {
type(ColumnTypeSQLite.TEXT, 64)
}
add("value") {
type(ColumnTypeSQLite.TEXT, 128)
type(ColumnTypeSQLite.TEXT)
}
}

Expand All @@ -31,4 +31,4 @@ class TypeSQLite(file: File) : Type() {
override fun tableVar(): Table<*, *> {
return tableVar
}
}
}

0 comments on commit 3fd7433

Please sign in to comment.