From 77ce3af5ac3a2c4810717457eaef30e472ecbf08 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 14 Mar 2022 11:36:45 +0100 Subject: [PATCH] Commit --- app/lib/App/Teamtable/Team.php | 1 + app/lib/App/Teamtable/TeamMapper.php | 11 +++--- app/lib/App/Timetable/Time.php | 12 +++++++ app/lib/App/Timetable/TimeMapper.php | 33 +++++++++++++++-- app/model/Cardreader.php | 48 +++++++++++++++++++++---- app/view/pages/teamtable/edit/index.php | 2 +- public/simulator.php | 28 ++++++++++++--- 7 files changed, 116 insertions(+), 19 deletions(-) diff --git a/app/lib/App/Teamtable/Team.php b/app/lib/App/Teamtable/Team.php index 81e9216..540f3b6 100644 --- a/app/lib/App/Teamtable/Team.php +++ b/app/lib/App/Teamtable/Team.php @@ -17,6 +17,7 @@ class Team public int $phone = 0; public int $participants = 0; public int $rounds = 0; + public ?int $bestTime = NULL; /** * PHP by default does not include multi byte functions. Therefore we use this diff --git a/app/lib/App/Teamtable/TeamMapper.php b/app/lib/App/Teamtable/TeamMapper.php index ada2743..eb2c744 100644 --- a/app/lib/App/Teamtable/TeamMapper.php +++ b/app/lib/App/Teamtable/TeamMapper.php @@ -29,6 +29,7 @@ class TeamMapper $team->setPhone($row['Telefon']); $team->setParticipants($row['Deltagere']); $team->setRounds($row['Runder']); + $team->bestTime = $row['Bestetid']; return $team; } @@ -82,9 +83,9 @@ class TeamMapper { $sth = $this->dbh->prepare( 'INSERT INTO lagtabell - (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder) + (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder, Bestetid) VALUES - (?, ?, ?, ?, ?, ?, ?)' + (?, ?, ?, ?, ?, ?, ?, ?)' ); $sth->execute([ $team->name, @@ -93,7 +94,8 @@ class TeamMapper $team->leader, $team->phone, $team->participants, - $team->rounds + $team->rounds, + $team->bestTime ]); $lastId = $this->dbh->lastInsertId(); return $this->get($lastId); @@ -105,7 +107,7 @@ class TeamMapper 'UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, - Runder = ? + Runder = ?, Bestetid = ? WHERE LagID = ?' ); @@ -117,6 +119,7 @@ class TeamMapper $team->phone, $team->participants, $team->rounds, + $team->bestTime, $team->id ]); return $this->get($team->id); diff --git a/app/lib/App/Timetable/Time.php b/app/lib/App/Timetable/Time.php index 3ea60df..51c7a2d 100644 --- a/app/lib/App/Timetable/Time.php +++ b/app/lib/App/Timetable/Time.php @@ -12,4 +12,16 @@ class Time public int $id; public int $teamId; 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; + } } \ No newline at end of file diff --git a/app/lib/App/Timetable/TimeMapper.php b/app/lib/App/Timetable/TimeMapper.php index 317ea80..436cb6b 100644 --- a/app/lib/App/Timetable/TimeMapper.php +++ b/app/lib/App/Timetable/TimeMapper.php @@ -3,6 +3,7 @@ namespace App\Timetable; use \PDO; +use \DateTime; class TimeMapper { @@ -13,16 +14,44 @@ class TimeMapper $this->dbh = $dbh; } - private function mapRowToTeam(array $row): Time + private function mapRowToTime(array $row): Time { $team = new Time(); + $team->id = $row['TidID']; + $team->setTeamId($row['LagID']); + $team->setDate(new DateTime($row['Tidspunkt'])); 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 { $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); $sth->execute([$time->teamId]); - return TRUE; + $lastId = $this->dbh->lastInsertId(); + return $this->get($lastId); } } \ No newline at end of file diff --git a/app/model/Cardreader.php b/app/model/Cardreader.php index 702c136..2e4bb5f 100644 --- a/app/model/Cardreader.php +++ b/app/model/Cardreader.php @@ -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); if ($team) { // team exists, insert into time table - $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); - $sth->execute([$team->id]); - return TRUE; + // and update team table best time + $prev_time = $this->timeMapper->getLatestByTeamId($team->id); + + $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 = new Team; - $team->cardnumber = $cardnumber; + $team->setCardnumber($cardnumber); $this->teamMapper->create($team); - return FALSE; + return 0; } } \ No newline at end of file diff --git a/app/view/pages/teamtable/edit/index.php b/app/view/pages/teamtable/edit/index.php index b299163..dbce1f4 100644 --- a/app/view/pages/teamtable/edit/index.php +++ b/app/view/pages/teamtable/edit/index.php @@ -27,7 +27,7 @@ echo "" . htmlspecialchars($team->phone) . ""; echo "" . htmlspecialchars($team->participants) . ""; echo "" . htmlspecialchars($team->rounds) . ""; - echo "" . "Ukjent" . ""; + echo "" . htmlspecialchars(($team->bestTime === NULL) ? "Ukjent" : $team->bestTime) . ""; echo ""; echo "Slett ]"; echo "Endre ]"; diff --git a/public/simulator.php b/public/simulator.php index f80e99e..9818a36 100644 --- a/public/simulator.php +++ b/public/simulator.php @@ -11,12 +11,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') $cardnumber = $_POST['cardnumber']; if (!(strlen($cardnumber) > 32)) { - if ($cardreader->receive($cardnumber)) - { - $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); - } else { - $app->session->flash("Opprettet lag for \"{$cardnumber}\""); + + $code = $cardreader->receive($cardnumber, 30); + switch ($code) { + case 0: + $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 { $app->session->flash('Kortnummer for langt!', 'danger'); }