Skip to content

Commit

Permalink
Merge pull request #134 from MahjongPantheon/support-ignore-seating-i…
Browse files Browse the repository at this point in the history
…n-interfaces

Support ignore seating in interfaces
  • Loading branch information
Oleg Klimenko committed Dec 11, 2019
2 parents 2af505c + cc1c304 commit 6f7ae56
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 210 deletions.
12 changes: 8 additions & 4 deletions Mimir/src/controllers/Events.php
Expand Up @@ -95,6 +95,8 @@ public function getAllRegisteredPlayers($eventIdList)
$localMap = [];
$teamNames = [];

$ignoredPlayers = PlayerRegistrationPrimitive::findIgnoredPlayersIdsByEvent($this->_db, $eventIdList);

if ($needLocalIds) {
$localMap = array_flip(PlayerRegistrationPrimitive::findLocalIdsMapByEvent($this->_db, $eventIdList[0]));
}
Expand All @@ -103,14 +105,15 @@ public function getAllRegisteredPlayers($eventIdList)
$teamNames = PlayerRegistrationPrimitive::findTeamNameMapByEvent($this->_db, $eventIdList[0]);
}

$data = array_map(function (PlayerPrimitive $p) use (&$localMap, &$teamNames) {
$data = array_map(function (PlayerPrimitive $p) use (&$localMap, &$teamNames, &$ignoredPlayers) {
return [
'id' => $p->getId(),
'display_name' => $p->getDisplayName(),
'alias' => $p->getAlias(),
'local_id' => empty($localMap[$p->getId()]) ? null : $localMap[$p->getId()],
'team_name' => empty($teamNames[$p->getId()]) ? null : $teamNames[$p->getId()],
'tenhou_id' => $p->getTenhouId()
'tenhou_id' => $p->getTenhouId(),
'ignore_seating' => in_array($p->getId(), $ignoredPlayers)
];
}, $players);

Expand Down Expand Up @@ -255,17 +258,18 @@ public function unregisterPlayerAdmin($playerId, $eventId)
* @param integer $eventId
* @param integer $ignoreSeating
* @throws \Exception
* @return void
* @return bool
*/
public function updatePlayerSeatingFlag($playerId, $eventId, $ignoreSeating)
{
$this->_log->addInfo('Update player seating flag id# ' . $playerId . ' for event id# ' . $eventId);
(new EventUserManagementModel($this->_db, $this->_config, $this->_meta))->updateSeatingFlag(
$result = (new EventUserManagementModel($this->_db, $this->_config, $this->_meta))->updateSeatingFlag(
$playerId,
$eventId,
$ignoreSeating
);
$this->_log->addInfo('Successfully updated player seating flag id# ' . $playerId . ' for event id# ' . $eventId);
return $result;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions Mimir/src/controllers/Seating.php
Expand Up @@ -137,6 +137,13 @@ public function makeIntervalSeating($eventId, $step)
$currentRatingTable = (new EventRatingTableModel($this->_db, $this->_config, $this->_meta))
->getRatingTable($eventList, 'rating', 'desc');

// In rare cases we want to exclude players from seating
$ignoredPlayerIds = PlayerRegistrationPrimitive::findIgnoredPlayersIdsByEvent($this->_db, $eventId);
// array_values required: array_filter maintains numeric keys!
$currentRatingTable = array_values(array_filter($currentRatingTable, function ($player) use (&$ignoredPlayerIds) {
return !in_array($player['id'], $ignoredPlayerIds);
}));

$seating = Seating::makeIntervalSeating($currentRatingTable, $step, true);

$tableIndex = 1;
Expand Down
2 changes: 1 addition & 1 deletion Mimir/src/helpers/Seating.php
Expand Up @@ -571,7 +571,7 @@ public static function makeIntervalSeating($currentRatingTable, $step, $randomiz
$tables = [];
$currentTable = [];

// These guys from bottom counld not be placed with desired interval, so they play with interval 1
// These guys from bottom could not be placed with desired interval, so they play with interval 1
$playersToSeatWithNoInterval = 4 * ((count($currentRatingTable) / 4) % $step);
// These guys from top should be placed as required
$playersPossibleToSeatWithInterval = count($currentRatingTable) - $playersToSeatWithNoInterval;
Expand Down
6 changes: 5 additions & 1 deletion Mimir/src/models/Event.php
Expand Up @@ -93,6 +93,9 @@ public function getCurrentSeating($eventId)
public function getTablesState($eventId, $includeAllRounds = false)
{
$reggedPlayers = PlayerRegistrationPrimitive::findRegisteredPlayersIdsByEvent($this->_db, $eventId);
$reggedPlayers = array_values(array_filter($reggedPlayers, function ($el) {
return !$el['ignore_seating'];
}));
$tablesCount = count($reggedPlayers) / 4;

$lastGames = SessionPrimitive::findByEventAndStatus($this->_db, $eventId, [
Expand Down Expand Up @@ -202,7 +205,8 @@ protected function _formatTablesState($lastGames, $reggedPlayers, $includeAllRou
'players' => array_map(function (PlayerPrimitive $p) use (&$playerIdMap) {
return [
'id' => $p->getId(),
'local_id' => $playerIdMap[$p->getId()],
// may be empty for excluded players in non-prescripted event, so it's fine.
'local_id' => empty($playerIdMap[$p->getId()]) ? 0 : $playerIdMap[$p->getId()],
'display_name' => $p->getDisplayName()
];
}, $game->getPlayers())
Expand Down
6 changes: 3 additions & 3 deletions Mimir/src/models/EventUserManagement.php
Expand Up @@ -136,7 +136,7 @@ public function unregisterPlayer($playerId, $eventId)
* @param $eventId
* @param $ignoreSeating
* @throws \Exception
* @return void
* @return bool
*/
public function updateSeatingFlag($playerId, $eventId, $ignoreSeating)
{
Expand All @@ -146,10 +146,10 @@ public function updateSeatingFlag($playerId, $eventId, $ignoreSeating)

$regItem = PlayerRegistrationPrimitive::findByPlayerAndEvent($this->_db, $playerId, $eventId);
if (empty($regItem)) {
return;
throw new EntityNotFoundException('Player is not registered for this event');
}

$regItem
return $regItem
->setIgnoreSeating($ignoreSeating)
->save();
}
Expand Down
11 changes: 8 additions & 3 deletions Mimir/src/primitives/PlayerRegistration.php
Expand Up @@ -289,20 +289,25 @@ public function setTeamName($teamName)
/**
* @param IDb $db
* @param $eventId
* @return array ['id' => int, 'local_id' => int|null][]
* @return array ['id' => int, 'local_id' => int|null, 'ignore_seating' => int][]
* @throws \Exception
*/
public static function findRegisteredPlayersIdsByEvent(IDb $db, $eventId)
{
return array_map(function (PlayerRegistrationPrimitive $p) {
return ['id' => $p->_playerId, 'local_id' => $p->_localId];
return ['id' => $p->_playerId, 'local_id' => $p->_localId, 'ignore_seating' => $p->_ignoreSeating];
}, self::_findBy($db, 'event_id', [$eventId]));
}

/**
* @param IDb $db
* @param array|integer $eventId
* @return integer[]
*/
public static function findIgnoredPlayersIdsByEvent(IDb $db, $eventId)
{
$result = $db->table(static::$_table)
->where('event_id', $eventId)
->whereIn('event_id', (array)$eventId)
->where('ignore_seating', 1)
->findArray();

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

0 comments on commit 6f7ae56

Please sign in to comment.