366 lines
11 KiB
Objective-C
366 lines
11 KiB
Objective-C
//
|
|
// RoomBoomManager.m
|
|
// YuMi
|
|
//
|
|
// Created by P on 2024/9/27.
|
|
//
|
|
|
|
#import "RoomBoomManager.h"
|
|
|
|
#import "BoomInfoModel.h"
|
|
#import "UserInfoModel.h"
|
|
#import "AttachmentModel.h"
|
|
#import "XPSkillCardPlayerManager.h"
|
|
|
|
@interface RoomBoomManager () <NIMChatManagerDelegate, NIMBroadcastManagerDelegate>
|
|
|
|
@property (nonatomic, strong) NSMutableArray <NSDictionary *> *bannerListeners;
|
|
@property (nonatomic, strong) NSMutableArray <NSDictionary *> *explosionListeners;
|
|
@property (nonatomic, strong) NSMutableArray <NSDictionary *> *enterRoomExplosionListeners;
|
|
@property (nonatomic, strong) NSMutableArray <NSDictionary *> *progressUpdateListeners;
|
|
@property (nonatomic, strong) NSMutableArray <NSDictionary *> *boomGiftsListeners;
|
|
|
|
@property (nonatomic, strong) NSMutableArray <BoomDetailModel *> *boomDetailArray;
|
|
|
|
@property (nonatomic, strong) NSMutableArray *boomEventsQueue;
|
|
@property (nonatomic, strong) NSMutableArray *giftEventsQueue;
|
|
@property (nonatomic, strong) NSMutableArray *bannerEventsQueue;
|
|
|
|
@property (nonatomic, assign) BOOL isBooming;
|
|
@property (nonatomic, assign) BOOL isGifting;
|
|
@property (nonatomic, assign) BOOL isBannering;
|
|
|
|
@property (nonatomic,strong) UserInfoModel *userInfo;
|
|
|
|
@end
|
|
|
|
@implementation RoomBoomManager
|
|
|
|
+ (instancetype)sharedManager {
|
|
static RoomBoomManager *instance;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
instance = [[self alloc] init];
|
|
|
|
[[NIMSDK sharedSDK].chatManager addDelegate:instance];
|
|
[[NIMSDK sharedSDK].broadcastManager addDelegate:instance];
|
|
|
|
[instance initArrays];
|
|
});
|
|
return instance;
|
|
}
|
|
|
|
- (void)initArrays {
|
|
_bannerListeners = [[NSMutableArray alloc] init];
|
|
_explosionListeners = [[NSMutableArray alloc] init];
|
|
_enterRoomExplosionListeners = [[NSMutableArray alloc] init];
|
|
_progressUpdateListeners = [[NSMutableArray alloc] init];
|
|
_boomGiftsListeners = [[NSMutableArray alloc] init];
|
|
_boomDetailArray = [[NSMutableArray alloc] init];
|
|
_boomEventsQueue = [[NSMutableArray alloc] init];
|
|
_giftEventsQueue = [[NSMutableArray alloc] init];
|
|
_bannerEventsQueue = [[NSMutableArray alloc] init];
|
|
}
|
|
|
|
- (void)saveUserInfo:(UserInfoModel *)userInfo {
|
|
_userInfo = userInfo;
|
|
}
|
|
|
|
- (void)test {
|
|
[self handleBannerUpdate:nil];
|
|
[self handleExplosionUpdate:nil];
|
|
}
|
|
|
|
- (NSArray *)loadBoomDetails {
|
|
return self.boomDetailArray;
|
|
}
|
|
|
|
- (void)leaveRoom {
|
|
self.boomEventsQueue = @[].mutableCopy;
|
|
}
|
|
|
|
- (void)updateBoomDetailArray:(NSArray <BoomDetailModel *> *)array {
|
|
self.boomDetailArray = array.mutableCopy;
|
|
}
|
|
|
|
- (void)updateBoomDetail:(BoomDetailModel *)boomDetail {
|
|
for (int i = 0; i < self.boomDetailArray.count; i++) {
|
|
BoomDetailModel *detail = [self.boomDetailArray xpSafeObjectAtIndex:i];
|
|
if (detail.level == boomDetail.level) {
|
|
detail.exp = boomDetail.exp;
|
|
detail.speed = boomDetail.speed;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)cleanBoomDetail {
|
|
|
|
}
|
|
|
|
- (void)registerBoomBanner:(BoomEventBlock)block target:(id)target {
|
|
@synchronized (self.bannerListeners) {
|
|
[self.bannerListeners addObject:@{NSStringFromClass([target class]) : block}];
|
|
}
|
|
}
|
|
|
|
- (void)registerBoomExplosion:(BoomEventBlock)block target:(id)target {
|
|
@synchronized (self.explosionListeners) {
|
|
[self.explosionListeners addObject:@{NSStringFromClass([target class]) : block}];
|
|
}
|
|
}
|
|
|
|
- (void)registerBoomEnterRoomExplosion:(BoomEventBlock)block target:(id)target {
|
|
@synchronized (self.enterRoomExplosionListeners) {
|
|
[self.enterRoomExplosionListeners addObject:@{NSStringFromClass([target class]) : block}];
|
|
}
|
|
}
|
|
|
|
- (void)registerBoomProgressUpdate:(BoomEventBlock)block target:(id)target {
|
|
@synchronized (self.progressUpdateListeners) {
|
|
[self.progressUpdateListeners addObject:@{NSStringFromClass([target class]) : block}];
|
|
}
|
|
}
|
|
|
|
- (void)registerBoomGiftDisplay:(BoomEventBlock)block target:(id)target {
|
|
@synchronized (self.boomGiftsListeners) {
|
|
[self.boomGiftsListeners addObject:@{NSStringFromClass([target class]) : block}];
|
|
}
|
|
}
|
|
|
|
- (void)checkAndStartBoomEvent {
|
|
if (self.isBooming ||
|
|
self.isGifting ||
|
|
self.boomEventsQueue.count == 0) {
|
|
return;
|
|
}
|
|
|
|
self.isBooming = YES;
|
|
NSString *url_1 = @"";
|
|
NSString *url_2 = @"";
|
|
id obj = self.boomEventsQueue.firstObject;
|
|
[self.boomEventsQueue xpSafeRemoveObjectAtIndex:0];
|
|
if ([obj isKindOfClass:[AttachmentModel class]]) {
|
|
Boom632Model *m = [Boom632Model modelWithJSON:[(AttachmentModel *)obj data]];
|
|
if ([XPSkillCardPlayerManager shareInstance].roomUid.integerValue != m.roomUid) {
|
|
[self explosionEnd];
|
|
return;
|
|
}
|
|
|
|
url_1 = m.countDownVapUrl;
|
|
url_2 = m.endVapUrl;
|
|
} else if ([obj isKindOfClass:[RoomBoomSignVo class]]) {
|
|
url_1 = [(RoomBoomSignVo *)obj countDownVapUrl];
|
|
url_2 = [(RoomBoomSignVo *)obj endVapUrl];
|
|
}
|
|
if (url_1.length > 0 && url_2.length > 0) {
|
|
[self handleExplosionUpdate:@[url_1, url_2]];
|
|
}else {
|
|
[self explosionEnd];
|
|
}
|
|
}
|
|
|
|
- (void)explosionEnd {
|
|
if (self.giftEventsQueue.count > 0) {
|
|
if (self.isGifting) {
|
|
return;
|
|
}
|
|
self.isGifting = YES;
|
|
id obj = self.giftEventsQueue.firstObject;
|
|
[self.giftEventsQueue xpSafeRemoveObjectAtIndex:0];
|
|
[self handleBoomGiftUpdate:obj];
|
|
} else {
|
|
self.isBooming = NO;
|
|
[self checkAndStartBoomEvent];
|
|
}
|
|
}
|
|
|
|
- (void)giftDisplayEnd {
|
|
self.isBooming = NO;
|
|
self.isGifting = NO;
|
|
[self checkAndStartBoomEvent];
|
|
}
|
|
|
|
- (void)checkAndStartBannerEvent {
|
|
if (self.isBannering) {
|
|
return;
|
|
}
|
|
if (self.bannerEventsQueue.count > 0) {
|
|
self.isBannering = YES;
|
|
id obj = self.bannerEventsQueue.firstObject;
|
|
[self.bannerEventsQueue xpSafeRemoveObjectAtIndex:0];
|
|
[self handleBannerUpdate:obj];
|
|
}
|
|
}
|
|
|
|
- (void)bannerDisplayEnd {
|
|
self.isBannering = NO;
|
|
[self checkAndStartBannerEvent];
|
|
}
|
|
|
|
- (void)removeEventListenerForTarget:(id)target {
|
|
@synchronized (self.progressUpdateListeners) {
|
|
NSString *key = NSStringFromClass([target class]);
|
|
for (NSDictionary *dic in self.progressUpdateListeners) {
|
|
if ([[dic allKeys] containsObject:key]) {
|
|
[self.progressUpdateListeners removeObject:dic];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@synchronized (self.explosionListeners) {
|
|
NSString *key = NSStringFromClass([target class]);
|
|
for (NSDictionary *dic in self.explosionListeners) {
|
|
if ([[dic allKeys] containsObject:key]) {
|
|
[self.explosionListeners removeObject:dic];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@synchronized (self.bannerListeners) {
|
|
NSString *key = NSStringFromClass([target class]);
|
|
for (NSDictionary *dic in self.bannerListeners) {
|
|
if ([[dic allKeys] containsObject:key]) {
|
|
[self.bannerListeners removeObject:dic];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@synchronized (self.boomGiftsListeners) {
|
|
NSString *key = NSStringFromClass([target class]);
|
|
for (NSDictionary *dic in self.boomGiftsListeners) {
|
|
if ([[dic allKeys] containsObject:key]) {
|
|
[self.boomGiftsListeners removeObject:dic];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@synchronized (self.enterRoomExplosionListeners) {
|
|
NSString *key = NSStringFromClass([target class]);
|
|
for (NSDictionary *dic in self.enterRoomExplosionListeners) {
|
|
if ([[dic allKeys] containsObject:key]) {
|
|
[self.enterRoomExplosionListeners removeObject:dic];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)receiveEnterRoomBoom:(BoomInfoModel *)model {
|
|
[self.boomEventsQueue addObjectsFromArray:model.roomBoomSignVoList];
|
|
[self checkAndStartBoomEvent];
|
|
}
|
|
|
|
- (void)receiveNIMResponse:(AttachmentModel *)attachment {
|
|
switch (attachment.second) {
|
|
case Custom_Message_Room_Boom_EXP: {
|
|
BoomDetailModel *boomDetail = [BoomDetailModel modelWithJSON:attachment.data];
|
|
[self handleProgressUpdate:boomDetail];
|
|
}
|
|
break;
|
|
case Custom_Message_Room_Boom_LevelUp: {
|
|
if ([[XPSkillCardPlayerManager shareInstance] isInRoomVC]) {
|
|
[self.boomEventsQueue addObject:attachment];
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateWhenBoomExplosion" object:nil];
|
|
[self.bannerEventsQueue addObject:attachment];
|
|
}
|
|
// [self.bannerEventsQueue addObject:attachment];
|
|
|
|
[self checkAndStartBoomEvent];
|
|
[self checkAndStartBannerEvent];
|
|
}
|
|
break;
|
|
case Custom_Message_Room_Boom_Award:
|
|
[self.giftEventsQueue addObject:attachment];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
- (void)handleProgressUpdate:(BoomDetailModel *)model {
|
|
[self updateBoomDetail:model];
|
|
|
|
@synchronized (self.progressUpdateListeners) {
|
|
for (NSDictionary *dic in self.progressUpdateListeners) {
|
|
BoomEventBlock listener = [dic allValues].firstObject;
|
|
if (listener) {
|
|
listener(model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)handleExplosionUpdate:(id)model {
|
|
@synchronized (self.explosionListeners) {
|
|
for (NSDictionary *dic in self.explosionListeners) {
|
|
BoomEventBlock listener = [dic allValues].firstObject;
|
|
if (listener) {
|
|
listener(model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)handleBannerUpdate:(id)model {
|
|
@synchronized (self.bannerListeners) {
|
|
for (NSDictionary *dic in self.bannerListeners) {
|
|
BoomEventBlock listener = [dic allValues].firstObject;
|
|
if (listener) {
|
|
listener(model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)handleBoomGiftUpdate:(id)model {
|
|
@synchronized (self.boomGiftsListeners) {
|
|
for (NSDictionary *dic in self.boomGiftsListeners) {
|
|
BoomEventBlock listener = [dic allValues].firstObject;
|
|
if (listener) {
|
|
listener(model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark -
|
|
- (void)onRecvMessages:(NSArray *)messages
|
|
{
|
|
for (NIMMessage * message in messages) {
|
|
if (message.messageType == NIMMessageTypeCustom) {
|
|
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
|
|
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
|
|
AttachmentModel * attachment = (AttachmentModel *)obj.attachment;
|
|
if (attachment.first == CustomMessageType_RoomBoom) {
|
|
[self receiveNIMResponse:attachment];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)onReceiveBroadcastMessage:(NIMBroadcastMessage *)broadcastMessage {
|
|
if ([AccountInfoStorage instance].getUid.length == 0) {
|
|
return;
|
|
}
|
|
|
|
if (broadcastMessage.content) {
|
|
NSDictionary *msgDictionary = [broadcastMessage.content toJSONObject];
|
|
AttachmentModel *attachment = [AttachmentModel modelWithJSON:msgDictionary[@"body"]];
|
|
NSString *partitionId = [NSString stringWithFormat:@"%@",attachment.data[@"partitionId"]];
|
|
if(![partitionId isEqualToString:self.userInfo.partitionId]){
|
|
return;
|
|
}
|
|
if (attachment.first == CustomMessageType_RoomBoom) {
|
|
[self receiveNIMResponse:attachment];
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|