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 TnkSession.queryPoint(Context context, boolean showProgress, ServiceCallback callback)

Description
   This method returns amount of points of a user managed by Tnk server. This method works asynchronously and delivers return values to ServiceCallback object you passed in. You should invoke this method in main UI thread. Return value is delivered to the second parameter of onReturn(Context context, Object result) method of callback object. Callback is executed in main UI thread.

Parameters
 Parameter  Description
 context Activity or Context object 
 showProgress  shows progress dialog or not.

 callback  ServiceCallback object in which return value is delivered. Return value is an Integer object representing user points.

Sample Code

@Override

public void onCreate(Bundle savedInstanceState) {

    // ...

    final TextView pointView = (TextView)findViewById(R.id.main_point);

    TnkSession.queryPoint(this, true, new ServiceCallback() {

        @Override

        public void onReturn(Context context, Object result) {

            Integer point = (Integer)result;

            pointView.setText(String.valueOf(point));

        }

    });


[Sync Invocation]

Method 
  - int TnkSession.queryPoint(Context context)

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

Parameters
 Parameter  Description
 context Activity or Context object 

Return : int
   Amount of user points in Tnk server.

Sample Code

static public void getPoint() {

    new Thread() {

        public void run() {

            int point = TnkSession.queryPoint(mActivity);

            showPoint(point); // your logic for user points

        }

    }.start();

}


2) TnkSession.purchaseItem()

Use this method to decrease user points managed by Tnk server. Both sync and async invocation are supported.

[Async Invocation]

Method 
  - void TnkSession.purchaseItem(Context context, int pointCost, String itemId, boolean showProgress, ServiceCallback callback)

Description
   Decrease user points in Tnk server. Return value is delivered to the second parameter of onReturn(Context context, Object result) method of callback object. Callback is executed in main UI thread.

Parameters
 Parameter  Description
 context Activity or Context object 
 pointCost amount of points to decrease
 itemId Item name or Id to purchase. 
This value is used only for logging purpose.
 showProgress shows progress dialog or not.

 callback ServiceCallback object in which return value is delivered.
Return value is long[] of size 2. long[0] is amount of points left in server. long[1] is 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 long[1] of negative value is returned. 

Sample Code

@Override

public void onClick(View v) {

    TnkSession.purchaseItem(MainActivity.this, 30, "item.00001", true,

        new ServiceCallback() {

            @Override

            public void onReturn(Context context, Object result) {

                long[] ret = (long[])result;

                if (ret[1] < 0) {

                     // error

                 } else {

                     Log.d("tnkad", "current point = " + ret[0] + ", transaction id = " + ret[1]);

                     pointView.setText(String.valueOf(ret[0]));

                  }

            }

        });


[Sync Invocation]

Method 
  - long[] TnkSession.purchaseItem(Context context, int pointCost, String itemId)

Description
   Decrease user points in Tnk server. This method blocks until a return value arrives.

Parameters
 Parameter Description
 contextActivity or Context object 
 pointCostamount of points to decrease
 itemIdItem name or Id to purchase. 
This value is used only for logging purpose.

Return : long[]
  - long[0] : amount of points left in Tnk server.
  - long[1] : unique transaction Id. Negative value indicate there was an error like 'point not enough'
Comments