Files
peko-ios/YuMi/Modules/YMRoom/View/RoomGame/View/MSRoomGameVC.m
2024-06-04 18:10:44 +08:00

369 lines
15 KiB
Objective-C

//
// MSRoomGameVC.m
// YuMi
//
// Created by duoban on 2024/5/27.
//
#import "MSRoomGameVC.h"
#import "MSRoomGameHeadView.h"
#import "MsRoomMessagChatHallView.h"
#import "MSRoomGameMsgView.h"
#import "MSRoomGameSendMsgView.h"
#import <AFNetworkReachabilityManager.h>
#import "MSRoomGameVictoryView.h"
#import "MSRoomGameQuitGameView.h"
#import "MSRoomGamePresenter.h"
#import "MSRoomGameProtocol.h"
#import <NIMSDK/NIMSDK.h>
#import "AttachmentModel.h"
#import "MSRoomGameSendTextView.h"
#import "MSRoomGameView.h"
#import "MSRoomGameResultsModel.h"
#import "Api.h"
#import "XPIAPRechargeViewController.h"
@interface MSRoomGameVC ()<MSRoomGameHeadViewDelegate,MSRoomGameQuitGameViewDelegate,MSRoomGameVictoryViewDelegate,NIMChatroomManagerDelegate, NIMChatManagerDelegate, NIMConversationManagerDelegate, NIMLoginManagerDelegate,MSRoomGameSendMsgViewDelegate,MSRoomGameViewDelegate,XPIAPRechargeViewControllerDelegate>
@property(nonatomic,strong) UIImageView *bgImageView;
@property(nonatomic,strong) MSRoomGameHeadView *headView;
@property(nonatomic,strong) MsRoomMessagChatHallView *chatView;
@property(nonatomic,strong) MSRoomGameMsgView *msgView;
@property(nonatomic,copy) NSString *roomId;
@property(nonatomic,strong) MSRoomGameSendMsgView *sendMsgView;
///小游戏的容器
@property(nonatomic,strong) MSRoomGameView *roomGameView;
@property(nonatomic,strong) WalletInfoModel *coinModel;
@property(nonatomic,assign) BOOL isShowResults;
@end
@implementation MSRoomGameVC
- (MSRoomGamePresenter *)createPresenter {
return [[MSRoomGamePresenter alloc] init];
}
-(void)dealloc{
[[NIMSDK sharedSDK].chatroomManager removeDelegate:self];
[[NIMSDK sharedSDK].chatManager removeDelegate:self];
[[NIMSDK sharedSDK].loginManager removeDelegate:self];
[[NIMSDK sharedSDK].conversationManager removeDelegate:self];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self installUI];
[self installConstraints];
[self.presenter getRoomGameDetailWithRoomType:@"0"];
[[NIMSDK sharedSDK].chatroomManager addDelegate:self];
[[NIMSDK sharedSDK].chatManager addDelegate:self];
[[NIMSDK sharedSDK].loginManager addDelegate:self];
[[NIMSDK sharedSDK].conversationManager addDelegate:self];
[self getCoinNum];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appDidBecomeActive) name:@"kAppDidBecomeActive" object:nil];
[self networkReachability];
}
-(void)getCoinNum{
NSString * uid = [AccountInfoStorage instance].getUid;
NSString * ticket = [AccountInfoStorage instance].getTicket;
[Api getUserWalletInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
WalletInfoModel * model = [WalletInfoModel modelWithDictionary:data.data];
self.coinModel = model;
} uid:uid ticket:ticket];
}
-(void)installUI{
[self.view addSubview:self.bgImageView];
[self.view addSubview:self.headView];
[self.view addSubview:self.msgView];
[self.view addSubview:self.sendMsgView];
}
-(void)installConstraints{
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(22));
make.leading.trailing.equalTo(self.view).inset(kGetScaleWidth(0));
make.height.mas_equalTo(kGetScaleWidth(236));
}];
[self.sendMsgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.bottom.equalTo(self.view);
make.height.mas_equalTo(kGetScaleWidth(70));
}];
[self.msgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self.view);
make.height.mas_equalTo(kGetScaleWidth(101));
make.bottom.equalTo(self.sendMsgView.mas_top).mas_offset(-kGetScaleWidth(10));
}];
}
- (BOOL)isHiddenNavBar {
return YES;
}
-(void)appDidBecomeActive{
if(self.headView.roomGameModel.data.roundStatus == 0 && self.headView.roomGameModel.data.matchStatus == 1){
[self.presenter getRoomGameDetailWithRoomType:@"0"];
}
}
///网络监听
-(void)networkReachability{
@kWeakify(self);
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
@kStrongify(self);
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable:
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
{
if(self.headView.roomGameModel.data.roundStatus == 0 && self.headView.roomGameModel.data.matchStatus == 1){
[self.presenter getRoomGameDetailWithRoomType:@"0"];
}
}
break;
default:
break;
}
}];
}
#pragma mark -MSRoomGameViewDelegate
- (void)getGameRsultsWithList:(NSArray *)list{
if(self.isShowResults == YES)return;
self.isShowResults = YES;
MSRoomGameVictoryView *victoryView = [[MSRoomGameVictoryView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
for (MSRoomGameResultsModel *obj in list) {
if([obj.uid isEqualToString:self.headView.mineModel.uid]){
obj.avatar = self.headView.mineModel.avatar;
obj.nick = self.headView.mineModel.nick;
}
if([obj.uid isEqualToString:self.headView.rivalModel.uid]){
obj.avatar = self.headView.rivalModel.avatar;
obj.nick = self.headView.rivalModel.nick;
}
obj.winNum = obj.rank == 1 ? self.chooseGameModel.scores.firstObject : self.chooseGameModel.scores.lastObject;
}
victoryView.delegate = self;
victoryView.resultsList = list;
[TTPopup popupView:victoryView style:TTPopupStyleAlert];
[self.presenter exitNIMRoom:self.roomId];
[self.roomGameView handleSelfInExitEvent];
[self.roomGameView destroyMG];
self.roomGameView = nil;
[self.headView setDataInNull];
}
#pragma mark- MSRoomGameSendMsgViewDelegate
- (void)clickSendMsgAction{
MSRoomGameSendTextView *sendTextView = [[MSRoomGameSendTextView alloc]initWithRoomId:self.roomId userInfo:self.userinfo];
[self.view addSubview:sendTextView];
}
#pragma mark - MSRoomGameProtocol
-(void)getRoomGameDetailsSuccessWithModel:(MSRoomGameModel *)model{
self.headView.roomGameModel = model;
self.headView.userInfo = self.userinfo;
self.roomId = model.roomId;
[self.presenter enterNIMRoom:model.roomId user:self.userinfo];
if(model.data.matchStatus == MSRoomGameInfoModelMatchSuccessfully && model.data.roundStatus == 0){
self.roomGameView.gameModel = model;
if(self.roomGameView.superview == nil){
[self.bgImageView addSubview:self.roomGameView];
[self.roomGameView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.bgImageView);
}];
[self.roomGameView onRoomEntered];
}
}else{
[self.roomGameView handleSelfInExitEvent];
[self.roomGameView destroyMG];
self.roomGameView = nil;
[self.headView setDataInNull];
}
}
#pragma mark -MSRoomGameVictoryViewDelegate
- (void)closeGameAction{
self.isShowResults = NO;
[self quitGameActionWtihIsColse:YES];
}
-(void)rematchGameAction{
self.isShowResults = NO;
if(self.coinModel.diamonds.integerValue < self.chooseGameModel.ticket.integerValue){
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.message = YMLocalizedString(@"XPTreasureFairyViewController5");
config.confirmButtonConfig.title = YMLocalizedString(@"XPTreasureFairyViewController6");
[TTPopup alertWithConfig:config confirmHandler:^{
XPIAPRechargeViewController * rechargeVC = [[XPIAPRechargeViewController alloc] init];
rechargeVC.delegate = self;
[self.navigationController pushViewController:rechargeVC animated:YES];
} cancelHandler:^{
}];
return;
}
[self.presenter startMatchGameWithroomId:@"" mgId:self.headView.roomGameModel.data.mgId gameMode:self.chooseGameModel.gameMode];
}
-(void)startMatchGameSuccess{
[self.presenter getRoomGameDetailWithRoomType:@"0"];
}
#pragma mark -XPIAPRechargeViewControllerDelegate
-(void)paySuccess{
[self getCoinNum];
}
#pragma mark - MSRoomGameQuitGameViewDelegate
- (void)quitGameAction{
[self quitGameActionWtihIsColse:YES];
}
-(void)quitGameActionWtihIsColse:(BOOL)isColse{
self.isShowResults = YES;
if(isColse == YES){
[self.presenter closeRoomGameWithRoomId:self.roomId];
}
[self.presenter exitNIMRoom:self.roomId];
[self.roomGameView handleSelfInExitEvent];
[self.roomGameView destroyMG];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -MSRoomGameHeadViewDelegate
- (void)clickQuitGameAction{
MSRoomGameQuitGameView *quitGameView = [[MSRoomGameQuitGameView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
quitGameView.delegate = self;
[TTPopup popupView:quitGameView style:TTPopupStyleAlert];
}
#pragma mark - NIMChatManagerDelegate
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
for (NIMMessage * message in messages) {
// 非房间内消息不处理
if (message.session.sessionType != NIMSessionTypeChatroom) {
continue;
}
if(![message.session.sessionId isEqualToString:self.roomId]){
continue;
}
if (message.messageType == NIMMessageTypeNotification) {
NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject;
NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content;
switch (content.eventType) {
case NIMChatroomEventTypeEnter:
{
[self.msgView handleNIMNotificationMessage:message];
break;
};
default:
break;
}
}else if (message.messageType == NIMMessageTypeText) {
[self.msgView handleNIMTextMessage:message];
}else 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_Chat_Room_Game && attachment.second == Custom_Message_Sub_Room_Game_Match_Success){
MSRoomGameModel *model = [MSRoomGameModel modelWithJSON:attachment.data];
[self.presenter exitNIMRoom:self.roomId];
self.roomId = model.roomId;
self.headView.roomGameModel = model;
[self.presenter enterNIMRoom:model.roomId user:self.userinfo];
self.roomGameView.gameModel = model;
if(self.roomGameView.superview == nil){
[self.bgImageView addSubview:self.roomGameView];
[self.roomGameView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.bgImageView);
}];
[self.roomGameView onRoomEntered];
}
}if(attachment.first == CustomMessageType_Chat_Room_Game && attachment.second == Custom_Message_Sub_Room_Game_Match_Fail){
MSRoomGameQuitGameView *quitGameView = [[MSRoomGameQuitGameView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
quitGameView.delegate = self;
quitGameView.isNormalFail = YES;
[TTPopup popupView:quitGameView style:TTPopupStyleAlert];
}if(attachment.first == CustomMessageType_Chat_Room_Game && attachment.second == Custom_Message_Sub_Room_Game_Early_Exit){
if(self.isShowResults == YES)return;
self.isShowResults = YES;
NSArray *list = [MSRoomGameResultsModel modelsWithArray:attachment.data[@"results"]];
MSRoomGameVictoryView *victoryView = [[MSRoomGameVictoryView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
victoryView.delegate = self;
victoryView.resultsList = list;
[TTPopup popupView:victoryView style:TTPopupStyleAlert];
[self.presenter exitNIMRoom:self.roomId];
[self.roomGameView handleSelfInExitEvent];
[self.roomGameView destroyMG];
self.roomGameView = nil;
[self.headView setDataInNull];
}
}
}
}
}
#pragma mark - NIMBroadcastDelegate
//发送消息成功回调
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
// 非本房间不处理
if(![message.session.sessionId isEqualToString:self.roomId]){
return;;
}
if (message.messageType == NIMMessageTypeText) {
[self.msgView handleNIMTextMessage:message];
}
}
#pragma mark - 懒加载
- (UIImageView *)bgImageView{
if(!_bgImageView){
_bgImageView = [UIImageView new];
_bgImageView.image = kImage(@"ms_room_game_underwa_bg");
_bgImageView.userInteractionEnabled = YES;
}
return _bgImageView;
}
- (MSRoomGameHeadView *)headView{
if(!_headView){
_headView = [[MSRoomGameHeadView alloc]initWithFrame:CGRectZero];
_headView.delegate = self;
_headView.chooseGameModel = self.chooseGameModel;
}
return _headView;
}
- (MSRoomGameMsgView *)msgView{
if(!_msgView){
_msgView = [[MSRoomGameMsgView alloc]initWithFrame:CGRectZero];
}
return _msgView;
}
- (MSRoomGameSendMsgView *)sendMsgView{
if(!_sendMsgView){
_sendMsgView = [[MSRoomGameSendMsgView alloc]initWithFrame:CGRectZero];
_sendMsgView.delegate = self;
}
return _sendMsgView;
}
- (MSRoomGameView *)roomGameView{
if(!_roomGameView){
_roomGameView = [[MSRoomGameView alloc]initWithFrame:CGRectZero];
_roomGameView.delegate = self;
}
return _roomGameView;
}
@end