This commit is contained in:
William 2022-05-08 20:00:11 +00:00
parent 32d3733fd6
commit 4205af4e8c
9 changed files with 103 additions and 41 deletions

View File

@ -11,4 +11,5 @@ return [
'charset' => 'utf8mb4', 'charset' => 'utf8mb4',
], ],
], ],
"baton_cooldown" => 30
]; ];

View File

@ -62,7 +62,7 @@ class User
throw new Exception("Can't get power level without being logged in!"); throw new Exception("Can't get power level without being logged in!");
} }
$sth = $this->database->conn->prepare( $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]); $sth->execute([$this->username, $this->password]);
$row = $sth->fetch(PDO::FETCH_ASSOC); $row = $sth->fetch(PDO::FETCH_ASSOC);
@ -91,7 +91,7 @@ class User
private function authenticate(string $username, string $password): bool private function authenticate(string $username, string $password): bool
{ {
$sth = $this->database->conn->prepare( $sth = $this->database->conn->prepare(
'SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?' 'SELECT * FROM brukertabell WHERE Brukernavn = ? AND Passord = ?'
); );
$sth->execute([$username, $password]); $sth->execute([$username, $password]);
if ($sth->rowCount()) if ($sth->rowCount())

View File

@ -54,7 +54,7 @@ class BatonReader
} }
$diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp(); $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? $this->time_mapper->delete($new_time->id); // i mean... it works?
return 2; return 2;

View File

@ -11,9 +11,13 @@ Denne siden krever JavaScript
Laster inn... Laster inn...
</div> </div>
<script src="../static/js/ResultService.js"></script> <script src="<?=$this->config['root_url']?>static/js/ResultService.js"></script>
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
new ResultService(alertBox, rankingTable, '../api/race/results.php?h=');
alertBox = document.getElementById("alertBox");
rankingTable = document.getElementById("rankingTable");
new ResultService(alertBox, rankingTable, '<?=$this->config['root_url']?>api/race/results.php?h=');
}); });
</script> </script>

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?=htmlspecialchars($title);?> Stafett for livet</title> <title><?=htmlspecialchars($title);?> Stafett for livet tellesystem</title>
<link rel="stylesheet" href="<?=$this->config['root_url']?>static/style/main.css"> <link rel="stylesheet" href="<?=$this->config['root_url']?>static/style/main.css">
<link rel="icon" href="<?=$this->config['root_url']?>static/img/cropped-kf-propell-ikon-32x32.png"> <link rel="icon" href="<?=$this->config['root_url']?>static/img/cropped-kf-propell-ikon-32x32.png">

12
public/api/race/baton.php Normal file
View File

@ -0,0 +1,12 @@
<?php require '../../../app/inc.php';
$baton_reader = $app->model('BatonReader');
$cardnumber = (string)filter_input(INPUT_GET, 'cardnumber');
$app->api(
$baton_reader->receive(
$cardnumber,
$app->config['baton_cooldown']
)
);

View File

