Understanding MochiAds Publisher Bridge – Part 3

In part 1 we saw how to configure a cross-domain policy file, calling the javascript and send the results to a webpage.

In part 2 we saw how to prevent cheating.

In this 3rd part we’ll make something useful with it. I am going to add to my Triqui MochiAds Arcade theme for WordPress a WordPress widget showing the latest scores submitted from my portal, triqui.com, but once you understand how to do it, you can easily change the script to make it fit your needs.

The first thing to do is creating a new table in your WordPress database. At this time, I only want to save player name, score and obviously the game unique id.

So my MySQL query is:

1
2
3
4
5
6
7
8
CREATE TABLE IF NOT EXISTS `wp_mochi_scores` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `when` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `gameid` text COLLATE utf8_unicode_ci NOT NULL,
  `name` text COLLATE utf8_unicode_ci NOT NULL,
  `score` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

as you can see, I added a primary auto increment key and a timestamp field, to determine when the score has been saved.

Then I need to change postscores.php this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
 
// your secret key
$key="f26ebddd6fcfb796ac1841bcc57af057";
 
// initializing an empty string - not strictly necessary
$string = "";
 
// ordering the associative POST array by keys
ksort($_POST);
 
// loop scanning through all POST array
foreach($_POST as $varname => $varvalue){
     // if the key is not "signature" then append key and url encoded values to the string
     if($varname!="signature"){
          $string.=$varname."=".rawurlencode($varvalue)."&";
     }
}
 
// removing the last character (a "&"")
$string = substr($string,0,strlen($string)-1);
 
// appeding the secret key to the string
$string.= $key;
 
// comparing the md5 encryption of the string with the "signature" variable
if(md5($string)==$_POST[signature]){
     // requiring the relative path to your wp-config.php file
     // according to your wp installation, this path may change
     // but in most cases it will work this way
     require("../../../wp-config.php");
     // connecting to wp database
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     // selecting the proper database
     $db = mysql_select_db($link,DB_NAME);
     // inserting the game tag, the score and the player name
     $result = mysql_query("insert into ".$table_prefix."mochi_scores (gameid,name,score) values('$_POST[gameID]','$_POST[name]','$_POST[score]')");
}
 
?>

Now I have a script that silently populates the mochi_scores table with the latest scores.

It’s time to show them in a widget.

If you don’t know what is a widget or how to create and activate it, follow How to create a WordPress Widget.

I created a file called mochi_lastscores.php with this content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/*
Plugin Name: Mochi lastscores
Plugin URI: http://www.emanueleferonato.com/
Description: Displays last scores submitted in your MochiAds Arcade
Author: Emanuele Feronato
Version: 1.0
Author URI: http://www.emanueleferonato.com/
*/
 
function mochi_lastscores_widget() {
     global $wpdb;
     echo"<li><h2>LATEST SCORES</h2>";
     // selecting the latest 10 scores
     $query = "select * from ".$wpdb->prefix."mochi_scores order by id desc limit 0,10";
     // saving scores inside an array called "scores"
     $scores = $wpdb->get_results($query,ARRAY_A);
     if($scores){
          echo "<ul>";
          // scanning all scores
          foreach($scores as $score){
               // looking for the entry in my mochi game table with the same game tag as the one I saved
               $query = "select * from ".$wpdb->prefix."mochi where game_tag = \"$score[gameid]\"";
               // saving the entry in an array called "game"
               $game = $wpdb->get_results($query,ARRAY_A);
               // if the entry has a blog_id...
               if($game[0][blog_id]){
                    // selecting the post in the wp post table with the same id as the game found in the mochi games table
                    $query = "select * from ".$wpdb->prefix."posts where ID = ".$game[0][blog_id];
                    // saving the entry in a variable called "post"
                    $post = $wpdb->get_results($query,ARRAY_A);
                    // printing the entry in the theme
                    echo "<li><span class = \"turnwhite\">$score[name]</span> just scored <span class = \"turnwhite\">$score[score]</span> on <a href = \"".$post[0][guid]."\">".$game[0][name]."</a></li>";
               }
          }
          echo "</ul>";
     }
     echo "</li>";
}
 
