TrackManiaControl/application/core/Maps/MapQueue.php

250 lines
6.7 KiB
PHP
Raw Normal View History

2013-12-31 12:25:03 +01:00
<?php
namespace ManiaControl\Maps;
2014-01-14 18:27:54 +01:00
2014-01-28 20:09:09 +01:00
use ManiaControl\Admin\AuthenticationManager;
2013-12-31 12:25:03 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
/**
* MapQueue Class
*
* @author steeffeen & kremsy
*/
class MapQueue implements CallbackListener, CommandListener {
/**
* Constants
*/
2014-01-14 18:27:54 +01:00
const CB_MAPQUEUE_CHANGED = 'MapQueue.MapQueueBoxChanged';
2013-12-31 12:25:03 +01:00
2014-01-28 20:09:09 +01:00
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';
2013-12-31 12:25:03 +01:00
const ADMIN_COMMAND_CLEAR_MAPQUEUE = 'clearmapqueue';
2014-01-14 18:27:54 +01:00
const ADMIN_COMMAND_CLEAR_JUKEBOX = 'clearjukebox';
2013-12-31 12:25:03 +01:00
/**
* Private properties
*/
private $maniaControl = null;
private $queuedMaps = array();
private $nextMap = null;
/**
2013-12-31 12:36:49 +01:00
* Create a new server MapQueue
2013-12-31 12:25:03 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-14 18:27:54 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ENDMAP, $this, 'endMap');
2013-12-31 12:25:03 +01:00
// Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN, false);
2014-01-28 20:09:09 +01:00
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_CLEAR_MAPQUEUE, AuthenticationManager::AUTH_LEVEL_MODERATOR);
2013-12-31 12:25:03 +01:00
//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);
}
/**
2013-12-31 12:36:49 +01:00
* Clears the map-queue via admin command clearmap queue
2014-01-14 18:27:54 +01:00
*
2013-12-31 12:25:03 +01:00
* @param array $chat
* @param Player $player
*/
2014-01-14 18:27:54 +01:00
public function command_ClearMapQueue(array $chat, Player $admin) {
2014-01-17 22:35:14 +01:00
$this->clearMapQueue($admin);
}
/**
* Clears the Map Queue
*
* @param $admin
*/
public function clearMapQueue($admin) {
2014-01-28 20:09:09 +01:00
if (!$this->maniaControl->authenticationManager->checkPermission($admin, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)) {
$this->maniaControl->authenticationManager->sendNotAllowed($admin);
return;
}
2013-12-31 12:25:03 +01:00
$title = $this->maniaControl->authenticationManager->getAuthLevelName($admin->authLevel);
2013-12-31 12:36:49 +01:00
//Destroy map - queue list
2013-12-31 12:25:03 +01:00
$this->queuedMaps = array();
$this->maniaControl->chat->sendInformation($title . ' $<' . $admin->nickname . '$> cleared the Queued-Map list!');
2014-01-14 18:27:54 +01:00
$this->maniaControl->log($title . ' ' . Formatter::stripCodes($admin->nickname) . ' cleared the Queued-Map list!');
2013-12-31 12:25:03 +01:00
// Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('clear'));
}
2014-01-17 22:35:14 +01:00
2014-01-28 20:10:38 +01:00
/**a
2013-12-31 12:25:03 +01:00
* Adds a Map to the map-queue
2014-01-14 18:27:54 +01:00
*
2013-12-31 12:25:03 +01:00
* @param $login
* @param $uid
*/
2014-01-14 18:27:54 +01:00
public function addMapToMapQueue($login, $uid) {
2013-12-31 12:25:03 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
//Check if the map is already juked
2014-01-28 20:09:09 +01:00
if (array_key_exists($uid, $this->queuedMaps)) {
2013-12-31 12:25:03 +01:00
$this->maniaControl->chat->sendError('Map is already in the Map-Queue', $login);
return;
}
//TODO recently maps not able to add to queue-amps setting, and management
$map = $this->maniaControl->mapManager->getMapByUid($uid);
$this->queuedMaps[$uid] = array($player, $map);
2013-12-31 12:36:49 +01:00
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> added $<' . $map->name . '$> to the Map Queue');
2013-12-31 12:25:03 +01:00
// Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('add', $this->queuedMaps[$uid]));
}
/**
* Revmoes a Map from the Map queue
2014-01-14 18:27:54 +01:00
*
2014-01-28 20:09:09 +01:00
* @param \ManiaControl\Players\Player $player
* @param $uid
* @internal param $login
2013-12-31 12:25:03 +01:00
*/
2014-01-28 20:09:09 +01:00
public function removeFromMapQueue(Player $player, $uid) {
if (!isset($this->queuedMaps[$uid])) {
return;
}
$map = $this->queuedMaps[$uid][1];
2013-12-31 12:25:03 +01:00
unset($this->queuedMaps[$uid]);
2014-01-28 20:09:09 +01:00
$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));
2013-12-31 12:25:03 +01:00
}
/**
* Called on endmap
2014-01-14 18:27:54 +01:00
*
2013-12-31 12:25:03 +01:00
* @param array $callback
*/
2014-01-14 18:27:54 +01:00
public function endMap(array $callback) {
2013-12-31 12:25:03 +01:00
$this->nextMap = null;
2014-01-28 20:09:09 +01:00
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE) {
2013-12-31 12:25:03 +01:00
//Skip Map if requester has left
2014-01-14 18:27:54 +01:00
foreach($this->queuedMaps as $queuedMap) {
2013-12-31 12:25:03 +01:00
$player = $queuedMap[0];
//found player, so play this map
2014-01-28 20:09:09 +01:00
if ($this->maniaControl->playerManager->getPlayer($player->login) != null) {
2013-12-31 12:25:03 +01:00
break;
}
2014-01-28 20:09:09 +01:00
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN) == FALSE) {
2013-12-31 12:36:49 +01:00
//Check if the queuer is a admin
2014-01-28 20:09:09 +01:00
if ($player->authLevel > 0) {
2013-12-31 12:25:03 +01:00
break;
}
}
// Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('skip', $queuedMap[0]));
2013-12-31 12:36:49 +01:00
//Player not found, so remove the map from the mapqueue
2013-12-31 12:25:03 +01:00
array_shift($this->queuedMaps);
$this->maniaControl->chat->sendInformation('Requested Map skipped because $<' . $player->nickname . '$> left!');
}
}
$this->nextMap = array_shift($this->queuedMaps);
2013-12-31 12:36:49 +01:00
//Check if Map Queue is empty
2014-01-28 20:09:09 +01:00
if ($this->nextMap == null) {
2013-12-31 12:25:03 +01:00
return;
2014-01-14 18:27:54 +01:00
}
2013-12-31 12:25:03 +01:00
$map = $this->nextMap[1];
2014-01-28 20:09:09 +01:00
try {
$this->maniaControl->client->chooseNextMap($map->fileName);
} catch(\Exception $e) {
//do nothing
2013-12-31 12:25:03 +01:00
}
}
/**
* Returns the next Map if the next map is a queuedmap or null if it's not
2014-01-14 18:27:54 +01:00
*
2013-12-31 12:25:03 +01:00
* @return null
*/
2014-01-14 18:27:54 +01:00
public function getNextMap() {
2013-12-31 12:25:03 +01:00
return $this->nextMap;
}
2014-01-14 18:27:54 +01:00
2014-01-14 20:35:31 +01:00
/**
* Returns the first Queued Map
*
* @return array(Player $player, Map $map)
*/
public function getNextQueuedMap() {
foreach($this->queuedMaps as $queuedMap) {
//return the first Queued Map
return $queuedMap;
}
return null;
}
2013-12-31 12:25:03 +01:00
/**
* Returns a list with the indexes of the queued maps
2014-01-14 18:27:54 +01:00
*
2013-12-31 12:25:03 +01:00
* @return array
*/
2014-01-14 18:27:54 +01:00
public function getQueuedMapsRanking() {
$i = 1;
2013-12-31 12:25:03 +01:00
$queuedMaps = array();
2014-01-14 18:27:54 +01:00
foreach($this->queuedMaps as $map) {
$map = $map[1];
2013-12-31 12:25:03 +01:00
$queuedMaps[$map->uid] = $i;
$i++;
}
return $queuedMaps;
}
2014-01-28 20:09:09 +01:00
/**
* Returns the Queuer of a Map
*
* @param $uid
* @return mixed
*/
public function getQueuer($uid) {
return $this->queuedMaps[$uid][0];
}
2013-12-31 12:25:03 +01:00
/**
* Dummy Function for testing
*/
2014-01-14 18:27:54 +01:00
public function printAllMaps() {
foreach($this->queuedMaps as $map) {
2013-12-31 12:25:03 +01:00
$map = $map[1];
var_dump($map->name);
}
}
}