This commit is contained in:
William 2022-03-14 11:36:45 +01:00
parent 800c5fd366
commit 77ce3af5ac
7 changed files with 116 additions and 19 deletions

View File

@ -17,6 +17,7 @@ class Team
public int $phone = 0; public int $phone = 0;
public int $participants = 0; public int $participants = 0;
public int $rounds = 0; public int $rounds = 0;
public ?int $bestTime = NULL;
/** /**
* PHP by default does not include multi byte functions. Therefore we use this * PHP by default does not include multi byte functions. Therefore we use this

View File

@ -29,6 +29,7 @@ class TeamMapper
$team->setPhone($row['Telefon']); $team->setPhone($row['Telefon']);
$team->setParticipants($row['Deltagere']); $team->setParticipants($row['Deltagere']);
$team->setRounds($row['Runder']); $team->setRounds($row['Runder']);
$team->bestTime = $row['Bestetid'];
return $team; return $team;
} }
@ -82,9 +83,9 @@ class TeamMapper
{ {
$sth = $this->dbh->prepare( $sth = $this->dbh->prepare(
'INSERT INTO lagtabell 'INSERT INTO lagtabell
(LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder) (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder, Bestetid)
VALUES VALUES
(?, ?, ?, ?, ?, ?, ?)' (?, ?, ?, ?, ?, ?, ?, ?)'
); );
$sth->execute([ $sth->execute([
$team->name, $team->name,
@ -93,7 +94,8 @@ class TeamMapper
$team->leader, $team->leader,
$team->phone, $team->phone,
$team->participants, $team->participants,
$team->rounds $team->rounds,
$team->bestTime
]); ]);
$lastId = $this->dbh->lastInsertId(); $lastId = $this->dbh->lastInsertId();
return $this->get($lastId); return $this->get($lastId);
@ -105,7 +107,7 @@ class TeamMapper
'UPDATE lagtabell SET 'UPDATE lagtabell SET
LagNavn = ?, Bedrift = ?, Kortnummer = ?, LagNavn = ?, Bedrift = ?, Kortnummer = ?,
Lagleder = ?, Telefon = ?, Deltagere = ?, Lagleder = ?, Telefon = ?, Deltagere = ?,
Runder = ? Runder = ?, Bestetid = ?
WHERE WHERE
LagID = ?' LagID = ?'
); );
@ -117,6 +119,7 @@ class TeamMapper
$team->phone, $team->phone,
$team->participants, $team->participants,
$team->rounds, $team->rounds,
$team->bestTime,
$team->id $team->id
]); ]);
return $this->get($team->id); return $this->get($team->id);

View File

@ -12,4 +12,16 @@ class Time
public int $id; public int $id;
public int $teamId; public int $teamId;
public DateTime $date; public DateTime $date;
public function setTeamId(int $teamId): Self
{
$this->teamId = $teamId;
return $this;
}
public function setDate(DateTime $date): Self
{
$this->date = $date;
return $this;
}
} }

View File

@ -3,6 +3,7 @@
namespace App\Timetable; namespace App\Timetable;
use \PDO; use \PDO;
use \DateTime;
class TimeMapper class TimeMapper
{ {
@ -13,16 +14,44 @@ class TimeMapper
$this->dbh = $dbh; $this->dbh = $dbh;
} }
private function mapRowToTeam(array $row): Time private function mapRowToTime(array $row): Time
{ {
$team = new Time(); $team = new Time();
$team->id = $row['TidID'];
$team->setTeamId($row['LagID']);
$team->setDate(new DateTime($row['Tidspunkt']));
return $team; return $team;
} }
public function getLatestByTeamId(int $teamId): ?Time
{
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE LagID = ? ORDER BY Tidspunkt DESC');
$sth->execute([$teamId]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTime($row);
}
return NULL;
}
public function get(int $id): ?Time
{
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE TidID = ?');
$sth->execute([$id]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTime($row);
}
return NULL;
}
public function create(Time $time): Time public function create(Time $time): Time
{ {
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
$sth->execute([$time->teamId]); $sth->execute([$time->teamId]);
return TRUE; $lastId = $this->dbh->lastInsertId();
return $this->get($lastId);
} }
} }

View File

@ -22,22 +22,56 @@ class Cardreader
} }
/** /**
* Returns TRUE if team exists, FALSE if not * Returns:
* 0 created team
* 1 started counting
* 2 counted round
* 3 counted too fast
*/ */
public function receive(string $cardnumber, int $timeout): bool public function receive(string $cardnumber, int $timeout): int
{ {
$team = $this->teamMapper->getByCardnumber($cardnumber); $team = $this->teamMapper->getByCardnumber($cardnumber);
if ($team) if ($team)
{ {
// team exists, insert into time table // team exists, insert into time table
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); // and update team table best time
$sth->execute([$team->id]); $prev_time = $this->timeMapper->getLatestByTeamId($team->id);
return TRUE;
$new_time = new Time;
$new_time->setTeamId($team->id);
// TODO: have this happen later so that it does not count when timeout
$new_time = $this->timeMapper->create($new_time);
// calculate best time for this team
if ($prev_time !== NULL)
{
$diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp();
if ($diff <= $timeout)
{
return 3;
}
if ($team->bestTime === NULL)
{
$team->bestTime = $diff;
$this->teamMapper->update($team);
}
if ($diff < $team->bestTime)
{
$team->bestTime = $diff;
$this->teamMapper->update($team);
}
return 2;
}
return 1;
} }
// team does not exist, lets create it // team does not exist, lets create it
$team = new Team; $team = new Team;
$team->cardnumber = $cardnumber; $team->setCardnumber($cardnumber);
$this->teamMapper->create($team); $this->teamMapper->create($team);
return FALSE; return 0;
} }
} }

