Because all members with PHP skills are currently in deep winter sleep, I tried to port our forum banner to support Maniaplanet/TrackMania² (you can see the result below).
But my PHP kowledge is very basic. I started to learn PHP just some weeks ago.
Our TMF banner used the TMFDataFetcher class by oorf|fuckfish (Our clan has access to the TrackMania stats server).
So I searched for a TMFDataFetcher script that was ported to Maniaplanet/Trackmania Canyon. But it looks like, no one has done it so far.
Then I tried to use the Maniaplanet Web Services to access the statistics on my own. It was very easy. You need only three lines of code.
But for a fast and efficient access you need to cache the requested data. But I found not sample code.
The TMFDataFetcher class use a MySQL database to cache all the rankings.
Because I only need the ladder points, the world and nation rank, and our our forum has a limited number of visitors, I decided to use a simple file based cache.
It seems to work, but if some one had allready written a better solution, please let me know.
I experienced one problem. If someone delete the cache file, it will be created anew. But without write access. file_put_contents() failed with a permission denied error.
I have no experience with Linux and its file system. Why was the file created wihout write access? And how to change my code to solve this problem?
Code: Select all
$login = $_GET['login'];
// cache file
$filename = __DIR__.'/signature.cache';
// get ladder points, world rank, nation rank
try
{
$worldrank = 0;
$nationrank = 0;
$ladderpoints = 0;
// very basic file based data caching
$refreshStats = true;
$timeNow = time();
// cache file existing?
if (file_exists($filename))
{
// read cached stats from file
$data = unserialize(file_get_contents($filename));
if (array_key_exists($login, $data))
{ // player found in stats
if (($timeNow - $data[$login]['cachetime']) < 43200) // 12 hours
{
// use cached player data
$ladderpoints = $data[$login]['ladderpoints'];
$worldrank = $data[$login]['worldrank'];
$nationrank = $data[$login]['nationrank'];
$refreshStats = false;
}
}
}
if ($refreshStats)
{
// get latest stats via Maniaplanet Web Services
$multiplayerCanyon = new \Maniaplanet\WebServices\Canyon\MultiplayerRankings($apiuser, $apipass);
$player = $multiplayerCanyon->getPlayer($login);
$ladderpoints = $player->points;
$count = count($player->ranks);
if ($count > 0)
$worldrank = $player->ranks[0]->rank;
if ($count > 1)
$nationrank = $player->ranks[1]->rank;
$data[$login]['ladderpoints'] = $ladderpoints;
$data[$login]['worldrank'] = $worldrank;
$data[$login]['nationrank'] = $nationrank;
$data[$login]['cachetime'] = $timeNow;
// write stats to cache file
// ToDo: check write permissions if the file must be rebuilt
file_put_contents($filename, serialize($data));
}
// draw stats
// ...
}
catch(\Maniaplanet\WebServices\Exception $e) {}