BattleStore

abstract class BattleStore

直播 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 BattleStore instance. 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:

StepRoleActionTriggered Event
1Host ACall requestBattleBattleListener.onBattleRequestReceived
2Host BCall acceptBattleBattleListener.onBattleRequestAccept
3SystemPK startsBattleListener.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

Observing State and Events

PK Operations

See Also

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val battleState: BattleState

PK state

Functions

Link copied to clipboard
abstract fun acceptBattle(battleID: String?, completion: CompletionHandler?)

Accept PK request

Link copied to clipboard
abstract fun addBattleListener(listener: BattleListener?)

Add PK callback listener

Link copied to clipboard
abstract fun cancelBattleRequest(battleID: String?, userIDList: List<String>, completion: CompletionHandler?)

Cancel PK request

Link copied to clipboard
abstract fun exitBattle(battleID: String?, completion: CompletionHandler?)

Exit PK

Link copied to clipboard
abstract fun rejectBattle(battleID: String?, completion: CompletionHandler?)

Reject PK request

Link copied to clipboard
abstract fun removeBattleListener(listener: BattleListener?)

Remove PK callback listener

Link copied to clipboard
abstract fun requestBattle(config: BattleConfig, userIDList: List<String>, timeout: Int, completion: BattleRequestCallback?)

Initiate PK request