SDKInterface.m 8.2 KB
//
//  UnionInterface.m
//  PoolSdk
//
//  Created by winFan on 11/23/15.
//  Copyright (c) 2015 winFan. All rights reserved.
//
#include "SDKInterface.h"
#import <UIKit/UIKit.h>
#import <CSSYGameSDK/CSSYGameSDK.h>


@interface SDKInterface()
{
    UIViewController* gameViewController;
    NSString* sessionId;
}
@end

@implementation SDKInterface : Interface


/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    gameViewController = control;
    
    [PoolConfig getInstance];
    
    NSString* referer = [[PoolConfig getInstance]getValueByKey:@"gamechannelid"];
    NSString* gameId = [[PoolConfig getInstance]getValueByKey:@"gameID"];
    NSString* gameName = [[PoolConfig getInstance]getValueByKey:@"gamename"];
    NSString* key = [[PoolConfig getInstance]getValueByKey:@"key"];
    [CSGameActivation connectionSDK:referer gameID:gameId gameName:gameName statisticsKey:key];
    
    
    int platform = [[PoolConfig getInstance]getValueByKey:@"platform"].intValue;
    [CSGameActivation setPlatform:platform];
    
    [CSGameActivation setDebug:YES];
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AccountLogout) name:CSGame_Account_LogoutSuccess object:nil];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    [CSGameAuth connectionLoginForController:gameViewController animated:NO block:^(NSString *userName, NSString *sessionID, NSString *timestamp, NSString *token) {
        
        NSLog(@"userName:%@",userName);
        
        sessionId = sessionID;
        PoolLoginInfo* loginInfo = [PoolLoginInfo alloc];
        [loginInfo setTimestamp:timestamp];
        [loginInfo setOpenId:userName];
        [loginInfo setSign:token];
        [loginInfo setOther:sessionID];
        [self loginCheck:loginInfo];
    } dismissSuccess:^{
        
    }];
}

- (void)loginCheck:(PoolLoginInfo*) loginInfo{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[PoolChecker getInstance] startCheck:loginInfo];
    });
}


/**
 支付 创建订单
 */
- (void)SDKpayWithPaymentInfo:(PoolCreateOrderInfo *)payInfo
{
    [super SDKpayWithPaymentInfo:payInfo];
}

/**
 SDK
 */
- (void)SDKgotoUserCenter
{
}

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    if ([REPORT_CREATEROLE isEqualToString:[reportInfo reportType]]) {
        
    }else if ([[reportInfo reportType] isEqualToString:REPORT_ENTER]) {
        [CSGameActivation setServer:[reportInfo serverId]];
        [CSGameAuth loginValidationWithSessionID:sessionId block:^(BOOL loginState) {
            if (loginState) {
                [CSGameAuth EnterData];
            }
        }];
    }else if([[reportInfo reportType ]isEqualToString:REPORT_ROLEUPGRADE]){
        [CSGamePush setlevel:[reportInfo playerLevel].intValue];
    }
}

/**
 SDK
 */
- (void)SDKexitSDK
{
    
}


-(void)AccountLogout
{
    //do someting
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"logoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"logoutSuccess" extendData:nil];
}

-(BOOL)hasLogout
{
    return YES;
}


- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [CSGameAuth connectionLogoutWithBlock:^(BOOL state) {
        [self AccountLogout];
    }];
}

- (void)handleOpenurl:(NSURL *)url
{
}

- (void)receivePayMessage : (NSNotification *)notification;
{
    NSString *order_id = [notification object];
    NSLog(@"订单ID:%@", order_id);
}

/**
 @return 商品名称
 */
- (NSString*)getProductNameByAmount:(NSString*)products amount:(NSString*)amount
{
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:products options:NSJSONWritingPrettyPrinted error:nil];
    if(jsonData){
        NSDictionary* productList = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        if(productList){
            NSEnumerator* keysList = [productList keyEnumerator];
            id keyValue;
            while (keyValue = [keysList nextObject])
            {
                NSString* appleAmount = [[productList objectForKey:keyValue] objectForKey:@"amount"];
                if(appleAmount.intValue == amount.intValue){
                     NSString* productName = [[productList objectForKey:keyValue] objectForKey:@"name"];
                    return productName;
                }
            }
        }
    }
    NSLog(@"get product name error amount:%@",amount);
    return @"元宝";
}

/**
 @return 商品ID
 */
- (NSString*)getProductIdByAmount:(NSString*)products amount:(NSString*)amount
{
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:products options:NSJSONWritingPrettyPrinted error:nil];
    if(jsonData){
        NSDictionary* productList = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        if(productList){
            NSEnumerator* keysList = [productList keyEnumerator];
            id keyValue;
            while (keyValue = [keysList nextObject])
            {
                NSLog(@"i found %@",keyValue);
                NSString* appleAmount = [[productList objectForKey:keyValue] objectForKey:@"amount"];
                NSLog(@"appleAmount:%@ amount:%@",appleAmount,amount);
                if(appleAmount.intValue == amount.intValue){
                    return keyValue;
                }
            }
        }
    }
    return @"unknow";
}


- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    PoolPayInfo *payInfo = [notification object];
    
    NSString *myamount = [payInfo postAmount];
    NSString *queryId = [payInfo queryId];
    NSString* productId = [self getProductIdByAmount:[payInfo products] amount:myamount];
  
    [CSGameIAPShare IAPShareBuyObject:productId Amount:myamount ExtraInfo:queryId block:^(BOOL state, NSString *error) {
        
    }];
}


-(NSString*)getCurrentTimestamp{
    // 时间戳转时间
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    NSString* timeStr = [[NSString alloc]initWithFormat:@"%.f",interval];
    return timeStr;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [CSGamePush handleLaunching:launchOptions
                successCallback:^{
                    NSLog(@"Handle receive success");
                } errorCallback:^{
                    NSLog(@"Handle receive error");
                }];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    NSString *deviceTokenStr = [CSGamePush registerDevice:deviceToken successCallback:^{
        NSLog(@"register push success");
    } errorCallback:^{
        NSLog(@" register push error");
    }];
    NSLog(@"device token is %@", deviceTokenStr);
}


//    收到通知的回调
//    @param application  UIApplication 实例
//    @param userInfo 推送时指定的参数
//    */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@" receive Notification");
    [CSGamePush handleReceiveNotification:userInfo
                          successCallback:^{
                              NSLog(@" Handle receive success");
                          } errorCallback:^{
                              NSLog(@" Handle receive error");
                          }];
}


/**
 收到静默推送的回调
 
 @param application  UIApplication 实例
 @param userInfo 推送时指定的参数
 @param completionHandler 完成回调
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@" receive slient Notification");
    [CSGamePush handleReceiveNotification:userInfo
                          successCallback:^{
                              NSLog(@" Handle receive success");
                          } errorCallback:^{
                              NSLog(@" Handle receive error");
                          }];
    
    completionHandler(UIBackgroundFetchResultNewData);
}



@end