improved map manager, actions, list

This commit is contained in:
Steffen Schröder
2014-01-06 18:50:26 +01:00
parent 012db8d8f4
commit 2e119828bc
4 changed files with 265 additions and 265 deletions

View File

@@ -15,7 +15,6 @@ use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\IconManager;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use WidgetPlugin;
/**
* Class offering commands to manage maps
@@ -32,10 +31,9 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
const ACTION_SKIP_MAP = 'MapList.NextMap';
/**
* Private properties
* Private Properties
*/
private $maniaControl = null;
private $mapList = null;
/**
* Create MapCommands instance
@@ -45,7 +43,6 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initActionsMenuButtons();
$this->mapList = new MapList($this->maniaControl);
// Register for admin chat commands
$this->maniaControl->commandManager->registerCommandListener('nextmap', $this, 'command_NextMap', true);
@@ -54,9 +51,8 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
$this->maniaControl->commandManager->registerCommandListener('removemap', $this, 'command_RemoveMap', true);
// Register for player chat commands
$this->maniaControl->commandManager->registerCommandListener('xlist', $this, 'command_xList');
$this->maniaControl->commandManager->registerCommandListener('list', $this, 'command_List');
$this->maniaControl->commandManager->registerCommandListener('maps', $this, 'command_List');
$this->maniaControl->commandManager->registerCommandListener(array('maps', 'list'), $this, 'command_List');
$this->maniaControl->commandManager->registerCommandListener(array('xmaps', 'xlist'), $this, 'command_xList');
// Menu Buttons
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_XLIST, $this, 'command_xList');
@@ -75,13 +71,13 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
$itemQuad->setImageFocus($this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_MOVER));
$itemQuad->setAction(self::ACTION_OPEN_XLIST);
$this->maniaControl->actionsMenu->addPlayerMenuItem($itemQuad, 5, 'Open MX List');
// Menu Open List
$itemQuad = new Quad_Icons64x64_1();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_ToolRoot);
$itemQuad->setAction(self::ACTION_OPEN_MAPLIST);
$this->maniaControl->actionsMenu->addPlayerMenuItem($itemQuad, 10, 'Open MapList');
// Menu RestartMap
$itemQuad = new Quad_UIConstruction_Buttons();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Reload);
@@ -132,7 +128,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
// TODO: user mx fetcher
// TODO: Use MX fetcher
$params = explode(' ', $chatCallback[1][2], 2);
if (count($params) < 2) {
$this->maniaControl->chat->sendUsageInfo('Usage example: //addmap 1234', $player->login);
@@ -144,7 +140,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
}
/**
* Handle nextmap command
* Handle /nextmap Command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
@@ -172,13 +168,13 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
}
/**
* Handle list maps command
* Handle /maps command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_List(array $chatCallback, Player $player) {
$this->mapList->showMapList($player);
$this->maniaControl->mapManager->mapList->showMapList($player);
}
/**
@@ -188,6 +184,6 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
* @param Player $player
*/
public function command_xList(array $chatCallback, Player $player) {
$this->mapList->showManiaExchangeList($chatCallback, $player);
$this->maniaControl->mapManager->mapList->showManiaExchangeList($chatCallback, $player);
}
}

View File

@@ -62,7 +62,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this,
'handleManialinkPageAnswer');
$this->maniaControl->callbackManager->registerCallbackListener(MapQueue::CB_MAPQUEUE_CHANGED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_MAPLIST_UPDATED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_MAPS_UPDATED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_KARMA_UPDATED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'updateWidget');
// TODO not working yet
@@ -232,7 +232,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
// Get Maplist
$mapList = $this->maniaControl->mapManager->getMapList();
$mapList = $this->maniaControl->mapManager->getMaps();
$this->mapListShown[$player->login] = self::SHOW_MAP_LIST;

View File

