286 lines
9.1 KiB
Objective-C
286 lines
9.1 KiB
Objective-C
//
|
||
// XPIMRoomMicQueueImpl.m
|
||
// xplan-ios
|
||
//
|
||
// Created by 冯硕 on 2021/10/21.
|
||
//
|
||
|
||
#import "XPIMRoomMicQueueImpl.h"
|
||
///Third
|
||
#import <NIMSDK/NIMSDK.h>
|
||
///Tool
|
||
#import "XPIMManager.h"
|
||
#import "RtcManager.h"
|
||
#import "AccountInfoStorage.h"
|
||
#import "NSObject+JSONString.h"
|
||
///Model
|
||
#import "MicroModel.h"
|
||
#import "MicroQueueItemModel.h"
|
||
#import "RoomInfoModel.h"
|
||
#import "UserInfoModel.h"
|
||
///P
|
||
#import "XPIMRoomMicQueueDelegate.h"
|
||
|
||
#define MIC_COUNT(Type) [self roomTypecpORnormal:Type]
|
||
|
||
@interface XPIMRoomMicQueueImpl ()
|
||
///麦序的状态 <麦序 麦序上的内容>
|
||
@property (nonatomic,strong) NSMutableDictionary<NSString *, MicroQueueItemModel *> *queue;
|
||
///代理数组
|
||
@property (nonatomic,strong) NSMutableArray *delegateArray;
|
||
|
||
///用户信息
|
||
@property (nonatomic,strong) UserInfoModel *userInfo;
|
||
@end
|
||
|
||
@implementation XPIMRoomMicQueueImpl
|
||
|
||
#pragma mark - Private Method
|
||
// 给宏定义赋值
|
||
-(NSInteger )roomTypecpORnormal:(RoomType )type{
|
||
if (type == RoomType_Game){
|
||
return 9;
|
||
}else{
|
||
return 9;
|
||
}
|
||
}
|
||
|
||
///提前绑定一下麦序
|
||
- (void)configDefaultMicQueue {
|
||
RoomInfoModel * roomfInfo = [XPIMManager shareManager].chatRoomManager.getCurrentRoomInfo;
|
||
for (int i = 0 ; i < MIC_COUNT(roomfInfo.type); i++) {
|
||
MicroModel *state = [[MicroModel alloc]init];
|
||
state.posState = MicroPosStateType_Free;
|
||
state.micState = MicroMicStateType_Open;
|
||
MicroQueueItemModel *micSequence = [[MicroQueueItemModel alloc]init];
|
||
micSequence.microState = state;
|
||
micSequence.userInfo = nil;
|
||
[self.queue setObject:micSequence forKey:[NSString stringWithFormat:@"%d",i-1]];
|
||
}
|
||
}
|
||
|
||
///根据uid 看用户是否在麦上 返回麦上的信息
|
||
- (MicroQueueItemModel *)findUserMicroModel:(NSInteger)uid {
|
||
NSArray * users = self.microQueue.allValues;
|
||
for (int i = 0; i < users.count; i++) {
|
||
MicroQueueItemModel * model = [users objectAtIndex:i];
|
||
if (model.userInfo && model.userInfo.uid == uid) {
|
||
return model;
|
||
}
|
||
}
|
||
return nil;
|
||
}
|
||
|
||
|
||
/// 移除队列
|
||
/// @param position 坑位
|
||
- (void)removeChatRoomQueue:(NSString *)position success:(void(^)(BOOL success))success{
|
||
if (position) {
|
||
NIMChatroomQueueRemoveRequest *request = [[NIMChatroomQueueRemoveRequest alloc]init];
|
||
request.key = position;
|
||
request.roomId = [NSString stringWithFormat:@"%ld", [XPIMManager shareManager].chatRoomManager.getCurrentRoomInfo.roomId];
|
||
[[NIMSDK sharedSDK].chatroomManager removeChatroomQueueObject:request completion:^(NSError * _Nullable error, NSDictionary<NSString *,NSString *> * _Nullable element) {
|
||
if (error == nil ) {
|
||
///成功
|
||
if (success) {
|
||
success(YES);
|
||
}
|
||
}else {
|
||
///失败
|
||
[self fetchRoomQueue:[NSString stringWithFormat:@"%ld", [XPIMManager shareManager].chatRoomManager.getCurrentRoomInfo.roomId]];
|
||
}
|
||
}];
|
||
}
|
||
}
|
||
|
||
///更新队列
|
||
- (void)updateChatroomQueueObject:(NSString *)position userInfo:(UserInfoModel *)userInfo {
|
||
NSDictionary * dic = @{@"uid":@(userInfo.uid), @"nick":userInfo.nick, @"gender":@(userInfo.gender), @"avatar":userInfo.avatar};
|
||
NIMChatroomQueueUpdateRequest *request = [[NIMChatroomQueueUpdateRequest alloc]init];
|
||
request.key = position;
|
||
request.value = [dic objectToJSONString];
|
||
request.roomId = [NSString stringWithFormat:@"%ld",[XPIMManager shareManager].chatRoomManager.getCurrentRoomInfo.roomId];
|
||
request.transient = YES;
|
||
[[NIMSDK sharedSDK].chatroomManager updateChatroomQueueObject:request completion:^(NSError * _Nullable error) {
|
||
if (error == nil ) {
|
||
|
||
}else {
|
||
|
||
}
|
||
}];
|
||
}
|
||
|
||
- (void)updateChatRoomQueue:(NSString *)position{
|
||
///TODO 需要获取用户信息
|
||
UserInfoModel * userInfo = self.userInfo;
|
||
MicroQueueItemModel * model = [self findUserMicroModel:userInfo.uid];
|
||
if (model) {///用户在坑位上的话
|
||
if (position.intValue != model.microState.position) {
|
||
[self removeChatRoomQueue:[NSString stringWithFormat:@"%d", model.microState.position] success:^(BOOL success) {
|
||
[self updateChatroomQueueObject:position userInfo:userInfo];
|
||
}];
|
||
} else {
|
||
[self updateChatroomQueueObject:position userInfo:userInfo];
|
||
}
|
||
|
||
} else {
|
||
[self updateChatroomQueueObject:position userInfo:userInfo];
|
||
}
|
||
}
|
||
|
||
|
||
#pragma mark - XPIMRoomMicQueueInterface
|
||
///进入房间成功之后根据扩展字段整理麦序
|
||
- (void)enterRoomRepairMicQueue:(NSDictionary *)micDictionary {
|
||
[self configDefaultMicQueue];
|
||
for (NSString *position in micDictionary.allKeys) {
|
||
MicroModel *state = [MicroModel modelWithJSON:micDictionary[position]];
|
||
MicroQueueItemModel *sequence = [self.queue objectForKey:position];
|
||
sequence.microState = state;
|
||
}
|
||
|
||
}
|
||
///获取房间队列
|
||
- (void)fetchRoomQueue:(NSString *)roomid {
|
||
[[NIMSDK sharedSDK].chatroomManager fetchChatroomQueue:roomid completion:^(NSError * _Nullable error, NSArray<NSDictionary<NSString *,NSString *> *> * _Nullable info) {
|
||
if (error == nil && info!= nil) {
|
||
for (NSDictionary *item in info) {
|
||
UserInfoModel *userInfo = [UserInfoModel modelWithJSON:item.allValues.firstObject];
|
||
NSString *position = item.allKeys.firstObject;
|
||
MicroQueueItemModel *sequence = [self.queue objectForKey:position];
|
||
sequence.userInfo = userInfo;
|
||
}
|
||
|
||
for (id<XPIMRoomMicQueueDelegate> delegate in self.delegateArray) {
|
||
if ([delegate respondsToSelector:@selector(onGetRoomMicroQueueSuccess:)]) {
|
||
[delegate onGetRoomMicroQueueSuccess:self.queue];
|
||
}
|
||
}
|
||
} else {
|
||
NSLog(@"获取队列失败");
|
||
}
|
||
}];
|
||
}
|
||
///麦序的队列
|
||
- (NSDictionary *)microQueue {
|
||
return [self.queue copy];
|
||
}
|
||
|
||
- (void)upMic:(NSString *)position {
|
||
///这个坑位 有没有人
|
||
UserInfoModel *userInfo = [[self.queue objectForKey:position] userInfo];
|
||
MicroModel *micState = [[self.queue objectForKey:position] microState];
|
||
if (userInfo) {
|
||
return;
|
||
} else {
|
||
NSString * uid = [[AccountInfoStorage instance] getUid];
|
||
if (micState.posState == MicroPosStateType_Free) {
|
||
[self updateChatRoomQueue:position];
|
||
} else {
|
||
[[[XPIMManager shareManager].chatRoomMemberManager rac_queryChatRoomMember:uid] subscribeNext:^(id _Nullable x) {
|
||
NIMChatroomMember * member = x;
|
||
if (member.type == NIMChatroomMemberTypeCreator || member.type == NIMTeamMemberTypeManager) {///房主或者管理员的话
|
||
[self updateChatRoomQueue:position];
|
||
}
|
||
}];
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)downMic:(NSString *)position {
|
||
[self removeChatRoomQueue:position success:nil];
|
||
}
|
||
|
||
///更新坑位的状态
|
||
- (void)updateMicroStatus:(NSArray *)microStatus {
|
||
for (MicroModel * microState in microStatus) {
|
||
MicroQueueItemModel *micSequence = [self.queue objectForKey:[NSString stringWithFormat:@"%d",microState.position]];
|
||
if (micSequence != nil) {
|
||
micSequence.microState = microState;
|
||
}
|
||
}
|
||
|
||
for (id<XPIMRoomMicQueueDelegate> delegate in self.delegateArray) {
|
||
if ([delegate respondsToSelector:@selector(onGetRoomMicroQueueSuccess:)]) {
|
||
[delegate onGetRoomMicroQueueSuccess:self.queue];
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)onReceiveRoomQueueUpdate:(NIMChatroomNotificationContent *)content {
|
||
NSDictionary *temp = (NSDictionary *)content.ext;
|
||
UserInfoModel *userInfo = [UserInfoModel modelWithJSON:[temp objectForKey:NIMChatroomEventInfoQueueChangeItemValueKey]];
|
||
//用户所在旧麦位,为空表示上麦,有值表示换麦或更新麦位信息
|
||
NSString *oldPosition;
|
||
//去重复 如果该uid在房间先清空
|
||
for (MicroQueueItemModel *sequence in self.queue.allValues) {
|
||
if (userInfo.uid == sequence.userInfo.uid) {
|
||
oldPosition = @(sequence.microState.position).stringValue;
|
||
sequence.userInfo = nil;
|
||
}
|
||
}
|
||
|
||
if ([temp[NIMChatroomEventInfoQueueChangeTypeKey] intValue] == 1) {//上麦
|
||
MicroQueueItemModel *sequence = [self.queue objectForKey:[temp objectForKey:NIMChatroomEventInfoQueueChangeItemKey]];
|
||
sequence.userInfo = userInfo;
|
||
///声网服务
|
||
MicroModel * micState = sequence.microState;
|
||
if (micState.micState == MicroMicStateType_Open) {
|
||
[[RtcManager instance] broadcast:YES];
|
||
} else {
|
||
[[RtcManager instance] broadcast:NO];
|
||
}
|
||
for (id<XPIMRoomMicQueueDelegate> delegate in self.delegateArray) {
|
||
if ([delegate respondsToSelector:@selector(onGetRoomMicroQueueSuccess:)]) {
|
||
[delegate onGetRoomMicroQueueSuccess:self.queue];
|
||
}
|
||
}
|
||
}else{//下麦
|
||
[[RtcManager instance] broadcast:NO];
|
||
for (id<XPIMRoomMicQueueDelegate> delegate in self.delegateArray) {
|
||
if ([delegate respondsToSelector:@selector(onGetRoomMicroQueueSuccess:)]) {
|
||
[delegate onGetRoomMicroQueueSuccess:self.queue];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)resetMicroQueue {
|
||
self.queue = nil;
|
||
}
|
||
|
||
///添加代理
|
||
- (void)addDelegate:(id<XPIMRoomMicQueueDelegate>)delegate {
|
||
if (delegate) {
|
||
[self.delegateArray addObject:delegate];
|
||
}
|
||
}
|
||
///移除代理
|
||
- (void)removeDelegate:(id<XPIMRoomMicQueueDelegate>)delegate {
|
||
if (delegate && [self.delegateArray containsObject:delegate]) {
|
||
[self.delegateArray removeObject:delegate];
|
||
delegate = nil;
|
||
}
|
||
}
|
||
|
||
#warning to do 该结构的时候 调整这个吧 上麦需要用户信息
|
||
- (void)setCurrentUserInfo:(UserInfoModel * _Nullable)userInfo {
|
||
self.userInfo = userInfo;
|
||
}
|
||
|
||
#pragma mark - Getters And Setters
|
||
- (NSMutableDictionary<NSString *,MicroQueueItemModel *> *)queue {
|
||
if (!_queue) {
|
||
_queue= [NSMutableDictionary dictionary];
|
||
}
|
||
return _queue;
|
||
}
|
||
- (NSMutableArray *)delegateArray {
|
||
if (!_delegateArray) {
|
||
_delegateArray = [NSMutableArray array];
|
||
}
|
||
return _delegateArray;
|
||
}
|
||
|
||
@end
|