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

43 lines
960 B
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
*
* This page compiles a set of data and sends it if the provided hash of
* the data is not equal.
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-03-30 12:11:39 +00:00
$prev_hash = (int)filter_input(INPUT_GET, 'h');
2022-03-30 08:51:46 +00:00
2022-04-04 09:09:58 +00:00
$data = [];
2022-04-14 20:59:42 +00:00
$times = $time_mapper->getAll();
2022-03-30 08:51:46 +00:00
2022-04-04 09:09:58 +00:00
foreach ($times as $time)
{
2022-04-14 20:59:42 +00:00
$team = $team_mapper->get($time->team_id);
2022-04-04 09:09:58 +00:00
$row = [
2022-04-21 12:41:19 +00:00
"name" => $team->name,
"date" => $time->date->getTimestamp()
2022-04-04 09:09:58 +00:00
];
array_push($data, $row);
}
$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);