diff --git a/app/lib/App/Timetable/TimeMapper.php b/app/lib/App/Timetable/TimeMapper.php index 436cb6b..8769ea9 100644 --- a/app/lib/App/Timetable/TimeMapper.php +++ b/app/lib/App/Timetable/TimeMapper.php @@ -54,4 +54,10 @@ class TimeMapper $lastId = $this->dbh->lastInsertId(); return $this->get($lastId); } + + public function delete(int $id): void + { + $sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE TidID = ?'); + $sth->execute([$id]); + } } \ No newline at end of file diff --git a/app/model/Cardreader.php b/app/model/Cardreader.php index fdaa905..12763d9 100644 --- a/app/model/Cardreader.php +++ b/app/model/Cardreader.php @@ -22,60 +22,60 @@ class Cardreader } /** - * TODO: yeah this sucks but it werks! * Returns: * 0 created team * 1 started counting - * 2 counted round - * 3 counted too fast + * 2 counted too fast! + * 3 counted round + * 4 counted round, new best! */ public function receive(string $cardnumber, int $timeout): int { $team = $this->teamMapper->getByCardnumber($cardnumber); - if ($team) + if (!$team) { - // team exists, insert into time table - // and update team table best time - $prev_time = $this->timeMapper->getLatestByTeamId($team->id); + // team does not exist, lets create it + $team = new Team; + $team->setCardnumber($cardnumber); + $this->teamMapper->create($team); + return 0; + } - $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); + // team exists, insert into time table + // and update team best time + $prev_time = $this->timeMapper->getLatestByTeamId($team->id); + $new_time = new Time; + $new_time->setTeamId($team->id); + $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; - } - - $team->rounds += 1; - $this->teamMapper->update($team); - - if ($team->bestTime === NULL) - { - $team->bestTime = $diff; - $this->teamMapper->update($team); - } - - if ($diff < $team->bestTime) - { - $team->bestTime = $diff; - $this->teamMapper->update($team); - } - return 2; - } + if ($prev_time === NULL) + { + // team has not ran previously return 1; } - // team does not exist, lets create it - $team = new Team; - $team->setCardnumber($cardnumber); - $this->teamMapper->create($team); - return 0; + $diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp(); + if ($diff <= $timeout) + { + $this->timeMapper->delete($new_time->id); // i mean... it works? + return 2; + } + + $team->rounds += 1; + $this->teamMapper->update($team); + + if ($team->bestTime === NULL) + { + $team->bestTime = $diff; + $this->teamMapper->update($team); + } + + if ($diff < $team->bestTime) + { + $team->bestTime = $diff; + $this->teamMapper->update($team); + return 4; + } + return 3; } } \ No newline at end of file diff --git a/public/race/simulator.php b/public/race/simulator.php index 874f40d..301d217 100644 --- a/public/race/simulator.php +++ b/public/race/simulator.php @@ -19,15 +19,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') break; case 1: - $app->session->flash('Startet telling', 'success'); + $app->session->flash('Startet telling', 'info'); break; case 2: - $app->session->flash('Passerte en runde', 'success'); + $app->session->flash('Runde gikk for fort!', 'danger'); break; case 3: - $app->session->flash('Runde gikk for fort!', 'danger'); + $app->session->flash('Passerte en runde', 'info'); + break; + + case 4: + $app->session->flash('Passerte en runde, ny rekord!', 'success'); break; default: