Commit
This commit is contained in:
parent
cdd1fa0575
commit
1144fc16f6
@ -54,4 +54,10 @@ class TimeMapper
|
|||||||
$lastId = $this->dbh->lastInsertId();
|
$lastId = $this->dbh->lastInsertId();
|
||||||
return $this->get($lastId);
|
return $this->get($lastId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function delete(int $id): void
|
||||||
|
{
|
||||||
|
$sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE TidID = ?');
|
||||||
|
$sth->execute([$id]);
|
||||||
|
}
|
||||||
}
|
}
|
@ -22,60 +22,60 @@ class Cardreader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: yeah this sucks but it werks!
|
|
||||||
* Returns:
|
* Returns:
|
||||||
* 0 created team
|
* 0 created team
|
||||||
* 1 started counting
|
* 1 started counting
|
||||||
* 2 counted round
|
* 2 counted too fast!
|
||||||
* 3 counted too fast
|
* 3 counted round
|
||||||
|
* 4 counted round, new best!
|
||||||
*/
|
*/
|
||||||
public function receive(string $cardnumber, int $timeout): int
|
public function receive(string $cardnumber, int $timeout): int
|
||||||
{
|
{
|
||||||
$team = $this->teamMapper->getByCardnumber($cardnumber);
|
$team = $this->teamMapper->getByCardnumber($cardnumber);
|
||||||
if ($team)
|
if (!$team)
|
||||||
{
|
{
|
||||||
// team exists, insert into time table
|
// team does not exist, lets create it
|
||||||
// and update team table best time
|
$team = new Team;
|
||||||
$prev_time = $this->timeMapper->getLatestByTeamId($team->id);
|
$team->setCardnumber($cardnumber);
|
||||||
|
$this->teamMapper->create($team);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
$new_time = new Time;
|
// team exists, insert into time table
|
||||||
$new_time->setTeamId($team->id);
|
// and update team best time
|
||||||
// TODO: have this happen later so that it does not count when timeout
|
$prev_time = $this->timeMapper->getLatestByTeamId($team->id);
|
||||||
$new_time = $this->timeMapper->create($new_time);
|
$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)
|
||||||
if ($prev_time !== NULL)
|
{
|
||||||
{
|
// team has not ran previously
|
||||||
$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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// team does not exist, lets create it
|
$diff = $new_time->date->getTimestamp() - $prev_time->date->getTimestamp();
|
||||||
$team = new Team;
|
if ($diff <= $timeout)
|
||||||
$team->setCardnumber($cardnumber);
|
{
|
||||||
$this->teamMapper->create($team);
|
$this->timeMapper->delete($new_time->id); // i mean... it works?
|
||||||
return 0;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,15 +19,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
$app->session->flash('Startet telling', 'success');
|
$app->session->flash('Startet telling', 'info');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
$app->session->flash('Passerte en runde', 'success');
|
$app->session->flash('Runde gikk for fort!', 'danger');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
Reference in New Issue
Block a user