From 744f4db361e1ff48cfac352adbbb972d79b79bdc Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 5 Nov 2024 18:02:15 +0100 Subject: [PATCH] la til sprekkindeks og caching --- table.php | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/table.php b/table.php index 90cf90e..5965071 100644 --- a/table.php +++ b/table.php @@ -3,7 +3,20 @@ date_default_timezone_set('UTC'); $GLOBALS['start_time'] = DateTime::createFromFormat(DateTime::ISO8601, "2024-11-02T08:53:00+01"); $GLOBALS['number_of_controls'] = 3; -//declare(strict_types=1); +// Caching +header("Last-Modified: " . date("F d Y H:i:s.", filemtime("passering.csv"))); +$etag = '"' . md5_file("passering.csv"). '"'; +header(header: 'ETag: ' . $etag ); + +if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) { + // If HTTP_IF_NONE_MATCH is same as the generated ETag => content is the same as browser cache + // So send a 304 Not Modified response header and exit + if($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { + header('HTTP/1.1 304 Not Modified', true, 304); + exit(); + } +} + class Runner { public int $id; @@ -92,6 +105,9 @@ function filter_runners(Runner $runner, $id) { } } +function time_diff(DateTime $date_1, DateTime $date_2) { + return $date_2->getTimestamp() - $date_1->getTimestamp(); +} $runners = []; $csv_runners = file_get_contents("db.csv"); @@ -220,6 +236,7 @@ else { 1. matpost 2. matpost Mål + Sprekkindeks "; $minikadaver_table = " @@ -233,7 +250,11 @@ else { $mini_num = 0; for ($i = 0; $i < count($runners); $i++) { $runner = $runners[$i]; - if ($runner->course == "Kadaverløpet") { + $tid_maal = ""; + if ($runner->splits[2] != false) { + $tid_maal = $GLOBALS['start_time']->diff($runner->splits[2])->format('%H:%I:%S'); + } + if ($runner->course == "Kadaverløpet") { $kadaver_num++; $tid_1_mat = ""; if ($runner->splits[0] != false) { @@ -243,18 +264,24 @@ else { $tid_2_mat = ""; if ($runner->splits[1] != false) { $tid_2_mat = $GLOBALS['start_time']->diff($runner->splits[1])->format('%H:%I:%S'); + try { + $sprekk = ""; + } + catch (DivisionByZeroError $e){ + $sprekk = ""; + } + catch (TypeError $e) { + $sprekk = ""; + } } $matposter = ""; } else { $mini_num++; } - $tid_maal = ""; - if ($runner->splits[2] != false) { - $tid_maal = $GLOBALS['start_time']->diff($runner->splits[2])->format('%H:%I:%S'); - } + if ($runner->course == "Kadaverløpet") { - $kadaver_table .= "$matposter\n"; + $kadaver_table .= "$matposter$sprekk\n"; } elseif ($runner->course == "Minikadaver'n") { $minikadaver_table .= "\n"; @@ -263,4 +290,5 @@ else { $kadaver_table .= "
" . number_format(100*(time_diff($GLOBALS['start_time'],$runner->splits[2]) - time_diff($GLOBALS['start_time'],$runner->splits[1])) / time_diff($GLOBALS['start_time'],$runner->splits[2]), 0) . "%$tid_1_mat$tid_2_mat
". $kadaver_num .".$runner->name$tid_maal
". $kadaver_num .".$runner->name$tid_maal
". "" .".$runner->name$tid_maal
"; $minikadaver_table .= ""; echo($kadaver_table); + }