log method improvement

This commit is contained in:
Steffen Schröder 2014-01-05 14:13:18 +01:00
parent 5f27ab3e4a
commit 7c2508a0af
2 changed files with 58 additions and 50 deletions

View File

@ -130,7 +130,10 @@ class ManiaControl implements CommandListener {
* *
* @param string $message * @param string $message
*/ */
public function log($message) { public function log($message, $stripCodes = false) {
if ($stripCodes) {
$message = Formatter::stripCodes($message);
}
logMessage($message); logMessage($message);
} }
@ -364,8 +367,7 @@ class ManiaControl implements CommandListener {
// Set api version // Set api version
if (!$this->client->query('SetApiVersion', self::API_VERSION)) { if (!$this->client->query('SetApiVersion', self::API_VERSION)) {
trigger_error( trigger_error("Couldn't set API version '" . self::API_VERSION . "'! This might cause problems. " . $this->getClientErrorText());
"Couldn't set API version '" . self::API_VERSION . "'! This might cause problems. " . $this->getClientErrorText());
} }
// Connect finished // Connect finished

View File

@ -12,9 +12,8 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use MXInfoFetcher; use MXInfoFetcher;
/** /**
* Manager for maps * Manager for Maps
* *
* @author kremsy & steeffeen * @author kremsy & steeffeen
*/ */
@ -24,15 +23,16 @@ class MapManager implements CallbackListener {
*/ */
const TABLE_MAPS = 'mc_maps'; const TABLE_MAPS = 'mc_maps';
const CB_MAPLIST_UPDATED = 'MapManager.MapListUpdated'; const CB_MAPLIST_UPDATED = 'MapManager.MapListUpdated';
/** /**
* Private properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $mapCommands = null; private $mapCommands = null;
private $mapList = array(); private $mapList = array();
private $mapListUids = array(); private $mapListUids = array();
private $currentMap = null; private $currentMap = null;
/** /**
* Public Properties * Public Properties
*/ */
@ -45,19 +45,18 @@ class MapManager implements CallbackListener {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Init database tables // Init database tables
$this->initTables(); $this->initTables();
// Create map commands instance // Create map commands instance
$this->mapCommands = new MapCommands($maniaControl); $this->mapCommands = new MapCommands($maniaControl);
$this->mapQueue = new MapQueue($this->maniaControl); $this->mapQueue = new MapQueue($this->maniaControl);
// Register for callbacks // Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapListModified'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapListModified');
} }
/** /**
@ -129,55 +128,56 @@ class MapManager implements CallbackListener {
* @param \ManiaControl\Maps\Map $map * @param \ManiaControl\Maps\Map $map
* @return bool * @return bool
*/ */
private function addMap(Map $map) { //TODO needed? private function addMap(Map $map) { // TODO needed?
$this->saveMap($map); $this->saveMap($map);
$this->mapListUids[$map->uid] = $map; $this->mapListUids[$map->uid] = $map;
$this->mapList[] = $map; $this->mapList[] = $map;
return true; return true;
} }
/** /**
* Erases a Map * Erases a Map
*
* @param $id * @param $id
* @param $uid * @param $uid
*/ */
public function eraseMap($id, $uid) { public function eraseMap($id, $uid) {
$map = $this->mapListUids[$uid]; $map = $this->mapListUids[$uid];
$this->maniaControl->client->query('RemoveMap', $map->fileName); $this->maniaControl->client->query('RemoveMap', $map->fileName);
$this->maniaControl->chat->sendSuccess('Map $<' . $map->name . '$> removed!'); //TODO specified message, who done it? $this->maniaControl->chat->sendSuccess('Map $<' . $map->name . '$> removed!');
$this->maniaControl->log(Formatter::stripCodes('Map $<' . $map->name . '$> removed!')); // TODO specified message, who done it?
$this->maniaControl->log('Map $<' . $map->name . '$> removed!', true);
unset($this->mapListUids[$uid]); unset($this->mapListUids[$uid]);
unset($this->mapList[$id]); unset($this->mapList[$id]);
} }
/** /**
* Updates the full Map list, needed on Init, addMap and on ShuffleMaps * Updates the full Map list, needed on Init, addMap and on ShuffleMaps
* @return null
*/ */
private function updateFullMapList(){ private function updateFullMapList() {
if(!$this->maniaControl->client->query('GetMapList', 100,0)){ //fetch 100 Maps if (!$this->maniaControl->client->query('GetMapList', 100, 0)) { // fetch 100 Maps
trigger_error("Couldn't fetch mapList. " . $this->maniaControl->getClientErrorText()); trigger_error("Couldn't fetch mapList. " . $this->maniaControl->getClientErrorText());
return null; return null;
} }
$tempList = array(); $tempList = array();
$mapList = $this->maniaControl->client->getResponse(); $mapList = $this->maniaControl->client->getResponse();
foreach($mapList as $rpcMap){ foreach ($mapList as $rpcMap) {
if(array_key_exists($rpcMap["UId"], $this->mapListUids)){ //Map already exists, only update index if (array_key_exists($rpcMap["UId"], $this->mapListUids)) { // Map already exists, only update index
$tempList[] = $this->mapListUids[$rpcMap["UId"]]; $tempList[] = $this->mapListUids[$rpcMap["UId"]];
}else{ //Insert Map Object }
else { // Insert Map Object
$map = new Map($this->maniaControl, $rpcMap); $map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map); $this->saveMap($map);
$tempList[] = $map; $tempList[] = $map;
$this->mapListUids[$map->uid] = $map; $this->mapListUids[$map->uid] = $map;
} }
} }
//restore Sorted Maplist // restore Sorted Maplist
$this->mapList = $tempList; $this->mapList = $tempList;
// Trigger own callback // Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPLIST_UPDATED, array(self::CB_MAPLIST_UPDATED)); $this->maniaControl->callbackManager->triggerCallback(self::CB_MAPLIST_UPDATED, array(self::CB_MAPLIST_UPDATED));
} }
@ -193,7 +193,7 @@ class MapManager implements CallbackListener {
return null; return null;
} }
$rpcMap = $this->maniaControl->client->getResponse(); $rpcMap = $this->maniaControl->client->getResponse();
if(!array_key_exists($rpcMap["UId"], $this->mapListUids)){ if (!array_key_exists($rpcMap["UId"], $this->mapListUids)) {
$map = new Map($this->maniaControl, $rpcMap); $map = new Map($this->maniaControl, $rpcMap);
$this->addMap($map); $this->addMap($map);
return $map; return $map;
@ -212,63 +212,69 @@ class MapManager implements CallbackListener {
} }
/** /**
* Get Current Map
*
* @return Map currentMap * @return Map currentMap
*/ */
public function getCurrentMap(){ public function getCurrentMap() {
return $this->currentMap; return $this->currentMap;
} }
/** /**
* Returns map By UID * Returns map By UID
*
* @param $uid * @param $uid
* @return mixed * @return mixed
*/ */
public function getMapByUid($uid){ public function getMapByUid($uid) {
return $this->mapListUids[$uid]; return $this->mapListUids[$uid];
} }
/** /**
* Handle BeginMap callback * Handle BeginMap callback
* *
* @param array $callback * @param array $callback
*/ */
public function handleBeginMap(array $callback) { public function handleBeginMap(array $callback) {
if(array_key_exists($callback[1][0]["UId"], $this->mapListUids)){ //Map already exists, only update index if (array_key_exists($callback[1][0]["UId"], $this->mapListUids)) { // Map already exists, only update index
$this->currentMap = $this->mapListUids[$callback[1][0]["UId"]]; $this->currentMap = $this->mapListUids[$callback[1][0]["UId"]];
}else{ //can this ever happen? }
else { // can this ever happen?
$this->currentMap = $this->fetchCurrentMapInfo(); $this->currentMap = $this->fetchCurrentMapInfo();
} }
} }
/** /**
* MapList modified by other controller or web panels * MapList modified by other controller or web panels
*
* @param array $callback * @param array $callback
*/ */
public function mapListModified(array $callback){ public function mapListModified(array $callback) {
$this->updateFullMapList(); $this->updateFullMapList();
} }
/** /**
*
* @return array * @return array
*/ */
public function getMapList(){ public function getMapList() {
return $this->mapList; return $this->mapList;
} }
/** /**
* Adds a Map from Mania Exchange * Adds a Map from Mania Exchange
* @param $mapId *
* @param $login * @param $mapId
* @param $login
*/ */
public function addMapFromMx($mapId, $login){ public function addMapFromMx($mapId, $login) {
// Check if ManiaControl can even write to the maps dir // Check if ManiaControl can even write to the maps dir
if (!$this->maniaControl->client->query('GetMapsDirectory')) { if (!$this->maniaControl->client->query('GetMapsDirectory')) {
trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText()); trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $login); $this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $login);
return; return;
} }
$mapDir = $this->maniaControl->client->getResponse(); $mapDir = $this->maniaControl->client->getResponse();
if (!is_dir($mapDir)) { if (!is_dir($mapDir)) {
trigger_error("ManiaControl doesn't have have access to the maps directory in '{$mapDir}'."); trigger_error("ManiaControl doesn't have have access to the maps directory in '{$mapDir}'.");
@ -283,27 +289,27 @@ class MapManager implements CallbackListener {
return; return;
} }
$mapDir .= $downloadDirectory . '/'; $mapDir .= $downloadDirectory . '/';
// Download the map // Download the map
if (is_numeric($mapId)) { if (is_numeric($mapId)) {
// Load from MX // Load from MX
$serverInfo = $this->maniaControl->server->getSystemInfo(); $serverInfo = $this->maniaControl->server->getSystemInfo();
$title = strtolower(substr($serverInfo['TitleId'], 0, 2)); $title = strtolower(substr($serverInfo['TitleId'], 0, 2));
// Check if map exists // Check if map exists
$url = "http://api.mania-exchange.com/{$title}/maps/{$mapId}?format=json"; $url = "http://api.mania-exchange.com/{$title}/maps/{$mapId}?format=json";
$mapInfo = FileUtil::loadFile($url, "application/json"); $mapInfo = FileUtil::loadFile($url, "application/json");
if (!$mapInfo || strlen($mapInfo) <= 0) { if (!$mapInfo || strlen($mapInfo) <= 0) {
// Invalid id // Invalid id
$this->maniaControl->chat->sendError('Invalid MX-Id!', $login); $this->maniaControl->chat->sendError('Invalid MX-Id!', $login);
return; return;
} }
$mapInfo = json_decode($mapInfo, true); $mapInfo = json_decode($mapInfo, true);
$mapInfo = $mapInfo[0]; $mapInfo = $mapInfo[0];
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}"; $url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}";
$file = FileUtil::loadFile($url); $file = FileUtil::loadFile($url);
if (!$file) { if (!$file) {
@ -321,7 +327,7 @@ class MapManager implements CallbackListener {
} }
// Check for valid map // Check for valid map
$mapFileName = $downloadDirectory . '/' . $fileName; $mapFileName = $downloadDirectory . '/' . $fileName;
if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) { if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) {
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText()); trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError('Error checking map!', $login); $this->maniaControl->chat->sendError('Error checking map!', $login);
@ -334,15 +340,15 @@ class MapManager implements CallbackListener {
return; return;
} }
// Add map to map list // Add map to map list
if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) { //TODO irgentein bug? if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) { // TODO irgentein bug?
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login); $this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login);
return; return;
} }
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!'); $this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
$this->updateFullMapList(); $this->updateFullMapList();
//Queue requested Map // Queue requested Map
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo['MapUID']); $this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo['MapUID']);
} }
// TODO: add local map by filename // TODO: add local map by filename