In Dual Battle | Dual Battle results | In Multi-player Battle | Multi-player Battle results |
| | | |
Click the Battle button | Stop Waiting for battle | Exit Battle |
| | |
Anchor receives Battle invitation | Anchor accepts Battle |
| |
// File location: iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/LivingView/Battle├── BattleCountDownBackgroundView.swift // Battle waiting countdown background style└── BattleCountDownView.swift // Battle waiting countdown foreground style
// File location:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/SingleBattleScoreView.swiftclass SingleBattleScoreView: UIView {...func constructViewHierarchy() {// View Hierarchy Construction}func activateConstraints() {// View Layout}}
// File location:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/BattleMemberInfoView.swiftclass BattleMemberInfoView: UIView {...func constructViewHierarchy() {// View Hierarchy Construction}func activateConstraints() {// View Layout}}
// File location:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/BattleInfoView.swiftclass BattleInfoView: UIView {...func showBattleResult(store: LiveStore) {// Battle Result Display}}
BattleService
. You can obtain the battle management object through store.serviceCenter.battleService
and call the relevant battle API functions to implement the battle feature. For example, in the interaction between Anchor A and B, refer to the diagram below for the specific interaction sequence.onBattleStarted
callback.requestBattle
, passing the maximum Battle duration in parameter config
, whether the inviter needs to reply with accept/reject, and passing anchor B's userId in parameter userIdList
, and passing the battle invitation wait duration in parameter timeout
.// File location:iOS/TUILiveKit/Source/Service/BattleService.swiftfunc requestBattle(config: TUIBattleConfig, userIdList: [String], timeout: TimeInterval) -> AnyPublisher<(TUIBattleInfo, [String: TUIBattleCode]), InternalError> {return Future<(TUIBattleInfo, [String: TUIBattleCode]), InternalError> { [weak self] promise inguard let self = self else { return }self.battleManager.requestBattle(config: config, userIdList: userIdList, timeout: timeout) { battleInfo, resultMap invar battleResult: [String: TUIBattleCode] = [:]resultMap.forEach { (key: String, value: NSNumber) inbattleResult[key] = TUIBattleCode(rawValue: value.intValue) ?? .unknown}promise(.success((battleInfo, battleResult)))} onError: { err, message inlet error = InternalError(error: err, message: message)promise(.failure(error))}}.eraseToAnyPublisher()}
onBattleRequestAccept
.onBattleRequestReceived
.// File Location: iOS/TUILiveKit/Source/Service/EngineServiceCenter.swiftfunc onBattleRequestReceived(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {guard let store = self.store else { return}store.dispatch(action: BattleActions.onBattleRequestReceived(payload:(battleInfo.battleId, inviter)))}
acceptBattle
.// File location:iOS/TUILiveKit/Source/Service/BattleService.swiftfunc acceptBattle(battleId: String) -> AnyPublisher<Void, InternalError> {return Future<Void, InternalError> { [weak self] promise inguard let self = self else { return }self.battleManager.acceptBattle(battleId: battleId) {promise(.success(()))} onError: { err, message inlet error = InternalError(error: err, message: message)promise(.failure(error))}}.eraseToAnyPublisher()}
onBattleStarted
.// File Location: iOS/TUILiveKit/Source/Service/EngineServiceCenter.swiftfunc onBattleStarted(battleInfo: TUIBattleInfo) {guard let store = self.store else { return }handleBattleStarted(battleInfo: battleInfo)}private func handleBattleStarted(battleInfo: TUIBattleInfo) {guard let store = self.store else { return }battleInfo.config.duration = battleInfo.config.duration + Double(battleInfo.startTime) - Date().timeIntervalSince1970store.dispatch(action: BattleActions.onBattleStarted(payload: battleInfo))let selfUserId = store.selectCurrent(UserSelectors.getSelfInfo).userIdif battleInfo.inviter.userId == selfUserId || battleInfo.inviteeList.contains(where: { $0.userId == selfUserId }) {store.dispatch(action: BattleActions.setIsOnBattle(payload: true))}}
onUserExitBattle
callback.onBattleEnded
callback.onBattleEnded
callback.exitBattle
to exit the battle.// File location:iOS/TUILiveKit/Source/Service/BattleService.swiftfunc exitBattle(battleId: String) -> AnyPublisher<Void, InternalError> {return Future<Void, InternalError> { [weak self] promise inguard let self = self else { return }self.battleManager.exitBattle(battleId: battleId) {promise(.success(()))} onError: { err, message inlet error = InternalError(error: err, message: message)promise(.failure(error))}}.eraseToAnyPublisher()}
onBattleEnded
callback and the battle end notification. // File Location: iOS/TUILiveKit/Source/Service/EngineServiceCenter.swiftfunc onBattleEnded(battleInfo: TUIBattleInfo, reason: TUIBattleStoppedReason) {guard let store = store else { return }store.dispatch(action: BattleActions.onBattleEnded(payload:(battleInfo, reason)))store.dispatch(action: BattleActions.setIsOnBattle(payload: false))}
Was this page helpful?