Skip to content

Instantly share code, notes, and snippets.

@Techmind
Last active April 1, 2020 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Techmind/631187d1b934914a791d6cf5e3a3d91b to your computer and use it in GitHub Desktop.
Save Techmind/631187d1b934914a791d6cf5e3a3d91b to your computer and use it in GitHub Desktop.
run with php -d memory_limit=4048M parse.php
<?php
//ini_set('memory_limit','4GB');
$pages = 30;
$size = 10000;
$total_games = 0;
$factions = [1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => []];
$players_by_game = [];
for ($p = 1; $p <= $pages; $p++)
{
echo date('Y-m-d H:i:s') . ": Page $p of $pages\n";
//$json = file_get_contents("https://api.faforever.com/data/gamePlayerStats?sort=-id&include=player&page[size]=${size}&filter[gamePlayerStats]=score%3E0&page[number]=${p}");
$json = file_get_contents("https://api.faforever.com/data/gamePlayerStats?sort=-id&include=player&page[size]=${size}&page[number]=${p}&filter[gamePlayerStats]=score!=0");
$arr = json_decode($json, true);
foreach ($arr['data'] as $k => $v)
{
$fac = $v['attributes']['faction'];
if (empty($factions[$fac]))
{
$factions[$fac] = ['wins' => 0, 'games' => 0, 'players_ids' => []];
}
$game_id = $v['relationships']['game']['data']['id'];
if (empty($players_by_game[$game_id]))
{
$players_by_game[$game_id] = [];
}
$players_by_game[$game_id][] = $fac;
$player_id = $v['relationships']['player']['data']['id'];
if ($v['attributes']['afterDeviation'] < 200 && $v['attributes']['afterMean'] > 1000 && $v['attributes']['beforeMean'] > 1000)
{
if ($v['attributes']['score'] != 0)
{
$is_win = ($v['attributes']['score'] > 0);
$scoreTime = $v['attributes']['scoreTime'];
$factions[$fac]['games']++;
if (!isset($factions[$fac]['players_ids'][$player_id]))
{
$factions[$fac]['players_ids'][$player_id] = ['games' => 0, 'wins' => 0];
}
if (empty($factions[$fac]['game_ids'][$game_id]))
{
$factions[$fac]['game_ids'][$game_id] = ['plays' => 0, 'wins' => 0];
}
$factions[$fac]['players_ids'][$player_id]['games']++;
if ($is_win)
{
$factions[$fac]['wins']++;
$factions[$fac]['game_ids'][$game_id]['wins']++;
$factions[$fac]['players_ids'][$player_id]['wins']++;
}
else
{
}
$factions[$fac]['game_ids'][$game_id]['plays']++;
if ($fac >= 1 && $fac <= 4)
{
$total_games++;
}
}
} else
{
if (empty($factions[$fac]['small_rating']))
{
$factions[$fac]['small_rating'] = 0;
}
$factions[$fac]['small_rating']++;
}
}
}
//var_dump($players_by_game);
// nomads ?
unset($factions[5]);
unset($factions[6]);
function cmp($a, $b) {
if ($a['games'] == $b['games']) {
return 0;
}
return ($a['games'] < $b['games']) ? 1 : -1;
}
// filter-out non-ladder
foreach ($factions as $fac => $row)
{
foreach ($row['game_ids'] as $game_id => $result_row)
{
if (count($players_by_game[$game_id]) > 2)
{
$factions[$fac]['games']-= $result_row['plays'];
$factions[$fac]['wins']-= $result_row['wins'];
$total_games-= $result_row['plays'];
if (empty($factions[$fac]['non_1x1']))
{
$factions[$fac]['non_1x1'] = 0;
}
$factions[$fac]['non_1x1']++;
}
else if (count($players_by_game[$game_id]) == 2)
{
$found = false;
$other_fac = null;
$game_factions = $players_by_game[$game_id];
for ($i = 0; $i < 2; $i++)
{
// ignore nomads
if ($game_factions[$i] > 4)
{
$other_fac = null;
if (empty($factions[$fac]['nomads_data']))
{
$factions[$fac]['nomads_data'] = 0;
}
$factions[$fac]['nomads_data']++;
$factions[$fac]['games']-= $result_row['plays'];
$factions[$fac]['wins']-= $result_row['wins'];
$total_games-= $result_row['plays'];
break;
}
if ($game_factions[$i] == $fac && !$found)
{
$found = true;
} else {
$other_fac = $game_factions[$i];
}
}
if ($other_fac)
{
if (empty($factions[$fac]['games_vs']))
{
$factions[$fac]['games_vs'] = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
}
if (empty($factions[$fac]['wins_vs']))
{
$factions[$fac]['wins_vs'] = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
}
$factions[$fac]['games_vs'][$other_fac]++;
if ($result_row['wins'])
{
$factions[$fac]['wins_vs'][$other_fac]++;
}
}
} else
{
if (empty($factions[$fac]['inc_data']))
{
$factions[$fac]['inc_data'] = 0;
}
$factions[$fac]['inc_data']++;
}
}
$factions[$fac]['players_cnt'] = count($factions[$fac]['players_ids']);
uasort($factions[$fac]['players_ids'], "cmp");
$factions[$fac]['players_ids_top_games'] = array_slice($factions[$fac]['players_ids'], 0, 10, true);
foreach ($factions[$fac]['players_ids_top_games'] as $p_id => $row)
{
$factions[$fac]['players_ids_top_games'][$p_id]['winrate'] = $row['wins'] / $row['games'];
unset($factions[$fac]['players_ids_top_games'][$p_id]['wins']);
}
unset($factions[$fac]['players_ids']);
$factions[$fac]['game_ids_cnt'] = count($factions[$fac]['game_ids']);
unset($factions[$fac]['game_ids']);
}
foreach ($factions as $fac => $row)
{
foreach ($factions[$fac]['wins_vs'] as $other_fac => $val)
{
$factions[$fac]['wins_vs'][$other_fac] = $val / $factions[$fac]['games_vs'][$other_fac];
}
}
echo "LAST entry time:" . $scoreTime . ", games - $total_games \n";
foreach ($factions as $i => $val)
{
$factions[$i]['win_pct'] = $factions[$i]['wins'] / $factions[$i]['games'];
$factions[$i]['usage_pct'] = $factions[$i]['games'] / $total_games;
}
echo json_encode($factions, JSON_PRETTY_PRINT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment