map communcation refactor and testing

This commit is contained in:
kremsy 2015-07-11 13:41:46 +02:00
parent cc7c36f3d8
commit a3cba8de76
2 changed files with 132 additions and 123 deletions

View File

@ -139,98 +139,8 @@ class MapManager implements CallbackListener, CommunicationListener {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt");
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WRITE_OWN_MAPLIST_FILE, false);
// Communication Listenings
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_CURRENT_MAP, $this, function ($data) {
return new CommunicationAnswer($this->getCurrentMap());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP_LIST, $this, function ($data) {
return new CommunicationAnswer($this->getMapList());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP, $this, function ($data) {
if (!is_object($data)) {
return new CommunicationAnswer("Error in provided Data", true);
}
if (property_exists($data, "mxId")) {
return new CommunicationAnswer($this->getMapByMxId($data->mxId));
} else if (property_exists($data, "mapUid")) {
return new CommunicationAnswer($this->getMapByUid($data->mxUid));
} else {
return new CommunicationAnswer("No mxId or mapUid provided.", true);
}
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::ADD_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mxId")) {
return new CommunicationAnswer("No valid mxId provided.", true);
}
if (!$this->getMapByMxId($data->mxId)) {
return new CommunicationAnswer("Map not found.", true);
}
$this->addMapFromMx($data->mxId, null);
return new CommunicationAnswer();
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::REMOVE_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$erase = false;
if (property_exists($data, "eraseMapFile")) {
$erase = $data->eraseMapFile;
}
$showMessage = true;
if (property_exists($data, "showChatMessage")) {
$showMessage = $data->showChatMessage;
}
$success = $this->removeMap(null, $data->mapUid, $erase, $showMessage);
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::UPDATE_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$this->updateMap(null, $data->mapUid);
return new CommunicationAnswer();
});
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`mxid` int(11),
`uid` varchar(50) NOT NULL,
`name` varchar(150) NOT NULL,
`authorLogin` varchar(100) NOT NULL,
`fileName` varchar(100) NOT NULL,
`environment` varchar(50) NOT NULL,
`mapType` varchar(50) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Map Data' AUTO_INCREMENT=1;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
return $result;
//Initlaize Communication Listenings
$this->initalizeCommunicationListenings();
}
/**
@ -391,8 +301,12 @@ class MapManager implements CallbackListener, CommunicationListener {
// Show Message
if ($message) {
$action = ($eraseFile ? 'erased' : 'removed');
$message = $admin->getEscapedNickname() . ' ' . $action . ' ' . $map->getEscapedName() . '!';
$action = ($eraseFile ? 'erased' : 'removed');
if ($admin) {
$message = $admin->getEscapedNickname() . ' ' . $action . ' ' . $map->getEscapedName() . '!';
} else {
$message = $map->getEscapedName() . ' got ' . $action . '!';
}
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
}
@ -547,19 +461,21 @@ class MapManager implements CallbackListener, CommunicationListener {
}
$map->lastUpdate = time();
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
if (!$update) {
// Message
$message = $player->getEscapedNickname() . ' added $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
// Queue requested Map
$this->maniaControl->getMapManager()->getMapQueue()->addMapToMapQueue($login, $mapInfo->uid);
} else {
$message = $player->getEscapedNickname() . ' updated $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
//TODO messages for communication
if ($login) {
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
if (!$update) {
// Message
$message = $player->getEscapedNickname() . ' added $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
// Queue requested Map
$this->maniaControl->getMapManager()->getMapQueue()->addMapToMapQueue($login, $mapInfo->uid);
} else {
$message = $player->getEscapedNickname() . ' updated $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
}
}
}
@ -970,4 +886,99 @@ class MapManager implements CallbackListener, CommunicationListener {
public function getMapsCount() {
return count($this->maps);
}
private function initalizeCommunicationListenings() {
// Communication Listenings
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_CURRENT_MAP, $this, function ($data) {
return new CommunicationAnswer($this->getCurrentMap());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP_LIST, $this, function ($data) {
return new CommunicationAnswer($this->getMaps());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP, $this, function ($data) {
if (!is_object($data)) {
return new CommunicationAnswer("Error in provided Data", true);
}
if (property_exists($data, "mxId")) {
return new CommunicationAnswer($this->getMapByMxId($data->mxId));
} else if (property_exists($data, "mapUid")) {
return new CommunicationAnswer($this->getMapByUid($data->mapUid));
} else {
return new CommunicationAnswer("No mxId or mapUid provided.", true);
}
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::ADD_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mxId")) {
return new CommunicationAnswer("No valid mxId provided.", true);
}
$this->addMapFromMx($data->mxId, null);
return new CommunicationAnswer();
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::REMOVE_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
if (!$this->getMapByUid($data->mapUid)) {
return new CommunicationAnswer("Map not found.", true);
}
$erase = false;
if (property_exists($data, "eraseMapFile")) {
$erase = $data->eraseMapFile;
}
$showMessage = true;
if (property_exists($data, "showChatMessage")) {
$showMessage = $data->showChatMessage;
}
$success = $this->removeMap(null, $data->mapUid, $erase, $showMessage);
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::UPDATE_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$this->updateMap(null, $data->mapUid);
return new CommunicationAnswer();
});
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`mxid` int(11),
`uid` varchar(50) NOT NULL,
`name` varchar(150) NOT NULL,
`authorLogin` varchar(100) NOT NULL,
`fileName` varchar(100) NOT NULL,
`environment` varchar(50) NOT NULL,
`mapType` varchar(50) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Map Data' AUTO_INCREMENT=1;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
return $result;
}
}

View File

@ -42,10 +42,10 @@ class MapQueue implements CallbackListener, CommandListener {
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $queuedMaps = array();
private $nextMap = null;
private $buffer = array();
private $nextNoQueue = false;
private $queuedMaps = array();
private $nextMap = null;
private $buffer = array();
private $nextNoQueue = false;
/**
* Construct a new map queue instance
@ -108,8 +108,7 @@ class MapQueue implements CallbackListener, CommandListener {
* @param Player $admin |null
*/
public function clearMapQueue(Player $admin = null) {
if ($admin && !$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)
) {
if ($admin && !$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)) {
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($admin);
return;
}
@ -308,8 +307,7 @@ class MapQueue implements CallbackListener, CommandListener {
// Check if map is in the buffer
if (in_array($uid, $this->buffer)) {
$this->maniaControl->getChat()->sendError('That map has recently been played!', $login);
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)
) {
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CLEAR_MAPQUEUE)) {
return;
}
}
@ -329,10 +327,10 @@ class MapQueue implements CallbackListener, CommandListener {
/**
* Remove a Map from the Map queue
*
* @param Player $player
* @param string $uid
* @param Player|null $player
* @param string $uid
*/
public function removeFromMapQueue(Player $player, $uid) {
public function removeFromMapQueue($player, $uid) {
if (!isset($this->queuedMaps[$uid])) {
return;
}
@ -340,7 +338,9 @@ class MapQueue implements CallbackListener, CommandListener {
$map = $this->queuedMaps[$uid][1];
unset($this->queuedMaps[$uid]);
$this->maniaControl->getChat()->sendInformation('$fa0$<$fff' . $map->name . '$> is removed from the Map-Queue by $<$fff' . $player->nickname . '$>.');
if ($player) {
$this->maniaControl->getChat()->sendInformation('$fa0$<$fff' . $map->name . '$> is removed from the Map-Queue by $<$fff' . $player->nickname . '$>.');
}
// Trigger callback
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAPQUEUE_CHANGED, array('remove', $map));
@ -359,8 +359,7 @@ class MapQueue implements CallbackListener, CommandListener {
}
$this->nextMap = null;
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SKIP_MAP_ON_LEAVE)
) {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SKIP_MAP_ON_LEAVE)) {
// Skip Map if requester has left
foreach ($this->queuedMaps as $queuedMap) {
$player = $queuedMap[0];
@ -423,8 +422,7 @@ class MapQueue implements CallbackListener, CommandListener {
return;
}
if (count($this->buffer) >= $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_BUFFERSIZE)
) {
if (count($this->buffer) >= $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_BUFFERSIZE)) {
array_shift($this->buffer);
}