TrackManiaControl/application/core/Maps/Jukebox.php

158 lines
3.8 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-28 19:48:06 +01:00
use ManiaControl\ManiaControl;
/**
* Jukebox Class
*
* @author steeffeen & kremsy
*/
2013-12-28 22:43:57 +01:00
class Jukebox implements CallbackListener {
2013-12-28 19:48:06 +01:00
/**
* Constants
*/
const CB_JUKEBOX_CHANGED = 'Jukebox.JukeBoxChanged';
2013-12-29 10:17:10 +01:00
const SETTING_SKIP_MAP_ON_LEAVE = 'Skip Map when the requester leaves';
const SETTING_SKIP_JUKED_ADMIN = 'Skip Map when admin leaves';
2013-12-28 19:48:06 +01:00
/**
* Private properties
*/
private $maniaControl = null;
private $jukedMaps = array();
/**
* 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_BEGINMAP, $this,'beginMap');
$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);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_JUKED_ADMIN, false);
2013-12-28 19:48:06 +01:00
}
2013-12-28 22:43:57 +01:00
/**
* Adds a Map to the jukebox
* @param $login
* @param $uid
*/
public function addMapToJukebox($login, $uid){
2013-12-28 19:48:06 +01:00
//Check if the map is already juked
if(array_key_exists($uid, $this->jukedMaps)){
//TODO message map already juked
return;
}
//TODO recently maps not able to add to jukebox setting, and management
2013-12-29 10:17:10 +01:00
2013-12-28 22:43:57 +01:00
$this->jukedMaps[$uid] = array($login, $this->maniaControl->mapManager->getMapByUid($uid));
2013-12-28 19:48:06 +01:00
//TODO Message
// Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_JUKEBOX_CHANGED, array('add', $this->jukedMaps[$uid]));
2013-12-28 22:43:57 +01:00
}
/**
* Revmoes a Map from the jukebox
* @param $login
* @param $uid
*/
public function removeFromJukebox($login, $uid){
//unset($this->jukedMapsUid[$uid]);
unset($this->jukedMaps[$uid]);
}
public function beginMap(){
}
2013-12-29 10:17:10 +01:00
/**
* Called on endmap
* @param array $callback
*/
public function endMap(array $callback){
2013-12-28 23:24:54 +01:00
2013-12-29 10:17:10 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE){
//Skip Map if requester has left
for($i = 0; $i < count($this->jukedMaps); $i++){
$jukedMap = reset($this->jukedMaps);
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
//found player, so play this map
if($this->maniaControl->playerManager->getPlayer($jukedMap[0]) != null){
break;
}
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_JUKED_ADMIN) == FALSE){
//TODO check in database if a the juker of the map is admin, and if he is, just break
}
2013-12-28 22:43:57 +01:00
2013-12-29 10:17:10 +01:00
// Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_JUKEBOX_CHANGED, array('skip', $jukedMap[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
array_shift($this->jukedMaps);
//TODO Message, report skip
}
}
2013-12-28 22:43:57 +01:00
$nextMap = array_shift($this->jukedMaps);
//Check if Jukebox is empty
2013-12-28 23:24:54 +01:00
if($nextMap == null)
2013-12-28 22:43:57 +01:00
return;
$nextMap = $nextMap[1];
2013-12-28 23:24:54 +01:00
$success = $this->maniaControl->client->query('ChooseNextMap', $nextMap->fileName);
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
/**
* Returns a list with the indexes of the juked maps
* @return array
*/
public function getJukeBoxRanking(){
$i = 1;
$jukedMaps = array();
foreach($this->jukedMaps as $map){
$map = $map[1];
$jukedMaps[$map->uid] = $i;
$i++;
}
return $jukedMaps;
}
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(){
foreach($this->jukedMaps as $map){
$map = $map[1];
var_dump($map->name);
}
}
2013-12-28 19:48:06 +01:00
}