refactored & fixed MapList

improved phpdoc & error messages
This commit is contained in:
Steffen Schröder 2014-05-09 19:05:08 +02:00
parent 8a0039422a
commit e50ee01d01
3 changed files with 87 additions and 77 deletions

View File

@ -57,8 +57,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $mapListShown = array(); private $playerCurrentPage = array();
private $mapsInListShown = array();
/** /**
* Create a new MapList Instance * Create a new MapList Instance
@ -110,52 +109,41 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
* Display a MapList on the Screen * Display a MapList on the Screen
* *
* @param Player $player * @param Player $player
* @param array $maps * @param array $mapList
* @param int $chunk * @param int $pageIndex
* @param int $startPage
*/ */
public function showMapList(Player $player, $maps = null, $chunk = 0, $startPage = null) { public function showMapList(Player $player, $mapList = null, $pageIndex = -1) {
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth(); $width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight(); $height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
$this->mapListShown[$player->login] = true; if ($pageIndex < 0) {
$queueBuffer = $this->maniaControl->mapManager->mapQueue->getQueueBuffer(); if (isset($this->playerCurrentPage[$player->login])) {
$pageIndex = $this->playerCurrentPage[$player->login];
} else {
$pageIndex = 0;
}
}
$this->playerCurrentPage[$player->login] = $pageIndex;
$queueBuffer = $this->maniaControl->mapManager->mapQueue->getQueueBuffer();
$chunkIndex = $this->getChunkIndexFromPageNumber($pageIndex);
$mapsBeginIndex = $this->getChunkMapsBeginIndex($chunkIndex);
// Get Maps // Get Maps
$mapList = null; if (!is_array($mapList)) {
$pageCount = null; $mapList = $this->maniaControl->mapManager->getMaps();
if (is_array($maps)) {
$mapList = array_slice($maps, $chunk, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$this->mapsInListShown[$player->login] = $maps;
$pageCount = ceil(count($maps) / self::MAX_MAPS_PER_PAGE);
} /*else if (array_key_exists($player->login, $this->mapsInListShown)) {
$completeList = $this->mapsInListShown[$player->login];
$this->mapsInListShown[$player->login] = $completeList;
$mapList = array_slice($completeList, $chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$pageCount = ceil(count($completeList) / self::MAX_MAPS_PER_PAGE);
} */ else {
$mapList = $this->maniaControl->mapManager->getMaps($chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$pageCount = ceil($this->maniaControl->mapManager->getMapsCount() / self::MAX_MAPS_PER_PAGE);
$this->mapsInListShown[$player->login] = $this->maniaControl->mapManager->getMaps();
} }
$mapList = array_slice($mapList, $mapsBeginIndex, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
//FIXME $totalMapsCount = $this->maniaControl->mapManager->getMapsCount();
/* $pagesCount = ceil($totalMapsCount / self::MAX_MAPS_PER_PAGE);
* not same result after map delete update (mapList contains still the removed map while mapManager->getMaps dont contain it)
* var_dump(count($mapList));
* var_dump($this->maniaControl->mapManager->getMapsCount());
* (failures happen in the middle else if, what is the use for the midle if?)
*/
// Create ManiaLink // Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID); $maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript(); $script = $maniaLink->getScript();
$paging = new Paging(); $paging = new Paging();
$script->addFeature($paging); $script->addFeature($paging);
if (!is_null($pageCount)) { $paging->setCustomMaxPageNumber($pagesCount);
$paging->setCustomMaxPageNumber($pageCount);
}
$paging->setChunkActionAppendsPageNumber(true); $paging->setChunkActionAppendsPageNumber(true);
$paging->setChunkActions(self::ACTION_PAGING_CHUNKS); $paging->setChunkActions(self::ACTION_PAGING_CHUNKS);
@ -225,11 +213,10 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** @var KarmaPlugin $karmaPlugin */ /** @var KarmaPlugin $karmaPlugin */
$karmaPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_KARMA_PLUGIN); $karmaPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_KARMA_PLUGIN);
$pageNumber = 1 + $chunk * self::MAX_PAGES_PER_CHUNK; $pageNumber = 1 + $chunkIndex * self::MAX_PAGES_PER_CHUNK;
$startPageNumber = (is_int($startPage) ? $startPage : $pageNumber); $paging->setStartPageNumber($pageIndex + 1);
$paging->setStartPageNumber($startPageNumber);
$id = 1 + $chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE; $id = 1 + $mapsBeginIndex;
$y = $height / 2 - 10; $y = $height / 2 - 10;
$pageFrames = array(); $pageFrames = array();
/** @var Map $map */ /** @var Map $map */
@ -488,16 +475,43 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'MapList'); $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'MapList');
} }
/**
* Get the Chunk Index with the given Page Index
*
* @param int $pageIndex
* @return int
*/
private function getChunkIndexFromPageNumber($pageIndex) {
$mapsCount = $this->maniaControl->mapManager->getMapsCount();
$pagesCount = ceil($mapsCount / self::MAX_MAPS_PER_PAGE);
if ($pageIndex > $pagesCount - 1) {
$pageIndex = $pagesCount - 1;
}
return floor($pageIndex / self::MAX_PAGES_PER_CHUNK);
}
/**
* Calculate the First Map Index to show for the given Chunk
*
* @param int $chunkIndex
* @return int
*/
private function getChunkMapsBeginIndex($chunkIndex) {
return $chunkIndex * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE;
}
/** /**
* Builds the confirmation frame * Builds the confirmation frame
* *
* @param ManiaLink $maniaLink * @param ManiaLink $maniaLink
* @param $y * @param float $y
* @param bool $mapUid * @param bool $mapUid
* @param bool $erase * @param bool $erase
* @return Frame * @return Frame
*/ */
public function buildConfirmFrame(Manialink $maniaLink, $y, $mapUid, $erase = false) { public function buildConfirmFrame(Manialink $maniaLink, $y, $mapUid, $erase = false) {
// TODO: get rid of the confirm frame to decrease xml size & network usage
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth(); $width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle(); $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle(); $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
@ -549,23 +563,23 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
* Unset the player if he opened another Main Widget * Unset the player if he opened another Main Widget
* *
* @param Player $player * @param Player $player
* @param $openedWidget * @param string $openedWidget
*/ */
public function handleWidgetOpened(Player $player, $openedWidget) { public function handleWidgetOpened(Player $player, $openedWidget) {
// unset when another main widget got opened // unset when another main widget got opened
if ($openedWidget != 'MapList') { if ($openedWidget != 'MapList') {
unset($this->mapListShown[$player->login]); unset($this->playerCurrentPage[$player->login]);
} }
} }
/** /**
* Closes the widget * Close the widget
* *
* @param \ManiaControl\Players\Player $player * @param Player $player
*/ */
public function closeWidget(Player $player) { public function closeWidget(Player $player) {
unset($this->mapListShown[$player->login]); // TODO: resolve duplicate with 'playerCloseWidget'
unset($this->mapsInListShown[$player->login]); unset($this->playerCurrentPage[$player->login]);
} }
/** /**
@ -600,30 +614,30 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
try { try {
$this->maniaControl->client->jumpToMapIdent($mapUid); $this->maniaControl->client->jumpToMapIdent($mapUid);
} catch (MapNotFoundException $e) { } catch (MapNotFoundException $e) {
$this->maniaControl->chat->sendError("Error while Jumping to Map Index"); $this->maniaControl->chat->sendError("Error on Jumping to Map Ident!");
break; break;
} }
$map = $this->maniaControl->mapManager->getMapByUid($mapUid); $map = $this->maniaControl->mapManager->getMapByUid($mapUid);
$message = '$<' . $player->nickname . '$> skipped to Map $z$<' . $map->name . '$>!'; $message = $player->getEscapedNickname() . ' skipped to Map $z' . $map->getEscapedName() . '!';
$this->maniaControl->chat->sendSuccess($message); $this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true); $this->maniaControl->log($message, true);
$this->playerCloseWidget($player); $this->playerCloseWidget($player);
break; break;
case self::ACTION_START_SWITCH_VOTE: case self::ACTION_START_SWITCH_VOTE:
/** @var $votesPlugin CustomVotesPlugin */ /** @var CustomVotesPlugin $votesPlugin */
$votesPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_CUSTOM_VOTE_PLUGIN); $votesPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_CUSTOM_VOTE_PLUGIN);
$map = $this->maniaControl->mapManager->getMapByUid($mapUid); $map = $this->maniaControl->mapManager->getMapByUid($mapUid);
$message = '$<' . $player->nickname . '$>$s started a vote to switch to $<' . $map->name . '$>!'; $message = $player->getEscapedNickname() . '$s started a vote to switch to ' . $map->getEscapedName() . '!';
$votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message); $votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message);
$self = $this; $self = $this;
$votesPlugin->startVote($player, 'switchmap', function ($result) use (&$self, &$votesPlugin, &$map) { $votesPlugin->startVote($player, 'switchmap', function ($result) use (&$self, &$votesPlugin, &$map) {
$self->maniaControl->chat->sendInformation('$sVote Successfully -> Map switched!'); $self->maniaControl->chat->sendInformation('$sVote Successful -> Map switched!');
$votesPlugin->undefineVote('switchmap'); $votesPlugin->undefineVote('switchmap');
//Don't queue on Map-Change //Don't queue on Map-Change
@ -647,21 +661,19 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
if (substr($actionId, 0, strlen(self::ACTION_PAGING_CHUNKS)) === self::ACTION_PAGING_CHUNKS) { if (substr($actionId, 0, strlen(self::ACTION_PAGING_CHUNKS)) === self::ACTION_PAGING_CHUNKS) {
// Paging chunks // Paging chunks
$neededPage = (int)substr($actionId, strlen(self::ACTION_PAGING_CHUNKS)); $neededPage = (int)substr($actionId, strlen(self::ACTION_PAGING_CHUNKS));
$chunk = (int)($neededPage / self::MAX_PAGES_PER_CHUNK - 0.5); $this->showMapList($player, null, $neededPage - 1);
$this->showMapList($player, null, $chunk, $neededPage);
} }
break; break;
} }
} }
/** /**
* Closes the widget * Close the widget for
* *
* @param Player $player * @param Player $player
*/ */
public function playerCloseWidget(Player $player) { public function playerCloseWidget(Player $player) {
unset($this->mapListShown[$player->login]); unset($this->playerCurrentPage[$player->login]);
unset($this->mapsInListShown[$player->login]);
$this->maniaControl->manialinkManager->closeWidget($player); $this->maniaControl->manialinkManager->closeWidget($player);
} }
@ -669,14 +681,12 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
* Reopen the widget on Map Begin, MapListChanged, etc. * Reopen the widget on Map Begin, MapListChanged, etc.
*/ */
public function updateWidget() { public function updateWidget() {
foreach ($this->mapListShown as $login => $shown) { foreach ($this->playerCurrentPage as $login => $pageIndex) {
if ($shown) { $player = $this->maniaControl->playerManager->getPlayer($login);
$player = $this->maniaControl->playerManager->getPlayer($login); if ($player) {
if ($player) { $this->showMapList($player, null, $pageIndex);
$this->showMapList($player); } else {
} else { unset($this->playerCurrentPage[$login]);
unset($this->mapListShown[$login]);
}
} }
} }
} }
@ -685,14 +695,13 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
* Reopen the widget on MapQueue changed * Reopen the widget on MapQueue changed
*/ */
public function updateWidgetQueue() { public function updateWidgetQueue() {
foreach ($this->mapListShown as $login => $shown) { // TODO: resolve duplicate with 'updateWidget' -> exact same coding?!
if ($shown) { foreach ($this->playerCurrentPage as $login => $pageIndex) {
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if ($player) { if ($player) {
$this->showMapList($player); $this->showMapList($player, null, $pageIndex);
} else { } else {
unset($this->mapListShown[$login]); unset($this->playerCurrentPage[$login]);
}
} }
} }
} }

