81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
use App\Core\Database;
|
|
use App\Teamtable\Team;
|
|
use App\Teamtable\TeamMapper;
|
|
use App\Timetable\Time;
|
|
use App\Timetable\TimeMapper;
|
|
|
|
class Cardreader
|
|
{
|
|
public PDO $dbh;
|
|
|
|
public TeamMapper $teamMapper;
|
|
|
|
public TimeMapper $timeMapper;
|
|
|
|
public function __construct(Database $database)
|
|
{
|
|
$this->dbh = $database->conn;
|
|
$this->teamMapper = new TeamMapper($this->dbh);
|
|
$this->timeMapper = new TimeMapper($this->dbh);
|
|
}
|
|
|
|
/**
|
|
* TODO: yeah this sucks but it werks!
|
|
* Returns:
|
|
* 0 created team
|
|
* 1 started counting
|
|
* 2 counted round
|
|
* 3 counted too fast
|
|
*/
|
|
public function receive(string $cardnumber, int $timeout): int
|
|
{
|
|
$team = $this->teamMapper->getByCardnumber($cardnumber);
|
|
if ($team)
|
|
{
|
|
// team exists, insert into time table
|
|
// 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;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
// team does not exist, lets create it
|
|
$team = new Team;
|
|
$team->setCardnumber($cardnumber);
|
|
$this->teamMapper->create($team);
|
|
return 0;
|
|
}
|
|
} |