triachievements/getAchievements.php

128 lines
No EOL
4.1 KiB
PHP

<?php
// ~ 4 minutes pour ~ 350 jeux
$config = parse_ini_file("config.ini");
define('API_KEY', $config["steam_api_key"]);
$apikey = $config["steam_api_key"];
/**
* retourne un objet json à partir d'une url
*/
function getJson($url)
{
$array = file_get_contents($url);
$ret = json_decode($array, true);
return $ret;
}
/**
* renvoie le nombre d'heures qu'il faut pour finir un jeu à 100%
*/
function getHours($appid)
{
$tempFile = tempnam(".", "tmp");
$url = "http://astats.astats.nl/astats/Steam_Game_Info.php?AppID="; // 813630";
$html = file_get_contents($url.$appid);
file_put_contents($tempFile, $html);
$searchFor = 'Hours to 100';
$pattern = preg_quote($searchFor, '/');
$pattern = "/^.*$pattern.*\$/m";
$matches = array();
if(preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE)){
$charpos = $matches[0][1];
list($before) = str_split($html, $charpos);
$line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
$file = new SplFileObject($tempFile);
$file->seek($line_number);
$content = $file->current();
$hours = preg_split('/ /', trim($content), 2);
unlink($tempFile);
return $hours[0];
}
else{
unlink($tempFile);
return null;
}
}
/**
* check si un jeu a des succès
*/
function hasAchievements($appid)
{
file_get_contents("http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=".$appid);
$pattern = preg_quote(' 403 ', '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match($pattern, $http_response_header[0])){
return false;
} else {
return true;
}
}
/**
* verifie si le jeu est fini à 100%
*/
function checkComplete($appid, $steamid)
{
$gameInfo = getJson("http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?key=".API_KEY."&steamid=".$steamid."&appid=" . $appid);
$listAchievements = $gameInfo["playerstats"]["achievements"];
foreach ($listAchievements as $key => $value) {
if ($value["achieved"] == 0) {
return false;
}
}
return true;
}
/**
* affiche un tableau HTML contenant les jeux triés par ordre croissant de temps de fin
*/
if (isset($_POST['steamid'])) {
$ret = array();
$steamid = $_POST['steamid'];
$url = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=".API_KEY."&steamid=".$steamid;
$listGames = file_get_contents($url);
if ($listGames == NULL) {
$ret = array('error' => 'User not found.', 'response' => $http_response_header);
} else {
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".API_KEY."&steamids=".$steamid;
$userInformations = getJson($url);
$ret["username"] = $userInformations["response"]["players"][0]["personaname"];
$listGamesJson = json_decode($listGames, true);
$listGamesJson = $listGamesJson["response"];
$url = "http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?key=".API_KEY."&steamid=".$steamid."&appid=";
foreach ($listGamesJson["games"] as $key => $value) {
$appid = $value["appid"];
if (hasAchievements($appid)) {
if (!checkComplete($appid, $steamid)) {
$gameName = getJson($url.$appid);
$gameName = $gameName["playerstats"]["gameName"];
$hours = getHours($appid);
if ($hours == null) {
$ret["gameWithoutTime"][] = array('gamename' => $gameName, 'hours' => $hours);
} else {
$ret["gameWithTime"][] = array('gamename' => $gameName, 'hours' => $hours);
}
}
}
}
}
//$fileJson = fopen('result.json', 'w');
//fwrite($fileJson, json_encode($ret));
//fclose($fileJson);
$retJson = json_encode($ret);
$_POST["game"] = $retJson;
header('Content-Type: application/json');
echo $retJson;
}
?>