2022-03-10 15:52:59 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-13 19:54:34 +00:00
|
|
|
use App\Core\Database;
|
|
|
|
use App\Teamtable\Team;
|
|
|
|
use App\Teamtable\TeamMapper;
|
|
|
|
use App\Timetable\Time;
|
|
|
|
use App\Timetable\TimeMapper;
|
2022-03-10 15:52:59 +00:00
|
|
|
|
|
|
|
class Cardreader
|
|
|
|
{
|
|
|
|
public PDO $dbh;
|
|
|
|
|
|
|
|
public TeamMapper $teamMapper;
|
|
|
|
|
2022-03-13 19:54:34 +00:00
|
|
|
public TimeMapper $timeMapper;
|
|
|
|
|
2022-03-10 15:52:59 +00:00
|
|
|
public function __construct(Database $database)
|
|
|
|
{
|
|
|
|
$this->dbh = $database->conn;
|
|
|
|
$this->teamMapper = new TeamMapper($this->dbh);
|
2022-03-13 19:54:34 +00:00
|
|
|
$this->timeMapper = new TimeMapper($this->dbh);
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-14 10:36:45 +00:00
|
|
|
* Returns:
|
|
|
|
* 0 created team
|
|
|
|
* 1 started counting
|
2022-03-16 12:37:24 +00:00
|
|
|
* 2 counted too fast!
|
|
|
|
* 3 counted round
|
|
|
|
* 4 counted round, new best!
|
2022-03-10 15:52:59 +00:00
|
|
|
*/
|
2022-03-14 10:36:45 +00:00
|
|
|
public function receive(string $cardnumber, int $timeout): int
|
2022-03-10 15:52:59 +00:00
|
|
|
{
|
|
|
|
$team = $this->teamMapper->getByCardnumber($cardnumber);
|
2022-03-16 12:37:24 +00:00
|
|
|
if (!$team)
|
2022-03-10 15:52:59 +00:00
|
|
|
{
|
2022-03-16 12:37:24 +00:00
|
|
|
// team does not exist, lets create it
|
|
|
|
$team = new Team;
|
|
|
|
$team->setCardnumber($cardnumber);
|
|
|
|
$this->teamMapper->create($team);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
// 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);
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
if ($prev_time === NULL)
|
|
|
|
{
|
|
|
|
// team has not ran previously
|
|
|
|
return 1;
|
|
|
|
}
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
$diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp();
|
|
|
|
if ($diff <= $timeout)
|
|
|
|
{
|
|
|
|
$this->timeMapper->delete($new_time->id); // i mean... it works?
|
|
|
|
return 2;
|
|
|
|
}
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
$team->rounds += 1;
|
|
|
|
$this->teamMapper->update($team);
|
2022-03-16 11:43:21 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
if ($team->bestTime === NULL)
|
|
|
|
{
|
|
|
|
$team->bestTime = $diff;
|
|
|
|
$this->teamMapper->update($team);
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
2022-03-16 12:37:24 +00:00
|
|
|
|
|
|
|
if ($diff < $team->bestTime)
|
|
|
|
{
|
|
|
|
$team->bestTime = $diff;
|
|
|
|
$this->teamMapper->update($team);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
return 3;
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|