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
|
|
|
|
2022-03-30 19:42:50 +00:00
|
|
|
class BatonReader
|
2022-03-10 15:52:59 +00:00
|
|
|
{
|
|
|
|
public PDO $dbh;
|
|
|
|
|
2022-04-14 20:59:42 +00:00
|
|
|
public TeamMapper $team_mapper;
|
|
|
|
public TimeMapper $time_mapper;
|
2022-03-13 19:54:34 +00:00
|
|
|
|
2022-03-10 15:52:59 +00:00
|
|
|
public function __construct(Database $database)
|
|
|
|
{
|
|
|
|
$this->dbh = $database->conn;
|
2022-04-14 20:59:42 +00:00
|
|
|
$this->team_mapper = new TeamMapper($this->dbh);
|
|
|
|
$this->time_mapper = 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
|
|
|
{
|
2022-04-14 20:59:42 +00:00
|
|
|
$team = $this->team_mapper->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);
|
2022-04-14 20:59:42 +00:00
|
|
|
$this->team_mapper->create($team);
|
2022-03-16 12:37:24 +00:00
|
|
|
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
|
2022-04-14 20:59:42 +00:00
|
|
|
$prev_time = $this->time_mapper->getLatestByTeamId($team->id);
|
2022-03-16 12:37:24 +00:00
|
|
|
$new_time = new Time;
|
|
|
|
$new_time->setTeamId($team->id);
|
2022-04-14 20:59:42 +00:00
|
|
|
$new_time = $this->time_mapper->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)
|
|
|
|
{
|
2022-04-14 20:59:42 +00:00
|
|
|
$this->time_mapper->delete($new_time->id); // i mean... it works?
|
2022-03-16 12:37:24 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2022-03-14 10:36:45 +00:00
|
|
|
|
2022-03-16 12:37:24 +00:00
|
|
|
$team->rounds += 1;
|
2022-04-14 20:59:42 +00:00
|
|
|
$this->team_mapper->update($team);
|
2022-03-16 11:43:21 +00:00
|
|
|
|
2022-04-14 20:59:42 +00:00
|
|
|
if ($team->best_time === NULL)
|
2022-03-16 12:37:24 +00:00
|
|
|
{
|
2022-04-14 20:59:42 +00:00
|
|
|
$team->best_time = $diff;
|
|
|
|
$this->team_mapper->update($team);
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
2022-03-16 12:37:24 +00:00
|
|
|
|
2022-04-14 20:59:42 +00:00
|
|
|
if ($diff < $team->best_time)
|
2022-03-16 12:37:24 +00:00
|
|
|
{
|
2022-04-14 20:59:42 +00:00
|
|
|
$team->best_time = $diff;
|
|
|
|
$this->team_mapper->update($team);
|
2022-03-16 12:37:24 +00:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
return 3;
|
2022-03-10 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|