This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/public/api/v1/race/sync.php

71 lines
1.5 KiB
PHP
Raw Normal View History

2022-04-14 20:59:42 +00:00
<?php $app = require '../../../../app/inc.php';
2022-03-30 08:51:46 +00:00
/**
* We originally wanted to use SSE for this, but the hosting provider
* did not support that so we resorted to simple polling instead
2022-04-13 19:22:21 +00:00
*
2022-04-26 13:00:38 +00:00
* This page compiles a set of times and sends it if the provided hash of
2022-04-13 19:22:21 +00:00
* the data is not equal.
2022-04-26 13:00:38 +00:00
*
* TODO: This code fucking sucks, all of it does
2022-03-30 08:51:46 +00:00
*/
use App\Teamtable\TeamMapper;
use App\Timetable\TimeMapper;
2022-04-14 20:59:42 +00:00
$team_mapper = new TeamMapper($app->database->conn);
$time_mapper = new TimeMapper($app->database->conn);
2022-03-30 08:51:46 +00:00
2022-04-26 13:00:38 +00:00
$prev_hash = filter_input(INPUT_GET, 'h');
2022-03-30 08:51:46 +00:00
2022-04-26 13:00:38 +00:00
$teams = [];
$name_map = [];
$times = [];
$time_ref = NULL;
2022-03-30 08:51:46 +00:00
2022-04-26 13:00:38 +00:00
foreach ($time_mapper->getAll() as $time)
2022-04-04 09:09:58 +00:00
{
2022-04-26 13:00:38 +00:00
if (!isset($teams[$time->team_id]))
{
$teams[$time->team_id] = $team_mapper->get($time->team_id);
}
$team = $teams[$time->team_id];
if (!isset($name_map[$team->id]))
{
2022-04-26 19:55:10 +00:00
$name_map[$team->id] = htmlspecialchars($team->name);
2022-04-26 13:00:38 +00:00
}
if ($time_ref === NULL)
{
$time_ref = $time->date->getTimestamp();
}
2022-04-04 09:09:58 +00:00
$row = [
2022-04-26 13:00:38 +00:00
"id" => $team->id,
"time" => ($time->date->getTimestamp() - $time_ref)
2022-04-04 09:09:58 +00:00
];
2022-04-26 13:00:38 +00:00
array_push($times, $row);
2022-04-04 09:09:58 +00:00
}
2022-04-26 13:00:38 +00:00
$data = [
"map" => [
"team" => [
"name" => $name_map,
],
"time_reference" => $time_ref
],
"times" => $times
];
$hash = hash('crc32', serialize($data));
2022-03-30 08:51:46 +00:00
if ($prev_hash !== $hash)
{
$app->api([
"hash" => $hash,
2022-04-04 09:09:58 +00:00
"data" => $data
2022-03-30 08:51:46 +00:00
]);
}
// return nothing
http_response_code(204);