improved send message, error, exception to admin

get connected admins method
This commit is contained in:
Steffen Schröder 2014-05-20 14:59:53 +02:00
parent 6ca5decdb4
commit 33b86cdca0
2 changed files with 64 additions and 40 deletions

View File

@ -194,22 +194,49 @@ class AuthenticationManager implements CallbackListener {
return $success;
}
/**
* Get all connected Players with at least the given Auth Level
*
* @param int $authLevel
* @return array
*/
public function getConnectedAdmins($authLevel = self::AUTH_LEVEL_MODERATOR) {
$players = $this->maniaControl->playerManager->getPlayers();
$admins = array();
foreach ($players as $player) {
if (self::checkRight($player, $authLevel)) {
array_push($admins, $player);
}
}
return $admins;
}
/**
* Check whether the Player has enough Rights
*
* @param Player $player
* @param int|Setting $neededAuthLevel
* @return bool
*/
public static function checkRight(Player $player, $neededAuthLevel) {
if ($neededAuthLevel instanceof Setting) {
$neededAuthLevel = $neededAuthLevel->value;
}
return ($player->authLevel >= $neededAuthLevel);
}
/**
* Get a List of all Admins
*
* @param int $authLevel
* @return array null
*/
public function getAdmins($authLevel = null) {
public function getAdmins($authLevel = self::AUTH_LEVEL_MODERATOR) {
// TODO: return Player objects
$mysqli = $this->maniaControl->database->mysqli;
if ($authLevel === null) {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "`
WHERE `authLevel` > 0
ORDER BY `authLevel` DESC;";
} else {
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "`
WHERE `authLevel` = {$authLevel};";
}
$query = "SELECT * FROM `" . PlayerManager::TABLE_PLAYERS . "`
WHERE `authLevel` > " . $authLevel . "
ORDER BY `authLevel` DESC;";
$result = $mysqli->query($query);
if (!$result) {
trigger_error($mysqli->error);
@ -219,6 +246,7 @@ class AuthenticationManager implements CallbackListener {
while ($row = $result->fetch_object()) {
array_push($admins, $row);
}
$result->free();
return $admins;
}
@ -291,20 +319,6 @@ class AuthenticationManager implements CallbackListener {
return $this->checkRight($player, $right);
}
/**
* Check whether the Player has enough Rights
*
* @param Player $player
* @param int|Setting $neededAuthLevel
* @return bool
*/
public static function checkRight(Player $player, $neededAuthLevel) {
if ($neededAuthLevel instanceof Setting) {
$neededAuthLevel = $neededAuthLevel->value;
}
return ($player->authLevel >= $neededAuthLevel);
}
/**
* Define a Minimum Right Level needed for an Action
*

View File

@ -76,7 +76,9 @@ class Chat {
$this->maniaControl->client->chatSendServerMessage($chatMessage);
} else {
$chatMessage = '$<$z$ff0' . $this->getPrefix($prefix) . $message . '$>';
$login = Player::parseLogin($login);
if (!is_array($login)) {
$login = Player::parseLogin($login);
}
try {
$this->maniaControl->client->chatSendServerMessage($chatMessage, $login);
} catch (UnknownPlayerException $e) {
@ -102,32 +104,27 @@ class Chat {
}
/**
* Sends a Error Message to all Connected Admins
* Send an Error Message to all Connected Admins
*
* @param $message
* @param int $minLevel (Constant from AuthenticationManager)
* @param bool $prefix
* @param string $message
* @param int $minLevel
* @param bool $prefix
*/
public function sendErrorToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_ERROR);
$this->sendMessageToAdmins($format . $message, $prefix);
$this->sendMessageToAdmins($format . $message, $minLevel, $prefix);
}
/**
* Sends a Message to all Connected Admins
*
* @param $message
* @param int $minLevel (Constant from AuthenticationManager)
* @param bool $prefix
* @param string $message
* @param int $minLevel
* @param bool|string $prefix
*/
public function sendMessageToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
//TODO specifiy in player or adminmanager a getAdmins() with minlevel function
foreach ($this->maniaControl->playerManager->getPlayers() as $player) {
/** @var Player $player */
if ($this->maniaControl->authenticationManager->checkRight($player, $minLevel)) {
$this->sendChat($message, $player->login, $prefix);
}
}
$admins = $this->maniaControl->authenticationManager->getConnectedAdmins($minLevel);
$this->sendChat($message, $admins, $prefix);
}
/**
@ -135,7 +132,7 @@ class Chat {
*
* @param string $message
* @param string $login
* @param string|bool $prefix
* @param bool|string $prefix
* @return bool
*/
public function sendSuccess($message, $login = null, $prefix = true) {
@ -168,6 +165,19 @@ class Chat {
return $this->sendChat($format . $message, $login, $prefix);
}
/**
* Send a Exception Message to all Connected Admins
*
* @param \Exception $exception
* @param int $minLevel
* @param bool|string $prefix
*/
public function sendExceptionToAdmins(\Exception $exception, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_ERROR);
$message = $format . "Exception: '{$exception->getMessage()}' ({$exception->getCode()})";
$this->sendMessageToAdmins($message, $minLevel, $prefix);
}
/**
* Send an usage info message to the given login
*