From 7fa6a0beda056824a38172520d13496ed6f2c377 Mon Sep 17 00:00:00 2001 From: kremsy Date: Wed, 15 Jan 2014 18:31:06 +0100 Subject: [PATCH] maplist optimations --- .../ManiaExchange/ManiaExchangeManager.php | 13 +++- application/core/Maps/MapManager.php | 76 ++++++++++++++++++- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/application/core/ManiaExchange/ManiaExchangeManager.php b/application/core/ManiaExchange/ManiaExchangeManager.php index 7c91006f..459c8c93 100644 --- a/application/core/ManiaExchange/ManiaExchangeManager.php +++ b/application/core/ManiaExchange/ManiaExchangeManager.php @@ -89,11 +89,20 @@ class ManiaExchangeManager { $saveMapStatement->close(); } + /** * Fetch Map Information from Mania Exchange + * @param null $map */ - public function fetchManiaExchangeMapInformations() { - $maps = $this->maniaControl->mapManager->getMaps(); + public function fetchManiaExchangeMapInformations($map = null) { + if(!$map){ + //Fetch Informations for whole Maplist + $maps = $this->maniaControl->mapManager->getMaps(); + }else{ + //Fetch Information for a single map + $maps[] = $map; + } + $mysqli = $this->maniaControl->database->mysqli; $mapIdString = ''; diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 78ba12e3..153fd5b0 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -45,6 +45,7 @@ class MapManager implements CallbackListener { */ private $maniaControl = null; private $maps = array(); + /** @var Map $currentMap */ private $currentMap = null; /** @@ -164,8 +165,9 @@ class MapManager implements CallbackListener { /** * Remove a Map * - * @param string $uid - * @param bool $eraseFile + * @param \ManiaControl\Players\Player $admin + * @param string $uid + * @param bool $eraseFile */ public function removeMap(Player $admin, $uid, $eraseFile = false) { //TODO erasefile? $map = $this->maps[$uid]; @@ -183,17 +185,55 @@ class MapManager implements CallbackListener { unset($this->maps[$uid]); } + + /** + * Restructures the Maplist + */ + public function restructureMapList() { + $currentIndex = $this->getMapIndex($this->currentMap); + + //No RestructureNeeded + if($currentIndex < 14) { + return true; + } + + $lowerMapArray = array(); + $higherMapArray = array(); + + $i = 0; + foreach($this->maps as $map) { + /** @var Map $map */ + if($i < $currentIndex) { + $lowerMapArray[] = $map->fileName; + } else { + $higherMapArray[] = $map->fileName; + } + $i++; + } + + $mapArray = array_merge($higherMapArray, $lowerMapArray); + + if(!$this->maniaControl->client->query('ChooseNextMapList', $mapArray)) { + trigger_error("Error while restructuring the Maplist. " . $this->maniaControl->getClientErrorText()); + return false; + } + + return true; + } + /** * Shuffles the MapList * + * @param Player $admin * @return bool */ public function shuffleMapList($admin = null) { - shuffle($this->maps); + $shuffledMaps = $this->maps; + shuffle($shuffledMaps); $mapArray = array(); - foreach($this->maps as $map) { + foreach($shuffledMaps as $map) { /** @var Map $map */ $mapArray[] = $map->fileName; } @@ -211,6 +251,8 @@ class MapManager implements CallbackListener { $this->maniaControl->log($message, true); } + //Restructure if needed + $this->restructureMapList(); return true; } @@ -298,7 +340,13 @@ class MapManager implements CallbackListener { public function handleOnInit(array $callback) { $this->updateFullMapList(); $this->fetchCurrentMap(); + + //Fetch Mx Infos $this->mxManager->fetchManiaExchangeMapInformations(); + + //Restructure Maplist + $this->restructureMapList(); + } /** @@ -337,8 +385,15 @@ class MapManager implements CallbackListener { $this->fetchCurrentMap(); } + //Restructure MapList if id is over 15 + $this->restructureMapList(); + + //Update the mx of the map (for update checks, etc.) + $this->mxManager->fetchManiaExchangeMapInformations($this->currentMap); + // Trigger own BeginMap callback $this->maniaControl->callbackManager->triggerCallback(self::CB_BEGINMAP, array(self::CB_BEGINMAP, $this->currentMap)); + } /** @@ -358,6 +413,19 @@ class MapManager implements CallbackListener { return array_values($this->maps); } + /** + * Returns the MapIndex of a given map + * + * @param Map $map + * @internal param $uid + * @return mixed + */ + public function getMapIndex(Map $map) { + $maps = $this->getMaps(); + return array_search($map, $maps); + } + + /** * Adds a Map from Mania Exchange *