Ajout de la première version fonctionnelle du site

This commit is contained in:
Rick 2020-06-24 16:46:39 +02:00
parent 579709fca7
commit cef27d06ae
Signed by: Rick
GPG key ID: 9570A7DB7CB2F436
6 changed files with 251 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
config.ini
# ---> Vim # ---> Vim
# Swap # Swap
[._]*.s[a-v][a-z] [._]*.s[a-v][a-z]

2
config.ini.example Normal file
View file

@ -0,0 +1,2 @@
# set your beautiful steam api key
steam_api_key = "<APIKEY HERE>"

128
getAchievements.php Normal file
View file

@ -0,0 +1,128 @@
<?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;
}
?>

50
getinformations.js Normal file
View file

@ -0,0 +1,50 @@
document.getElementById("box").innerHTML = "";
function makeHtml(jsonData) {
jsonData.forEach(element => {
console.log(element);
box = document.createElement("div");
gameName = document.createElement("p");
hours = document.createElement("p");
text = document.createTextNode(element["gamename"]);
gameName.appendChild(text);
text = document.createTextNode(element["hours"]);
hours.appendChild(text);
box.appendChild(gameName);
box.appendChild(hours);
bigbox.appendChild(box);
});
}
function makeArray(jsonData) {
document.getElementById("box").innerHTML = "";
bigbox = document.getElementById("box");
console.log(jsonData);
if (jsonData.hasOwnProperty('error')) {
console.log("error");
error = document.createElement("div");
error.id="error";
text = document.createTextNode(jsonData["error"]);
error.appendChild(text);
bigbox.appendChild(error);
} else {
document.getElementById("username").innerHTML = jsonData["username"];
jsonData["gameWithTime"].sort((a, b) => parseFloat(a.hours) - parseFloat(b.hours));
makeHtml(jsonData["gameWithTime"]);
makeHtml(jsonData["gameWithoutTime"]);
}
}
function sendData() {
text = document.createTextNode("Chargement en cours....");
document.getElementById("box").innerHTML = "";
loading = document.createElement("div");
loading.id = "loading";
loading.appendChild(text);
document.getElementById("box").appendChild(loading);
var xhr = new XMLHttpRequest();
xhr.open("POST", "getAchievements.php");
xhr.onload=function(event){
makeArray(event.target.response);
};
var formData = new FormData(document.getElementById("myform"));
xhr.responseType = 'json';
xhr.send(formData);
}

34
index.css Normal file
View file

@ -0,0 +1,34 @@
body {
background-color: aquamarine;
}
#box {
border: 1px solid black;
background-color: aqua;
}
#box div {
border-bottom: 1px solid black;
}
#error {
padding: 30px;
border: 10px solid #c94c4c;
background-color:#eca1a6;
}
#loading {
text-align: center;
background-color: red;
padding: 50px;
animation: loadingScreen 2s alternate infinite;
}
@keyframes loadingScreen {
0% {
transform: scale(1);
}
100% {
transform: scale(1.5);
}
}

35
index.html Normal file
View file

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="index.css">
<title>Document</title>
</head>
<body>
<header>
<!-- <a href="">Comptes enregistrés</a>
--> <a href="https://semapy.xyz/projets/triAchievements/">A propros</a>
</header>
<form id="myform">
<input type="text" name="steamid" id="steamid" placeholder="SteamID">
<input type="button" value="Charger le temps" onclick="sendData();">
</form>
<h1>Compte de <span id="username"></span></h1>
<div id="box">
<div id="error">
Activez le JavaScript et vérifiez si le fichier JS est au bon endroit.
</div>
</div>
<footer>
<p>Ce site n'est pas affilié à Astat ou Steam.</p>
<p>Code source</p>
</footer>
<script src="getinformations.js">
</script>
</body>
</html>