Skip to content

Commit

Permalink
Add ranking tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Petah committed Sep 3, 2020
1 parent 6275b15 commit 2e1c7bb
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 0 deletions.
122 changes: 122 additions & 0 deletions debug/rankings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
function readRankings($file)
{
$mySnakes = ['Project Z', 'Tak', 'Rando', 'Keep Away', 'Tail Chase'];
$rankings = json_decode(file_get_contents($file), true);
$datasets = [];
$labels = [];
foreach ($rankings as $time => $players) {
$labels[] = $time;
foreach ($players as $player => $rank) {
if (count($datasets) > 15 && !in_array($player, $mySnakes)) {
continue;
}
$datasets[$player] = [
'label' => $player,
'fill' => false,
'data' => [],
];
}
}
foreach ($labels as $dataIndex => $time) {
foreach ($datasets as &$dataset) {
$dataset['data'][$dataIndex] = null;
foreach ($rankings[$time] as $player => $rank) {
if ($player == $dataset['label']) {
$dataset['data'][$dataIndex] = (int) $rank;
break;
}
}
}
}
$datasets = array_values($datasets);
return [$labels, $datasets, str_replace('.json', '', basename($file))];
}
?>
<html ng-app="battleSnake" ng-controller="RootController">

<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</head>

<body ng-cloak>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.6.1/randomColor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<div class="container-fluid">
<div class="row">
<?php foreach (glob(__DIR__ . '/../scrapes/*.json') as $file) : ?>
<?php [$labels, $datasets, $label] = readRankings($file); ?>
<div class="col-lg-4">
<canvas id="<?= $label; ?>" width="400" height="400"></canvas>
</div>
<script>
(() => {
const LABELS = <?= json_encode($labels); ?>;
const DATASETS = <?= json_encode($datasets); ?>;

var ctx = document.getElementById(<?= json_encode($label); ?>).getContext('2d');
window.myLine = new Chart(ctx, {
type: 'line',
data: {
labels: LABELS.map(time => new Date(time)),
datasets: DATASETS.map((d) => {
const color = randomColor({
seed: d.label,
});
d.backgroundColor = color;
d.borderColor = color;
return d;
}),
},
options: {
responsive: true,
legend: {
position: 'left',
},
title: {
display: true,
text: <?= json_encode($label); ?>,
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
type: 'time',
time: {
tooltipFormat: 'll HH:mm'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
display: true,
ticks: {
reverse: true,
min: 1,
},
scaleLabel: {
display: true,
labelString: 'Rank'
}
}]
}
}
});
})();
</script>
<?php endforeach; ?>
</div>
</div>
</body>

</html>
157 changes: 157 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
"@babel/core": "^7.11.5",
"@babel/preset-env": "^7.11.5",
"@types/angular": "^1.7.2",
"@types/axios": "^0.14.0",
"@types/cheerio": "^0.22.21",
"@types/color": "^3.0.1",
"@types/express": "^4.17.8",
"@types/jquery": "^3.5.1",
"@types/node": "^11.15.21",
"@types/websocket": "^1.0.1",
"axios": "^0.20.0",
"babelify": "^10.0.0",
"browserify": "^16.5.2",
"cheerio": "^1.0.0-rc.3",
"color": "^3.1.2",
"concurrently": "^5.3.0",
"forever": "^0.15.3",
Expand Down
2 changes: 2 additions & 0 deletions scrapes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading

0 comments on commit 2e1c7bb

Please sign in to comment.