Skip to content

Commit

Permalink
update: 多个变更
Browse files Browse the repository at this point in the history
1. 领取奖励后将写入B站账户MID和玩家UUID
2. 玩家登录或换绑账户时记录日志
  • Loading branch information
BingZi-233 committed Dec 15, 2024
1 parent b65e89d commit e82dcd3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=online.bingzi.bilibili.video
version=2.0.0
version=2.0.1
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.incremental.useClasspathSnapshot=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import online.bingzi.bilibili.video.internal.cache.Cache
import online.bingzi.bilibili.video.internal.entity.BindEntity
import online.bingzi.bilibili.video.internal.entity.CookieEntity
import online.bingzi.bilibili.video.internal.entity.ReceiveEntity
import online.bingzi.bilibili.video.internal.entity.BindLogEntity
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.getProxyPlayer
Expand Down Expand Up @@ -51,6 +52,43 @@ object BilibiliVideoAPI {
return Cache.cookieCache.get(playerUUID)
}

/**
* Log bind operation
* <p>
* 记录绑定操作日志
*
* @param playerUUID 玩家UUID
* @param playerName 玩家名称
* @param bilibiliMid B站账户MID
* @param bilibiliName B站账户名称
* @param operationType 操作类型
* @param oldBilibiliMid 旧B站账户MID
* @param oldBilibiliName 旧B站账户名称
*
* @author BingZi-233
* @since 2.0.1
*/
private fun logBindOperation(
playerUUID: UUID,
playerName: String,
bilibiliMid: String,
bilibiliName: String,
operationType: String,
oldBilibiliMid: String? = null,
oldBilibiliName: String? = null
) {
val bindLogEntity = BindLogEntity(
playerUUID = playerUUID,
playerName = playerName,
bilibiliMid = bilibiliMid,
bilibiliName = bilibiliName,
operationType = operationType,
oldBilibiliMid = oldBilibiliMid,
oldBilibiliName = oldBilibiliName
)
bindLogEntity.create()
}

