diff --git a/core/Admin/AuthenticationManager.php b/core/Admin/AuthenticationManager.php index 8aa8961b..dd6ef9c1 100644 --- a/core/Admin/AuthenticationManager.php +++ b/core/Admin/AuthenticationManager.php @@ -4,6 +4,7 @@ namespace ManiaControl\Admin; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\Callbacks; +use ManiaControl\Callbacks\EchoListener; use ManiaControl\Logger; use ManiaControl\ManiaControl; use ManiaControl\Players\Player; @@ -17,7 +18,7 @@ use ManiaControl\Settings\Setting; * @copyright 2014-2015 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class AuthenticationManager implements CallbackListener { +class AuthenticationManager implements CallbackListener, EchoListener { /* * Constants */ @@ -33,6 +34,8 @@ class AuthenticationManager implements CallbackListener { const AUTH_NAME_MASTERADMIN = 'MasterAdmin'; const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged'; + const ECHO_GRANT_LEVEL = 'ManiaControl.AuthenticationManager.GrandLevel'; + const ECHO_REVOKE_LEVEL = 'ManiaControl.AuthenticationManager.RevokeLevel'; /* * Private properties */ @@ -52,6 +55,26 @@ class AuthenticationManager implements CallbackListener { // Callbacks $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit'); + + // Echo Grant Level Command (Usage: sendEcho json_encode("player" => "loginName", "level" => "AUTH_LEVEL_NUMBER") + $this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_GRANT_LEVEL, $this, function ($params) { + if (property_exists($params, 'level') && property_exists($params, 'player')) { + $player = $this->maniaControl->getPlayerManager()->getPlayer($params->player); + if ($player) { + $this->grantAuthLevel($player, $params->level); + } + } + }); + + // Echo Revoke Level Command (Usage: sendEcho json_encode("player" => "loginName") + $this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_REVOKE_LEVEL, $this, function ($params) { + if (property_exists($params, 'player')) { + $player = $this->maniaControl->getPlayerManager()->getPlayer($params->player); + if ($player) { + $this->maniaControl->getAuthenticationManager()->grantAuthLevel($player, self::AUTH_LEVEL_PLAYER); + } + } + }); } /** @@ -83,12 +106,12 @@ class AuthenticationManager implements CallbackListener { */ public static function getAuthLevelInt($authLevelParam) { if (is_object($authLevelParam) && property_exists($authLevelParam, 'authLevel')) { - return (int)$authLevelParam->authLevel; + return (int) $authLevelParam->authLevel; } if (is_string($authLevelParam)) { return self::getAuthLevel($authLevelParam); } - return (int)$authLevelParam; + return (int) $authLevelParam; } /** @@ -98,7 +121,7 @@ class AuthenticationManager implements CallbackListener { * @return int */ public static function getAuthLevel($authLevelName) { - $authLevelName = (string)$authLevelName; + $authLevelName = (string) $authLevelName; switch ($authLevelName) { case self::AUTH_NAME_MASTERADMIN: return self::AUTH_LEVEL_MASTERADMIN; @@ -189,7 +212,7 @@ class AuthenticationManager implements CallbackListener { } $success = true; foreach ($loginElements as $loginElement) { - $login = (string)$loginElement; + $login = (string) $loginElement; $adminStatement->bind_param('si', $login, $masterAdminLevel); $adminStatement->execute(); if ($adminStatement->error) { @@ -271,7 +294,7 @@ class AuthenticationManager implements CallbackListener { if (!$player || !is_numeric($authLevel)) { return false; } - $authLevel = (int)$authLevel; + $authLevel = (int) $authLevel; if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) { return false; } diff --git a/core/Callbacks/EchoManager.php b/core/Callbacks/EchoManager.php index 1a902a6d..e9ef5323 100644 --- a/core/Callbacks/EchoManager.php +++ b/core/Callbacks/EchoManager.php @@ -32,6 +32,24 @@ class EchoManager implements CallbackListener, EchoListener { $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_ECHO, $this, 'handleEchos'); } + /** + * Sends an Echo Message + * + * @param string $name + * @param mixed $data (can be array, object or string) + * @return bool + * @throws \Maniaplanet\DedicatedServer\InvalidArgumentException + */ + public function sendEcho($name, $data) { + if (is_string($data)) { + $success = $this->maniaControl->getClient()->dedicatedEcho($data, $name); + } else { + $success = $this->maniaControl->getClient()->dedicatedEcho(json_encode($data), $name); + } + + return $success; + } + /** * Register a new Echo Listener * @@ -101,29 +119,28 @@ class EchoManager implements CallbackListener, EchoListener { $params = func_get_args(); $params = array_slice($params, 1, null, true); - var_dump($params); + //var_dump($params); foreach ($this->echoListenings[$callbackName] as $listening) { /** @var Listening $listening */ $listening->triggerCallbackWithParams($params); } } - //TODO temporary testmethod, remove - public function test() { - // $this->maniaControl->getEchoManager()->test(); - $msg = json_encode(array("player" => "abc")); - //$callback = array("test1", "test2"); - $this->maniaControl->getClient()->dedicatedEcho($msg, "ManiaControl.PlayerManager.WarnPlayer"); - } - /** * Handle the given Callback * * @param array $callback */ public function handleEchos($param) { - $name = $param[1][0]; - $message = json_decode($param[1][1]); + $name = $param[1][0]; + if (is_object($decode = json_decode($param[1][1]))) { + $message = $decode; + } else { + $message = $param[1][1]; + } + + var_dump($name); + var_dump($message); switch ($name) { case 'ManiaControl.Restart': $this->maniaControl->restart($message); diff --git a/core/Players/PlayerActions.php b/core/Players/PlayerActions.php index 90b41cf5..29ea7953 100644 --- a/core/Players/PlayerActions.php +++ b/core/Players/PlayerActions.php @@ -288,9 +288,10 @@ class PlayerActions { * * @param string $adminLogin * @param string $targetLogin + * @param bool $calledByAdmin */ - public function warnPlayer($adminLogin, $targetLogin) { - if ($adminLogin != 'EchoListener') { + public function warnPlayer($adminLogin, $targetLogin, $calledByAdmin = true) { + if ($calledByAdmin) { $admin = $this->maniaControl->getPlayerManager()->getPlayer($adminLogin); if (!$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_WARN_PLAYER) ) { @@ -362,11 +363,11 @@ class PlayerActions { $this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $target); // Announce warning - if ($adminLogin != 'EchoListener') { + if ($calledByAdmin) { $title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin->authLevel); $chatMessage = $title . ' ' . $admin->getEscapedNickname() . ' warned ' . $target->getEscapedNickname() . '!'; } else { - $chatMessage = $target->getEscapedNickname() . ' got an Administrative warning!'; + $chatMessage = $target->getEscapedNickname() . ' got an administrative warning!'; } $this->maniaControl->getChat()->sendInformation($chatMessage); diff --git a/core/Players/PlayerManager.php b/core/Players/PlayerManager.php index 0fc8194d..1752a3c2 100644 --- a/core/Players/PlayerManager.php +++ b/core/Players/PlayerManager.php @@ -111,7 +111,7 @@ class PlayerManager implements CallbackListener, TimerListener, EchoListener { // Echo Warn Command (Usage: sendEcho json_encode("player" => "loginName") $this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_WARN_PLAYER, $this, function ($params) { - $this->playerActions->warnPlayer("EchoListener", $params->player); + $this->playerActions->warnPlayer(null, $params->player, false); }); } diff --git a/plugins/MCTeam/WidgetPlugin.php b/plugins/MCTeam/WidgetPlugin.php index 1cc9b4f9..9e789fd1 100644 --- a/plugins/MCTeam/WidgetPlugin.php +++ b/plugins/MCTeam/WidgetPlugin.php @@ -12,6 +12,7 @@ use FML\Script\Script; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\TimerListener; +use ManiaControl\Commands\HelpManager; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\IconManager; use ManiaControl\Players\Player;