improved mapmanager

This commit is contained in:
kremsy 2014-01-10 12:22:37 +01:00
parent a925f423a8
commit b37a1ed3b7
3 changed files with 30 additions and 32 deletions

View File

@ -95,7 +95,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
* @param \ManiaControl\Players\Player $player * @param \ManiaControl\Players\Player $player
*/ */
public function command_RemoveMap(array $chat, Player $player) { public function command_RemoveMap(array $chat, Player $player) {
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) { if(!$this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_REMOVE_MAP)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
@ -105,13 +105,8 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login); $this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return; return;
} }
// Remove map
if(!$this->maniaControl->client->query('RemoveMap', $map->fileName)) { $this->maniaControl->mapManager->removeMap($player, $map);
trigger_error("Couldn't remove current map. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return;
}
$this->maniaControl->chat->sendSuccess('Map removed.', $player->login);
} }
/** /**

View File

@ -687,7 +687,8 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->mapManager->addMapFromMx($mapId, $player->login); $this->maniaControl->mapManager->addMapFromMx($mapId, $player->login);
break; break;
case self::ACTION_ERASE_MAP: case self::ACTION_ERASE_MAP:
$this->maniaControl->mapManager->removeMap($mapId, $actionArray[3]); //actionArray[3] = mapUid
$this->maniaControl->mapManager->removeMap($player, $actionArray[3]);
$this->showMapList($player); $this->showMapList($player);
break; break;
case self::ACTION_SWITCH_MAP: case self::ACTION_SWITCH_MAP:

View File

@ -7,6 +7,7 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\FileUtil; use ManiaControl\FileUtil;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
require_once __DIR__ . '/Map.php'; require_once __DIR__ . '/Map.php';
require_once __DIR__ . '/MapCommands.php'; require_once __DIR__ . '/MapCommands.php';
@ -42,7 +43,6 @@ class MapManager implements CallbackListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $maps = array(); private $maps = array();
private $mapsUids = array();
private $currentMap = null; private $currentMap = null;
/** /**
@ -137,18 +137,22 @@ class MapManager implements CallbackListener {
/** /**
* Remove a Map * Remove a Map
* *
* @param int $id
* @param string $uid * @param string $uid
* @param bool $eraseFile * @param bool $eraseFile
*/ */
public function removeMap($id, $uid, $eraseFile = false) { //TODO erasefile? public function removeMap(Player $admin, $uid, $eraseFile = false) { //TODO erasefile?
$map = $this->mapsUids[$uid]; $map = $this->maps[$uid];
$this->maniaControl->client->query('RemoveMap', $map->fileName);
$this->maniaControl->chat->sendSuccess('Map $<' . $map->name . '$> removed!'); // Remove map
// TODO specified message, who done it? if(!$this->maniaControl->client->query('RemoveMap', $map->fileName)) {
$this->maniaControl->log('Map $<' . $map->name . '$> removed!', true); trigger_error("Couldn't remove current map. " . $this->maniaControl->getClientErrorText());
unset($this->mapsUids[$uid]); $this->maniaControl->chat->sendError("Couldn't remove map.", $admin);
unset($this->maps[$id]); return;
}
$this->maniaControl->chat->sendSuccess('$<' . $admin->nickname . '$> removed $<' . $map->name . '$>');
$this->maniaControl->log('$<' . $admin->nickname . '$> removed $<' . $map->name . '$>', true);
unset($this->maps[$uid]);
} }
/** /**
@ -164,14 +168,13 @@ class MapManager implements CallbackListener {
$maps = $this->maniaControl->client->getResponse(); $maps = $this->maniaControl->client->getResponse();
foreach($maps as $rpcMap) { foreach($maps as $rpcMap) {
if(array_key_exists($rpcMap["UId"], $this->mapsUids)) { if(array_key_exists($rpcMap["UId"], $this->maps)) {
// Map already exists, only update index // Map already exists, only update index
$tempList[] = $this->mapsUids[$rpcMap["UId"]]; $tempList[$rpcMap["UId"]] = $this->maps[$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->uid] = $map;
$this->mapsUids[$map->uid] = $map;
} }
} }
@ -193,15 +196,14 @@ class MapManager implements CallbackListener {
return false; return false;
} }
$rpcMap = $this->maniaControl->client->getResponse(); $rpcMap = $this->maniaControl->client->getResponse();
if(array_key_exists($rpcMap["UId"], $this->mapsUids)) { if(array_key_exists($rpcMap["UId"], $this->maps)) {
$this->currentMap = $this->mapsUids[$rpcMap["UId"]]; $this->currentMap = $this->maps[$rpcMap["UId"]];
return true; return true;
} }
$map = new Map($this->maniaControl, $rpcMap); $map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map); $this->saveMap($map);
$this->mapsUids[$map->uid] = $map; $this->maps[$map->uid] = $map;
$this->maps[] = $map; $this->currentMap = $map;
$this->currentMap = $map;
return true; return true;
} }
@ -232,10 +234,10 @@ class MapManager implements CallbackListener {
* @return mixed * @return mixed
*/ */
public function getMapByUid($uid) { public function getMapByUid($uid) {
if(!isset($this->mapsUids[$uid])) { if(!isset($this->maps[$uid])) {
return null; return null;
} }
return $this->mapsUids[$uid]; return $this->maps[$uid];
} }
/** /**
@ -244,9 +246,9 @@ class MapManager implements CallbackListener {
* @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->mapsUids)) { if(array_key_exists($callback[1][0]["UId"], $this->maps)) {
// Map already exists, only update index // Map already exists, only update index
$this->currentMap = $this->mapsUids[$callback[1][0]["UId"]]; $this->currentMap = $this->maps[$callback[1][0]["UId"]];
} else { } else {
// can this ever happen? // can this ever happen?
$this->fetchCurrentMap(); $this->fetchCurrentMap();