function init_mochi_lastscores(){
     register_sidebar_widget("Mochi Last Scores", "mochi_lastscores_widget");     
}
 
add_action("plugins_loaded", "init_mochi_lastscores");
 
?>

And you can find the result in the left sidebar in my triqui.com portal.

This feature will be added in my next Triqui MochiAds Arcade theme for WordPress update, but meanwhile you can easily set it up on your own.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 5.00 out of 5)
Loading ... Loading ...
WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 15 comments

  1. macs

    on August 24, 2009 at 8:35 pm

    Sono colpito dal lavoro che hai fatto oltre che dalla semplicita’ del tuo sito . Sto pensando di utilizzare sia il tuo plugin che il tema, avrei pero’ alcune domande. Se possibile scrivimi via mail. Grazie e complimenti. Macs

  2. Albert

    on August 25, 2009 at 10:03 am

    /me waiting for the wordpress plugin and the new version of the triqui theme

  3. Joe

    on September 6, 2009 at 8:35 pm

    Ciao Emanuele, intanto complimenti per l’articolo e il blog che leggo sempre volentieri. Volevo chiederti quali modifiche bisognerebbe apportare al plugin mochi_latestscores affichè visualizzi gli ultimi score di un singolo gioco nella stessa pagina del gioco. Premettendo che le mie conoscenze di php e mySQL sono davvero scarse, pensavo di modicare questa riga di codice:

    $query = “select * from “.$wpdb->prefix.”mochi_scores where gameid=QUALCOSA? limit 0,10″;

    tuttavia non so con quale variabile (o altro) sostituire il QUALCOSA?. Grazie per l’attenzione! Ciao!

  4. Emanuele Feronato

    on September 7, 2009 at 12:40 pm

    It’s not possible right now, since I do not pass the game id as a meta…

    Good idea for next theme/plugin release anyway…

  5. Joe

    on September 7, 2009 at 3:55 pm

    Ok. So I’ll be waiting for your next plugin! Thanks!

  6. Engedi

    on October 4, 2009 at 11:41 am

    Do you know how to integrate wp user and mochi score? So that when user is login to wp, their score will be save as the username automatically.

  7. hiscorer

    on October 14, 2009 at 3:57 pm

    line 33 of mochi_lastscores.php

    should be
    $post[0][post_name]

    instead of
    $post[0][guid]

  8. scotttswan

    on October 23, 2009 at 4:08 am

    I’m not sure if you wanted to leave your secret key in this article but you have ;)

  9. Harry

    on November 4, 2009 at 11:51 am

    Hi,
    I created the table as instructed above
    1) created mochi_lastscores.php in wp-content/plugins folder
    2) created postscores.php under wp-content/themes/triqui folder
    How do I add this to the sidebar? I can’t even see the mochi_lastscores.php in the plugins section either.
    Can somebody help?
    Much appreciate your help

  10. Harry

    on November 4, 2009 at 11:53 am

    Another interesting thing I have come across is when a user tries to login in to the account, it goes straight to the WP-Profile page instead of the Game URL.
    Since this should have worked out-of-the-box I was wondering what’s missing here?
    THanks

  11. Harry

    on November 4, 2009 at 11:53 pm

    aahh nailed it.

  12. Harry

    on November 12, 2009 at 12:50 pm

    1)Custom table created xxxxxx_mochi_scores

    2) created mochi_lastscores.php in wp-content/plugins folder (change mochi_scores table to xxxxxx_mochi_scores)
    3) created postscores.php under wp-content/themes/triqui folder
    When a user plays a game, the scores are displayed.

    Do I need to update any other file?
    Any help would be much appreciated

  13. Dom

    on January 4, 2010 at 6:17 am

    Great tutorial series. I used it as a jumping off point for my site

  14. sj

    on February 22, 2010 at 9:06 am

    very nice tutorial series…. i dun use wordpress but this helped me to set up it for one of site i made :)
    thnx alot

  15. Mauro (mauro269)

    on June 2, 2010 at 2:46 pm

    This seems it doesn’t work anymore. I’ve opened a post on Mochiads forum:
    https://www.mochimedia.com/community/forum/topic/submit-score-on-wordpress

    If you have suggestions, please reply here or in that thread.
    Thanks