From 9505fdf9744e317eb29d8cfed2984e1101137c8f Mon Sep 17 00:00:00 2001 From: kremsy Date: Tue, 28 Jan 2014 20:09:09 +0100 Subject: [PATCH] mapqueue improvements --- application/core/Maps/MapList.php | 19 +++++++-- application/core/Maps/MapManager.php | 2 +- application/core/Maps/MapQueue.php | 60 ++++++++++++++++++++-------- application/plugins/QueuePlugin.php | 55 +++++++++++++------------ 4 files changed, 90 insertions(+), 46 deletions(-) diff --git a/application/core/Maps/MapList.php b/application/core/Maps/MapList.php index 545c18fd..da18b752 100644 --- a/application/core/Maps/MapList.php +++ b/application/core/Maps/MapList.php @@ -37,6 +37,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { const ACTION_ERASE_MAP = 'MapList.EraseMap'; const ACTION_SWITCH_MAP = 'MapList.SwitchMap'; const ACTION_QUEUED_MAP = 'MapList.QueueMap'; + const ACTION_UNQUEUE_MAP = 'MapList.UnQueueMap'; const ACTION_CHECK_UPDATE = 'MapList.CheckUpdate'; const ACTION_CLEAR_MAPQUEUE = 'MapList.ClearMapQueue'; const MAX_MAPS_PER_PAGE = 15; @@ -122,7 +123,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $maniaLink->add($frame); //Admin Buttons - if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) { + if ($this->maniaControl->authenticationManager->checkPermission($player, MapQueue::SETTING_PERMISSION_CLEAR_MAPQUEUE)) { //Clear Map-Queue $label = new Label_Button(); $frame->add($label); @@ -280,7 +281,16 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $label->setTextSize(1.5); $label->setText($queuedMaps[$map->uid]); $label->setTextColor('fff'); - $script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => '$<' . $map->name . '$> is on Map-Queue Position: ' . $queuedMaps[$map->uid])); + + //Checks if the Player who openend the Widget has queued the map + $queuer = $this->maniaControl->mapManager->mapQueue->getQueuer($map->uid); + if ($queuer->login == $player->login) { + $script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => 'Remove $<' . $map->name . '$> from the Map Queue')); + $label->setAction(self::ACTION_UNQUEUE_MAP . '.' . $map->uid); + } else { + $script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => '$<' . $map->name . '$> is on Map-Queue Position: ' . $queuedMaps[$map->uid])); + } + } else { // Map-Queue-Map-Button $queueLabel = new Label_Button(); @@ -292,7 +302,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $queueLabel->setText('+'); $queueLabel->setTextColor('09f'); - $script->addTooltip($queueLabel, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => 'Add Map to the Map Queue: $<' . $map->name . '$>')); + $script->addTooltip($queueLabel, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => 'Add $<' . $map->name . '$> to the Map Queue')); } if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_REMOVE_MAP)) { @@ -504,6 +514,9 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { case self::ACTION_QUEUED_MAP: $this->maniaControl->mapManager->mapQueue->addMapToMapQueue($callback[1][1], $actionArray[2]); break; + case self::ACTION_UNQUEUE_MAP: + $this->maniaControl->mapManager->mapQueue->removeFromMapQueue($player, $actionArray[2]); + break; } } diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 729934ad..235bd200 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -205,7 +205,7 @@ class MapManager implements CallbackListener { $map = $this->maps[$uid]; //Unset the Map everywhere - $this->mapQueue->removeFromMapQueue($admin->login, $map->uid); + $this->mapQueue->removeFromMapQueue($admin, $map->uid); $this->mxManager->unsetMap($map->mx->id); // Remove map diff --git a/application/core/Maps/MapQueue.php b/application/core/Maps/MapQueue.php index d2c2e3ce..e5013597 100644 --- a/application/core/Maps/MapQueue.php +++ b/application/core/Maps/MapQueue.php @@ -2,6 +2,7 @@ namespace ManiaControl\Maps; +use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Commands\CommandListener; @@ -20,8 +21,9 @@ class MapQueue implements CallbackListener, CommandListener { */ const CB_MAPQUEUE_CHANGED = 'MapQueue.MapQueueBoxChanged'; - const SETTING_SKIP_MAP_ON_LEAVE = 'Skip Map when the requester leaves'; - const SETTING_SKIP_MAPQUEUE_ADMIN = 'Skip Map when admin leaves'; + const SETTING_SKIP_MAP_ON_LEAVE = 'Skip Map when the requester leaves'; + const SETTING_SKIP_MAPQUEUE_ADMIN = 'Skip Map when admin leaves'; + const SETTING_PERMISSION_CLEAR_MAPQUEUE = 'Clear Mapqueue'; const ADMIN_COMMAND_CLEAR_MAPQUEUE = 'clearmapqueue'; const ADMIN_COMMAND_CLEAR_JUKEBOX = 'clearjukebox'; @@ -46,6 +48,8 @@ class MapQueue implements CallbackListener, CommandListener { $this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN, false); + $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_CLEAR_MAPQUEUE, AuthenticationManager::AUTH_LEVEL_MODERATOR); + //Register Admin Commands $this->maniaControl->commandManager->registerCommandListener(self::ADMIN_COMMAND_CLEAR_JUKEBOX, $this, 'command_ClearMapQueue', true); $this->maniaControl->commandManager->registerCommandListener(self::ADMIN_COMMAND_CLEAR_MAPQUEUE, $this, 'command_ClearMapQueue', true); @@ -67,6 +71,11 @@ class MapQueue implements CallbackListener, CommandListener { * @param $admin */ public function clearMapQueue($admin) { + if (!$this->maniaControl->authenticationManager->checkPermission($admin, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)) { + $this->maniaControl->authenticationManager->sendNotAllowed($admin); + return; + } + $title = $this->maniaControl->authenticationManager->getAuthLevelName($admin->authLevel); //Destroy map - queue list @@ -90,7 +99,7 @@ class MapQueue implements CallbackListener, CommandListener { $player = $this->maniaControl->playerManager->getPlayer($login); //Check if the map is already juked - if(array_key_exists($uid, $this->queuedMaps)) { + if (array_key_exists($uid, $this->queuedMaps)) { $this->maniaControl->chat->sendError('Map is already in the Map-Queue', $login); return; } @@ -105,17 +114,26 @@ class MapQueue implements CallbackListener, CommandListener { // Trigger callback $this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('add', $this->queuedMaps[$uid])); - } /** * Revmoes a Map from the Map queue * - * @param $login - * @param $uid + * @param \ManiaControl\Players\Player $player + * @param $uid + * @internal param $login */ - public function removeFromMapQueue($login, $uid) { + public function removeFromMapQueue(Player $player, $uid) { + if (!isset($this->queuedMaps[$uid])) { + return; + } + $map = $this->queuedMaps[$uid][1]; unset($this->queuedMaps[$uid]); + + $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> removed $<' . $map->name . '$> from the Map Queue'); + + // Trigger callback + $this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('remove', $map)); } @@ -126,20 +144,20 @@ class MapQueue implements CallbackListener, CommandListener { */ public function endMap(array $callback) { $this->nextMap = null; - if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE) { + if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE) { //Skip Map if requester has left foreach($this->queuedMaps as $queuedMap) { $player = $queuedMap[0]; //found player, so play this map - if($this->maniaControl->playerManager->getPlayer($player->login) != null) { + if ($this->maniaControl->playerManager->getPlayer($player->login) != null) { break; } - if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN) == FALSE) { + if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN) == FALSE) { //Check if the queuer is a admin - if($player->authLevel > 0) { + if ($player->authLevel > 0) { break; } } @@ -157,16 +175,16 @@ class MapQueue implements CallbackListener, CommandListener { $this->nextMap = array_shift($this->queuedMaps); //Check if Map Queue is empty - if($this->nextMap == null) { + if ($this->nextMap == null) { return; } $map = $this->nextMap[1]; - $success = $this->maniaControl->client->chooseNextMap($map->fileName); - if(!$success) { //TODO error code? - //trigger_error('[' . $this->maniaControl->client->getErrorCode() . '] ChooseNextMap - ' . $this->maniaControl->client->getErrorCode(), E_USER_WARNING); - return; + try { + $this->maniaControl->client->chooseNextMap($map->fileName); + } catch(\Exception $e) { + //do nothing } } @@ -209,6 +227,16 @@ class MapQueue implements CallbackListener, CommandListener { return $queuedMaps; } + /** + * Returns the Queuer of a Map + * + * @param $uid + * @return mixed + */ + public function getQueuer($uid) { + return $this->queuedMaps[$uid][0]; + } + /** * Dummy Function for testing */ diff --git a/application/plugins/QueuePlugin.php b/application/plugins/QueuePlugin.php index 3241fc4f..20c09140 100644 --- a/application/plugins/QueuePlugin.php +++ b/application/plugins/QueuePlugin.php @@ -3,7 +3,6 @@ use FML\Controls\Frame; use FML\Controls\Labels\Label_Button; use FML\Controls\Labels\Label_Text; use FML\Controls\Quad; - use FML\Controls\Quads\Quad_Icons64x64_1; use FML\ManiaLink; use ManiaControl\Callbacks\CallbackListener; @@ -74,7 +73,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $this->maniaControl->settingManager->initSetting($this, self::QUEUE_MAX, 8); foreach($this->maniaControl->playerManager->getPlayers() as $player) { - if($player->isSpectator) { + if ($player->isSpectator) { $this->spectators[$player->login] = $player->login; $this->maniaControl->client->forceSpectator($player->login, 1); $this->showJoinQueueWidget($player); @@ -158,12 +157,12 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $login = $callback[1]->login; $player = $this->maniaControl->playerManager->getPlayer($login); - if($player->isSpectator) { + if ($player->isSpectator) { $this->spectators[$player->login] = $player->login; $this->maniaControl->client->forceSpectator($player->login, 1); $this->showJoinQueueWidget($player); } else { - if(count($this->queue) != 0) { + if (count($this->queue) != 0) { $this->maniaControl->client->forceSpectator($player->login, 1); $this->spectators[$player->login] = $player->login; $this->showJoinQueueWidget($player); @@ -179,7 +178,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns public function handlePlayerDisconnect(array $callback) { /** @var Player $player */ $player = $callback[1]; - if(isset($this->spectators[$player->login])) { + if (isset($this->spectators[$player->login])) { unset($this->spectators[$player->login]); } $this->removePlayerFromQueue($player->login); @@ -195,25 +194,29 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $login = $callback[1][0]['Login']; $player = $this->maniaControl->playerManager->getPlayer($login); - if(!is_null($player)) { - if($player->isSpectator) { - if(!isset($this->spectators[$player->login])) { + if (!is_null($player)) { + if ($player->isSpectator) { + if (!isset($this->spectators[$player->login])) { $this->maniaControl->client->forceSpectator($player->login, 1); $this->spectators[$player->login] = $player->login; $this->showJoinQueueWidget($player); } } else { $this->removePlayerFromQueue($player->login); - if(isset($this->spectators[$player->login])) unset($this->spectators[$player->login]); + if (isset($this->spectators[$player->login])) { + unset($this->spectators[$player->login]); + } $found = false; foreach($this->showPlay as $showPlay) { - if($showPlay['player']->login == $player->login) { + if ($showPlay['player']->login == $player->login) { $found = true; } } - - if(!$found) $this->hideQueueWidget($player); + + if (!$found) { + $this->hideQueueWidget($player); + } } } } @@ -222,7 +225,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns * Function called on every second. */ public function handleEverySecond() { - if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > (count($this->maniaControl->playerManager->players)-count($this->spectators))) { + if ($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > (count($this->maniaControl->playerManager->players) - count($this->spectators))) { $this->moveFirstPlayerToPlay(); } @@ -232,7 +235,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns } foreach($this->showPlay as $showPlay) { - if(($showPlay['time'] + 5) < time()) { + if (($showPlay['time'] + 5) < time()) { $this->hideQueueWidget($showPlay['player']); unset($this->showPlay[$showPlay['player']->login]); } @@ -265,7 +268,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns * Function used to move the first queued player to the */ private function moveFirstPlayerToPlay() { - if(count($this->queue) > 0) { + if (count($this->queue) > 0) { $firstPlayer = $this->maniaControl->playerManager->getPlayer($this->queue[0]->login); $this->forcePlayerToPlay($firstPlayer); } @@ -277,10 +280,10 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns * @param Player $player */ private function forcePlayerToPlay(Player $player) { - if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) { + if ($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) { $this->maniaControl->client->forceSpectator($player->login, 2); $this->maniaControl->client->forceSpectator($player->login, 0); - if(isset($this->spectators[$player->login])) { + if (isset($this->spectators[$player->login])) { unset($this->spectators[$player->login]); } $this->removePlayerFromQueue($player->login); @@ -297,13 +300,13 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns */ private function addPlayerToQueue(Player $player) { foreach($this->queue as $queuedPlayer) { - if($queuedPlayer->login == $player->login) { + if ($queuedPlayer->login == $player->login) { $this->maniaControl->chat->sendError('You\'re already in the queue!', $player->login); return false; } } - - if($this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX) > count($this->queue)) { + + if ($this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX) > count($this->queue)) { $this->queue[count($this->queue)] = $player; $this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff' . $player->nickname . '$> just joined the queue!'); } @@ -318,7 +321,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $count = 0; $newQueue = array(); foreach($this->queue as $queuePlayer) { - if($queuePlayer->login != $login) { + if ($queuePlayer->login != $login) { $newQueue[$count] = $queuePlayer; $count++; } @@ -375,17 +378,17 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $inQueue = false; foreach($this->queue as $queuedPlayer) { - if($queuedPlayer->login == $player->login) { + if ($queuedPlayer->login == $player->login) { $inQueue = true; } } - if($inQueue) { + if ($inQueue) { $message = '$fff$sYou\'re in the queue (click to unqueue).'; $position = 0; foreach(array_values($this->queue) as $i => $queuePlayer) { - if($player->login == $queuePlayer->login) { + if ($player->login == $queuePlayer->login) { $position = ($i + 1); } } @@ -396,7 +399,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $statusLabel->setAction(self::ML_REMOVEFROMQUEUE); $cameraQuad->setAction(self::ML_REMOVEFROMQUEUE); } else { - if(count($this->queue) < $max_queue) { + if (count($this->queue) < $max_queue) { $message = '$0ff$sClick to join spectator waiting list.'; $messageLabel->setAction(self::ML_ADDTOQUEUE); $backgroundQuad->setAction(self::ML_ADDTOQUEUE); @@ -412,7 +415,7 @@ class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAns $messageLabel->setText($message); $messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall); - $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue'); + $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player); } /**