finished and tested first version of echomanager

This commit is contained in:
kremsy 2015-06-19 18:30:44 +02:00
parent 7527561263
commit 351a0b424c
5 changed files with 64 additions and 22 deletions

View File

@ -4,6 +4,7 @@ namespace ManiaControl\Admin;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\EchoListener;
use ManiaControl\Logger; use ManiaControl\Logger;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
@ -17,7 +18,7 @@ use ManiaControl\Settings\Setting;
* @copyright 2014-2015 ManiaControl Team * @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class AuthenticationManager implements CallbackListener { class AuthenticationManager implements CallbackListener, EchoListener {
/* /*
* Constants * Constants
*/ */
@ -33,6 +34,8 @@ class AuthenticationManager implements CallbackListener {
const AUTH_NAME_MASTERADMIN = 'MasterAdmin'; const AUTH_NAME_MASTERADMIN = 'MasterAdmin';
const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged'; const CB_AUTH_LEVEL_CHANGED = 'AuthenticationManager.AuthLevelChanged';
const ECHO_GRANT_LEVEL = 'ManiaControl.AuthenticationManager.GrandLevel';
const ECHO_REVOKE_LEVEL = 'ManiaControl.AuthenticationManager.RevokeLevel';
/* /*
* Private properties * Private properties
*/ */
@ -52,6 +55,26 @@ class AuthenticationManager implements CallbackListener {
// Callbacks // Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit'); $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) { public static function getAuthLevelInt($authLevelParam) {
if (is_object($authLevelParam) && property_exists($authLevelParam, 'authLevel')) { if (is_object($authLevelParam) && property_exists($authLevelParam, 'authLevel')) {
return (int)$authLevelParam->authLevel; return (int) $authLevelParam->authLevel;
} }
if (is_string($authLevelParam)) { if (is_string($authLevelParam)) {
return self::getAuthLevel($authLevelParam); return self::getAuthLevel($authLevelParam);
} }
return (int)$authLevelParam; return (int) $authLevelParam;
} }
/** /**
@ -98,7 +121,7 @@ class AuthenticationManager implements CallbackListener {
* @return int * @return int
*/ */
public static function getAuthLevel($authLevelName) { public static function getAuthLevel($authLevelName) {
$authLevelName = (string)$authLevelName; $authLevelName = (string) $authLevelName;
switch ($authLevelName) { switch ($authLevelName) {
case self::AUTH_NAME_MASTERADMIN: case self::AUTH_NAME_MASTERADMIN:
return self::AUTH_LEVEL_MASTERADMIN; return self::AUTH_LEVEL_MASTERADMIN;
@ -189,7 +212,7 @@ class AuthenticationManager implements CallbackListener {
} }
$success = true; $success = true;
foreach ($loginElements as $loginElement) { foreach ($loginElements as $loginElement) {
$login = (string)$loginElement; $login = (string) $loginElement;
$adminStatement->bind_param('si', $login, $masterAdminLevel); $adminStatement->bind_param('si', $login, $masterAdminLevel);
$adminStatement->execute(); $adminStatement->execute();
if ($adminStatement->error) { if ($adminStatement->error) {
@ -271,7 +294,7 @@ class AuthenticationManager implements CallbackListener {
if (!$player || !is_numeric($authLevel)) { if (!$player || !is_numeric($authLevel)) {
return false; return false;
} }
$authLevel = (int)$authLevel; $authLevel = (int) $authLevel;
if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) { if ($authLevel >= self::AUTH_LEVEL_MASTERADMIN) {
return false; return false;
} }

View File

@ -32,6 +32,24 @@ class EchoManager implements CallbackListener, EchoListener {
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_ECHO, $this, 'handleEchos'); $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 * Register a new Echo Listener
* *
@ -101,21 +119,13 @@ class EchoManager implements CallbackListener, EchoListener {
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, null, true); $params = array_slice($params, 1, null, true);
var_dump($params); //var_dump($params);
foreach ($this->echoListenings[$callbackName] as $listening) { foreach ($this->echoListenings[$callbackName] as $listening) {
/** @var Listening $listening */ /** @var Listening $listening */
$listening->triggerCallbackWithParams($params); $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 * Handle the given Callback
* *
@ -123,7 +133,14 @@ class EchoManager implements CallbackListener, EchoListener {
*/ */
public function handleEchos($param) { public function handleEchos($param) {
$name = $param[1][0]; $name = $param[1][0];
$message = json_decode($param[1][1]); if (is_object($decode = json_decode($param[1][1]))) {
$message = $decode;
} else {
$message = $param[1][1];
}
var_dump($name);
var_dump($message);
switch ($name) { switch ($name) {
case 'ManiaControl.Restart': case 'ManiaControl.Restart':
$this->maniaControl->restart($message); $this->maniaControl->restart($message);

View File

@ -288,9 +288,10 @@ class PlayerActions {
* *
* @param string $adminLogin * @param string $adminLogin
* @param string $targetLogin * @param string $targetLogin
* @param bool $calledByAdmin
*/ */
public function warnPlayer($adminLogin, $targetLogin) { public function warnPlayer($adminLogin, $targetLogin, $calledByAdmin = true) {
if ($adminLogin != 'EchoListener') { if ($calledByAdmin) {
$admin = $this->maniaControl->getPlayerManager()->getPlayer($adminLogin); $admin = $this->maniaControl->getPlayerManager()->getPlayer($adminLogin);
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_WARN_PLAYER) if (!$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_WARN_PLAYER)
) { ) {
@ -362,11 +363,11 @@ class PlayerActions {
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $target); $this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $target);
// Announce warning // Announce warning
if ($adminLogin != 'EchoListener') { if ($calledByAdmin) {
$title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin->authLevel); $title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin->authLevel);
$chatMessage = $title . ' ' . $admin->getEscapedNickname() . ' warned ' . $target->getEscapedNickname() . '!'; $chatMessage = $title . ' ' . $admin->getEscapedNickname() . ' warned ' . $target->getEscapedNickname() . '!';
} else { } else {
$chatMessage = $target->getEscapedNickname() . ' got an Administrative warning!'; $chatMessage = $target->getEscapedNickname() . ' got an administrative warning!';
} }
$this->maniaControl->getChat()->sendInformation($chatMessage); $this->maniaControl->getChat()->sendInformation($chatMessage);

View File

@ -111,7 +111,7 @@ class PlayerManager implements CallbackListener, TimerListener, EchoListener {
// Echo Warn Command (Usage: sendEcho json_encode("player" => "loginName") // Echo Warn Command (Usage: sendEcho json_encode("player" => "loginName")
$this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_WARN_PLAYER, $this, function ($params) { $this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_WARN_PLAYER, $this, function ($params) {
$this->playerActions->warnPlayer("EchoListener", $params->player); $this->playerActions->warnPlayer(null, $params->player, false);
}); });
} }

View File

@ -12,6 +12,7 @@ use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\HelpManager;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\IconManager; use ManiaControl\Manialinks\IconManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;