View File

@ -192,10 +192,11 @@ class MapManager implements CallbackListener {
*/ */
public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) { public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) {
if (!isset($this->maps[$uid])) { if (!isset($this->maps[$uid])) {
$this->maniaControl->chat->sendError("Map is not existing!", $admin->login); $this->maniaControl->chat->sendError("Map does not exist!", $admin);
return; return;
} }
/** @var Map $map */
$map = $this->maps[$uid]; $map = $this->maps[$uid];
// Unset the Map everywhere // Unset the Map everywhere
@ -211,6 +212,7 @@ class MapManager implements CallbackListener {
} catch (MapNotInCurrentSelectionException $e) { } catch (MapNotInCurrentSelectionException $e) {
} }
unset($this->maps[$uid]);
if ($eraseFile) { if ($eraseFile) {
// Check if ManiaControl can even write to the maps dir // Check if ManiaControl can even write to the maps dir
@ -219,19 +221,17 @@ class MapManager implements CallbackListener {
// Delete map file // Delete map file
if (!@unlink($mapDir . $map->fileName)) { if (!@unlink($mapDir . $map->fileName)) {
trigger_error("Couldn't remove Map '{$mapDir}{$map->fileName}'."); trigger_error("Couldn't remove Map '{$mapDir}{$map->fileName}'.");
$this->maniaControl->chat->sendError("ManiaControl couldn't remove the MapFile.", $admin->login); $this->maniaControl->chat->sendError("ManiaControl couldn't remove the MapFile.", $admin);
return; return;
} }
} }
// Show Message // Show Message
if ($message) { if ($message) {
$message = '$<' . $admin->nickname . '$> removed $<' . $map->name . '$>!'; $message = $admin->getEscapedNickname() . ' removed ' . $map->getEscapedName() . '!';
$this->maniaControl->chat->sendSuccess($message); $this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true); $this->maniaControl->log($message, true);
} }
unset($this->maps[$uid]);
} }
/** /**

View File

@ -276,7 +276,7 @@ class MapQueue implements CallbackListener, CommandListener {
} }
/** /**
* Revmoes a Map from the Map queue * Remove a Map from the Map queue
* *
* @param Player $player * @param Player $player
* @param string $uid * @param string $uid
@ -285,6 +285,7 @@ class MapQueue implements CallbackListener, CommandListener {
if (!isset($this->queuedMaps[$uid])) { if (!isset($this->queuedMaps[$uid])) {
return; return;
} }
/** @var Map $map */
$map = $this->queuedMaps[$uid][1]; $map = $this->queuedMaps[$uid][1];
unset($this->queuedMaps[$uid]); unset($this->queuedMaps[$uid]);