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:

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:

 $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:

LATEST SCORES

"; // 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 "
    "; // 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 "
  • $score[name] just scored $score[score] on ".$game[0][name]."
  • "; } } echo "
"; } echo ""; } 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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers