Files
peko-ios/YuMi/Modules/YMRoom/View/MenuContainerView/MSRoomMenuGameVC.m

421 lines
16 KiB
Objective-C

//
// MSRoomMenuGameVC.m
// YuMi
//
// Created by duoban on 2024/4/29.
//
#import "Api+Room.h"
#import "MSRoomMenuGameVC.h"
#import "MSRoomMenuGameView.h"
#import "XPRoomHalfWebView.h"
#import "XPWebViewController.h"
#import "XPCandyTreeViewController.h"
#import "XPSailingViewController.h"
#import "XPRoomViewController.h"
#import "MSRoomGameWebVC.h"
#import "RoomAnimationView.h"
#import "XPRoomMoreItemModel.h"
#import "GuildSuperAdminInfoModel.h"
#import "XPRoomPKViewController.h"
#import "XPAcrossRoomPKViewController.h"
#import "LuckyPackageViewController.h"
#import "XCCurrentVCStackManager.h"
#import <NIMSDK/NIMSDK.h>
#import "Api+LittleGame.h"
#import "Api+RoomSetting.h"
#import "UserInfoModel.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface MSRoomMenuGameVC ()<MSRoomMenuGameViewDelegate>
@property(nonatomic,strong) MSRoomMenuGameView *gameView;
@property(nonatomic, assign) MSRoomMenuType type;
///host 代理
@property (nonatomic,weak) id<RoomHostDelegate>hostDelegate;
@property (nonatomic, assign) CGPoint startTouchPoint;
@end
@implementation MSRoomMenuGameVC
-(void)dealloc{
}
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate{
self = [super init];
if(self){
self.hostDelegate = delegate;
}
return self;
}
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate
roomMenuType:(MSRoomMenuType)type {
self = [super init];
if(self){
self.type = type;
self.hostDelegate = delegate;
}
return self;
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self installUI];
[self installConstraints];
[self loadMenuData];
[self addTargetViewSwipeDownGesture];
}
- (void)addTargetViewSwipeDownGesture {
// 创建下滑手势识别器
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
// 将手势添加到目标视图
[self.gameView addGestureRecognizer:pan];
}
- (void)handlePan:(UIPanGestureRecognizer *)gesture {
// 获取触摸点
CGPoint touchPoint = [gesture locationInView:self.gameView];
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
// 记录起始触摸点
self.startTouchPoint = touchPoint;
break;
case UIGestureRecognizerStateChanged: {
// 计算滑动距离
CGFloat distance = touchPoint.y - self.startTouchPoint.y;
// 判断滑动方向是否向下
if (distance > 0) {
// 判断滑动距离是否超过阈值
if (distance >= 150.0) {
[self handleSwipeDown];
}
}
}
break;
case UIGestureRecognizerStateEnded:
// 重置起始触摸点
self.startTouchPoint = CGPointZero;
break;
default:
break;
}
}
- (void)handleSwipeDown {
// 处理下滑手势
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)loadMenuData {
switch (self.type) {
case MSRoomMenuTypePK:
[self updateGamePlayList:@[]];
break;
case MSRoomMenuTypeGame:
case MSRoomMenuTypeLittleGame:{
[self getGameData];
[self getLittleGameList];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self updateGameModels];
});
}
break;
default:
break;
}
}
- (void)updateGameModels {
RACSubject* playRAC = [RACSubject subject];
RACSubject* littleGameRAC = [RACSubject subject];
@kWeakify(self);
[[RACSignal combineLatest:@[playRAC,
littleGameRAC]
reduce:^id(NSArray<ActivityInfoModel*> *playModels,
NSArray<LittleGameInfoModel*> *littleGameModels){
@kStrongify(self);
[self.gameView updateDataSource:playModels gameList:littleGameModels];
if (self.updatePlayListAndGameList) {
self.updatePlayListAndGameList(playModels, littleGameModels);
}
return nil;
}] subscribeError:^(NSError * _Nullable error) {
[XNDJTDDLoadingTool showErrorWithMessage:error.domain];
}];
RoomInfoModel *roomInfo = self.hostDelegate.getRoomInfo;
NSString * roomId = [NSString stringWithFormat:@"%ld", roomInfo.uid];
[Api getPlayList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
NSArray <ActivityInfoModel *>* array = [ActivityInfoModel modelsWithArray:data.data];
[playRAC sendNext:array];
[playRAC sendCompleted];
} else {
[playRAC sendError:[NSError errorWithDomain:[NSString isEmpty:msg] ? @"" : msg code:code userInfo:nil]];
}
} roomId:roomId];
NSString *roomUid = [NSString stringWithFormat:@"%ld",
self.hostDelegate.getRoomInfo.uid];
[Api getLittleGameList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
NSArray * array = [LittleGameInfoModel modelsWithArray:data.data];
[littleGameRAC sendNext:array];
[littleGameRAC sendCompleted];
} else {
[littleGameRAC sendError:[NSError errorWithDomain:[NSString isEmpty:msg] ? @"" : msg code:code userInfo:nil]];
}
} roomUid:roomUid];
}
-(void)getGameData{
NSMutableArray *resourceList = [self.hostDelegate getPlayList];
if(resourceList.count > 0){
self.gameView.playList = resourceList;
}
}
- (void)updateGamePlayList:(NSArray *)array {
RoomInfoModel *roomInfo = self.hostDelegate.getRoomInfo;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:@[
[self createRoomPKModel:roomInfo],
[self createCrossRoomPKModel:roomInfo]
]];
self.gameView.playList = tempArray;
}
- (XPRoomMoreItemModel *)createRoomPKModel:(RoomInfoModel *)roomInfo {
XPRoomMoreItemModel * roomPK = [[XPRoomMoreItemModel alloc] init];
roomPK.imageName= @"room_pk_menu_icon";
roomPK.type = (roomInfo.roomModeType == RoomModeType_Open_PK_Mode) ?
RoomMoreMenuType_Room_PK_Close :
RoomMoreMenuType_Room_PK_Open;
return roomPK;
}
- (XPRoomMoreItemModel *)createCrossRoomPKModel:(RoomInfoModel *)roomInfo {
XPRoomMoreItemModel * acrossRoomPK = [[XPRoomMoreItemModel alloc] init];
acrossRoomPK.imageName = @"crossroom_pk_menu_icon";
acrossRoomPK.type = (roomInfo.roomModeType == RoomModeType_Open_AcrossRoomPK_mode) ?
RoomMoreMenuType_Room_Across_PK_Close :
RoomMoreMenuType_Room_Across_PK_Open;
return acrossRoomPK;
}
- (void)getLittleGameList {
NSMutableArray *resourceList = [self.hostDelegate getLittleGameList];
if(resourceList.count > 0){
self.gameView.littleGameList = resourceList;
}
}
-(void)installUI{
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.gameView];
}
-(void)installConstraints{
[self.gameView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
#pragma mark- MSRoomMenuGameViewDelegate
- (void)didClickBackBtnAction{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)ms_didSelectLittleGameItemAtIndexPath:(LittleGameInfoModel *)model {
if (self.hostDelegate.isManagerOrOwner) {
@kWeakify(self);
[TTPopup alertWithMessage:YMLocalizedString(@"MSRoomMenuGameVC0") confirmHandler:^{
@kStrongify(self);
[self startLittleGameWith:model];
} cancelHandler:^{}];
} else {
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"MSRoomMenuGameVC1")];
}
}
- (void)startLittleGameWith:(LittleGameInfoModel *)model {
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
NSMutableDictionary * params = [NSMutableDictionary dictionary];
NSString * ticket = [AccountInfoStorage instance].getTicket;
[params setObject:ticket forKey:@"ticket"];
[params setObject:@(roomInfo.uid).stringValue forKey:@"uid"];
[params setObject:[NSString stringWithFormat:@"%ld", roomInfo.uid] forKey:@"roomUid"];
if (roomInfo.title.length > 0) {
[params setObject:roomInfo.title forKey:@"title"];
}
if (roomInfo.roomPwd.length > 0) {
[params setObject:roomInfo.roomPwd forKey:@"roomPwd"];
} else{
[params setObject:@"" forKey:@"roomPwd"];
}
if (roomInfo.tagId > 0) {
[params setObject:[NSString stringWithFormat:@"%ld", roomInfo.tagId] forKey:@"tagId"];
}
if ([model.mgId isEqualToString:@"0"]) {
[params setObject:@(RoomType_Room) forKey:@"type"];
[params setObject:@"0" forKey:@"mgId"];
} else {
[params setObject:@(RoomType_MiniGame) forKey:@"type"];
[params setObject:model.mgId forKey:@"mgId"];
}
[params setObject:@(roomInfo.hasAnimationEffect) forKey:@"hasAnimationEffect"];
@kWeakify(self);
[Api ownerUpdateRoomInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
[self dismissViewControllerAnimated:YES completion:nil];
if (code == 200) {
} else {
[XNDJTDDLoadingTool showErrorWithMessage:msg];
}
} params:params];
}
- (void)handleMoreItem:(XPRoomMoreItemModel *)model {
RoomInfoModel *roomInfo = self.hostDelegate.getRoomInfo;
switch (model.type) {
case RoomMoreMenuType_Room_PK_Close: {
XPRoomPKViewController * roomPKVC = [[XPRoomPKViewController alloc] initWithDelegate:self.hostDelegate];
[self.hostDelegate.getCurrentNav pushViewController:roomPKVC animated:YES];
}
break;
case RoomMoreMenuType_Room_PK_Open: {
if (roomInfo.roomModeType == RoomModeType_Open_Blind) {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController20")];
} else if (roomInfo.roomModeType == RoomModeType_Open_AcrossRoomPK_mode) {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController21")];
} else {
XPRoomPKViewController * roomPKVC = [[XPRoomPKViewController alloc] initWithDelegate:self.hostDelegate];
[self.hostDelegate.getCurrentNav pushViewController:roomPKVC animated:YES];
}
}
break;
case RoomMoreMenuType_Room_Across_PK_Close: {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController15")];
}
break;
case RoomMoreMenuType_Room_Across_PK_Open:{
if (roomInfo.roomModeType == RoomModeType_Open_Blind) {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController13")];
} else if (roomInfo.roomModeType == RoomModeType_Open_PK_Mode) {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController14")];
} else {
XPAcrossRoomPKViewController *acrossRoomPKVC = [[XPAcrossRoomPKViewController alloc] initWithRoomUid:[NSString stringWithFormat:@"%ld", roomInfo.uid]];
[self.hostDelegate.getCurrentNav presentViewController:acrossRoomPKVC animated:YES completion:nil];
}
}
break;
default:
break;
}
}
- (void)handleActivityInfo:(ActivityInfoModel *)model {
if ([model.code isEqualToString:@"JOYPLAY"]) {
[self clickGameVC:model];
} else if ([model.code isEqualToString:@"LUCKY_BAG"]) {
if ([[self.hostDelegate getUserInfo] isRechargeUser]) {
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"1.0.37_text_52")];
return;
}
LuckyPackageViewController *giftVC = [[LuckyPackageViewController alloc] initWithdelegate:self.hostDelegate];
[[XCCurrentVCStackManager shareManager].getCurrentVC addChildViewController:giftVC];
[[XCCurrentVCStackManager shareManager].getCurrentVC.view addSubview:giftVC.view];
} else if ([model.code isEqualToString:@"FIND_LOVE"]) {
[self lookLoveTapRecognizer];
} else if([model.code isEqualToString:@"NAUTICAL_ADVENTURE"]) {
[self sailTapRecognizer];
}else if([model.code isEqualToString:@"BAISHUN"]){
[self clickGameVC:model];
} else if ([model.code isEqualToString:@"LEADERCC"]) {
[self clickGameVC:model];
} else if(model.skipType == ActivitySkipType_Web) {
if(model.showType == ActivityShowType_Half){
XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init];
webView.isPlayView = YES;
webView.roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
webView.url = model.skipContent;
[TTPopup popupView:webView style:TTPopupStyleActionSheet];
return;
}
XPWebViewController * webVC = [[XPWebViewController alloc] initWithRoomUID:@(self.hostDelegate.getRoomInfo.uid).stringValue];
webVC.url = model.skipContent;
[self.hostDelegate.getCurrentNav pushViewController:webVC animated:YES];
}
}
-(void)ms_didSelectItemAtIndexPath:(id)model {
self.gameView.hidden = YES;
[self dismissViewControllerAnimated:YES completion:^{
if ([model isKindOfClass:[XPRoomMoreItemModel class]]) {
[self handleMoreItem:(XPRoomMoreItemModel *)model];
} else if ([model isKindOfClass:[ActivityInfoModel class]]) {
[self handleActivityInfo:(ActivityInfoModel *)model];
}
}];
}
-(void)clickGameVC:(ActivityInfoModel *)model{
if ([self.hostDelegate isKindOfClass:[XPRoomViewController class]]){
MSRoomGameWebVC *vc = [[MSRoomGameWebVC alloc] initWithDelegate:self.hostDelegate gameModel:model];
vc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
XPRoomViewController *roomVC = (XPRoomViewController *)self.hostDelegate;
[roomVC addChildViewController:vc];
RoomAnimationView *animationView;
for (id obj in self.hostDelegate.getSuperView.subviews) {
if ([obj isKindOfClass:[RoomAnimationView class]]){
animationView = obj;
break;
}
}
[self.hostDelegate.getSuperView addSubview:vc.view];
vc.view.tag = 913;
}
}
- (void)lookLoveTapRecognizer {
if ([self.hostDelegate isKindOfClass:[XPRoomViewController class]]){
XPCandyTreeViewController * candyTreeVC = [[XPCandyTreeViewController alloc] initWithDelegate:self.hostDelegate];
candyTreeVC.view.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
[self.hostDelegate.getSuperView addSubview:candyTreeVC.view];
XPRoomViewController *vc = (XPRoomViewController *)self.hostDelegate;
[vc addChildViewController:candyTreeVC];
[candyTreeVC.navigationController setNavigationBarHidden:YES animated:NO];
[UIView animateWithDuration:0.1 animations:^{
candyTreeVC.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
}completion:^(BOOL finished) {
}];
}
}
- (void)sailTapRecognizer {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
XPSailingViewController * sailingVC = [[XPSailingViewController alloc] initWithRoomUid:roomUid];
[self.hostDelegate.getCurrentNav presentViewController:sailingVC animated:YES completion:nil];
}
#pragma mark - 懒加载
- (MSRoomMenuGameView *)gameView{
if(!_gameView){
_gameView = [[MSRoomMenuGameView alloc]initWithFrame:CGRectZero];
_gameView.delegate = self;
}
return _gameView;
}
@end