/**
* Save or update player bind entity
* <p>
Expand All @@ -63,11 +101,53 @@ object BilibiliVideoAPI {
* @since 2.0.0
*/
fun saveOrUpdatePlayerBindEntity(proxyPlayer: ProxyPlayer, bindEntity: BindEntity) {
val entity = BindEntity.getDao().queryForId(proxyPlayer.uniqueId) ?: let {
val oldEntity = BindEntity.getDao().queryForId(proxyPlayer.uniqueId)
if (oldEntity == null) {
// 新绑定
bindEntity.create()
bindEntity
logBindOperation(
playerUUID = proxyPlayer.uniqueId,
playerName = proxyPlayer.name,
bilibiliMid = bindEntity.bilibiliMid!!,
bilibiliName = bindEntity.bilibiliName!!,
operationType = "BIND"
)
} else {
// 换绑
if (oldEntity.bilibiliMid != bindEntity.bilibiliMid) {
logBindOperation(
playerUUID = proxyPlayer.uniqueId,
playerName = proxyPlayer.name,
bilibiliMid = bindEntity.bilibiliMid!!,
bilibiliName = bindEntity.bilibiliName!!,
operationType = "CHANGE",
oldBilibiliMid = oldEntity.bilibiliMid,
oldBilibiliName = oldEntity.bilibiliName
)
}
bindEntity.update()
}
entity.update()
}

/**
* Delete player bind entity
* <p>
* 删除玩家绑定数据
*
* @param bindEntity [BindEntity]
*
* @author BingZi-233
* @since 2.0.1
*/
fun deletePlayerBindEntity(bindEntity: BindEntity) {
logBindOperation(
playerUUID = bindEntity.playerUUID!!,
playerName = bindEntity.playerName!!,
bilibiliMid = bindEntity.bilibiliMid!!,
bilibiliName = bindEntity.bilibiliName!!,
operationType = "UNBIND"
)
bindEntity.delete()
}

/**
Expand Down Expand Up @@ -279,4 +359,22 @@ object BilibiliVideoAPI {
)
receiveEntity.create()
}

/**
* Check player receive entity by player UUID and bv
* <p>
* 通过玩家UUID和BV检查玩家领取数据
* true - 已领取
* false - 未领取
*
* @param playerUUID Player UUID
* @param bilibiliBv Bilibili BV
* @return [Boolean]
*
* @author BingZi-233
* @since 2.0.1
*/
fun checkPlayerReceiveEntityByPlayerUUIDAndBv(playerUUID: UUID, bilibiliBv: String): Boolean {
return ReceiveEntity.getDao().queryForMatchingArgs(ReceiveEntity(playerUUID = playerUUID, bilibiliBv = bilibiliBv)).isNotEmpty()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package online.bingzi.bilibili.video.internal.entity

import com.j256.ormlite.dao.Dao
import com.j256.ormlite.field.DataType
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.misc.BaseDaoEnabled
import com.j256.ormlite.table.DatabaseTable
import online.bingzi.bilibili.video.internal.helper.DatabaseHelper
import java.util.*

/**
* Bind log entity
* <p>
* 绑定日志实体
*
* @property id 日志ID
* @property playerUUID 玩家UUID
* @property playerName 玩家名称
* @property bilibiliMid B站账户MID
* @property bilibiliName B站账户名称
* @property operationType 操作类型(BIND-绑定, CHANGE-换绑, UNBIND-解绑)
* @property oldBilibiliMid 旧B站账户MID(换绑时记录)
* @property oldBilibiliName 旧B站账户名称(换绑时记录)
* @property createTime 创建时间
* @constructor Create empty Bind log entity
*
* @author BingZi-233
* @since 2.0.1
*/
@DatabaseTable(tableName = "bilibili_video_bind_log")
data class BindLogEntity(
@DatabaseField(generatedId = true)
var id: Int? = null,
@DatabaseField
var playerUUID: UUID? = null,
@DatabaseField
var playerName: String? = null,
@DatabaseField
var bilibiliMid: String? = null,
@DatabaseField
var bilibiliName: String? = null,
@DatabaseField
var operationType: String? = null,
@DatabaseField
var oldBilibiliMid: String? = null,
@DatabaseField
var oldBilibiliName: String? = null,
@DatabaseField(
dataType = DataType.DATE_STRING,
format = "yyyy-MM-ss HH:mm:ss",
columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL",
readOnly = true,
canBeNull = false
)
var createTime: Date? = null
) : BaseDaoEnabled<BindLogEntity, Int>() {
init {
dao = DatabaseHelper.bindLogEntityDaoSource
}

companion object {
fun getDao(): Dao<BindLogEntity, Int> {
return DatabaseHelper.bindLogEntityDaoSource
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import online.bingzi.bilibili.video.internal.database.Database
import online.bingzi.bilibili.video.internal.entity.BindEntity
import online.bingzi.bilibili.video.internal.entity.CookieEntity
import online.bingzi.bilibili.video.internal.entity.ReceiveEntity
import online.bingzi.bilibili.video.internal.entity.BindLogEntity
import java.util.*

/**
Expand Down Expand Up @@ -55,6 +56,18 @@ internal object DatabaseHelper {
DaoManager.createDao(Database.connectionSource, ReceiveEntity::class.java)
}

/**
* Bind log entity dao source
* <p>
* 绑定日志实体数据源
*
* @author BingZi-233
* @since 2.0.1
*/
val bindLogEntityDaoSource: Dao<BindLogEntity, Int> by lazy {
DaoManager.createDao(Database.connectionSource, BindLogEntity::class.java)
}

/**
* Build MySQL jdbc url
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ object CommandReceive {
// 获取与发送者唯一 ID 绑定的 Bilibili 视频实体
BilibiliVideoAPI.getPlayerBindEntity(sender.uniqueId)?.let { bindEntity ->
// 检查该玩家是否已经领取过该 bv 对应的奖励
if (BilibiliVideoAPI.checkPlayerReceiveEntityByMidAndBv(bindEntity.bilibiliMid!!, bilibiliBv)) {
if (BilibiliVideoAPI.checkPlayerReceiveEntityByMidAndBv(bindEntity.bilibiliMid!!, bilibiliBv) ||
BilibiliVideoAPI.checkPlayerReceiveEntityByPlayerUUIDAndBv(sender.uniqueId, bilibiliBv)) {
val bilibiliPlayerReceiveFailedEvent = BilibiliPlayerReceiveFailedEvent(sender.castSafely<Player>()!!, bilibiliBv)
// 调用事件
bilibiliPlayerReceiveFailedEvent.call()
Expand Down Expand Up @@ -91,7 +92,8 @@ object CommandReceive {
// 获取与该玩家唯一 ID 绑定的 Bilibili 视频实体
BilibiliVideoAPI.getPlayerBindEntity(proxyPlayer.uniqueId)?.let { bindEntity ->
// 检查该玩家是否已经领取过该 bv 对应的奖励
if (BilibiliVideoAPI.checkPlayerReceiveEntityByMidAndBv(bindEntity.bilibiliMid!!, bilibiliBv)) {
if (BilibiliVideoAPI.checkPlayerReceiveEntityByMidAndBv(bindEntity.bilibiliMid!!, bilibiliBv) ||
BilibiliVideoAPI.checkPlayerReceiveEntityByPlayerUUIDAndBv(proxyPlayer.uniqueId, bilibiliBv)) {
val bilibiliPlayerReceiveFailedEvent = BilibiliPlayerReceiveFailedEvent(proxyPlayer.castSafely<Player>()!!, bilibiliBv)
// 调用事件
bilibiliPlayerReceiveFailedEvent.call()
Expand Down

0 comments on commit e82dcd3

Please sign in to comment.