双人 PK 中 | 双人 PK 结果 | 多人 PK 中 | 多人 PK 结果 |
| | | |
点击 PK 按钮 | 停止 PK 等待 | 断开 PK |
| | |
主播收到 PK 邀请 | 主播接受 PK |
| |
// 文件位置:iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/LivingView/Battle├── BattleCountDownBackgroundView.swift // PK 等待倒计时背景样式└── BattleCountDownView.swift // PK 等待倒计时前景样式
// 文件位置:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/SingleBattleScoreView.swiftclass SingleBattleScoreView: UIView {...func constructViewHierarchy() {// 视图层级构建}func activateConstraints() {// 视图layout布局}}
// 文件位置:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/BattleMemberInfoView.swiftclass BattleMemberInfoView: UIView {...func constructViewHierarchy() {// 视图层级构建}func activateConstraints() {// 视图layout布局}}
// 文件位置:iOS/TUILiveKit/Source/View/LiveRoom/View/Common/Battle/BattleInfoView.swiftclass BattleInfoView: UIView {...func showBattleResult(store: LiveStore) {// PK结果展示}}
BattleService
实现的,您可通过 store.serviceCenter.battleService
获取到 PK 管理类对象,进而调用 PK 相关 API 函数,实现 PK 功能。以主播 A 和主播 B 的 PK 为例,具体交互时序可参考下图。onBattleStarted
回调。requestBattle
发起PK,在参数 config
中传入 PK 最大时长、是否需要邀请方回复同意/拒绝,在参数 userIdList
中传入主播 B 的 userId,在参数 timeout
中传入 PK 邀请等待时长。// 文件位置: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
接收PK请求回调。// 文件位置: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
接受PK请求。// 文件位置: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
接收 PK 开始回调。// 文件位置: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
回调。onBattleEnded
回调。onBattleEnded
回调。exitBattle
退出PK。// 文件位置: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
回调,收到 PK 结束通知。 // 文件位置: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))}
本页内容是否解决了您的问题?