Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer before start #242

Merged
merged 6 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Mimir/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
'getNextPrescriptedSeating' => ['SeatingController', 'getNextSeatingForPrescriptedEvent'],
'getPrescriptedEventConfig' => ['EventsController', 'getPrescriptedEventConfig'],
'updatePrescriptedEventConfig' => ['EventsController', 'updatePrescriptedEventConfig'],
'initStartingTimer' => ['EventsController', 'initStartingTimer'],
'getStartingTimer' => ['EventsController', 'getStartingTimer'],

'addErrorLog' => ['MiscController', 'addErrorLog'],
];
14 changes: 14 additions & 0 deletions Mimir/data/migrations/20220406011544_add_start_timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddStartTimer extends AbstractMigration
{
public function change()
{
$this->table('event')
->addColumn('next_game_start_time', 'integer', ['default' => 0])
->addColumn('time_to_start', 'integer', ['default' => 600])
->save();
}
}
57 changes: 57 additions & 0 deletions Mimir/src/controllers/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EventsController extends Controller
* @param int $lobbyId Tenhou lobby id for online tournaments
* @param bool $isTeam If event is team tournament
* @param bool $isPrescripted If tournament should have predefined seating
* @param int $autostartTimer Interval before games autostart
* @param string $rulesetChangesJson Json-encoded changes for base ruleset
* @throws BadActionException
* @throws InvalidParametersException
Expand All @@ -58,6 +59,7 @@ public function createEvent(
$lobbyId,
$isTeam,
$isPrescripted,
$autostartTimer,
$rulesetChangesJson
) {
$this->_log->addInfo('Creating new event with [' . $ruleset . '] rules');
Expand Down Expand Up @@ -113,6 +115,7 @@ public function createEvent(
->setUseTimer(1)
->setUsePenalty(1)
->setIsTeam($isTeam ? 1 : 0)
->setTimeToStart($autostartTimer)
->setIsPrescripted($isPrescripted ? 1 : 0)
;
break;
Expand Down Expand Up @@ -160,6 +163,7 @@ public function createEvent(
* @param int $lobbyId Tenhou lobby id for online tournaments
* @param bool $isTeam If event is team tournament
* @param bool $isPrescripted If tournament should have predefined seating
* @param int $autostartTimer Interval before games are started automatically
* @param string $rulesetChangesJson Json-encoded changes for base ruleset
* @throws BadActionException
* @throws InvalidParametersException
Expand All @@ -178,6 +182,7 @@ public function updateEvent(
$lobbyId,
$isTeam,
$isPrescripted,
$autostartTimer,
$rulesetChangesJson
) {
$this->_log->addInfo('Updating event with [' . $ruleset . '] rules');
Expand Down Expand Up @@ -213,6 +218,7 @@ public function updateEvent(
$event
->setAutoSeating($isPrescripted ? 0 : 1)
->setIsTeam($isTeam ? 1 : 0)
->setTimeToStart($autostartTimer)
->setIsPrescripted($isPrescripted ? 1 : 0)
;
} elseif ($event->getIsOnline()) { // Should be online tournament
Expand Down Expand Up @@ -271,6 +277,7 @@ public function getEventForEdit($id)
'minGames' => $event->getMinGamesCount(),
'isTeam' => $event->getIsTeam(),
'isPrescripted' => $event->getIsPrescripted(),
'autostart' => $event->getTimeToStart(),
'rulesetChanges' => json_encode($event->getRulesetChanges() ?: [])
];

Expand Down Expand Up @@ -825,6 +832,8 @@ public function getTimerState($eventId)
}

$response['waiting_for_timer'] = ($event[0]->getGamesStatus() == EventPrimitive::GS_SEATING_READY);
$response['have_autostart'] = ($event[0]->getNextGameStartTime() > 0 && $event[0]->getTimeToStart() > 0);
$response['autostart_timer'] = $event[0]->getNextGameStartTime() - time();

$this->_log->addInfo('Successfully got timer data for event id#' . $eventId);

Expand Down Expand Up @@ -1079,6 +1088,7 @@ public function getCountries($addr = '')
*/
public function rebuildEventScoring($eventId)
{
$this->_log->addInfo('Rebuilding ratings for event #' . $eventId);
if (!$this->_meta->isEventAdminById($eventId)) {
throw new BadActionException("You don't have enough privileges to rebuild ratings for this event");
}
Expand All @@ -1098,6 +1108,53 @@ public function rebuildEventScoring($eventId)
$session->recreateHistory();
}

$this->_log->addInfo('Rebuild ratings successful for event #' . $eventId);
return true;
}

/**
* @param int $eventId
* @return bool
* @throws BadActionException
* @throws InvalidParametersException
*/
public function initStartingTimer($eventId)
{
$this->_log->addInfo('Setting starting timer for event #' . $eventId);
if (!$this->_meta->isEventAdminById($eventId)) {
throw new BadActionException("You don't have enough privileges to init starting timer for this event");
}

$event = EventPrimitive::findById($this->_ds, [$eventId]);
if (empty($event)) {
throw new InvalidParametersException('Event id#' . $eventId . ' not found in DB');
}

$success = $event[0]->setNextGameStartTime(time() + $event[0]->getTimeToStart())->save();
if ($success) {
$this->_log->addInfo('Successfully set starting timer for event #' . $eventId);
} else {
$this->_log->addInfo('Failed to set starting timer for event #' . $eventId);
}

return $success;
}

/**
* @param int $eventId
* @return int seconds to start
* @throws InvalidParametersException
*/
public function getStartingTimer($eventId)
{
$this->_log->addInfo('Getting starting timer for event #' . $eventId);

$event = EventPrimitive::findById($this->_ds, [$eventId]);
if (empty($event)) {
throw new InvalidParametersException('Event id#' . $eventId . ' not found in DB');
}

$this->_log->addInfo('Successfully got starting timer for event #' . $eventId);
return $event[0]->getNextGameStartTime() - time();
}
}
2 changes: 2 additions & 0 deletions Mimir/src/models/InteractiveSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function startGame($eventId, $playerIds, $tableIndex = null, $replayHash
throw new InvalidUserException('Some players do not exist in DB, check ids');
}

$event[0]->setNextGameStartTime(0)->save();

$newSession = new SessionPrimitive($this->_ds);
$success = $newSession
->setEvent($event[0])
Expand Down
50 changes: 50 additions & 0 deletions Mimir/src/primitives/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class EventPrimitive extends Primitive
'is_prescripted' => '_isPrescripted',
'min_games_count' => '_minGamesCount',
'finished' => '_finished',
'next_game_start_time' => '_nextGameStartTime',
'time_to_start' => '_timeToStart'
];

