added paging chunks to maplist

This commit is contained in:
Steffen Schröder 2014-04-28 16:59:55 +02:00
parent cac3550856
commit 8b83825344
5 changed files with 109 additions and 68 deletions

View File

@ -11,7 +11,7 @@ use FML\Script\ScriptInclude;
/** /**
* Script Feature realising a Mechanism for browsing through Pages * Script Feature realising a Mechanism for browsing through Pages
* *
* @author steeffeen * @author steeffeen
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder * @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
@ -29,7 +29,7 @@ class Paging extends ScriptFeature {
protected $pages = array(); protected $pages = array();
protected $buttons = array(); protected $buttons = array();
protected $label = null; protected $label = null;
protected $customMinPageNumber = null; protected $startPageNumber = null;
protected $customMaxPageNumber = null; protected $customMaxPageNumber = null;
protected $previousChunkAction = null; protected $previousChunkAction = null;
protected $nextChunkAction = null; protected $nextChunkAction = null;
@ -37,7 +37,7 @@ class Paging extends ScriptFeature {
/** /**
* Construct a new Paging Script Feature * Construct a new Paging Script Feature
* *
* @param Label $label (optional) Page Number Label * @param Label $label (optional) Page Number Label
*/ */
public function __construct(Label $label = null) { public function __construct(Label $label = null) {
@ -48,7 +48,7 @@ class Paging extends ScriptFeature {
/** /**
* Add a new Page Control * Add a new Page Control
* *
* @param Control $pageControl Page Control * @param Control $pageControl Page Control
* @param string $pageNumber (optional) Page Number * @param string $pageNumber (optional) Page Number
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
@ -64,7 +64,7 @@ class Paging extends ScriptFeature {
/** /**
* Append a Page * Append a Page
* *
* @param PagingPage $page Paging Page * @param PagingPage $page Paging Page
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -75,7 +75,7 @@ class Paging extends ScriptFeature {
/** /**
* Add a new Button to browse through the Pages * Add a new Button to browse through the Pages
* *
* @param Control $buttonControl Button used for Browsing * @param Control $buttonControl Button used for Browsing
* @param int $browseAction (optional) Number of browsed Pages per Click * @param int $browseAction (optional) Number of browsed Pages per Click
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
@ -97,7 +97,7 @@ class Paging extends ScriptFeature {
/** /**
* Append a Button to browse through Pages * Append a Button to browse through Pages
* *
* @param PagingButton $button Paging Button * @param PagingButton $button Paging Button
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -108,7 +108,7 @@ class Paging extends ScriptFeature {
/** /**
* Set the Label showing the Page Number * Set the Label showing the Page Number
* *
* @param Label $label Page Number Label * @param Label $label Page Number Label
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -119,19 +119,18 @@ class Paging extends ScriptFeature {
} }
/** /**
* Set a custom Minimum Page Number for using Chunks * Set the Start Page Number
* *
* @param int $minPageNumber Custom Minimum Page Number * @param int $startPageNumber Page Number to start with
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
public function setCustomMinPageNumber($minPageNumber) { public function setStartPageNumber($startPageNumber) {
$this->customMinPageNumber = (int) $minPageNumber; $this->startPageNumber = (int) $startPageNumber;
return $this;
} }
/** /**
* Set a custom Maximum Page Number for using Chunks * Set a custom Maximum Page Number for using Chunks
* *
* @param int $maxPageNumber Custom Maximum Page Number * @param int $maxPageNumber Custom Maximum Page Number
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -142,7 +141,7 @@ class Paging extends ScriptFeature {
/** /**
* Set the Action triggered when the previous Chunk is needed * Set the Action triggered when the previous Chunk is needed
* *
* @param string $previousChunkAction Triggered Action * @param string $previousChunkAction Triggered Action
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -153,7 +152,7 @@ class Paging extends ScriptFeature {
/** /**
* Set the Action triggered when the next Chunk is needed * Set the Action triggered when the next Chunk is needed
* *
* @param string $nextChunkAction Triggered Action * @param string $nextChunkAction Triggered Action
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
*/ */
@ -162,9 +161,21 @@ class Paging extends ScriptFeature {
return $this; return $this;
} }
/**
* Set the Actions triggered when another Chunk is needed
*
* @param string $chunkAction Triggered Action
* @return \FML\Script\Features\Paging
*/
public function setChunkActions($chunkAction) {
$this->setNextChunkAction($chunkAction);
$this->setPreviousChunkAction($chunkAction);
return $this;
}
/** /**
* Set if the Chunk Action should get the needed Page Number appended * Set if the Chunk Action should get the needed Page Number appended
* *
* @param bool $appendPageNumber Whether to append the needed Page Number * @param bool $appendPageNumber Whether to append the needed Page Number
* @return \FML\Script\Features\Paging * @return \FML\Script\Features\Paging
* *
@ -187,19 +198,15 @@ class Paging extends ScriptFeature {
$currentPageVariable = self::VAR_CURRENT_PAGE; $currentPageVariable = self::VAR_CURRENT_PAGE;
$updatePageFunction = self::FUNCTION_UPDATE_CURRENT_PAGE; $updatePageFunction = self::FUNCTION_UPDATE_CURRENT_PAGE;
$minPage = $this->getMinPage(); $minPageNumber = 1;
$startPageNumber = $minPage->getPageNumber(); $startPageNumber = (is_int($this->startPageNumber) ? $this->startPageNumber : $minPageNumber);
$minPageNumber = $startPageNumber;
if (is_int($this->customMinPageNumber)) {
$minPageNumber = $this->customMinPageNumber;
}
$maxPage = $this->getMaxPage(); $maxPage = $this->getMaxPage();
$maxPageNumber = $maxPage->getPageNumber(); $maxPageNumber = $this->customMaxPageNumber;
if (is_int($this->customMaxPageNumber)) { if (!is_int($maxPageNumber)) {
$maxPageNumber = $this->customMaxPageNumber; $maxPageNumber = $maxPage->getPageNumber();
} }
$pagingId = $minPage->getControl()->getId(true); $pagingId = $maxPage->getControl()->getId(true);
$pageLabelId = ''; $pageLabelId = '';
if ($this->label) { if ($this->label) {
$pageLabelId = $this->label->getId(true); $pageLabelId = $this->label->getId(true);
@ -247,6 +254,7 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
PageFound = True; PageFound = True;
} }
} }
log(Now^PageFound^CurrentPage^_Pages);
if (!PageFound && _BrowseAction != 0) { if (!PageFound && _BrowseAction != 0) {
declare Text ChunkAction; declare Text ChunkAction;
if (_BrowseAction < 0) { if (_BrowseAction < 0) {
@ -257,6 +265,7 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
if (_ChunkActionAppendPageNumber) { if (_ChunkActionAppendPageNumber) {
ChunkAction ^= CurrentPage; ChunkAction ^= CurrentPage;
} }
log(Now^ChunkAction);
TriggerPageAction(ChunkAction); TriggerPageAction(ChunkAction);
} }
if (_PageLabelId == \"\") return; if (_PageLabelId == \"\") return;
@ -267,10 +276,10 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
$script->addScriptFunction($updatePageFunction, $functionText); $script->addScriptFunction($updatePageFunction, $functionText);
return $this; return $this;
} }
/** /**
* Get the minimum Page * Get the minimum Page
* *
* @return \FML\Script\Features\PagingPage * @return \FML\Script\Features\PagingPage
*/ */
protected function getMinPage() { protected function getMinPage() {
@ -288,7 +297,7 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
/** /**
* Get the maximum Page * Get the maximum Page
* *
* @return \FML\Script\Features\PagingPage * @return \FML\Script\Features\PagingPage
*/ */
protected function getMaxPage() { protected function getMaxPage() {
@ -306,7 +315,7 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
/** /**
* Build the Array Text for the Pages * Build the Array Text for the Pages
* *
* @return string * @return string
*/ */
protected function getPagesArrayText() { protected function getPagesArrayText() {
@ -319,7 +328,7 @@ Void {$updatePageFunction}(Text _PagingId, Text _PageLabelId, Integer _BrowseAct
/** /**
* Build the Array Text for the Page Buttons * Build the Array Text for the Page Buttons
* *
* @return string * @return string
*/ */
protected function getPageButtonsArrayText() { protected function getPageButtonsArrayText() {

View File

@ -81,7 +81,9 @@ class Script {
else { else {
$scriptFunction = new ScriptFunction($name, $text); $scriptFunction = new ScriptFunction($name, $text);
} }
$this->functions[$scriptFunction->getName()] = $scriptFunction; if (!in_array($scriptFunction, $this->functions)) {
array_push($this->functions, $scriptFunction);
}
return $this; return $this;
} }

View File

@ -38,15 +38,6 @@ class ScriptFunction {
return $this; return $this;
} }
/**
* Get the Name
*
* @return string
*/
public function getName() {
return $this->name;
}
/** /**
* Set the Text * Set the Text
* *

View File

@ -28,7 +28,7 @@ use MCTeam\KarmaPlugin;
/** /**
* MapList Widget Class * MapList Widget Class
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
@ -45,7 +45,9 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
const ACTION_UNQUEUE_MAP = 'MapList.UnQueueMap'; const ACTION_UNQUEUE_MAP = 'MapList.UnQueueMap';
const ACTION_CHECK_UPDATE = 'MapList.CheckUpdate'; const ACTION_CHECK_UPDATE = 'MapList.CheckUpdate';
const ACTION_CLEAR_MAPQUEUE = 'MapList.ClearMapQueue'; const ACTION_CLEAR_MAPQUEUE = 'MapList.ClearMapQueue';
const ACTION_PAGING_CHUNKS = 'MapList.PagingChunk.';
const MAX_MAPS_PER_PAGE = 15; const MAX_MAPS_PER_PAGE = 15;
const MAX_PAGES_PER_CHUNK = 2;
const DEFAULT_KARMA_PLUGIN = 'MCTeam\KarmaPlugin'; const DEFAULT_KARMA_PLUGIN = 'MCTeam\KarmaPlugin';
const DEFAULT_CUSTOM_VOTE_PLUGIN = 'CustomVotesPlugin'; const DEFAULT_CUSTOM_VOTE_PLUGIN = 'CustomVotesPlugin';
@ -58,7 +60,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Create a new MapList Instance * Create a new MapList Instance
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
@ -79,7 +81,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Clears the Map Queue * Clears the Map Queue
* *
* @param array $chatCallback * @param array $chatCallback
* @param Player $player * @param Player $player
*/ */
@ -90,7 +92,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Check for Map Updates * Check for Map Updates
* *
* @param array $chatCallback * @param array $chatCallback
* @param Player $player * @param Player $player
*/ */
@ -103,11 +105,14 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
} }
/** /**
* Displayes a MapList on the screen * Display a MapList on the Screen
* *
* @param Player $player * @param Player $player
* @param array $maps
* @param int $chunk
* @param int $startPage
*/ */
public function showMapList(Player $player, $maps = null) { public function showMapList(Player $player, $maps = null, $chunk = 0, $startPage = null) {
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth(); $width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight(); $height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
@ -115,16 +120,15 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$queueBuffer = $this->maniaControl->mapManager->mapQueue->getQueueBuffer(); $queueBuffer = $this->maniaControl->mapManager->mapQueue->getQueueBuffer();
// Get Maps // Get Maps
if (is_null($maps) && $maps != 'redirect') { $mapList = array();
$mapList = $this->maniaControl->mapManager->getMaps(); if (is_array($maps)) {
$mapList = $maps;
} }
else { else if ($maps !== 'redirect') {
if (array_key_exists($player->login, $this->mapsInListShown) && $maps == 'redirect') { $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);
$mapList = $this->mapsInListShown[$player->login]; }
} else if (array_key_exists($player->login, $this->mapsInListShown)) {
else { $mapList = $this->mapsInListShown[$player->login];
$mapList = $maps;
}
} }
$this->mapsInListShown[$player->login] = $mapList; $this->mapsInListShown[$player->login] = $mapList;
@ -134,6 +138,9 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$script = $maniaLink->getScript(); $script = $maniaLink->getScript();
$paging = new Paging(); $paging = new Paging();
$script->addFeature($paging); $script->addFeature($paging);
$paging->setCustomMaxPageNumber($this->maniaControl->mapManager->getMapsCount() / self::MAX_MAPS_PER_PAGE);
$paging->setChunkActionAppendsPageNumber(true);
$paging->setChunkActions(self::ACTION_PAGING_CHUNKS);
// Main frame // Main frame
$frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging); $frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging);
@ -204,8 +211,12 @@ 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;
$startPageNumber = (is_int($startPage) ? $startPage : $pageNumber);
$paging->setStartPageNumber($startPageNumber);
$id = 1; $id = 1 + $chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE;
$y = $height / 2 - 10; $y = $height / 2 - 10;
$pageFrames = array(); $pageFrames = array();
/** /**
@ -228,7 +239,8 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
array_push($pageFrames, $pageFrame); array_push($pageFrames, $pageFrame);
$y = $height / 2 - 10; $y = $height / 2 - 10;
$paging->addPage($pageFrame); $paging->addPage($pageFrame, $pageNumber);
$pageNumber++;
} }
// Map Frame // Map Frame
@ -293,6 +305,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$labels = $this->maniaControl->manialinkManager->labelLine($mapFrame, $array); $labels = $this->maniaControl->manialinkManager->labelLine($mapFrame, $array);
if (isset($labels[3])) { if (isset($labels[3])) {
/** /**
*
* @var Label $label * @var Label $label
*/ */
$label = $labels[3]; $label = $labels[3];
@ -440,7 +453,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Builds the confirmation frame * Builds the confirmation frame
* *
* @param ManiaLink $maniaLink * @param ManiaLink $maniaLink
* @param $y * @param $y
* @param $id * @param $id
@ -498,7 +511,7 @@ 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 $openedWidget
*/ */
@ -511,7 +524,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Closes the widget * Closes the widget
* *
* @param \ManiaControl\Players\Player $player * @param \ManiaControl\Players\Player $player
*/ */
public function closeWidget(Player $player) { public function closeWidget(Player $player) {
@ -520,7 +533,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Closes the widget * Closes the widget
* *
* @param Player $player * @param Player $player
*/ */
public function playerCloseWidget(Player $player) { public function playerCloseWidget(Player $player) {
@ -530,12 +543,13 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
/** /**
* Handle ManialinkPageAnswer Callback * Handle ManialinkPageAnswer Callback
* *
* @param array $callback * @param array $callback
*/ */
public function handleManialinkPageAnswer(array $callback) { public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$actionArray = explode('.', $actionId); $actionArray = explode('.', $actionId);
if (count($actionArray) <= 2) { if (count($actionArray) <= 2) {
return; return;
} }
@ -575,6 +589,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
break; break;
case self::ACTION_START_SWITCH_VOTE: case self::ACTION_START_SWITCH_VOTE:
/** /**
*
* @var $votesPlugin CustomVotesPlugin * @var $votesPlugin CustomVotesPlugin
*/ */
$votesPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_CUSTOM_VOTE_PLUGIN); $votesPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_CUSTOM_VOTE_PLUGIN);
@ -584,6 +599,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$message = '$<' . $player->nickname . '$>$s started a vote to switch to $<' . $map->name . '$>!'; $message = '$<' . $player->nickname . '$>$s started a vote to switch to $<' . $map->name . '$>!';
/** /**
*
* @var Map $map * @var Map $map
*/ */
$votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message); $votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message);
@ -613,6 +629,14 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->mapManager->mapQueue->removeFromMapQueue($player, $actionArray[2]); $this->maniaControl->mapManager->mapQueue->removeFromMapQueue($player, $actionArray[2]);
$this->showMapList($player, 'redirect'); $this->showMapList($player, 'redirect');
break; break;
default:
if (substr($actionId, 0, strlen(self::ACTION_PAGING_CHUNKS)) === self::ACTION_PAGING_CHUNKS) {
// 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, $chunk, $neededPage);
}
break;
} }
} }

View File

@ -562,8 +562,23 @@ class MapManager implements CallbackListener {
* *
* @return array * @return array
*/ */
public function getMaps() { public function getMaps($offset = null, $length = null) {
return array_values($this->maps); if ($offset === null) {
return array_values($this->maps);
}
if ($length === null) {
return array_slice($this->maps, $offset);
}
return array_slice($this->maps, $offset, $length);
}
/**
* Get the Number of Maps
*
* @return int
*/
public function getMapsCount() {
return count($this->maps);
} }
/** /**