B. Point Management

Points can be managed by Tnk Server or by your server depending on the 'Point management' setting. When you selected 'Managed by Tnk', all points are managed by Tnk server and you can inquiry and withdraw points of users using APIs described in this chapter. If you selected 'Callback URL', all the point reward events are sent to your server through the callback URL you entered. For callback URL, see 1.6 Callback URL.

1) TnkSession - queryPoint

Inquiry amount of points of a user from Tnk server. Both sync and async invocation are supported.
Invoking this method in main UI thread, you have to use async method not to your UI freeze until data arrives from server.
But if you are going to invoke this method not in main UI thread but in other threads such as threads in game engine, you have to create a new thread and invoke sync method.

[Async Invocation]

Method 
  - (void) queryPoint:(id)target action:(SEL)action;

Description
   This method returns amount of points of a user managed by Tnk server. This method works asynchronously and delivers return values to action SELECTOR of  target object. 

Parameters
 Name  Description
 target  Object in which the result value is delivered.
 action  SELECTOR with one (NSNumber *) parameter.

Sample Code

- (void) pointReceived:(NSNumber *)point {

    [self.userPoints setText:[point stringValue]]; 

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    [[TnkSession sharedInstance] queryPoint:self action:@selector(pointReceived:)];



[Sync Invocation]

Method 
  - (NSInteger) queryPoint;

Description
   This method returns amount of points of a user managed by Tnk server. This method blocks until a return value arrives.

Return : NSInteger
   Amount of user points in Tnk server.

Sample Code

 
NSInteger userPoint = [[TnkSession sharedInstancequeryPoint];


2) TnkSession - purchaseItem

Use this method to decrease user points managed by Tnk server. Only async invocation is supported.

Method 
  - (void) purchaseItem:(NSString *)itemId cost:(NSInteger)poinCost target:(id)target action:(SEL)action;

Parameters
Name  Description
 itemId Item name or Id to purchase. 
This value is used only for logging purpose.
 pointCost amount of points to decrease
 target Object in which the result value is delivered.
 action SELECTOR with two (NSNumber *) parameters. First one has amount of points left in server, and second one has a unique transaction Id to distinguish this invocation. If the amount of points of a user is less than pointCost value, points will not be decreased and negative value is returned in the second parameter. 

Sample Code

- (void) purchaseResultReceived:(NSNumber *)pointRemain transactionId:(NSNumber *)trId {

    [self.userPoints setText:[pointRemain stringValue]];

    

    if ([trId integerValue] < 0) {

        // error

        UIAlertView *viewAlert = [[UIAlertView alloc] initWithTitle:@"purchaseItem Error" 

                                                            message:@"purchase Error!!!"

                                                           delegate:nil 

                                                  cancelButtonTitle:@"Confirm" 

                                                  otherButtonTitles:nil];

        

        [viewAlert show];

        

        [viewAlert autorelease]; 

    }

    else {

        // success

        UIAlertView *viewAlert = [[UIAlertView alloc] initWithTitle:@"purchaseItem Success" 

                                                            message:[trId stringValue]

                                                           delegate:nil 

                                                  cancelButtonTitle:@"Confirm" 

                                                  otherButtonTitles:nil];

        

        [viewAlert show];

        

        [viewAlert autorelease]; 

    }

}


- (IBAction)purchaseItemPressed:(id)sender {

    

    NSInteger pointCost = [self.itemCost.text integerValue];

    [[TnkSession sharedInstance] purchaseItem:self.itemId.text 

                                                                cost:pointCost 

                                                             target:self 

                                                           action:@selector(purchaseResultReceived:transactionId:)];

}



Comments