Battle Store
直播 PK 管理相关接口,管理 PK 的创建、加入、离开等操作。
Overview
BattleStore Manages all PK-related operations between hosts, including initiating, accepting, rejecting, and exiting processes. PK feature enables real-time interactive battles between hosts. BattleStore provides a comprehensive set of APIs to manage the entire PK lifecycle.
Core Features
PK Request Management: Hosts can initiate PK requests, and invitees can accept or reject
State Management: Real-time tracking of PK information, participating users, and scores
Event-Driven Architecture: Provides complete PK event callbacks
Timeout Handling: Built-in timeout mechanism for PK requests
Important: Always use the method BattleStore.create with a valid live room ID to create a
BattleStoreinstance. Do not attempt to initialize directly. PK state updates are delivered through the battleState publisher. Subscribe to it to receive real-time updates about PK information, participating users, and scores.
PK Workflow
The following table shows a typical PK workflow:
| Step | Role | Action | Triggered Event |
| 1 | Host A | Call requestBattle | BattleListener.onBattleRequestReceived |
| 2 | Host B | Call acceptBattle | BattleListener.onBattleRequestAccept |
| 3 | System | PK starts | BattleListener.onBattleStarted |
Warning: If a PK request does not receive a response within the specified timeout, the BattleListener.onBattleRequestTimeout event will be triggered. Always handle timeout scenarios in the UI.
Usage Example
// Create store instance
val store = BattleStore.create("live_room_123")
// Subscribe to state changes
lifecycleScope.launch {
store.battleState.currentBattleInfo.collect { battleInfo ->
battleInfo?.let {
println("Current PK ID: ${it.battleID}")
}
}
}
// Add PK event listener
store.addBattleListener(object : BattleListener() {
override fun onBattleStarted(battleInfo: BattleInfo, inviter: SeatUserInfo, invitees: List<SeatUserInfo>) {
println("PK started, initiator: ${inviter.userName}")
}
override fun onBattleEnded(battleInfo: BattleInfo, reason: BattleEndedReason?) {
println("PK ended, reason: $reason")
}
})
// Initiate PK request
val config = BattleConfig(duration = 300, needResponse = true)
store.requestBattle(config, listOf("user_456"), 30, object : BattleRequestCallback {
override fun onSuccess(battleInfo: BattleInfo, resultMap: Map<String, Int>) {
println("PK request successful: ${battleInfo.battleID}")
}
override fun onError(code: Int, desc: String) {
println("PK request failed: $desc")
}
})Warning: If a PK request does not receive a response within the specified timeout, the
`BattleEvent/onBattleRequestTimeout(battleID:inviter:invitee:)`event will be triggered. Always handle timeout scenarios in the UI.
Topics
Creating Instance
BattleStore.create - Create BattleStore instance
Observing State and Events
battleState - PK state data
addBattleListener/removeBattleListener - PK event callbacks
PK Operations
requestBattle - Initiate PK request
cancelBattleRequest - Cancel PK request
acceptBattle - Accept PK request
rejectBattle - Reject PK request
exitBattle - Exit PK
See Also
Functions
Accept PK request
Add PK callback listener
Cancel PK request
Exit PK
Reject PK request
Remove PK callback listener
Initiate PK request