@@ -7,7 +7,6 @@ use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use MXInfoFetcher;
require_once __DIR__ . '/Map.php';
require_once __DIR__ . '/MapCommands.php';
@@ -24,22 +23,23 @@ class MapManager implements CallbackListener {
* Constants
*/
const TABLE_MAPS = 'mc_maps';
const CB_MAPLIST_UPDATED = 'MapManager.MapListUpdated';
const CB_MAPS_UPDATED = 'MapManager.MapsUpdated';
const CB_KARMA_UPDATED = 'MapManager.KarmaUpdated';
/**
* Private Properties
*/
private $maniaControl = null;
private $mapCommands = null;
private $mapList = array();
private $mapListUids = array();
private $currentMap = null;
/**
* Public Properties
*/
public $mapQueue = null;
public $mapCommands = null;
public $mapList = null;
/**
* Private Properties
*/
private $maniaControl = null;
private $maps = array();
private $mapsUids = array();
private $currentMap = null;
/**
* Construct map manager
@@ -48,18 +48,17 @@ class MapManager implements CallbackListener {
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Init database tables
$this->initTables();
// Create map commands instance
$this->mapList = new MapList($this->maniaControl);
$this->mapCommands = new MapCommands($maniaControl);
$this->mapQueue = new MapQueue($this->maniaControl);
// Register for callbacks
$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_MP_MAPLISTMODIFIED, $this, 'mapListModified');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified');
}
/**
@@ -90,10 +89,10 @@ class MapManager implements CallbackListener {
}
/**
* Save map to the database
* Save a Map in the Database
*
* @param \ManiaControl\Maps\Map $map
* @return boolean
* @return bool
*/
private function saveMap(Map &$map) {
$mysqli = $this->maniaControl->database->mysqli;
@@ -126,82 +125,75 @@ class MapManager implements CallbackListener {
}
/**
* Add a map to the MapList
* Remove a Map
*
* @param Map $map
* @return bool
* @param int $id
* @param string $uid
* @param bool $eraseFile
*/
private function addMap(Map $map) { // TODO needed?
$this->saveMap($map);
$this->mapListUids[$map->uid] = $map;
$this->mapList[] = $map;
return true;
}
/**
* Erases a Map
*
* @param $id
* @param $uid
*/
public function eraseMap($id, $uid) {
$map = $this->mapListUids[$uid];
public function removeMap($id, $uid, $eraseFile = false) {
$map = $this->mapsUids[$uid];
$this->maniaControl->client->query('RemoveMap', $map->fileName);
$this->maniaControl->chat->sendSuccess('Map $<' . $map->name . '$> removed!');
// TODO specified message, who done it?
$this->maniaControl->log('Map $<' . $map->name . '$> removed!', true);
unset($this->mapListUids[$uid]);
unset($this->mapList[$id]);
unset($this->mapsUids[$uid]);
unset($this->maps[$id]);
}
/**
* Updates the full Map list, needed on Init, addMap and on ShuffleMaps
*/
private function updateFullMapList() {
if (!$this->maniaControl->client->query('GetMapList', 100, 0)) { // fetch 100 Maps
if (!$this->maniaControl->client->query('GetMapList', 100, 0)) {
trigger_error("Couldn't fetch mapList. " . $this->maniaControl->getClientErrorText());
return null;
}
$tempList = array();
$mapList = $this->maniaControl->client->getResponse();
foreach ($mapList as $rpcMap) {
if (array_key_exists($rpcMap["UId"], $this->mapListUids)) { // Map already exists, only update index
$tempList[] = $this->mapListUids[$rpcMap["UId"]];
$maps = $this->maniaControl->client->getResponse();
foreach ($maps as $rpcMap) {
if (array_key_exists($rpcMap["UId"], $this->mapsUids)) {
// Map already exists, only update index
$tempList[] = $this->mapsUids[$rpcMap["UId"]];
}
else { // Insert Map Object
$map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map);
$tempList[] = $map;
$this->mapListUids[$map->uid] = $map;
$this->mapsUids[$map->uid] = $map;
}
}
// restore Sorted Maplist
$this->mapList = $tempList;
$this->maps = $tempList;
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPLIST_UPDATED, array(self::CB_MAPLIST_UPDATED));
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPS_UPDATED, array(self::CB_MAPS_UPDATED));
}
/**
* Fetch current map
* Fetch current Map
*
* @return \ManiaControl\Maps\Map
* @return bool
*/
private function fetchCurrentMapInfo() {
private function fetchCurrentMap() {
if (!$this->maniaControl->client->query('GetCurrentMapInfo')) {
trigger_error("Couldn't fetch map info. " . $this->maniaControl->getClientErrorText());
return null;
return false;
}
$rpcMap = $this->maniaControl->client->getResponse();
if (!array_key_exists($rpcMap["UId"], $this->mapListUids)) {
$map = new Map($this->maniaControl, $rpcMap);
$this->addMap($map);
return $map;
if (!array_key_exists($rpcMap["UId"], $this->mapsUids)) {
$this->currentMap = $this->mapsUids[$rpcMap["UId"]];
return true;
}
return $this->mapListUids[$rpcMap["UId"]];
$map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map);
$this->mapsUids[$map->uid] = $map;
$this->maps[] = $map;
$this->currentMap = $map;
return true;
}
/**
@@ -211,7 +203,7 @@ class MapManager implements CallbackListener {
*/
public function handleOnInit(array $callback) {
$this->updateFullMapList();
$this->currentMap = $this->fetchCurrentMapInfo();
$this->fetchCurrentMap();
}
/**
@@ -230,7 +222,10 @@ class MapManager implements CallbackListener {
* @return mixed
*/
public function getMapByUid($uid) {
return $this->mapListUids[$uid];
if (!isset($this->mapsUids[$uid])) {
return null;
}
return $this->mapsUids[$uid];
}
/**
@@ -239,20 +234,22 @@ class MapManager implements CallbackListener {
* @param array $callback
*/
public function handleBeginMap(array $callback) {
if (array_key_exists($callback[1][0]["UId"], $this->mapListUids)) { // Map already exists, only update index
$this->currentMap = $this->mapListUids[$callback[1][0]["UId"]];
if (array_key_exists($callback[1][0]["UId"], $this->mapsUids)) {
// Map already exists, only update index
$this->currentMap = $this->mapsUids[$callback[1][0]["UId"]];
}
else { // can this ever happen?
$this->currentMap = $this->fetchCurrentMapInfo();
else {
// can this ever happen?
$this->fetchCurrentMap();
}
}
/**
* MapList modified by other controller or web panels
* Handle Maps Modified Callback
*
* @param array $callback
*/
public function mapListModified(array $callback) {
public function mapsModified(array $callback) {
$this->updateFullMapList();
}
@@ -260,8 +257,8 @@ class MapManager implements CallbackListener {
*
* @return array
*/
public function getMapList() {
return $this->mapList;
public function getMaps() {
return $this->maps;
}
/**
@@ -343,7 +340,8 @@ class MapManager implements CallbackListener {
return;
}
// Add map to map list
if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) { // TODO irgentein bug?
if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) {
// TODO irgendein bug?
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login);
return;
}