What’s in a new social game? Fundamentally, a game is an “experience” built around a series of interactions between the player, the content, and other players. Sure, the concept of the game, the storyline, the characters, and social components all have a huge influence on the game experience. But at the heart of a game’s attraction are the objects in the game world, the mechanics associated with these objects and the rewards offered to players. To power all this virtual activity you need a high-performance and scalable database.
In this blog, I’ll share how Couchbase can be used to power your basic game elements – Objects, Actions and Rewards
Game Objects
Game Object Type | Document Value |
---|---|
Player |
{
“type”: “player”, “uuid”: “35767d02-???a958”, “name”: “DonPinto4540”, “score”:663, “level”:4, “loggedIn”: false } |
Item |
{
“type”:“item”, “name”:“Axe_e5890c94-???11”, “uuid”:“e5890c94-???11c6-???”, “ownerId”:“Dale9887” } |
Monster |
{
“type”:“monster”, “name”:“Goliath9932”, “uuid”:“d10dfc1b-???0412-???41”, “hitpoints”:370, “experienceWhenKilled”:52, } |
In Couchbase, JSON documents are used to model game objects and are identified using the document id. For these documents, the type field is used to define the type of the object being stored.
Game Actions
Game Rewards
Game leaderboards can be created in Couchbase Server 2.0 using views. A view in Couchbase is written using map/reduce functions that generate an index. For the JSON game objects shown in the game objects section, a map function (as shown below) can be used to generate a list of player scores. If the jsonType field of the document is of type player, we emit the score of the player and the id.
if (doc.jsonType == “player”){
emit([“Score”, doc.score], doc._id);
}
}
-
To query the view using REST you can use the following curl command –
curl -X GET -H 'Content-Type: application/json'
‘http://bucketname:password@localhost:8092/bucketname/_design/designdocname/_view/
viewname?descending=true&connection_timeout=60000&limit=10&skip=0’
-
To query the view using PHP you can use the code snippet as shown below and print out the top 10 player scores –
/* Setup couchbase connection object */
$lbview = $cb->getView(“bucketname“, “viewname“);
$apage = $lbview->getResult(array(“descending” => true));
$rowCtr = 0;
foreach($apage->rows AS $row) {
if($rowCtr <= 9)
{
printf(“Score %s for user %s.n“, $row->key[1], $row->value);
$rowCtr = $rowCtr + 1;
}
else
break;
}
Final Thoughts
Would views be fast enough to generate leaderboards out of each user\’s Facebook friends?
Getting player rankings with a view:
http://www.couchbase.com/usin…