Skip to content

Commit

Permalink
BP赛记录收卡失败次数
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Jan 9, 2024
1 parent 54a7bf2 commit b9bcae3
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 31 deletions.
32 changes: 30 additions & 2 deletions src/main/kotlin/BpData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
9 changes: 8 additions & 1 deletion src/main/kotlin/RoomType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ sealed interface RoomType {
fun randSpells(games: Array<String>, ranks: Array<String>?, difficulty: Difficulty): Array<Spell>

@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
}
56 changes: 38 additions & 18 deletions src/main/kotlin/RoomTypeBP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/RoomTypeLink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("不支持的操作")
Expand Down
15 changes: 12 additions & 3 deletions src/main/kotlin/RoomTypeNormal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("不支持的操作")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/message/GetSpellsCs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/message/SpellListSc.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tfcc.bingo.message

import org.tfcc.bingo.BpData
import org.tfcc.bingo.LinkData
import org.tfcc.bingo.Spell

Expand All @@ -15,5 +16,6 @@ class SpellListSc(
val status: IntArray?,
val phase: Int,
val linkData: LinkData?,
val lastGetTime: LongArray
val bpData: BpData?,
val lastGetTime: LongArray,
)
3 changes: 2 additions & 1 deletion src/main/kotlin/message/StartGameCs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/message/StopGameCs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/message/UpdateSpellCs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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超出范围")
Expand All @@ -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())
Expand All @@ -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
)
)
)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/message/UpdateSpellSc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class UpdateSpellSc(
val idx: Int,
val status: Int,
val whoseTurn: Int,
val banPick: Int
val banPick: Int,
val isReset: Boolean
)

0 comments on commit b9bcae3

Please sign in to comment.