首次提交

This commit is contained in:
启星
2025-08-08 11:05:33 +08:00
parent 1b3bb91b4a
commit adc1a2a25d
8803 changed files with 708874 additions and 0 deletions

View File

@@ -0,0 +1,465 @@
//
// Created by kaniel on 2022/5/26.
// Copyright (c) 2022 Sud.Tech (https://sud.tech). All rights reserved.
//
#import "RCMicRoomViewController+Game.h"
#import <objc/runtime.h>
#import <SudMGP/ISudCfg.h>
@implementation SudMGPLoadConfigModel
@end
#pragma mark =======RCMicRoomViewController (Game)=======
@implementation RCMicRoomViewController (Game)
#pragma mark ======= =======
/// SudMGPWrapper
- (void)createSudMGPWrapper {
//
[[SudMGP getCfg] setBackgroundMode:NO];
// SudMGPWrapper
self.sudFSTAPPDecorator = [[SudFSTAPPDecorator alloc] init];
self.sudFSMMGDecorator = [[SudFSMMGDecorator alloc] init];
//
[self.sudFSMMGDecorator setEventListener:self];
}
///
/// getCode: code
/// sud-mgp-doc(https://github.com/SudTechnology/sud-mgp-doc)
///
/// 1. SDKcode<getCode>
/// 2. SudMGP SDK<SudMGP initSDK>
/// 3. SudMGP SDK<SudMGP loadMG>
- (void)loginGame:(SudMGPLoadConfigModel *)configModel {
NSString *appID = SUDMGP_APP_ID;
NSString *appKey = SUDMGP_APP_KEY;
if (appID.length == 0 || appKey.length == 0) {
NSLog(@"Game appID or appKey is empty");
return;
}
__weak typeof(self) weakSelf = self;
// 1. SDKcode<reqGameLoginWithSuccess>
[self getCode:configModel.userId success:^(NSString *code, NSError *error, int retCode) {
[weakSelf initSudMGPSDK:configModel code:code];
} fail:^(NSError *error) {
NSLog(@"getCode err:%@", error.debugDescription);
}];
}
/// 退 SudMGP SDK
- (void)logoutGame {
//
[self.sudFSMMGDecorator clearAllStates];
[self.sudFSTAPPDecorator destroyMG];
}
/// code
/// @param success
/// @param fail
- (void)getCode:(NSString *)userId success:(void (^)(NSString *code, NSError *error, int retCode))success fail:(void(^)(NSError *error))fail {
if (userId.length == 0) {
if (fail) {
fail([NSError errorWithDomain:nil code:-1 userInfo:@{NSDebugDescriptionErrorKey: @"参数错误"}]);
}
NSLog(@"用户ID不能为空");
return;
}
NSDictionary *dicParam = @{@"uid": userId, @"login_token":GVUSER.token};
[self postHttpRequestWithURL:[NSString stringWithFormat:@"%@%@", VERSION_HTTPS_SERVER, GAME_LOGIN_URL] param:dicParam success:^(NSDictionary *rootDict) {
if ([rootDict safeIntForKey:@"code"] == 200) {
NSDictionary *dic = [rootDict objectForKey:@"data"];
/// codesdk
NSString *code = [dic objectForKey:@"code"];
int retCode = (int) [[dic objectForKey:@"ret_code"] longValue];
success(code, nil, retCode);
}else {
[MBManager showBriefAlert:[rootDict safeStringForKey:@"msg"]];
}
} failure:^(NSError *error) {
NSLog(@"login game server error:%@", error.debugDescription);
if (fail) {
fail(error);
}
}];
}
/// SudMDP SDK
- (void)initSudMGPSDK:(SudMGPLoadConfigModel *)configModel code:(NSString *)code {
__weak typeof(self) weakSelf = self;
// SudMGP initSDKSudMGP
[self logoutGame];
if (configModel.gameId <= 0) {
NSLog(@"游戏ID为空无法加载游戏:%@, currentRoomID:%@", @(configModel.gameId), configModel.roomId);
return;
}
// 2. SudMGP SDK<SudMGP initSDK>
[SudMGP initSDK:SUDMGP_APP_ID
appKey:SUDMGP_APP_KEY
isTestEnv:GAME_TEST_ENV
listener:^(int retCode, const NSString *retMsg) {
if (retCode != 0) {
/// ,
NSLog(@"ISudFSMMG:initGameSDKWithAppID:初始化sdk失败 :%@(%@)", retMsg, @(retCode));
return;
}
NSLog(@"ISudFSMMG:initGameSDKWithAppID:初始化游戏SDK成功");
// SDK
[weakSelf loadGame:configModel code:code];
}];
}
/// MG
/// @param configModel model
- (void)loadGame:(SudMGPLoadConfigModel *)configModel code:(NSString *)code {
NSLog(@"loadMG:userId:%@, gameRoomId:%@, gameId:%@", configModel.userId, configModel.roomId, @(configModel.gameId));
if (configModel.userId.length == 0 ||
configModel.roomId.length == 0 ||
code.length == 0 ||
configModel.language.length == 0 ||
configModel.gameId <= 0) {
NSLog(@"loadGame: 游戏加载参数存在异常空值请检查参数loadMG传入参数");
return;
}
//
[self.sudFSMMGDecorator setCurrentUserId:configModel.userId];
// 3. SudMGP SDK<SudMGP loadMG>iSudFSTAPP
id <ISudFSTAPP> iSudFSTAPP = [SudMGP loadMG:configModel.userId
roomId:configModel.roomId
code:code
mgId:configModel.gameId
language:configModel.language
fsmMG:self.sudFSMMGDecorator
rootView:configModel.gameView];
[self.sudFSTAPPDecorator setISudFSTAPP:iSudFSTAPP];
}
#pragma mark =======SudFSMMGListener SDK=======
#pragma mark
/// Config
- (void)onGetGameCfg:(nonnull id <ISudFSMStateHandle>)handle dataJson:(nonnull NSString *)dataJson {
// TODO:
//
GameCfgModel *m = [GameCfgModel defaultCfgModel];
[handle success:m.toJSON];
}
/// View
- (void)onGetGameViewInfo:(nonnull id <ISudFSMStateHandle>)handle dataJson:(nonnull NSString *)dataJson {
// TODO:
// px x px
CGFloat scale = [[UIScreen mainScreen] nativeScale];
//
CGSize screenSize = [UIScreen mainScreen].bounds.size;
//
UIEdgeInsets safeArea = [self safeAreaInsets];
//
CGFloat statusBarHeight = safeArea.top == 0 ? 20 : safeArea.top;
GameViewInfoModel *m = [[GameViewInfoModel alloc] init];
//
m.view_size.width = screenSize.width * scale;
m.view_size.height = screenSize.height * scale;
//
//
m.view_game_rect.top = (statusBarHeight + 80) * scale + 250;
//
m.view_game_rect.left = 0;
//
m.view_game_rect.right = 0;
//
m.view_game_rect.bottom = (safeArea.bottom + 100) * scale + 200;
m.ret_code = 0;
m.ret_msg = @"success";
[handle success:m.toJSON];
}
#pragma mark
///
- (void)onGameStarted {
///
NSLog(@"游戏加载完毕");
}
///
- (void)onGameDestroyed {
NSLog(@"游戏已销毁");
}
/// code
- (void)onExpireCode:(nonnull id <ISudFSMStateHandle>)handle dataJson:(nonnull NSString *)dataJson {
// TODO: codecode
// Code
[self getCode:self.sudFSMMGDecorator.currentUserId success:^(NSString *code, NSError *error, int retCode) {
//
[self.sudFSTAPPDecorator updateCode:code];
//
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
} fail:^(NSError *error) {
NSLog(@"getCode err:%@", error.debugDescription);
//
[handle failure:[self.sudFSMMGDecorator handleMGFailure]];
}];
}
#pragma mark
/// : MG_COMMON_SELF_CLICK_READY_BTN
- (void)onGameMGCommonSelfClickReadyBtn:(nonnull id <ISudFSMStateHandle>)handle model:(MGCommonSelfClickReadyBtn *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : MG_COMMON_SELF_CLICK_GAME_SETTLE_AGAIN_BTN
- (void)onGameMGCommonSelfClickGameSettleAgainBtn:(nonnull id <ISudFSMStateHandle>)handle model:(MGCommonSelfClickGameSettleAgainBtn *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : MG_COMMON_SELF_CLICK_START_BTN
- (void)onGameMGCommonSelfClickStartBtn:(nonnull id <ISudFSMStateHandle>)handle model:(MGCommonSelfClickStartBtn *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// -
/// : MG_COMMON_PUBLIC_MESSAGE
- (void)onGameMGCommonPublicMessage:(id <ISudFSMStateHandle>)handle model:(MGCommonPublicMessageModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
[self onSendGameMsg:model];
}
- (void)onSendGameMsg:(MGCommonPublicMessageModel *)model {
if ([self onGetIsCaptain] == NO) {
return;
}
NSMutableString *mStr = [[NSMutableString alloc] init];
for (GamePublicMsg *m in model.msg) {
if (m.text.zh_CN && m.text.zh_CN.length > 0) {
[mStr appendString:m.text.zh_CN];
}
if (m.user.name && m.user.name.length > 0) {
[mStr appendString:m.user.name];
}
}
if (mStr.length > 0) {
[self onUpdateGameMsg:[mStr copy]];
NSLog(@"打印游戏信息-%@", mStr);
}
}
/// : MG_COMMON_KEY_WORD_TO_HIT
- (void)onGameMGCommonKeyWordToHit:(id <ISudFSMStateHandle>)handle model:(MGCommonKeyWrodToHitModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
///
/// : MG_COMMON_GAME_STATE
- (void)onGameMGCommonGameState:(id <ISudFSMStateHandle>)handle model:(MGCommonGameState *)model {
if (model.gameState != 2) {
self.gameBanMic = self.gameBanSpeaker = NO;
}
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : ASR( MG_COMMON_GAME_ASR
- (void)onGameMGCommonGameASR:(id <ISudFSMStateHandle>)handle model:(MGCommonGameASRModel *)model {
/// ||
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
///
/// : MG_COMMON_PLAYER_IN
- (void)onPlayerMGCommonPlayerIn:(id <ISudFSMStateHandle>)handle userId:(NSString *)userId model:(MGCommonPlayerInModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
[self onSetDefaultCaptain];
}
/// : MG_COMMON_PLAYER_READY
- (void)onPlayerMGCommonPlayerReady:(id <ISudFSMStateHandle>)handle userId:(NSString *)userId model:(MGCommonPlayerReadyModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : MG_COMMON_PLAYER_CAPTAIN
- (void)onPlayerMGCommonPlayerCaptain:(id <ISudFSMStateHandle>)handle userId:(NSString *)userId model:(MGCommonPlayerCaptainModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : MG_COMMON_PLAYER_PLAYING
- (void)onPlayerMGCommonPlayerPlaying:(id <ISudFSMStateHandle>)handle userId:(NSString *)userId model:(MGCommonPlayerPlayingModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
/// : MG_DG_PAINTING
- (void)onPlayerMGDGPainting:(nonnull id <ISudFSMStateHandle>)handle userId:(nonnull NSString *)userId model:(MGDGPaintingModel *)model {
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
///
/// : MG_COMMON_GAME_SELF_MICROPHONE
- (void)onGameMGCommonGameSelfMicrophone:(nonnull id <ISudFSMStateHandle>)handle model:(MGCommonGameSelfMicrophone *)model {
if (self.sudFSMMGDecorator.gameStateType == 2) {
//RTC
self.gameBanMic = !model.isOn;
[self onChangeGameMic:model.isOn];
[MBManager showBriefAlert:model.isOn?@"您可以发言了":@"您暂时不能发言"];
}
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
///
/// : MG_COMMON_GAME_SELF_HEADEPHONE
- (void)onGameMGCommonGameSelfHeadphone:(nonnull id <ISudFSMStateHandle>)handle model:(MGCommonGameSelfHeadphone *)model {
if (self.sudFSMMGDecorator.gameStateType == 2) {
//RTC
self.gameBanSpeaker = !model.isOn;
[self onChangeGameMic:model.isOn];
[MBManager showBriefAlert:model.isOn?@"声音已打开":@"声音已关闭"];
}
[handle success:[self.sudFSMMGDecorator handleMGSuccess]];
}
#pragma mark ======= private =======
- (void)setSudFSMMGDecorator:(SudFSMMGDecorator *)sudFSMMGDecorator {
objc_setAssociatedObject(self, @selector(sudFSMMGDecorator), sudFSMMGDecorator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (SudFSMMGDecorator *)sudFSMMGDecorator {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setSudFSTAPPDecorator:(SudFSTAPPDecorator *)sudFSTAPPDecorator {
objc_setAssociatedObject(self, @selector(sudFSTAPPDecorator), sudFSTAPPDecorator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (SudFSTAPPDecorator *)sudFSTAPPDecorator {
return objc_getAssociatedObject(self, _cmd);
}
///
- (void)postHttpRequestWithURL:(NSString *)api
param:(NSDictionary *)param
success:(void (^)(NSDictionary *_Nonnull))success
failure:(void (^)(id _Nonnull))failure {
//
NSURL *url = [NSURL URLWithString:api];
//
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//
request.HTTPMethod = @"POST";
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//
if (param) {
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONReadingMutableContainers error:nil];
request.HTTPBody = bodyData;
}
//session
NSURLSession *session = [NSURLSession sharedSession];
//
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
if (failure) {
failure(error);
}
return;
}
NSError *error;
NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error) {
if (failure) {
failure(error);
}
return;
}
if (success) {
success(responseObject);
}
});
}];
//
[dataTask resume];
}
///
-(UIEdgeInsets)safeAreaInsets {
if (@available(iOS 11.0, *)) {
return [[[UIApplication sharedApplication] keyWindow] safeAreaInsets];
} else {
// Fallback on earlier versions
}
return UIEdgeInsetsZero;
}
#pragma mark ======= =======
///
- (void)handleGameKeywordHitting:(NSString *)content {
if (self.sudFSMMGDecorator.isHitBomb) {
if ([self isPureInt:content]) {
///
[self.sudFSTAPPDecorator notifyAppComonDrawTextHit:false keyWord:@"" text:content];
}
return;
}
if (self.sudFSMMGDecorator.keyWordHiting == YES && [content isEqualToString:self.sudFSMMGDecorator.drawKeyWord]) {
///
[self.sudFSTAPPDecorator notifyAppComonDrawTextHit:true keyWord:self.sudFSMMGDecorator.drawKeyWord text:self.sudFSMMGDecorator.drawKeyWord];
}
}
- (BOOL)isPureInt:(NSString *)string {
NSScanner *scan = [NSScanner scannerWithString:string];
int val;
return [scan scanInt:&val] && [scan isAtEnd];
}
//
- (BOOL)onGetIsCaptain {
return [self.sudFSMMGDecorator isPlayerIsCaptain:BJUserManager.userInfo.uid];
}
//
- (void)onSetDefaultCaptain {
if ([self onGetIsRoomOwner]) {
[self.sudFSTAPPDecorator notifyAppComonSetCaptainStateWithUserId:BJUserManager.userInfo.uid];
}
}
@end