protected function _getFieldsTransforms()
Expand Down Expand Up @@ -89,6 +91,8 @@ protected function _getFieldsTransforms()
'_hideResults' => $this->_integerTransform(),
'_minGamesCount' => $this->_integerTransform(),
'_finished' => $this->_integerTransform(),
'_nextGameStartTime' => $this->_integerTransform(),
'_timeToStart' => $this->_integerTransform(),
'_ruleset' => [
'serialize' => function (\Common\Ruleset $rules) {
return $rules->title();
Expand Down Expand Up @@ -249,6 +253,16 @@ protected function _getFieldsTransforms()
* @var integer
*/
protected $_finished;
/**
* What interval next game should start in
* @var integer
*/
protected $_timeToStart;
/**
* Next game start time
* @var integer
*/
protected $_nextGameStartTime;
/**
* Status of games in event: one of
* - seating_ready
Expand Down Expand Up @@ -844,6 +858,42 @@ public function setIsFinished($isFinished)
return $this;
}

/**
* @return int
*/
public function getTimeToStart()
{
return $this->_timeToStart;
}

/**
* @param int $time
* @return EventPrimitive
*/
public function setTimeToStart($time)
{
$this->_timeToStart = $time;
return $this;
}

/**
* @return int
*/
public function getNextGameStartTime()
{
return $this->_nextGameStartTime;
}

/**
* @param int $startTime
* @return EventPrimitive
*/
public function setNextGameStartTime($startTime)
{
$this->_nextGameStartTime = $startTime;
return $this;
}

/**
* Check if events are compatible (can be used in aggregated event).
*
Expand Down
12 changes: 6 additions & 6 deletions Mimir/tests/RealApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public function testGameConfig()
public function testTimer()
{
$response = $this->_client->execute('getTimerState', [1]);
$this->assertEquals([
'started' => false,
'finished' => false,
'time_remaining' => null,
'waiting_for_timer' => false
], $response);

$this->assertFalse($response['started']);
$this->assertFalse($response['finished']);
$this->assertNull($response['time_remaining']);
$this->assertFalse($response['waiting_for_timer']);
$this->assertFalse($response['have_autostart']);

$this->assertTrue($this->_client->execute('startTimer', [1]));
$response = $this->_client->execute('getTimerState', [1]);
Expand Down
1 change: 1 addition & 0 deletions Rheda/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'/tourn/(?<action>toggleHideResults)' => 'TournamentControlPanel',
'/tourn/(?<action>finalizeSessions)' => 'TournamentControlPanel',
'/tourn/(?<action>sendNotification)' => 'TournamentControlPanel',
'/tourn/(?<action>resetStartingTimer)' => 'TournamentControlPanel',

'/prescript' => 'PrescriptControls',

Expand Down
Binary file modified Rheda/i18n/de_DE.UTF-8/LC_MESSAGES/messages.mo
Binary file not shown.