Showing MochiAds leaderboards in any Flash movie or web page
Ok, so you created your awesome game, embedded MochiAds leaderboards and want to publish high scores in your homepage, in another Flash movie, or wherever you want, just outside the game.
Just think about Kongregate, that shows top scores on the right of the game you are playing.
That's what we are going to do.
Information stored into MochiAds leaderboards
Every player who enters the leaderboard is saved with its name, the score (obviously), the ISO 3166-1 alpha-2 Country code and the timestamp of his score.
ISO 3166-1 alpha-2 country codes are two-letter country codes in the ISO 3166-1 standard to represent countries and dependent territories.
They are published by the International Organization for Standardization (ISO) as part of its ISO 3166 standard.
Read this page for further information.
The timestamp is the number of millisecond passed from January 1st, 1970
There are other information you can get from MochiAds leaderboards such as the number of entries for each leaderboard, but in this example I am only showing an all-time top 10 chart, so I already have enough information.
Refer to MochiAds Developer Support for the full list of options.
Flash Vs Php
If you just want to display your top 10 in a Flash movie, then you are lucky... MochiAds proviedes an API that does almost all the job.
Problems come when you want to show results in an HTML page.
There isn't any API to do it and there won't be any in the future, as soon as I know.
Too good I am a PROgrammer and I can arrange it anyway.
Let's say I want to display the Top 10 scores for Summer Couples.
Flash Top 10
This is the movie with the name of the dynamic text instances I created

And this is the commented actionscript:
-
// connecting to mochi services the id below is the id of your game
-
mochi.MochiServices.connect("summer couples id");
-
// setting the board id - the id below is the one of your leaderboard and NOT the one of your game
-
mochi.MochiScores.setBoardID("summer couples board id");
-
// sending the request for leaderboard datas
-
// "display table" is the funcion to be called once the request is done
-
mochi.MochiScores.requestList(this, "display_table");
-
// ... and here it is the function
-
function display_table(leaderboard:Object) {
-
// changing the "Loading..." text
-
messagetxt.text = "Top 10";
-
// creating a variable called array_with_leaderboard that will store all rows of the ALL TIME leaderboard
-
// change alltime with:
-
// daily — scores for the daily table
-
// weekly — scores for the weekly table
-
// monthly — guess what?
-
array_with_leaderboard = leaderboard.scores.alltime.rows;
-
// scanning through top 10
-
for (x=0; x<10; x++) {
-
// updadint place field
-
place.text += (x+1)+"\n";
-
// updating points field
-
points.text += array_with_leaderboard[x][0]+"\n";
-
// updating names field
-
names.text += array_with_leaderboard[x][1]+"\n";
-
// updating country field
-
country.text += array_with_leaderboard[x][2]+"\n";
-
// updating timestamp field
-
timestamp.text += array_with_leaderboard[x][3]+"\n";
-
}
-
// sending the variables to /downloads/showmochitable.php page with POST method and display it on "mochiframe" iframe
-
// you don't need it if you only want to display values in flash
-
getURL("/downloads/showmochitable.php", "mochiframe", "POST");
-
}
And here it is my chart for Summer Couples
I did not convert the timestamp and the Coutry code because it's not the aim of this tutorial. Feel free to do it for me and I'll publish your version.
I also did not include any check for errors, so if you see a lot of "undefined", that's because at the moment MochiAds is suffering of some technical problem and cannot display leaderboards.
But when leaderboards work, the script works too.
Remove line 33 if you don't want to show leaderboards in HTML.
HTML (with Php) Top 10
The key of HTML top 10 is in the getURL at line 33.
Now some of you will say it would be better to use something like sendAndLoad but I am going to use getURL for two reasons:
1) In this example I am going to display top 10 in a table, but I would want to do it with an alert called by JavaScript (or any other JS function)
2) I don't have to store anything in a database, just processing POST results
The downside is I am getting a CSV string and not an array... this means sometimes (in example if the name of the player has a comma) I can get wrong result.
The perfect solution would be parsing results in JSON format and post them with getURL (anyone willing do do it?)
Anyway, you can just hide the previous Flash movie (or make it 1x1 pixels sized) and have your results in an iframe with this Php script:
-
<?php
-
-
//exploding POST variable removing all commas
-
-
// displaying results
-
for($x=0;$x<10;$x++){
-
$count = $x*4;
-
$html .= "<tr>";
-
$html .= "<td>".($x+1)."</td>";
-
$html .= "<td>".$scores[$count]."</td>";
-
$html .= "<td>".$scores[$count+1]."</td>";
-
// showing a flag according to Country code
-
$html .= "<td><img src = \"flags/".$scores[$count+2].".gif\"></td>";
-
// converting timestamp in readable format
-
$html .= "</tr>";
-
}
-
-
?>
-
-
<html>
-
<body>
-
<table cellpadding = "3" cellspacing = "0" border ="0">
-
</table>
-
</body>
-
</html>
That outputs this page:
This page too will work only after the leaderboard is correctly displayed in the Flash movie.
No check for errors in this page too, but you are free to improve it.
I used some free flag icons you can find on famfamfam and called with the proper Country code, so the italian flag is it.gif, the french one is fr.gif and so on.
Timestamp is converted using Php date() function, but remember to divide the original timestamp by 1,000 because the original one is in milliseconds while Php accepts timestamps in seconds.
The first thing I'll do now, is enabling leaderboard browsing for my games... if everything goes ok I would like to ask MochiAds developers to share their leaderboard IDs in order to have a lot of games with highscores on Triqui.com.
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
2 Responses to “Showing MochiAds leaderboards in any Flash movie or web page”
Leave a Reply
Trackbacks
-
Create a Wordpress MochiAds Leaderboards Widget : Emanuele Feronato on
October 17th, 2008 1:39 pm
[...] time ago I blogged about Showing MochiAds leaderboards in any Flash movie or web page with a script made by myself that displayed MochiAds leaderboards on your [...]

(14 votes, average: 4.57 out of 5)
For the flags you can also use the ones on our CDN… here’s an example for the US flag in PNG format:
* http://xs.mochiads.com/static/lib/flags/us.png
These are also in SWF format for loadMovie usage:
* http://xs.mochiads.com/static/lib/flags/us.swf