@ -1,10 +1,6 @@
<?php $app = require '../../../app/inc.php'; <?php $app = require '../../../app/inc.php';
/** /**
* We originally wanted to use SSE for this, but the hosting provider * Magique
* 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\Teamtable\TeamMapper;
@ -17,7 +13,7 @@ $prev_hash = filter_input(INPUT_GET, 'h');
$teams = []; $teams = [];
$team_map = []; $team_map = [];
$times = []; $laps = [];
$time_ref = NULL; $time_ref = NULL;
foreach ($time_mapper->getAll() as $time) foreach ($time_mapper->getAll() as $time)
@ -33,7 +29,6 @@ foreach ($time_mapper->getAll() as $time)
$team_map[$team->id] = [ $team_map[$team->id] = [
"name" => htmlspecialchars($team->name), "name" => htmlspecialchars($team->name),
"company" => htmlspecialchars($team->company), "company" => htmlspecialchars($team->company),
"rounds" => htmlspecialchars($team->rounds),
]; ];
} }
@ -47,7 +42,7 @@ foreach ($time_mapper->getAll() as $time)
"time" => ($time->date->getTimestamp() - $time_ref) "time" => ($time->date->getTimestamp() - $time_ref)
]; ];
array_push($times, $row); array_push($laps, $row);
} }
$data = [ $data = [
@ -55,7 +50,7 @@ $data = [
"team" => $team_map, "team" => $team_map,
"time_reference" => $time_ref "time_reference" => $time_ref
], ],
"times" => $times "laps" => $laps
]; ];
$hash = hash('crc32', serialize($data)); $hash = hash('crc32', serialize($data));
@ -67,5 +62,5 @@ if ($prev_hash !== $hash)
"data" => $data "data" => $data
]); ]);
} }
// return nothing
http_response_code(204); http_response_code(204);

View File

@ -11,7 +11,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
$cardnumber = (string)$_POST['cardnumber']; $cardnumber = (string)$_POST['cardnumber'];
try { try {
$code = $baton_reader->receive($cardnumber, -1); $code = $baton_reader->receive($cardnumber, $app->config['baton_cooldown']);
switch ($code) { switch ($code) {
case 0: case 0:
$app->session->flash('Opprettet nytt lag', 'success'); $app->session->flash('Opprettet nytt lag', 'success');

View File

@ -1,21 +1,35 @@
class ResultService /**
{ * Yea i know but it works for now
constructor(alertBox, rankingTable, endpoint) */
{
class ResultService {
constructor(alertBox, rankingTable, endpoint) {
this.alertBox = alertBox; this.alertBox = alertBox;
this.rankingTable = rankingTable; this.rankingTable = rankingTable;
this.endpoint = endpoint; this.endpoint = endpoint;
this.hash = 0; this.hash = 0;
this.loop(); this.loop();
} }
async loop() async loop() {
{
try { try {
await this.updateTable(); await this.updateTable();
this.alertBox.classList.add("hidden"); this.alertBox.classList.add("hidden");
} catch (error) { } catch (error) {
console.log(error); console.log(error);
this.hash = 0;
this.alertBox.innerHTML = "<b>Noe gikk galt: </b>" + error; this.alertBox.innerHTML = "<b>Noe gikk galt: </b>" + error;
this.alertBox.classList.remove("hidden"); this.alertBox.classList.remove("hidden");
} }
@ -25,21 +39,54 @@ class ResultService
}, 1000); }, 1000);
} }
async updateTable() async updateTable() {
{
let response = await fetch(this.endpoint + this.hash); let response = await fetch(this.endpoint + this.hash);
if (response.status === 204) if (response.status === 204) {
{
return; return;
} }
let json = await response.json(); let JSONResponse = await response.json();
this.hash = json.hash; this.hash = JSONResponse.hash;
let data = json.data; let tableRows = [];
data.times = data.times.reverse(); 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 = ""; let tableHTML = "";
tableHTML += "<table>"; tableHTML += "<table>";
@ -47,20 +94,22 @@ class ResultService
tableHTML += "<th>#</th>"; tableHTML += "<th>#</th>";
tableHTML += "<th>Lag</th>"; tableHTML += "<th>Lag</th>";
tableHTML += "<th>Bedrift</th>"; tableHTML += "<th>Bedrift</th>";
tableHTML += "<th>Runde</th>";
tableHTML += "<th>Tid</th>"; tableHTML += "<th>Tid</th>";
tableHTML += "<th>Runder</th>"; tableHTML += "<th>Passert</th>";
tableHTML += "</tr>"; tableHTML += "</tr>";
let iterator = 0; let iterator = tableRows.length + 1;
data.times.forEach(element => { tableRows.forEach(row => {
iterator++; iterator--;
tableHTML += "<tr>"; tableHTML += "<tr>";
tableHTML += "<td>" + iterator + "</td>"; tableHTML += "<td>" + iterator + "</td>";
tableHTML += "<td>" + data.map.team[element.id].name + "</td>"; tableHTML += "<td>" + row.team.name + "</td>";
tableHTML += "<td>" + data.map.team[element.id].company + "</td>"; tableHTML += "<td>" + row.team.company + "</td>";
tableHTML += "<td>" + element.time + "</td>"; tableHTML += "<td>" + ((row.round === 0) ? 'START' : row.round) + "</td>";
tableHTML += "<td>" + data.map.team[element.id].rounds+ "</td>"; tableHTML += "<td>" + ((row.trip === null) ? 'START' : row.trip) + "</td>";
tableHTML += "<td>" + (new Date(row.time * 1000).toLocaleString()) + "</td>";
tableHTML += "</tr>"; tableHTML += "</tr>";
}); });
@ -68,4 +117,5 @@ class ResultService
this.rankingTable.innerHTML = tableHTML; this.rankingTable.innerHTML = tableHTML;
} }
} }