Files
JJBB/Sdk/iOS/Traceless/SdkHub.m
2024-08-23 15:49:34 +08:00

190 lines
6.9 KiB
Objective-C

#include "SdkHub.h"
#include "Unity/UnityInterface.h"
@implementation SdkHub
const char *_unityReceiver = "SdkControl";
static SdkHub *_sharedInstance = nil;
+(SdkHub *)sharedInstance {
if (!_sharedInstance)
{
_sharedInstance = [[self alloc]init];
[_sharedInstance addNotification];
}
return _sharedInstance;
}
-(void)addNotification {
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(inited:)
name:kVSCPInitNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(logined:)
name:kVSCPLoginNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(logouted:)
name:kVSCPLogoutNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(payed:)
name:kVSCPPayNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(switchChild:)
name:kVSCPSwitchChildNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(syncGameDataed:)
name:kVSCPSyncGameDateNotification // 同步数据完成
object:nil];
}
-(void)inited:(NSNotification *)notification {
NSDictionary *dic = [notification userInfo];
const int code = [[dic objectForKey:@"code"] intValue];
NSString *msg = [dic objectForKey:@"msg"];
if (code == 0)
{
NSLog(@"Sdk init success: %@!", msg);
}
else
{
NSLog(@"Sdk init failed: %@", msg);
// Delay 1.0 seconds to init again!
[self performSelector:@selector(Initize:) withObject:nil afterDelay:1.0];
}
}
-(void)logined:(NSNotification *)notification {
NSDictionary *dic = [notification userInfo];
self.uid = [dic objectForKey:@"uid"];
NSString *token = [dic objectForKey:@"token"];
NSString *message = [NSString stringWithFormat:@"%@,%@,%@", @"success", self.uid, token];
[self SendMessage:@"LoginComplete" args:message];
NSLog(@"Login Success %@ - %@", self.uid, token);
}
-(void)syncGameDataed:(NSNotification *)notification {
}
-(void)logouted:(NSNotification *)notification {
NSDictionary *dic = [notification userInfo];
const int code = [[dic objectForKey:@"code"] intValue];
NSString *msg = [dic objectForKey:@"msg"];
if (code == 0)
{
self.uid = nil;
[self SendMessage:@"LogoutComplete" args:@"success"];
NSLog(@"Logout Success: %@", msg);
}
else
{
[self SendMessage:@"LogoutComplete" args:@"failed"];
NSLog(@"Logout Failed: %@", msg);
}
}
-(void)payed:(NSNotification *)notification {
NSDictionary *dic = [notification userInfo];
const int code = [[dic objectForKey:@"code"] intValue];
NSString *msg = [dic objectForKey:@"msg"];
NSString *orderId = [dic objectForKey:@"orderID"];
if (code == 0)
{
[self SendMessage:@"PaymentComplete" args:@"success"];
NSLog(@"Payment Success: %@, %@!", orderId, msg);
}
else
{
[self SendMessage:@"PaymentComplete" args:@"failed"];
NSLog(@"Payment Failed: %@, %@!", orderId, msg);
}
}
-(void)switchChild:(NSNotification *)notification {
NSDictionary *dic = [notification userInfo];
self.uid = [dic objectForKey:@"uid"];
NSString *token = [dic objectForKey:@"token"];
[self SendMessage:@"LogoutComplete" args:@"success"];
NSString *message = [NSString stringWithFormat:@"%@,%@,%@", @"success", self.uid, token];
[self SendMessage:@"LoginComplete" args:message];
NSLog(@"Switch Account: %@ - %@", self.uid, token);
}
-(void)SendMessage:(NSString*)func args:(NSString*)args {
const char* funcString = [func UTF8String];
const char* argsString = [args UTF8String];
UnitySendMessage(_unityReceiver, funcString, argsString);
}
-(void)Initize {
TracelessInitConfigure *config = [[TracelessInitConfigure alloc]initWithAppid:@"qyxmlios" URLSchemes:@"com.jjyou.qyxmlios.traceless"];
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma initSdk:config];
}
-(void)Login:(BOOL)isAuto {
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma login];
}
char* cStringCopy(const char* string)
{
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
-(char*)GetChannel {
NSString *result = @"Traceless";
return cStringCopy([result UTF8String]);
}
-(void)Logout {
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma logout];
}
-(void)SwitchAccount {
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma switchChild];
}
-(void)Payment:(char*)payString {
NSString *source = [NSString stringWithUTF8String:payString];
NSLog(source);
NSArray *parameters = [source componentsSeparatedByString:@","];
TracelessPayInfo *payInfo = [[TracelessPayInfo alloc]init];
payInfo.uid = self.uid;
payInfo.cporder = [parameters objectAtIndex:0];
payInfo.sid = [parameters objectAtIndex:1];
payInfo.amount = [parameters objectAtIndex:2];
payInfo.item_id = [parameters objectAtIndex:3];
payInfo.item_name = [parameters objectAtIndex:4];
payInfo.extra = [parameters objectAtIndex:5];
payInfo.role_id = [parameters objectAtIndex:6];
payInfo.role_name = [parameters objectAtIndex:7];
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma pay:payInfo];
}
-(void)SubmitRole:(char*)roleString {
NSString *source = [NSString stringWithUTF8String:roleString];
NSLog(source);
NSArray *parameters = [source componentsSeparatedByString:@","];
TracelessGameRoleData *roleInfo = [[TracelessGameRoleData alloc]init];
roleInfo.uid = self.uid;
roleInfo.sid = [parameters objectAtIndex:0];
roleInfo.sname = [parameters objectAtIndex:1];
roleInfo.role_id = [parameters objectAtIndex:2];
roleInfo.role_name = [parameters objectAtIndex:3];
roleInfo.role_level = [parameters objectAtIndex:4];
roleInfo.role_ctime = [parameters objectAtIndex:5];
TracelessSdkManage *sdkma = [TracelessSdkManage defaultManager];
[sdkma syncGameData:roleInfo];
}
@end