123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- //
- // IOSPlatformSDK.m
- // OYGameSKD
- //
- // Created by duowan123 on 2021/1/21.
- // Copyright © 2021 jiketeam. All rights reserved.
- #import "IOSPlatformSDK.h"
- #import "SearchDeviceViewController.h"
- #import "PopupView.h"
- #import "HTTPDataProcession.h"
- #import "UserFriendsModel.h"
- #import "BTDataProcess.h"
- #import "AlgorithmTool.h"
- #pragma mark ============================>> ios call unity C语言指针声明 start
- /**
- *运动数据
- * left: 左鞋动作代码
- * right: 右鞋动作代码
- * 对应 enum CMD_MOTION
- */
- typedef void (* MotionHandler) (int cusid, int left, int right);
- /**
- * 踏步状态,频率
- * leftStatus: 左鞋(0:停止, 1:慢,2:快)
- * rightStatus: 右鞋(0:停止, 1:慢,2:快)
- * leftFrag: 左鞋(每分钟步频)
- * rightFrag: 右鞋(每分钟步频)
- */
- typedef void (* StepHandler)(int cusid, int leftStatus, int rightStatus, int leftFrag, int rightFrag);
- /**
- * 返回蓝牙设备的基本信息,用于界面上显示当前的设备状况,app 跳转的时候传过来的
- * name: 设备名称
- * status: 设备状态 @see enum BLE_STATE
- * electricity: 设备电量 取值范围[0 - 100] 对比左右鞋 handler较低的电量
- */
- typedef void (* DeviceHandler)(int cusid, const char *name, const char *address, int status, int electricity);
- /**
- * 好友列表 unity call ios 异步请求数据后 ios call unity
- * 调用api GetUserFriends() 的回调
- * code: 请求结果代码 0: 成功, 非0: 根据业务处理
- * json: 好友列表数据 , json 数组
- *
- */
- typedef void (* UserFriendsHandler)(int code, const char * json);
- /**
- * 调用api GetUserFriends() 的回调
- * code: 请求结果代码 0: 成功, 非0: 根据业务处理
- * json: 好友列表数据 , json 数组
- */
- typedef void (* InviteFriendHandler)(int code, const char * userJson, const char * inviteInfo);//
- /**
- *游戏首页交互动作的回调
- * cusid: 主副设备
- * code: 交互代码
- */
- typedef void (* ActionHandler) (int cusid, int code);
- ActionHandler actionHandler;
- MotionHandler motionHandler;
- StepHandler stepHandler;
- DeviceHandler deviceHandler;
- UserFriendsHandler userFriendsHandler;
- InviteFriendHandler inviteFriendHandler;
- #pragma mark ============================>> ios call unity C语言指针声明 结束
- @interface IOSPlatformSDK ()
- //app跳转 用户信息
- #define IOSSDK_USERINFO @"iosSdk_userInfo"
- //缓存的步频数据
- #define IOSSDK_BLESDKDATA @"iosSdk_bleSdk_frequency"
- //custom System UserDefaults
- #define IOS_NSUSERDEFAULT [NSUserDefaults standardUserDefaults]
- #define CUS_NOTIFICATIONCENTER [NSNotificationCenter defaultCenter]
- //计算游戏启动时间
- @property(nonatomic,strong)NSDate * firstTime;
- //游戏持续时间
- @property(nonatomic,assign)NSTimeInterval durationTime;
- @end
- @implementation IOSPlatformSDK
- #pragma mark ============================>> SDK 单例
- static IOSPlatformSDK * instance;
- +(instancetype)sharedInstance{
- return [[self alloc] init];
- }
- + (instancetype)allocWithZone:(struct _NSZone *)zone{
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [super allocWithZone:zone];
- });
- return instance;
- }
- - (instancetype)init{
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [super init];
- [self initNotification];
- });
- return instance;
- }
- #pragma mark ============================>> public method app跳转直链
- -(void)startWithUrl:(NSURL*)url{//
-
- if (url==nil){
- return;
- }
- NSString * urlString = [url absoluteString];
- NSArray * stringArr = [urlString componentsSeparatedByString:@"//"];
- NSString * arr2 = stringArr[1];
- NSString* finsalString = [arr2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSDictionary * urlDict = [AlgorithmTool dictionaryWithJsonString:finsalString];
- NSLog(@"Schemes_Url转码的dict ========= %@",urlDict);
-
- if (urlDict!=nil){
- //缓存用户信息数据
- [IOS_NSUSERDEFAULT setObject:urlDict forKey:IOSSDK_USERINFO];
- [IOS_NSUSERDEFAULT synchronize];
- //主副设备:deviceType 游戏种类:game_id mac地址:macAddress
- BTDataProcess* bleInstance = [BTDataProcess sharedInstance];
- bleInstance.deviceType = DEVICETYPE_MAIN;
- NSString * game_id = urlDict[@"game_id"];
- bleInstance.game_id = (GAME_TYPE)[game_id intValue];
- if ([urlDict objectForKey:@"mac"]!=nil){
- bleInstance.macAddress = [urlDict objectForKey:@"mac"];
- }
- //链接蓝牙
- [bleInstance initCBCentralManager];
- //数据存入plist
- bleInstance.allMotionCountBLock = ^(int jump_count, int crouch_count, int step_count){
- // NSLog(@"jump_count = %d crouch_count = %d step_count = %d ",jump_count,crouch_count,step_count);
- [self cacheAllMotionCount:jump_count crouchCount:crouch_count stepCount:step_count];
- };
-
- }
-
- }
- #pragma mark ============================>> private 各类通知处理事件
- -(void)initNotification{
-
- //监听各类通知action
- [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(gameStartInitData) name:@"gameStartInitData" object:nil];
- [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(gameEndInitData:) name:@"gameEndInitData" object:nil];
- [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(searchBLEAction:) name:@"searchBLEAction" object:nil];
- //监听 app group 消息 ==>> 好友邀请信息
- CFStringRef invite = (__bridge CFStringRef)@"INVITE_NOTIFICATION";
- CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
- NULL,
- inviteFriends,
- invite,
- NULL,
- CFNotificationSuspensionBehaviorDeliverImmediately);
-
- }
- //趣动app:广播的通知回调
- void inviteFriends (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
-
- //group.com.Oujia.AppAndGame 沙河数据
- NSUserDefaults* userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.Oujia.AppAndGame"];
- //invite json字符串转字典
- NSString * jsonString = [userDefault objectForKey:@"invite"];
- NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
- NSDictionary *invite = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
- NSLog(@"group通知监听好友邀请信息 invite jsonString = %@",invite);
- if (invite!=nil){
- //user字典转json字符串
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:invite options:NSJSONWritingPrettyPrinted error:nil];
- NSString * inviteJsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- const char * user =[inviteJsonString UTF8String];
- //邀请信息
- NSString * infoString = invite[@"info"];
- const char * info =[infoString UTF8String];
- NSLog(@"unity 获取好友邀请信息 GetInviteInfo ===>> %@ %@",inviteJsonString,infoString);
- inviteFriendHandler(0,user,info);
- }
-
- }
- //通知开始游戏
- -(void)gameStartInitData{
- NSLog(@"unity call ios 通知开始游戏 --> IOSPlatformSDK");
- if ([self existUserInfo]){
- self.firstTime = [NSDate date];
- }
- }
- //通知结束游戏
- -(void)gameEndInitData:(NSNotification*)notification{
- NSLog(@"unity call ios 通知结束游戏 --> IOSPlatformSDK");
- }
- //通知弹出蓝牙搜索框
- -(void)searchBLEAction:(NSNotification*)notification{
-
- NSLog(@"unity call ios 通知游戏弹窗 --> IOSPlatformSDK");
- if ([instance existUserInfo]==NO){
- return;
- }
- /***************************初始化蓝牙数据处理工具***************************/
- //主副设备:deviceType 游戏种类:game_id mac地址:macAddress
- BTDataProcess* bleInstance = [BTDataProcess sharedInstance];
- NSDictionary * notificationDict = notification.userInfo;
- NSNumber * typeNumber = [notificationDict objectForKey:@"type"];
- int type = [typeNumber intValue];
- bleInstance.deviceType = (DEVICE_TYPE)type;
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSString * game_id = jsonDict[@"game_id"];
- bleInstance.game_id = (GAME_TYPE)[game_id intValue];
- bleInstance.macAddress = @"";
- //初始化蓝牙CBCentralManager
- [bleInstance initCBCentralManager];
- //缓存动作数据
- bleInstance.allMotionCountBLock = ^(int jump_count, int crouch_count, int step_count){
- // NSLog(@"jump_count = %d crouch_count = %d step_count = %d ",jump_count,crouch_count,step_count);
- [self cacheAllMotionCount:jump_count crouchCount:crouch_count stepCount:step_count];
- };
- /***************************搜索蓝牙外设弹窗***************************/
- SearchDeviceViewController * searchVC = [SearchDeviceViewController new];
- [self addTOwindow:searchVC];
- //searchVC搜索蓝牙Button
- searchVC.searchDeviceTask = ^{
- [bleInstance initCBCentralManager];
- };
- //BTDataProces搜索到的蓝牙外设-->searchVC
- bleInstance.deviceArrBLock = ^(NSMutableArray * arr){//蓝牙数据回调 刷新popView数据
- searchVC.deviceArray = arr;
- [searchVC.tableView reloadData];
- [searchVC hideAnimation];
- };
- //点击searchVC的tableViewCell链接选中的蓝牙
- searchVC.connectDeviceBlock = ^(CBPeripheral * peripheral){
- bleInstance.deviceType = (DEVICE_TYPE)type;
- // NSLog(@"connectDeviceBlock = %@ %ld",peripheral,(long)bleInstance.deviceType);
- [bleInstance connectPeripheral:peripheral];
- };
-
- }
- #pragma mark ============================>> private 全局弹窗
- -(void)addTOwindow:(UIViewController*)searchVC{
-
- UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
- UIViewController *parent = rootVC;
- while ((parent = rootVC.presentedViewController) != nil ){
- rootVC = parent;
- }
- while ([rootVC isKindOfClass:[UINavigationController class]]){
- rootVC = [(UINavigationController *)rootVC topViewController];
- }
- [rootVC addChildViewController:searchVC];
- [rootVC.view addSubview:searchVC.view];
-
- }
- #pragma mark ============================>> private 缓存处理
- //是否已经缓存到用户信息
- -(BOOL)existUserInfo{
-
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- if (jsonDict==nil){
- [PopupView showCusHUDA:@"获取趣动用户数据失败"];
- return YES;
- }else{
- return YES;
- }
-
- }
- //玩游戏过程中缓存 动作数据 和时间戳
- -(void)cacheAllMotionCount:(int)jump_count crouchCount:(int)crouch_count stepCount:(int)step_count{
-
- //游戏持续时间
- self.durationTime = [[NSDate date] timeIntervalSinceDate:self.firstTime];//游戏总共经历的时长 秒数
- //缓存pilst文件
- NSMutableDictionary * bleDict = [NSMutableDictionary new];
- [bleDict setObject:[NSNumber numberWithInt:self.durationTime] forKey:@"duration"];
- [bleDict setObject:[NSNumber numberWithInt:jump_count] forKey:@"jump"];
- [bleDict setObject:[NSNumber numberWithInt:crouch_count] forKey:@"crouch"];
- [bleDict setObject:[NSNumber numberWithInt:step_count] forKey:@"step"];
- NSDictionary * dict = [NSDictionary dictionaryWithDictionary:bleDict];
- //归档缓存
- [IOS_NSUSERDEFAULT setObject:dict forKey:IOSSDK_BLESDKDATA];
- [IOS_NSUSERDEFAULT synchronize];
-
- }
- #pragma mark ============================>> ios call unity
- -(void)bridgingMotionAction:(int)cusid
- left:(int)left
- right:(int)right{
- if (motionHandler!=nil){
- motionHandler(cusid,left,right);
- }
- }
- -(void)bridgingStepAction:(int)cusid
- leftStatus:(int)leftStatus
- rightStatus:(int)rightStatus
- leftFrag:(int)leftFrag
- rightFrag:(int)rightFrag{
- // NSLog(@"deviceHandler ===>> %d %@ %@ %d %d",cusid,name,address,status,electricity);
- stepHandler(cusid,leftStatus,rightStatus,leftFrag,rightFrag);
- }
- -(void)bridgingDeviceAction:(int)cusid
- name:(NSString*)name
- address:(NSString*)address
- status:(int)status
- electricity:(int)electricity{
- // NSLog(@"deviceHandler ===>> %d %@ %@ %d %d",cusid,name,address,status,electricity);
- //
- const char * charName =[name UTF8String];
- const char * charAddress =[address UTF8String];
- deviceHandler(cusid,charName,charAddress,status,electricity);
- }
- -(void)bridgingInteraction:(int)cusid
- code:(int)code{
- actionHandler(cusid,code);
- }
- @end
- #pragma mark ============================>> unity call ios
- /**
- * c语言字符串指针malloc
- */
- char* MakeStringCopy(const char* string){
-
- if (string == NULL)
- return NULL;
- char* res = (char*)malloc(strlen(string) + 1);
- strcpy(res, string);
- return res;
-
- }
- /**
- * unity call ios 主动接收 函数指针
- * MotionHandler 用于ios回调 "蓝牙动作",在👆🏻bridgingMotionAction中实现
- */
- extern "C" {void PointerMotionHandler(MotionHandler handler){
- motionHandler = handler;
- NSLog(@"PutIntPtr MotionHandler 指针内存地址 ===>> %p",&motionHandler);
- }
- }
- /**
- * unity call ios 主动接收 函数指针
- * StepHandler 用于ios回调 "鞋子步频",在👆🏻bridgingStepAction中实现
- */
- extern "C" { void PointerStepHandler(StepHandler handler){
- stepHandler = handler;
- NSLog(@"PutIntPtr StepHandler 指针内存地址 ===>> %p",&stepHandler);
- }
- }
- /**
- * unity call ios 主动接收 函数指针
- * StepHandler 用于ios回调 "鞋子蓝牙状态和电量",在👆🏻bridgingDeviceAction中实现
- */
- extern "C" {void PointerDeviceHandler(DeviceHandler handler){
- deviceHandler = handler;
- NSLog(@"PutIntPtr DeviceHandler 指针内存地址 ===>> %p",&deviceHandler);
- }
- }
- /**
- * unity call ios 主动接收 函数指针
- * ActionHandler 用于ios回调“”蓝牙和游戏大厅交互动作数据”,在👆🏻bridgingMotionAction中实现
- */
- extern "C" {void PointerActionHandler(ActionHandler handler){
- actionHandler = handler;
- NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&actionHandler);
- }
- }
- /**
- * unity call ios 主动接收 函数指针
- * UserFriendsHandler 用于ios回调 "当前用户好友列表",在👇🏻GetUserFriends中实现
- */
- extern "C" {void PointerUserFriendsHandler(UserFriendsHandler handler){
- userFriendsHandler = handler;
- NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&userFriendsHandler);
- }
- }
- /**
- * unity call ios 主动接收 函数指针
- * InviteFriendHandler 用于ios回调 "接收好友邀请" ,在👇🏻GetInviteInfo中实现
- */
- extern "C" {void PointerInviteFriendHandler(InviteFriendHandler handler){
- inviteFriendHandler = handler;
- NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&inviteFriendHandler);
- }
- }
- /**
- * 游戏开始
- */
- void GameStart(){
- [CUS_NOTIFICATIONCENTER postNotificationName:@"gameStartInitData" object:nil];
- NSLog(@"GameStart =====>> ");
- }
- /**
- * 游戏结束
- * level: level
- * score: score
- * record: record
- * mode: mode
- * opponentId: opponentId
- */
- void GameEnd(int level, double score, int record, int mode, int opponentId){
-
- [CUS_NOTIFICATIONCENTER postNotificationName:@"gameEndInitData" object:nil];
- if ([instance existUserInfo]==NO){
- return;
- }
- NSMutableDictionary * sportRecord = [NSMutableDictionary new];
- //缓存的token、game_id
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSString * token = [jsonDict objectForKey:@"token"];
- NSString * game_id = jsonDict[@"game_id"];
- [sportRecord setObject:game_id forKey:@"game_id"];//游戏类型
- //预留字段:默认
- [sportRecord setObject:@0 forKey:@"consume"];//卡路里默认0
- [sportRecord setObject:@0 forKey:@"screen"];//默认0
- [sportRecord setObject:@0 forKey:@"distance"];//废弃
- [sportRecord setObject:@0 forKey:@"is_cancel"];//是否正常停止游戏 正常0 不正常1
- //unity回调数据
- [sportRecord setObject:[NSNumber numberWithInt:level] forKey:@"level"];
- [sportRecord setObject:[NSNumber numberWithDouble:score] forKey:@"score"];
- [sportRecord setObject:[NSNumber numberWithInt:record] forKey:@"record"];
- [sportRecord setObject:[NSNumber numberWithInt:mode] forKey:@"mode"];
- [sportRecord setObject:[NSNumber numberWithInt:opponentId] forKey:@"opponentId"];
- //缓存的动作数据和持续时间
- NSDictionary * acheDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_BLESDKDATA];
- if (acheDict!=nil){
- [sportRecord addEntriesFromDictionary:acheDict];
- }else{
- [sportRecord setObject:@0 forKey:@"jump"];
- [sportRecord setObject:@0 forKey:@"crouch"];
- [sportRecord setObject:@0 forKey:@"step"];
- [sportRecord setObject:@0 forKey:@"duration"];
- }
-
- NSLog(@"GameEnd =====>> %@",sportRecord);
-
- NSString * urlString = @"http://shoes-api.hiyd.com/gameRecord/addGame";
-
- [HTTPDataProcession uploadHTTPDataWithUrlString:urlString withParams:sportRecord token:token success:^(id data){
-
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- if (dic!=nil){
- int code = [dic[@"code"] intValue];
- if (code==0){//
- NSLog(@"请求成功 %@",dic);
- }else{
- NSLog(@"请求错误 =========== >>> %@",dic[@"msg"]);
- }
-
- }
-
- }fail:^(id errorDes){
-
- NSLog(@"请求失败 =========== >>> %@",errorDes);
- }];
-
- }
- /**
- * 搜索设备
- * type: 设备ID
- */
- void SearchDevice(int type){
- //unity 有两个search按钮 type 0是主设备 1是副设备
- NSDictionary * dict = @{@"type":@(type)};
- NSLog(@"SearchDevice ===>> %@",dict);
- [CUS_NOTIFICATIONCENTER postNotificationName:@"searchBLEAction" object:nil userInfo:dict];
- }
- /**
- * 链接设备
- * type: 设备ID
- */
- void ConnectDevice(int type){
- NSLog(@"ConnectDevice ===>> %d",type);
- //unity 有两个connect按钮 type 0是第一个玩家 1是第二个玩家
- }
- /**
- * 震动
- * type: 设备ID
- * duration: 时长 ms [100 .. 1000]
- */
- void Vibrate(int type,int duration){
- //unity 有两个search按钮 type 0是主设备 1是副设备
- NSMutableDictionary * obj = [NSMutableDictionary new];
- [obj setObject:@(type) forKey:@"type"];
- [obj setObject:@(duration) forKey:@"duration"];
- NSDictionary * dict = [NSDictionary dictionaryWithDictionary:obj];
- NSLog(@"vibrate ===>> %@",dict);
- [CUS_NOTIFICATIONCENTER postNotificationName:@"vibrationAction" object:nil userInfo:dict];
- }
- /**
- * unity获取用户好友 -->> ios端根据用户token值请求http好友列表 ios将列表json的数组数据传给unity
- */
- void GetUserFriends(){
-
- if ([instance existUserInfo]==NO){
- return;
- }
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSString * token = [jsonDict objectForKey:@"token"];
- NSLog(@"gameFriends token ===>> %@ ",token);
- NSString *urlString = @"http://shoes-api.hiyd.com/forum/friends?limit=1000";
- [HTTPDataProcession getHTTPDataProcession:urlString token:token success:^(id data){
- // 网络访问成功
- // NSLog(@"data=%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
- NSError * error;
- NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
-
- // NSLog(@"gameFriends dictionary === %@ ",dictionary);
- if (!error){
-
- int code = [dictionary[@"code"] intValue];
- if (code==0){//
-
- NSDictionary * dict = dictionary[@"data"];
- NSArray * arr = dict[@"list"];
-
- NSMutableArray * userArr = [NSMutableArray new];//存储数据
- for (NSDictionary * object in arr){
-
- NSDictionary * socialInfo = object[@"socialInfo"];
- // NSDictionary * dataDict = data[@"data"];
- // NSString * token = dataDict[@"token"];
- // NSLog(@"获取token值成功");
- UserFriendsModel * model = [UserFriendsModel new];
- model.code = 0;
- [model setValuesForKeysWithDictionary:socialInfo];
-
- // NSLog(@"model =========== >>> %d %@ %@ %d %@ %@",model.trans_id,model.nickname,model.city,model.gender,model.status,model.avatar);
-
- NSMutableDictionary * gameFriends = [NSMutableDictionary new];
- [gameFriends setValue:model.status forKey:@"status"];
- NSMutableDictionary * userInfo = [NSMutableDictionary new];
- [userInfo setObject:[NSNumber numberWithInt:model.trans_id] forKey:@"id"];
- [userInfo setValue:model.nickname forKey:@"nickname"];
- [userInfo setObject:[NSNumber numberWithInt:model.gender] forKey:@"gender"];
- [userInfo setValue:model.city forKey:@"city"];
- [userInfo setValue:model.avatar forKey:@"avatar"];
- [gameFriends setValue:userInfo forKey:@"user"];
-
- // NSLog(@"gameFriends =========== >>>%@",gameFriends);
- [userArr addObject:gameFriends];
- }
-
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userArr options:NSJSONWritingPrettyPrinted error:nil];
-
- NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
-
- NSLog(@"GetUserFriends ==>> unity 请求好友列表 %@ ",jsonString);
-
- const char * cusChar =[jsonString UTF8String];
-
- userFriendsHandler(code,cusChar);//异步回调
-
- }
-
- }
-
- }fail:^(id errorDes){
-
- }];
- }
- /**
- * unity获取用户信息 -->> 解析从Spotr App的传过来的RL_Seme ios返回json数据给游戏
- */
- char * GetUserInfoJson(){
-
- if ([instance existUserInfo]==NO){
- return nil;
- }else{
-
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSDictionary * user = jsonDict[@"user"];
-
- if (user!=nil){
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error:nil];
- NSString * userJsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- const char * cuschart =[userJsonString UTF8String];
- NSLog(@"GetUserInfoJson ===>> unity 获取用户信息 %@",userJsonString);
- //要先copy 不然c#调用free的时候回闪退
- return MakeStringCopy(cuschart);
- }else{
- return MakeStringCopy("");
- }
- }
- }
- /**
- * unity可以主动断开链接
- * type: 设备类型 0: 主, 1: 副
- */
- void DisConnectDevice(int type){
- NSLog(@"disConnectDevice ===>>");
- }
- /**
- * 主动邀请好友
- * friendid: 游戏好友ID
- */
- void InviteFriend(int friendid,char * info){
-
- if ([instance existUserInfo]==NO){
- return;
- }
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSString * token = [jsonDict objectForKey:@"token"];
- NSString * invite_user_id = [NSString stringWithFormat:@"%d",friendid];
- NSString * invite = [NSString stringWithFormat:@"%s",info];
- NSString * game_id = jsonDict[@"game_id"];
- NSLog(@"InviteFriend ===>> token:%@ friendid:%d info:%s",token,friendid,info);
-
- NSString *urlString = [NSString stringWithFormat:@"http://shoes-api.hiyd.com/game/invite?invite_user_id=%@&invite=%@&game_id=%@",invite_user_id,invite,game_id];
- [HTTPDataProcession getHTTPDataProcession:urlString token:token success:^(id data){
-
- NSError * error;
- NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
- // NSLog(@"gameFriends dictionary === %@ ",dictionary);
- if (!error){
-
- int code = [dictionary[@"code"] intValue];
- if (code==0){//
-
- // NSLog(@"邀请成功");
- dispatch_async(dispatch_get_main_queue(), ^{
- [PopupView showCusHUDA:@"邀请成功"];
- });
- }
-
- }
- }fail:^(id errorDes){
- }];
- }
- /**
- * unity界面准备好后可以申请邀请信息,回调在inviteFriendHandler
- * friendid: 游戏好友ID
- */
- void GetInviteInfo(){
- NSLog(@"GetInviteInfo ===>>");
- if ([instance existUserInfo]==NO){
- return;
- }else{
- NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
- NSDictionary * invite = jsonDict[@"invite"];
- if (invite!=nil){
- //user字典转json字符串
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:invite options:NSJSONWritingPrettyPrinted error:nil];
- NSString * inviteJsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- const char * user =[inviteJsonString UTF8String];
- //邀请信息
- NSString * infoString = invite[@"info"];
- const char * info =[infoString UTF8String];
- NSLog(@" GetInviteInfo ===>> unity 获取好友邀请信息 %@ %@",inviteJsonString,infoString);
- inviteFriendHandler(0,user,info);
- }
- }
- }
- /**
- * 投屏
- */
- void ScreenProjection(){
- NSLog(@"ScreenProjection ===>>");
- }
|