Commit
This commit is contained in:
parent
32d3733fd6
commit
4205af4e8c
@ -11,4 +11,5 @@ return [
|
||||
'charset' => 'utf8mb4',
|
||||
],
|
||||
],
|
||||
"baton_cooldown" => 30
|
||||
];
|
@ -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())
|
||||
|
@ -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;
|
||||
|
@ -11,9 +11,13 @@ Denne siden krever JavaScript
|
||||
Laster inn...
|
||||
</div>
|
||||
|
||||
<script src="../static/js/ResultService.js"></script>
|
||||
<script src="<?=$this->config['root_url']?>static/js/ResultService.js"></script>
|
||||
<script>
|
||||
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>
|
@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<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="icon" href="<?=$this->config['root_url']?>static/img/cropped-kf-propell-ikon-32x32.png">
|
||||
|
12
public/api/race/baton.php
Normal file
12
public/api/race/baton.php
Normal 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']
|
||||
)
|
||||
);
|
@ -1,10 +1,6 @@
|
||||
<?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.
|
||||
* Magique
|
||||
*/
|
||||
|
||||
use App\Teamtable\TeamMapper;
|
||||
@ -17,7 +13,7 @@ $prev_hash = filter_input(INPUT_GET, 'h');
|
||||
|
||||
$teams = [];
|
||||
$team_map = [];
|
||||
$times = [];
|
||||
$laps = [];
|
||||
$time_ref = NULL;
|
||||
|
||||
foreach ($time_mapper->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);
|
@ -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');
|
||||
|
@ -1,21 +1,35 @@
|
||||
class ResultService
|
||||
{
|
||||
constructor(alertBox, rankingTable, endpoint)
|
||||
{
|
||||
/**
|
||||
* Yea i know but it works for now
|
||||
*/
|
||||
|
||||
class ResultService {
|
||||
|
||||
constructor(alertBox, rankingTable, endpoint) {
|
||||
|
||||
this.alertBox = alertBox;
|
||||
|
||||
this.rankingTable = rankingTable;
|
||||
|
||||
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 = "<b>Noe gikk galt: </b>" + 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 += "<table>";
|
||||
@ -47,20 +94,22 @@ class ResultService
|
||||
tableHTML += "<th>#</th>";
|
||||
tableHTML += "<th>Lag</th>";
|
||||
tableHTML += "<th>Bedrift</th>";
|
||||
tableHTML += "<th>Runde</th>";
|
||||
tableHTML += "<th>Tid</th>";
|
||||
tableHTML += "<th>Runder</th>";
|
||||
tableHTML += "<th>Passert</th>";
|
||||
tableHTML += "</tr>";
|
||||
|
||||
let iterator = 0;
|
||||
data.times.forEach(element => {
|
||||
iterator++;
|
||||
let iterator = tableRows.length + 1;
|
||||
tableRows.forEach(row => {
|
||||
iterator--;
|
||||
|
||||
tableHTML += "<tr>";
|
||||
tableHTML += "<td>" + iterator + "</td>";
|
||||
tableHTML += "<td>" + data.map.team[element.id].name + "</td>";
|
||||
tableHTML += "<td>" + data.map.team[element.id].company + "</td>";
|
||||
tableHTML += "<td>" + element.time + "</td>";
|
||||
tableHTML += "<td>" + data.map.team[element.id].rounds+ "</td>";
|
||||
tableHTML += "<td>" + row.team.name + "</td>";
|
||||
tableHTML += "<td>" + row.team.company + "</td>";
|
||||
tableHTML += "<td>" + ((row.round === 0) ? 'START' : row.round) + "</td>";
|
||||
tableHTML += "<td>" + ((row.trip === null) ? 'START' : row.trip) + "</td>";
|
||||
tableHTML += "<td>" + (new Date(row.time * 1000).toLocaleString()) + "</td>";
|
||||
tableHTML += "</tr>";
|
||||
});
|
||||
|
||||
@ -68,4 +117,5 @@ class ResultService
|
||||
|
||||
this.rankingTable.innerHTML = tableHTML;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user