View File

@ -27,7 +27,7 @@
echo "<td>" . htmlspecialchars($team->phone) . "</td>"; echo "<td>" . htmlspecialchars($team->phone) . "</td>";
echo "<td>" . htmlspecialchars($team->participants) . "</td>"; echo "<td>" . htmlspecialchars($team->participants) . "</td>";
echo "<td>" . htmlspecialchars($team->rounds) . "</td>"; echo "<td>" . htmlspecialchars($team->rounds) . "</td>";
echo "<td>" . "Ukjent" . "</td>"; echo "<td>" . htmlspecialchars(($team->bestTime === NULL) ? "Ukjent" : $team->bestTime) . "</td>";
echo "<td>"; echo "<td>";
echo "<span>[&nbsp;<a class='danger' href='delete.php?item={$team->id}&confirm=1'>Slett</a>&nbsp;]</span>"; echo "<span>[&nbsp;<a class='danger' href='delete.php?item={$team->id}&confirm=1'>Slett</a>&nbsp;]</span>";
echo "<span>[&nbsp;<a class='info' href='update.php?item={$team->id}'>Endre</a>&nbsp;]</span>"; echo "<span>[&nbsp;<a class='info' href='update.php?item={$team->id}'>Endre</a>&nbsp;]</span>";

View File

@ -11,12 +11,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
$cardnumber = $_POST['cardnumber']; $cardnumber = $_POST['cardnumber'];
if (!(strlen($cardnumber) > 32)) { if (!(strlen($cardnumber) > 32)) {
if ($cardreader->receive($cardnumber))
{ $code = $cardreader->receive($cardnumber, 30);
$app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); switch ($code) {
} else { case 0:
$app->session->flash("Opprettet lag for \"{$cardnumber}\""); $app->session->flash('Opprettet nytt lag', 'success');
break;
case 1:
$app->session->flash('Startet telling', 'success');
break;
case 2:
$app->session->flash('Passerte en runde', 'success');
break;
case 3:
$app->session->flash('Runde gikk for fort!', 'danger');
break;
default:
$app->session->flash('Uhhh?? Dette skulle ikke skje', 'danger');
break;
} }
} else { } else {
$app->session->flash('Kortnummer for langt!', 'danger'); $app->session->flash('Kortnummer for langt!', 'danger');
} }