This commit is contained in:
William 2022-03-16 13:37:24 +01:00
parent cdd1fa0575
commit 1144fc16f6
3 changed files with 55 additions and 45 deletions

View File

@ -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]);
}
}

View File

@ -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;
}
}

View File

@ -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: