diff --git a/src/main/kotlin/BpData.kt b/src/main/kotlin/BpData.kt index 1af45e3..142511b 100644 --- a/src/main/kotlin/BpData.kt +++ b/src/main/kotlin/BpData.kt @@ -4,5 +4,33 @@ data class BpData( var whoseTurn: Int, var banPick: Int, var round: Int, - var lessThan4: Boolean -) + var lessThan4: Boolean, + var spellFailedCountA: IntArray, // 左边玩家符卡失败次数 + var spellFailedCountB: IntArray, // 右边玩家符卡失败次数 +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as BpData + + if (whoseTurn != other.whoseTurn) return false + if (banPick != other.banPick) return false + if (round != other.round) return false + if (lessThan4 != other.lessThan4) return false + if (!spellFailedCountA.contentEquals(other.spellFailedCountA)) return false + if (!spellFailedCountB.contentEquals(other.spellFailedCountB)) return false + + return true + } + + override fun hashCode(): Int { + var result = whoseTurn + result = 31 * result + banPick + result = 31 * result + round + result = 31 * result + lessThan4.hashCode() + result = 31 * result + spellFailedCountA.contentHashCode() + result = 31 * result + spellFailedCountB.contentHashCode() + return result + } +} diff --git a/src/main/kotlin/RoomType.kt b/src/main/kotlin/RoomType.kt index ea37e0a..2e6a115 100644 --- a/src/main/kotlin/RoomType.kt +++ b/src/main/kotlin/RoomType.kt @@ -20,5 +20,12 @@ sealed interface RoomType { fun randSpells(games: Array, ranks: Array?, difficulty: Difficulty): Array @Throws(HandlerException::class) - fun handleUpdateSpell(room: Room, token: String, idx: Int, status: SpellStatus, now: Long): SpellStatus + fun handleUpdateSpell( + room: Room, + token: String, + idx: Int, + status: SpellStatus, + now: Long, + isReset: Boolean + ): SpellStatus } diff --git a/src/main/kotlin/RoomTypeBP.kt b/src/main/kotlin/RoomTypeBP.kt index 7d0f308..9870db2 100644 --- a/src/main/kotlin/RoomTypeBP.kt +++ b/src/main/kotlin/RoomTypeBP.kt @@ -13,7 +13,9 @@ object RoomTypeBP : RoomType { whoseTurn = if (room.lastWinner > 0) room.lastWinner - 1 else ThreadLocalRandom.current().nextInt(2), banPick = 1, round = 0, - lessThan4 = false + lessThan4 = false, + spellFailedCountA = IntArray(25), + spellFailedCountB = IntArray(25), ) } @@ -23,25 +25,43 @@ object RoomTypeBP : RoomType { } @Throws(HandlerException::class) - override fun handleUpdateSpell(room: Room, token: String, idx: Int, status: SpellStatus, now: Long): SpellStatus { + override fun handleUpdateSpell( + room: Room, + token: String, + idx: Int, + status: SpellStatus, + now: Long, + isReset: Boolean + ): SpellStatus { val st = room.spellStatus!![idx] // SpellLog.logSpellOperate(status, room.spells!![idx], token) - if (token == room.players[0]) { - if (room.bpData!!.whoseTurn != 0) - throw HandlerException("不是你的回合") - if (st != SpellStatus.NONE || - room.bpData!!.banPick == 0 && status != SpellStatus.LEFT_SELECT || - room.bpData!!.banPick == 1 && status != SpellStatus.BANNED - ) throw HandlerException("权限不足") - nextRound(room) - } else if (token == room.players[1]) { - if (room.bpData!!.whoseTurn != 1) - throw HandlerException("不是你的回合") - if (st != SpellStatus.NONE || - room.bpData!!.banPick == 0 && status != SpellStatus.RIGHT_SELECT || - room.bpData!!.banPick == 1 && status != SpellStatus.BANNED - ) throw HandlerException("权限不足") - nextRound(room) + when (token) { + room.players[0] -> { + if (room.bpData!!.whoseTurn != 0) + throw HandlerException("不是你的回合") + if (st != SpellStatus.NONE || + room.bpData!!.banPick == 0 && status != SpellStatus.LEFT_SELECT || + room.bpData!!.banPick == 1 && status != SpellStatus.BANNED + ) throw HandlerException("权限不足") + nextRound(room) + } + + room.players[1] -> { + if (room.bpData!!.whoseTurn != 1) + throw HandlerException("不是你的回合") + if (st != SpellStatus.NONE || + room.bpData!!.banPick == 0 && status != SpellStatus.RIGHT_SELECT || + room.bpData!!.banPick == 1 && status != SpellStatus.BANNED + ) throw HandlerException("权限不足") + nextRound(room) + } + + else -> { + if (!isReset && status == SpellStatus.NONE) { + if (st == SpellStatus.LEFT_SELECT) room.bpData!!.spellFailedCountA[idx]++ + else if (st == SpellStatus.RIGHT_SELECT) room.bpData!!.spellFailedCountB[idx]++ + } + } } return status } diff --git a/src/main/kotlin/RoomTypeLink.kt b/src/main/kotlin/RoomTypeLink.kt index c2d01c4..33ec5d9 100644 --- a/src/main/kotlin/RoomTypeLink.kt +++ b/src/main/kotlin/RoomTypeLink.kt @@ -23,7 +23,14 @@ object RoomTypeLink : RoomType { } @Throws(HandlerException::class) - override fun handleUpdateSpell(room: Room, token: String, idx: Int, status: SpellStatus, now: Long): SpellStatus { + override fun handleUpdateSpell( + room: Room, + token: String, + idx: Int, + status: SpellStatus, + now: Long, + isReset: Boolean + ): SpellStatus { val st = room.spellStatus!![idx] if (status == SpellStatus.BANNED) throw HandlerException("不支持的操作") diff --git a/src/main/kotlin/RoomTypeNormal.kt b/src/main/kotlin/RoomTypeNormal.kt index d9e3b80..34932fd 100644 --- a/src/main/kotlin/RoomTypeNormal.kt +++ b/src/main/kotlin/RoomTypeNormal.kt @@ -15,7 +15,14 @@ object RoomTypeNormal : RoomType { } @Throws(HandlerException::class) - override fun handleUpdateSpell(room: Room, token: String, idx: Int, status: SpellStatus, now: Long): SpellStatus { + override fun handleUpdateSpell( + room: Room, + token: String, + idx: Int, + status: SpellStatus, + now: Long, + isReset: Boolean + ): SpellStatus { val st = room.spellStatus!![idx] if (status == BANNED) throw HandlerException("不支持的操作") @@ -43,7 +50,8 @@ object RoomTypeNormal : RoomType { status LEFT_SELECT -> { - val remainSelectTime = if (room.lastGetTime[0] == 0L) 0 else ((room.cdTime - 1) * 1000 - now + room.startMs + room.totalPauseMs + room.lastGetTime[0]) + val remainSelectTime = + if (room.lastGetTime[0] == 0L) 0 else ((room.cdTime - 1) * 1000 - now + room.startMs + room.totalPauseMs + room.lastGetTime[0]) if (remainSelectTime > 0) throw HandlerException("还有${remainSelectTime / 1000 + 1}秒才能选卡") if (st == RIGHT_SELECT) BOTH_SELECT else status @@ -69,7 +77,8 @@ object RoomTypeNormal : RoomType { status RIGHT_SELECT -> { - val remainSelectTime = if (room.lastGetTime[1] == 0L) 0 else ((room.cdTime - 1) * 1000 - now + room.startMs + room.totalPauseMs + room.lastGetTime[1]) + val remainSelectTime = + if (room.lastGetTime[1] == 0L) 0 else ((room.cdTime - 1) * 1000 - now + room.startMs + room.totalPauseMs + room.lastGetTime[1]) if (remainSelectTime > 0) throw HandlerException("还有${remainSelectTime / 1000 + 1}秒才能选卡") if (st == LEFT_SELECT) BOTH_SELECT else status diff --git a/src/main/kotlin/message/GetSpellsCs.kt b/src/main/kotlin/message/GetSpellsCs.kt index 9355fab..3cf5777 100644 --- a/src/main/kotlin/message/GetSpellsCs.kt +++ b/src/main/kotlin/message/GetSpellsCs.kt @@ -26,6 +26,7 @@ class GetSpellsCs : Handler { pauseEndMs = room.pauseEndMs, status = IntArray(room.spellStatus!!.size) { i -> room.spellStatus!![i].value }, linkData = room.linkData, + bpData = room.bpData, phase = room.phase, lastGetTime = room.lastGetTime ) diff --git a/src/main/kotlin/message/SpellListSc.kt b/src/main/kotlin/message/SpellListSc.kt index 57dc37b..5a214c5 100644 --- a/src/main/kotlin/message/SpellListSc.kt +++ b/src/main/kotlin/message/SpellListSc.kt @@ -1,5 +1,6 @@ package org.tfcc.bingo.message +import org.tfcc.bingo.BpData import org.tfcc.bingo.LinkData import org.tfcc.bingo.Spell @@ -15,5 +16,6 @@ class SpellListSc( val status: IntArray?, val phase: Int, val linkData: LinkData?, - val lastGetTime: LongArray + val bpData: BpData?, + val lastGetTime: LongArray, ) diff --git a/src/main/kotlin/message/StartGameCs.kt b/src/main/kotlin/message/StartGameCs.kt index 9727d29..4cca4a2 100644 --- a/src/main/kotlin/message/StartGameCs.kt +++ b/src/main/kotlin/message/StartGameCs.kt @@ -47,12 +47,13 @@ class StartGameCs : Handler { whoseTurn = room.bpData?.whoseTurn ?: 0, banPick = room.bpData?.banPick ?: 0, linkData = room.linkData, + bpData = room.bpData, phase = room.phase, pauseBeginMs = 0L, pauseEndMs = 0L, status = null, totalPauseTime = 0L, - lastGetTime = room.lastGetTime + lastGetTime = room.lastGetTime, ) ) ) diff --git a/src/main/kotlin/message/StopGameCs.kt b/src/main/kotlin/message/StopGameCs.kt index 82e88d2..5e28965 100644 --- a/src/main/kotlin/message/StopGameCs.kt +++ b/src/main/kotlin/message/StopGameCs.kt @@ -31,6 +31,7 @@ class StopGameCs(val winner: Int) : Handler { room.pauseBeginMs = 0 room.pauseEndMs = 0 room.bpData = null + room.linkData = null Store.putRoom(room) if (winner == -1) Store.notifyPlayerInfo(token, protoName) diff --git a/src/main/kotlin/message/UpdateSpellCs.kt b/src/main/kotlin/message/UpdateSpellCs.kt index d13b128..9f5975a 100644 --- a/src/main/kotlin/message/UpdateSpellCs.kt +++ b/src/main/kotlin/message/UpdateSpellCs.kt @@ -5,7 +5,7 @@ import org.tfcc.bingo.Store import org.tfcc.bingo.Supervisor import org.tfcc.bingo.toSpellStatus -class UpdateSpellCs(val idx: Int, val status: Int) : Handler { +class UpdateSpellCs(val idx: Int, val status: Int, val isReset: Boolean = false) : Handler { @Throws(HandlerException::class) override fun handle(ctx: ChannelHandlerContext, token: String, protoName: String) { if (idx < 0 || idx >= 25) throw HandlerException("idx超出范围") @@ -16,7 +16,7 @@ class UpdateSpellCs(val idx: Int, val status: Int) : Handler { if (!room.started) throw HandlerException("游戏还没开始") if (room.host != token && !room.players.contains(token)) throw HandlerException("没有权限") val now = System.currentTimeMillis() - val newStatus = room.type.handleUpdateSpell(room, token, idx, spellStatus, now) + val newStatus = room.type.handleUpdateSpell(room, token, idx, spellStatus, now, isReset) room.spellStatus!![idx] = newStatus val playerIndex = room.players.indexOf(token) if (playerIndex >= 0 && spellStatus.isGetStatus()) @@ -33,7 +33,8 @@ class UpdateSpellCs(val idx: Int, val status: Int) : Handler { idx, newStatus.value, room.bpData?.whoseTurn ?: 0, - room.bpData?.banPick ?: 0 + room.bpData?.banPick ?: 0, + isReset ) ) ) diff --git a/src/main/kotlin/message/UpdateSpellSc.kt b/src/main/kotlin/message/UpdateSpellSc.kt index 032cc81..61fe3dd 100644 --- a/src/main/kotlin/message/UpdateSpellSc.kt +++ b/src/main/kotlin/message/UpdateSpellSc.kt @@ -4,5 +4,6 @@ class UpdateSpellSc( val idx: Int, val status: Int, val whoseTurn: Int, - val banPick: Int + val banPick: Int, + val isReset: Boolean )