diff --git a/app/config.example.php b/app/config.example.php index 7d01dab..fde3593 100644 --- a/app/config.example.php +++ b/app/config.example.php @@ -11,4 +11,5 @@ return [ 'charset' => 'utf8mb4', ], ], + "baton_cooldown" => 30 ]; \ No newline at end of file diff --git a/app/lib/App/Core/User.php b/app/lib/App/Core/User.php index 25d0ef6..cc184e1 100644 --- a/app/lib/App/Core/User.php +++ b/app/lib/App/Core/User.php @@ -62,7 +62,7 @@ class User throw new Exception("Can't get power level without being logged in!"); } $sth = $this->database->conn->prepare( - 'SELECT Nivå FROM brukertabell WHERE Navn = ? AND Passord = ?' + 'SELECT Nivå FROM brukertabell WHERE Brukernavn = ? AND Passord = ?' ); $sth->execute([$this->username, $this->password]); $row = $sth->fetch(PDO::FETCH_ASSOC); @@ -91,7 +91,7 @@ class User private function authenticate(string $username, string $password): bool { $sth = $this->database->conn->prepare( - 'SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?' + 'SELECT * FROM brukertabell WHERE Brukernavn = ? AND Passord = ?' ); $sth->execute([$username, $password]); if ($sth->rowCount()) diff --git a/app/model/BatonReader.php b/app/model/BatonReader.php index 4c36b8c..265c226 100644 --- a/app/model/BatonReader.php +++ b/app/model/BatonReader.php @@ -54,7 +54,7 @@ class BatonReader } $diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp(); - if ($diff <= $timeout) + if ($diff < $timeout) { $this->time_mapper->delete($new_time->id); // i mean... it works? return 2; diff --git a/app/view/pages/race/results.php b/app/view/pages/race/results.php index a944735..4fbbc70 100644 --- a/app/view/pages/race/results.php +++ b/app/view/pages/race/results.php @@ -11,9 +11,13 @@ Denne siden krever JavaScript Laster inn... - + \ No newline at end of file diff --git a/app/view/template/header.php b/app/view/template/header.php index cd0e338..a0f76ec 100644 --- a/app/view/template/header.php +++ b/app/view/template/header.php @@ -4,7 +4,7 @@ - <?=htmlspecialchars($title);?> – Stafett for livet + <?=htmlspecialchars($title);?> – Stafett for livet tellesystem diff --git a/public/api/race/baton.php b/public/api/race/baton.php new file mode 100644 index 0000000..4448be0 --- /dev/null +++ b/public/api/race/baton.php @@ -0,0 +1,12 @@ +model('BatonReader'); + +$cardnumber = (string)filter_input(INPUT_GET, 'cardnumber'); + +$app->api( + $baton_reader->receive( + $cardnumber, + $app->config['baton_cooldown'] + ) +); \ No newline at end of file diff --git a/public/api/race/results.php b/public/api/race/results.php index 7922f53..49b54c4 100644 --- a/public/api/race/results.php +++ b/public/api/race/results.php @@ -1,10 +1,6 @@ getAll() as $time) @@ -33,7 +29,6 @@ foreach ($time_mapper->getAll() as $time) $team_map[$team->id] = [ "name" => htmlspecialchars($team->name), "company" => htmlspecialchars($team->company), - "rounds" => htmlspecialchars($team->rounds), ]; } @@ -47,7 +42,7 @@ foreach ($time_mapper->getAll() as $time) "time" => ($time->date->getTimestamp() - $time_ref) ]; - array_push($times, $row); + array_push($laps, $row); } $data = [ @@ -55,7 +50,7 @@ $data = [ "team" => $team_map, "time_reference" => $time_ref ], - "times" => $times + "laps" => $laps ]; $hash = hash('crc32', serialize($data)); @@ -67,5 +62,5 @@ if ($prev_hash !== $hash) "data" => $data ]); } -// return nothing + http_response_code(204); \ No newline at end of file diff --git a/public/race/configure/simulator.php b/public/race/configure/simulator.php index ae743c6..7a8e4a9 100644 --- a/public/race/configure/simulator.php +++ b/public/race/configure/simulator.php @@ -11,7 +11,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') $cardnumber = (string)$_POST['cardnumber']; try { - $code = $baton_reader->receive($cardnumber, -1); + $code = $baton_reader->receive($cardnumber, $app->config['baton_cooldown']); switch ($code) { case 0: $app->session->flash('Opprettet nytt lag', 'success'); diff --git a/public/static/js/ResultService.js b/public/static/js/ResultService.js index 7b52459..f9f593b 100644 --- a/public/static/js/ResultService.js +++ b/public/static/js/ResultService.js @@ -1,21 +1,35 @@ -class ResultService -{ - constructor(alertBox, rankingTable, endpoint) - { - this.alertBox = alertBox; +/** + * Yea i know but it works for now + */ + +class ResultService { + + constructor(alertBox, rankingTable, endpoint) { + + this.alertBox = alertBox; + this.rankingTable = rankingTable; - this.endpoint = endpoint; + + this.endpoint = endpoint; + this.hash = 0; + this.loop(); } - async loop() - { + async loop() { try { + await this.updateTable(); + this.alertBox.classList.add("hidden"); + } catch (error) { + console.log(error); + + this.hash = 0; + this.alertBox.innerHTML = "Noe gikk galt: " + error; this.alertBox.classList.remove("hidden"); } @@ -25,21 +39,54 @@ class ResultService }, 1000); } - async updateTable() - { + async updateTable() { + let response = await fetch(this.endpoint + this.hash); - if (response.status === 204) - { + if (response.status === 204) { return; } - let json = await response.json(); + let JSONResponse = await response.json(); - this.hash = json.hash; + this.hash = JSONResponse.hash; - let data = json.data; - data.times = data.times.reverse(); + let tableRows = []; + let tempLaps = []; + JSONResponse.data.laps.forEach(lap => { + + let roundTripTime = null; + + let prevLap = tempLaps[lap.id]; + + let round = 0; + + if (prevLap) { + roundTripTime = lap.time - prevLap.time; + + prevLap.round += 1; + + lap.round = prevLap.round; + + round = lap.round; + } else { + lap.round = 0; + } + + tempLaps[lap.id] = lap; + + tableRows.push({ + team: { + name: JSONResponse.data.map.team[lap.id].name, + company: JSONResponse.data.map.team[lap.id].company + }, + trip: roundTripTime, + time: lap.time + JSONResponse.data.map.time_reference, + round: round + }); + }); + + tableRows = tableRows.reverse(); let tableHTML = ""; tableHTML += ""; @@ -47,20 +94,22 @@ class ResultService tableHTML += ""; tableHTML += ""; tableHTML += ""; + tableHTML += ""; tableHTML += ""; - tableHTML += ""; + tableHTML += ""; tableHTML += ""; - let iterator = 0; - data.times.forEach(element => { - iterator++; + let iterator = tableRows.length + 1; + tableRows.forEach(row => { + iterator--; tableHTML += ""; tableHTML += ""; - tableHTML += ""; - tableHTML += ""; - tableHTML += ""; - tableHTML += ""; + tableHTML += ""; + tableHTML += ""; + tableHTML += ""; + tableHTML += ""; + tableHTML += ""; tableHTML += ""; }); @@ -68,4 +117,5 @@ class ResultService this.rankingTable.innerHTML = tableHTML; } + }
#LagBedriftRundeTidRunderPassert
" + iterator + "" + data.map.team[element.id].name + "" + data.map.team[element.id].company + "" + element.time + "" + data.map.team[element.id].rounds+ "" + row.team.name + "" + row.team.company + "" + ((row.round === 0) ? 'START' : row.round) + "" + ((row.trip === null) ? 'START' : row.trip) + "" + (new Date(row.time * 1000).toLocaleString()) + "