69 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php $app = require '../../../app/inc.php';
 | |
| /**
 | |
|  * We originally wanted to use SSE for this, but the hosting provider
 | |
|  * did not support that so we resorted to simple polling instead
 | |
|  * 
 | |
|  * This page compiles a set of times and sends it if the provided hash of 
 | |
|  * the data is not equal.
 | |
|  */
 | |
| 
 | |
| use App\Teamtable\TeamMapper;
 | |
| use App\Timetable\TimeMapper;
 | |
| 
 | |
| $team_mapper = new TeamMapper($app->database->conn);
 | |
| $time_mapper = new TimeMapper($app->database->conn);
 | |
| 
 | |
| $prev_hash = filter_input(INPUT_GET, 'h');
 | |
| 
 | |
| $teams    = [];
 | |
| $name_map = [];
 | |
| $times    = [];
 | |
| $time_ref = NULL;
 | |
| 
 | |
| foreach ($time_mapper->getAll() as $time)
 | |
| {
 | |
|     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]))
 | |
|     {
 | |
|         $name_map[$team->id] = htmlspecialchars($team->name);
 | |
|     }
 | |
| 
 | |
|     if ($time_ref === NULL)
 | |
|     {
 | |
|         $time_ref = $time->date->getTimestamp();
 | |
|     }
 | |
| 
 | |
|     $row = [
 | |
|         "id"   => $team->id,
 | |
|         "time" => ($time->date->getTimestamp() - $time_ref)
 | |
|     ];
 | |
| 
 | |
|     array_push($times, $row);
 | |
| }
 | |
| 
 | |
| $data = [
 | |
|     "map" => [
 | |
|         "team" => [
 | |
|             "name" => $name_map,
 | |
|         ],
 | |
|         "time_reference" => $time_ref
 | |
|     ],
 | |
|     "times" => $times
 | |
| ];
 | |
| 
 | |
| $hash = hash('crc32', serialize($data));
 | |
| 
 | |
| if ($prev_hash !== $hash)
 | |
| {
 | |
|     $app->api([
 | |
|             "hash" => $hash,
 | |
|             "data" => $data
 | |
|         ]);
 | |
| }
 | |
| // return nothing
 | |
| http_response_code(204); |