SDKInterface.m 8.58 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>

@interface SDKInterface()
{
    LYGameConfig *_config;
    NSString *uid;
}
@end


@implementation SDKInterface : Interface


int mExchange;

/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    [PoolConfig getInstance];
    NSLog(@"sdk chushihua");
    
    [LYGameProxy instance].platformDelegate = self;
    _config = [[LYGameConfig alloc]init];;
    
    NSString* productId = [[PoolConfig getInstance]getValueByKey:@"productId"];
    NSString* signKey = [[PoolConfig getInstance] getValueByKey:@"signKey"];
    
    [_config setProductId:productId];
    [_config setLinnyouKey:signKey];
    [_config setDebugState:TRUE];
    [[LYGameProxy instance] initWithConfig:_config];
    
    NSString* appleProductIdsStr = [[PoolConfig getInstance]getValueByKey:@"apple_productids"];
    
    NSSet* appleProductSet = [[NSSet alloc]initWithArray:[appleProductIdsStr componentsSeparatedByString:@","]];
    
    [[LYSDK instance]updateProducts:appleProductSet];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    [[PoolChecker getInstance]setPoolSdkDelegate:self];
    [[LYGameProxy instance]login:nil];
}

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

/**
 SDK
 */
- (void)SDKgotoUserCenter
{
    [[LYGameProxy instance] enterUserCenter];
}

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    if ([REPORT_CREATEROLE isEqualToString:[reportInfo reportType]]) {
        
    }else if ([[reportInfo reportType] isEqualToString:REPORT_ENTER]) {
        [[LYGameProxy instance] initServer:reportInfo.serverId UID:uid];
    }
}


/**
 SDK
 */
- (void)SDKexitSDK
{
    [[LYGameProxy instance]hideTouch];
}

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [[LYGameProxy instance]logout:nil];
}

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

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



/**
 @return 商品ID
 */
- (NSSet*)getProductIdSet:(NSString*)products
{
    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];
            return [[NSSet alloc]initWithArray:[keysList allObjects]];
        }
    }
    return nil;
}

/**
 @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";
}

/**
 @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 @"元宝";
}

- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    PoolPayInfo *payInfo = [notification object];
    NSString* amount = [payInfo postAmount];
    NSString* products = [payInfo products];
    
    NSString* productName = [self getProductNameByAmount:products amount:amount];
    NSString* productId = [self getProductIdByAmount:products amount:amount];
    
    int amountInt = amount.intValue * 100;
    
    NSLog(@"productName:%@productId%@ amount:%d",productName,productId,amountInt);
    
    [[LYGameProxy instance]payPlatform:productName itemId:productId amount:amountInt callBackInfo:[payInfo queryId] orderDetail:[payInfo custom]];
    
    int64_t delayInSeconds = 3.0;// 延迟的时间
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"chage state");
        [[PoolChecker getInstance] setOnlyOneTimePay:false];
    });
}


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



//平台初始化成功
-(void) onGameInitFinished:(LYResult*)result
{
    if(result.isOK){
        [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    }else{
        [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitFail" statusCode:POOLSDK_ERROR_INIT_FAILED description:result.msgLocal extendData:nil];
    }
}

- (NSString*)encodeStr:(NSString *)string
{
//    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:string] invertedSet];
//    NSString *encodedStr = [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
//    
    NSString* encodedStr = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return encodedStr;
}

//登录结果回调
-(void) onGameLogin :(LYResult*)result
{
    if(result.isOK){
        PoolLoginInfo *loginInfo = [PoolLoginInfo alloc];
   
        NSString* sdkData = [[result msg] stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
        sdkData = [sdkData stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        
        loginInfo.other = sdkData;
        loginInfo.timestamp = [self getCurrentTimestamp];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[PoolChecker getInstance] startCheckDelegate:loginInfo];
        });
        [[LYGameProxy instance]showTouch];
        
    }else{
        [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"loginFail" statusCode:POOLSDK_ERROR_LOGIN_FAILED description:result.msgLocal extendData:nil];
    }
}

//登出结果回调
-(void) onGameLogout:(LYResult*)result
{
    if (result.isOK) {
        [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"LogoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"LogoutSuccess" extendData:nil];
    }
    [[LYGameProxy instance]hideTouch];
}

//服务器初始化回调
-(void) onGameServerInit:(LYResult*)result
{
}

//用户状态失效回调
-(void) onUserStateInvalid
{
}

//用户支付回调
-(void) onGamePay:(LYResult*)result
{
}

-(void)PoolSdkLoginSuccess:(PoolLoginInfo *)loginInfo
{
    NSLog(@"login success %@",loginInfo);
    uid = loginInfo.openId;
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"LoginSuccess" statusCode:POOLSDK_NO_ERROR description:@"LoginSucecess" extendData:loginInfo];
}

- (void)PoolSdkLoginFail:(NSString *)msg
{
    NSLog(@"login fail %@",msg);
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"loginFail" statusCode:POOLSDK_ERROR_LOGIN_FAILED description:msg extendData:nil];
}

@end