Thursday, February 19, 2009

cocos Live: a high score server for cocos2d

Cocos Live is a service that tries to help iPhone Indie Game Developers by giving them an API to solve their online requirements.

As of version 0.1, the only feature available is the High Score Server. Future versions will have more features.

To use the High Score Server you need to:

  1. Have a valid Google account
  2. Sign in into Cocos Live with that account
  3. Create a game, customize it, and copy the secret key


Requesting a score

-(void) requestScore{
// create a Request object for the game "DemoGame"
// You need to implement the Score Request Delegate
ScoreServerRequest *request = [[ScoreServerRequest alloc] initWithGameName:@"DemoGame" delegate:self];

/* use kQueryFlagIgnore to request World scores */
tQueryFlags flags = kQueryFlagIgnore;

/* or use kQueryFlagCountry to request the best scores of your country */
// tQueryFlags flags = kQueryFlagByCountry;

// request the first 15 scores ( offset:0 limit:15)
// request AllTime best scores (this is the only supported option in v0.1
// request the scores for the category "easy"
[request requestScores:kQueryAllTime limit:15 offset:0 flags:flags category:@"easy"];

// Release. It won't be freed from memory until the connection fails or suceeds
[request release];
}

// ScoreRequest delegate
-(void) scoreRequestOk: (id) sender
{
// the scores are stored in a NSArray
NSArray *scores = [sender parseScores];

// Display them as you wish: using a UITableView,
// a custom CocosNode, etc...
}

-(void) scoreRequestFail: (id) sender
{
// request failed. Display an error message.
}

Posting a score

-(void) postScore{
// Create que "post" object for the game "DemoGame"
// The gameKey is the secret key that is generated when you create you game in cocos live.
// This secret key is used to prevent spoofing the high scores
ScoreServerPost *server = [[ScoreServerPost alloc] initWithGameName:@"DemoGame" gameKey:@"e8e0765de336f46b17a39ad652ee4d39" delegate:self];

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:4];

// usr_ are fields that can be modified.
// set score
[dict setObject: [NSNumber numberWithInt: 340 ] forKey:@"cc_score"];
// set speed
[dict setObject: [NSNumber numberWithInt: 120 ] forKey:@"usr_speed"];
// set angle
[dict setObject: [NSNumber numberWithInt: 92 ] forKey:@"usr_angle"];
// set playername
[dict setObject:@"Ricardo" forKey:@"usr_playername"];
// set player type
[dict setObject: [NSNumber numberWithInt: 0 ] forKey:@"usr_playertype"];


// cc_ are fields that cannot be modified. cocos fields
// set category... it can be "easy", "medium", whatever you want.
[dict setObject:@"easy" forKey:@"cc_category"];

[server sendScore:dict];

// Release. It won't be freed from memory until the connection fails or suceeds
[server release];
}

// PostScore Delegate
-(void) scorePostOk: (id) sender
{
// Score post successful
}

-(void) scorePostFail: (id) sender
{
// score post failed
//
tPostStatus status = [sender postStatus];
if( status == kPostStatusPostFailed ) {
// an error with the server ?
// try again
}
else if( status == kPostStatusConnectionFailed ) {
// a error establishing the connection ?
// turn-on wifi, and then try again
}
}
For further information download cocos2d for iPhone v0.7, try the cocosLiveDemo example and read the cocosLiveDemo.m file.

4 comments:

Unknown said...

hi, why i cant submit my game score
if i need to contract cocos live administer to enable ?
many thanks!

Unknown said...

my game's name in cocos live is:
iboy

riq said...

Could you post the error that you are receiving in http://groups.google.com/group/cocoslive-discuss ?

thanks

Ginsberg said...

This is the greatest thing ever - riq rules.