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
|
|
|
|
* 2 counted round
|
|
|
|
* 3 counted too fast
|
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);
|
|
|
|
if ($team)
|
|
|
|
{
|
|
|
|
// team exists, insert into time table
|
2022-03-14 10:36:45 +00:00
|
|
|
// 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;
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-10 15:52:59 +00:00
|
|
|
// team does not exist, lets create it
|
|
|
|
$team = new Team;
|
2022-03-14 10:36:45 +00:00
|
|
|
$team->setCardnumber($cardnumber);
|
2022-03-10 15:52:59 +00:00
|
|
|
$this->teamMapper->create($team);
|
2022-03-14 10:36:45 +00:00
|
|
|
return 0;
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|