BattleStore class abstract

Live PK management APIs for creating, joining, and leaving PK sessions.

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.

Key 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 factory method BattleStore.create with a valid live room ID to create a BattleStore instance. Do not attempt to initialize directly.

Note: 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

Usage Example

// Create store instance
final store = BattleStore.create('live_room_123');

// Define listeners
late final VoidCallback battleInfoListener = _onBattleInfoChanged;
late final VoidCallback battleUsersListener = _onBattleUsersChanged;

void _onBattleInfoChanged() {
    final battleInfo = store.battleState.currentBattleInfo.value;
    if (battleInfo != null) {
        print('Current PK ID: ${battleInfo.battleID}');
    }
}

void _onBattleUsersChanged() {
    print('PK user count: ${store.battleState.battleUsers.value.length}');
}

// Subscribe to state changes
store.battleState.currentBattleInfo.addListener(battleInfoListener);
store.battleState.battleUsers.addListener(battleUsersListener);

// Add PK event listener
final battleListener = BattleListener(
    onBattleStarted: (battleInfo, inviter, invitees) {
        print('PK started, initiator: ${inviter.userName}');
    },
    onBattleEnded: (battleInfo, reason) {
        print('PK ended, reason: $reason');
    },
);
store.addBattleListener(battleListener);

// Initiate PK request
final config = BattleConfig(duration: 300, needResponse: true);
final result = await store.requestBattle(
    config: config,
    userIDList: ['user_456'],
    timeout: 30,
);
if (result.code == 0) {
    print('PK request successful: ${result.battleInfo?.battleID}');
} else {
    print('PK request failed: ${result.message}');
}

// Unsubscribe when done
store.battleState.currentBattleInfo.removeListener(battleInfoListener);
store.battleState.battleUsers.removeListener(battleUsersListener);
store.removeBattleListener(battleListener);

Topics

Creating Instance

Observing State and Events

PK Operations

See Also

Constructors

BattleStore()

Properties

battleState → BattleState
PK state
no setter
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

acceptBattle(String battleID) → Future<CompletionHandler>
Accept PK request
addBattleListener(BattleListener listener) → void
Add PK callback listener
cancelBattleRequest({required String battleID, required List<String> userIDList}) → Future<CompletionHandler>
Cancel PK request
exitBattle(String battleID) → Future<CompletionHandler>
Exit PK
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rejectBattle(String battleID) → Future<CompletionHandler>
Reject PK request
removeBattleListener(BattleListener listener) → void
Remove PK callback listener
requestBattle({required BattleConfig config, required List<String> userIDList, required int timeout}) → Future<BattleRequestCompletionHandler>
Initiate PK request
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

create(String liveID) → BattleStore
Create BattleStore instance