From aebd249237f5765c1b899a368b329ea18db9736f Mon Sep 17 00:00:00 2001 From: William Date: Wed, 30 Mar 2022 08:51:46 +0000 Subject: [PATCH] Commit --- app/lib/App/Core/App.php | 2 +- app/view/pages/race/live-results.php | 25 ++++++++++++++---- public/race/simulator.php | 6 +++-- public/race/stream.php | 39 ---------------------------- public/race/sync.php | 27 +++++++++++++++++++ 5 files changed, 52 insertions(+), 47 deletions(-) delete mode 100644 public/race/stream.php create mode 100644 public/race/sync.php diff --git a/app/lib/App/Core/App.php b/app/lib/App/Core/App.php index 8d30419..2cb8820 100644 --- a/app/lib/App/Core/App.php +++ b/app/lib/App/Core/App.php @@ -72,7 +72,7 @@ class App /** * Turn data array into json response */ - public function api(array $data, int $status_code = 200): void + public function api($data, int $status_code = 200): void { // set headers http_response_code($status_code); diff --git a/app/view/pages/race/live-results.php b/app/view/pages/race/live-results.php index 158682e..40a6dfe 100644 --- a/app/view/pages/race/live-results.php +++ b/app/view/pages/race/live-results.php @@ -1,6 +1,6 @@

Live resultater


- +
@@ -8,9 +8,24 @@
Some shit Some shit
\ No newline at end of file diff --git a/public/race/simulator.php b/public/race/simulator.php index 3b409ad..4174d0c 100644 --- a/public/race/simulator.php +++ b/public/race/simulator.php @@ -1,12 +1,14 @@ model('Cardreader'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { - if (isset($_POST['cardnumber']) && strlen($_POST['cardnumber']) !== 0) + if (isset($_POST['cardnumber']) && strlen((string)$_POST['cardnumber']) !== 0) { - $cardnumber = $_POST['cardnumber']; + $cardnumber = (string)$_POST['cardnumber']; try { $code = $cardreader->receive($cardnumber, 5); diff --git a/public/race/stream.php b/public/race/stream.php deleted file mode 100644 index 108591d..0000000 --- a/public/race/stream.php +++ /dev/null @@ -1,39 +0,0 @@ -database->conn); -$timeMapper = new TimeMapper($app->database->conn); - -/** - * Send events to client with Server-Sent Events(SSE) - */ -$sse = new EventLoop(); -$sse->interval = 1; - -$persist_obj = new class { - public ?int $prev_last_insert = NULL; - }; - -$sse->start( - function () use ($timeMapper, $teamMapper, &$persist_obj) { - - $time = $timeMapper->getLatest(); - - if ($time) - { - $last_insert = $time->date->getTimestamp(); - - if ( $persist_obj->prev_last_insert == NULL - || $last_insert > $persist_obj->prev_last_insert - ) { - $persist_obj->prev_last_insert = $last_insert; - - return($timeMapper->getAll()); - } - } - } -); \ No newline at end of file diff --git a/public/race/sync.php b/public/race/sync.php new file mode 100644 index 0000000..1f2217c --- /dev/null +++ b/public/race/sync.php @@ -0,0 +1,27 @@ +database->conn); +$timeMapper = new TimeMapper($app->database->conn); + +$prev_hash = (int)filter_input(INPUT_GET, 'hash'); + +$times = $timeMapper->getAll(); + +$hash = crc32(serialize($timeMapper->getAll())); + +if ($prev_hash !== $hash) +{ + $app->api([ + "hash" => $hash, + "data" => $times + ]); +} +// return nothing +http_response_code(204); \ No newline at end of file