72 lines
2.5 KiB
PHP
72 lines
2.5 KiB
PHP
<?php
|
|
|
|
require_once(__DIR__."/libutil.php");
|
|
require_once(__DIR__."/libcustomerrorgen.php");
|
|
|
|
function get_team_info($game, $team_id) {
|
|
//read from cache first
|
|
$cache_file_name=__DIR__."/teaminfo_cache_".$game.".json";
|
|
$cache_file=false;
|
|
if(file_exists($cache_file_name)&&is_readable($cache_file_name)) {
|
|
$cache_file=file_get_contents_with_lock($cache_file_name);
|
|
}
|
|
|
|
$cache_file=json_decode($cache_file, true);
|
|
if($cache_file===null) {
|
|
$cache_file=false;
|
|
}
|
|
|
|
if($cache_file===false||!array_key_exists($team_id, $cache_file)) {
|
|
require(__DIR__."/libvar_gsec.php");
|
|
|
|
if(!array_key_exists($game, $gsecret_l)) {
|
|
gen_error_400("game ".$game." is not found in config file.");
|
|
exit;
|
|
}
|
|
$ginfo=$gsecret_l[$game];
|
|
unset($gsecret_l);
|
|
|
|
$url=$ginfo["gurlbase"]."/api/game/".$ginfo["gid"]."/scoreboard";
|
|
$url=preg_replace("#/+#", "/", $url);
|
|
|
|
$cu=curl_init($url);
|
|
curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);
|
|
//curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, false);
|
|
//curl_setopt($cu, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($cu, CURLOPT_CONNECTTIMEOUT_MS, 2000);
|
|
curl_setopt($cu, CURLOPT_TIMEOUT_MS, 7000);
|
|
$re=curl_exec($cu);
|
|
$err=curl_error($cu);
|
|
curl_close($cu);
|
|
if(strlen($err)>0||$re===false||strlen($re)<=0) {
|
|
gen_error_500("api request failed when trying to fetch team_info: ".$err);
|
|
exit;
|
|
}
|
|
$jo=json_decode($re, true);
|
|
if($jo===null) {
|
|
gen_error_500("api request failed when trying to fetch team_info: cannot decode response as json");
|
|
exit;
|
|
}
|
|
if(!array_key_exists("items", $jo)) {
|
|
gen_error_500("api request failed when trying to fetch team_info: key \"items\" does not exist");
|
|
exit;
|
|
}
|
|
$jo1=[];
|
|
for($a=0; $a<count($jo["items"]); $a++) {
|
|
$jo1["team_".(string)$jo["items"][$a]["id"]]=$jo["items"][$a];
|
|
}
|
|
$cache_file=$jo1;
|
|
unset($jo1);
|
|
unset($jo);
|
|
unset($a);
|
|
file_put_contents_with_lock($cache_file_name, json_encode($cache_file, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
if(!array_key_exists($team_id, $cache_file)) {
|
|
//unexpected logic here... why...
|
|
gen_error_500("unexpected logic: team token is valid, but is not found in team_info");
|
|
exit;
|
|
}
|
|
return $cache_file[$team_id];
|
|
}
|