TrackManiaControl/application/core/Maps/Jukebox.php

189 lines
5.4 KiB
PHP
Raw Normal View History

2013-12-28 19:48:06 +01:00
<?php
namespace ManiaControl\Maps;
2013-12-28 22:43:57 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2013-12-29 12:01:13 +01:00
use ManiaControl\Commands\CommandListener;
use ManiaControl\Formatter;
2013-12-28 19:48:06 +01:00
use ManiaControl\ManiaControl;
2013-12-29 12:01:13 +01:00
use ManiaControl\Players\Player;
2013-12-28 19:48:06 +01:00
/**
* Jukebox Class
*
* @author steeffeen & kremsy
*/
2013-12-31 12:25:03 +01:00
class MapQueue implements CallbackListener, CommandListener {
2013-12-28 19:48:06 +01:00
/**
* Constants
*/
2013-12-31 12:25:03 +01:00
const CB_MAPQUEUE_CHANGED = 'MapQueue.MapQueueBoxChanged';
2013-12-30 11:23:00 +01:00
2013-12-29 10:17:10 +01:00
const SETTING_SKIP_MAP_ON_LEAVE = 'Skip Map when the requester leaves';
2013-12-31 12:25:03 +01:00
const SETTING_SKIP_MAPQUEUE_ADMIN = 'Skip Map when admin leaves';
2013-12-28 19:48:06 +01:00
2013-12-31 12:25:03 +01:00
const ADMIN_COMMAND_CLEAR_MAPQUEUE = 'clearmapqueue';
2013-12-29 12:01:13 +01:00
const ADMIN_COMMAND_CLEAR_JUKEBOX = 'clearjukebox';
2013-12-28 19:48:06 +01:00
/**
* Private properties
*/
private $maniaControl = null;
2013-12-31 12:25:03 +01:00
private $queuedMaps = array();
2013-12-30 11:23:00 +01:00
private $nextMap = null;
2013-12-28 19:48:06 +01:00
/**
* Create a new server jukebox
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-12-28 22:43:57 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ENDMAP, $this,'endMap');
2013-12-28 19:48:06 +01:00
2013-12-29 10:17:10 +01:00
// Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE, true);
2013-12-31 12:25:03 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN, false);
2013-12-29 12:01:13 +01:00
//Register Admin Commands
$this->maniaControl->commandManager->registerCommandListener(self::ADMIN_COMMAND_CLEAR_JUKEBOX, $this, 'command_ClearJukebox', true);
2013-12-31 12:25:03 +01:00
$this->maniaControl->commandManager->registerCommandListener(self::ADMIN_COMMAND_CLEAR_MAPQUEUE, $this, 'command_ClearJukebox', true);
2013-12-29 12:01:13 +01:00
}
/**
2013-12-31 12:25:03 +01:00
* Clears the map-queue via admin command clearjukebox
2013-12-29 12:01:13 +01:00
* @param array $chat
* @param Player $player
*/
2013-12-31 12:25:03 +01:00
public function command_ClearQueuedMaps(array $chat, Player $admin){
2013-12-29 12:01:13 +01:00
$title = $this->maniaControl->authenticationManager->getAuthLevelName($admin->authLevel);
//Destroy jukebox list
2013-12-31 12:25:03 +01:00
$this->queuedMaps = array();
2013-12-29 12:01:13 +01:00
2013-12-31 12:25:03 +01:00
$this->maniaControl->chat->sendInformation($title . ' $<' . $admin->nickname . '$> cleared the Queued-Map list!');
$this->maniaControl->log($title .' ' . Formatter::stripCodes($admin->nickname) . ' cleared the Queued-Map list!');
2013-12-29 12:01:13 +01:00
// Trigger callback
2013-12-31 12:25:03 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('clear'));
2013-12-28 19:48:06 +01:00
}
2013-12-28 22:43:57 +01:00
/**
2013-12-31 12:25:03 +01:00
* Adds a Map to the map-queue
2013-12-28 22:43:57 +01:00
* @param $login
* @param $uid
*/
2013-12-31 12:25:03 +01:00
public function addMapToMapQueue($login, $uid){ //TODO if from MX other message
2013-12-29 12:01:13 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
2013-12-28 19:48:06 +01:00
//Check if the map is already juked
2013-12-31 12:25:03 +01:00
if(array_key_exists($uid, $this->queuedMaps)){
$this->maniaControl->chat->sendError('Map is already in the Map-Queue', $login);
2013-12-28 19:48:06 +01:00
return;
}
2013-12-31 12:25:03 +01:00
//TODO recently maps not able to add to queue-amps setting, and management
2013-12-28 19:48:06 +01:00
2013-12-29 12:01:13 +01:00
$map = $this->maniaControl->mapManager->getMapByUid($uid);
2013-12-29 10:17:10 +01:00
2013-12-31 12:25:03 +01:00
$this->queuedMaps[$uid] = array($player, $map);
2013-12-28 19:48:06 +01:00
2013-12-31 12:25:03 +01:00
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> added $<' . $map->name . '$> to the Map-Queue');
2013-12-28 19:48:06 +01:00
// Trigger callback
2013-12-31 12:25:03 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('add', $this->queuedMaps[$uid]));
2013-12-28 22:43:57 +01:00
}
/**
2013-12-31 12:25:03 +01:00
* Revmoes a Map from the Map queue
2013-12-28 22:43:57 +01:00
* @param $login
* @param $uid
*/
2013-12-31 12:25:03 +01:00
public function removeFromMapQueue($login, $uid){
unset($this->queuedMaps[$uid]);
2013-12-28 22:43:57 +01:00
}
2013-12-29 10:17:10 +01:00
/**
* Called on endmap
* @param array $callback
*/
public function endMap(array $callback){
2013-12-30 11:23:00 +01:00
$this->nextMap = null;
2013-12-29 10:17:10 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE){
2013-12-29 12:01:13 +01:00
2013-12-29 10:17:10 +01:00
//Skip Map if requester has left
2013-12-31 12:25:03 +01:00
foreach($this->queuedMaps as $queuedMap){
$player = $queuedMap[0];
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
//found player, so play this map
2013-12-29 12:01:13 +01:00
if($this->maniaControl->playerManager->getPlayer($player->login) != null){
2013-12-29 10:17:10 +01:00
break;
}
2013-12-28 22:43:57 +01:00
2013-12-31 12:25:03 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAPQUEUE_ADMIN) == FALSE){
2013-12-29 12:01:13 +01:00
//Check if the juker is a admin
if($player->authLevel > 0){
break;
}
2013-12-29 10:17:10 +01:00
}
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
// Trigger callback
2013-12-31 12:25:03 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('skip', $queuedMap[0]));
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
//Player not found, so remove the map from the jukebox
2013-12-31 12:25:03 +01:00
array_shift($this->queuedMaps);
2013-12-29 10:17:10 +01:00
2013-12-31 12:25:03 +01:00
$this->maniaControl->chat->sendInformation('Requested Map skipped because $<' . $player->nickname . '$> left!');
2013-12-29 10:17:10 +01:00
}
}
2013-12-29 12:01:13 +01:00
2013-12-31 12:25:03 +01:00
$this->nextMap = array_shift($this->queuedMaps);
2013-12-28 22:43:57 +01:00
//Check if Jukebox is empty
2013-12-30 11:23:00 +01:00
if($this->nextMap == null)
2013-12-28 22:43:57 +01:00
return;
2013-12-30 11:23:00 +01:00
$map = $this->nextMap[1];
2013-12-28 22:43:57 +01:00
2013-12-30 11:23:00 +01:00
$success = $this->maniaControl->client->query('ChooseNextMap', $map->fileName);
2013-12-28 23:24:54 +01:00
if (!$success) {
trigger_error('[' . $this->maniaControl->client->getErrorCode() . '] ChooseNextMap - ' . $this->maniaControl->client->getErrorCode(), E_USER_WARNING);
return;
}
2013-12-28 19:48:06 +01:00
}
2013-12-28 22:43:57 +01:00
2013-12-30 11:23:00 +01:00
/**
* Returns the next Map if the next map is a juked map or null if it's not
* @return null
*/
public function getNextMap(){
return $this->nextMap;
}
/**
* Returns a list with the indexes of the juked maps
* @return array
*/
2013-12-31 12:25:03 +01:00
public function getQueuedMapsRanking(){
$i = 1;
2013-12-31 12:25:03 +01:00
$queuedMaps = array();
foreach($this->queuedMaps as $map){
$map = $map[1];
2013-12-31 12:25:03 +01:00
$queuedMaps[$map->uid] = $i;
$i++;
}
2013-12-31 12:25:03 +01:00
return $queuedMaps;
}
2013-12-28 23:35:40 +01:00
2013-12-29 10:17:10 +01:00
/**
* Dummy Function for testing
*/
2013-12-28 22:43:57 +01:00
public function printAllMaps(){
2013-12-31 12:25:03 +01:00
foreach($this->queuedMaps as $map){
2013-12-28 22:43:57 +01:00
$map = $map[1];
var_dump($map->name);
}
}
2013-12-28 19:48:06 +01:00
}