diff --git a/application/core/Players/PlayerCommands.php b/application/core/Players/PlayerCommands.php new file mode 100644 index 00000000..69768403 --- /dev/null +++ b/application/core/Players/PlayerCommands.php @@ -0,0 +1,217 @@ +maniaControl = $maniaControl; + + // Register for commands + $this->maniaControl->commandManager->registerCommandListener('/teambalance', $this, 'command_TeamBalance'); + $this->maniaControl->commandManager->registerCommandListener('/autoteambalance', $this, 'command_TeamBalance'); + $this->maniaControl->commandManager->registerCommandListener('/kick', $this, 'command_Kick'); + $this->maniaControl->commandManager->registerCommandListener('/forcespec', $this, 'command_ForceSpectator'); + $this->maniaControl->commandManager->registerCommandListener('/forcespectator', $this, 'command_ForceSpectator'); + $this->maniaControl->commandManager->registerCommandListener('/forceplay', $this, 'command_ForcePlayer'); + $this->maniaControl->commandManager->registerCommandListener('/forceplayer', $this, 'command_ForcePlayer'); + $this->maniaControl->commandManager->registerCommandListener('/addfakeplayers', $this, 'command_AddFakePlayers'); + $this->maniaControl->commandManager->registerCommandListener('/removefakeplayers', $this, 'command_RemoveFakePlayers'); + } + + /** + * Handle //teambalance command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_TeamBalance(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('AutoTeamBalance'); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> balanced Teams!'); + } + + /** + * Handle //kick command + * + * @param array $chat + * @param Player $player + */ + public function command_Kick(array $chat, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $params = explode(' ', $chat[1][2], 3); + if (!isset($params[1])) { + $this->maniaControl->chat->sendUsageInfo('Usage example: //kick login', $player->login); + return; + } + $target = $params[1]; + $target = $this->maniaControl->playerManager->getPlayer($target); + if (!$target) { + $this->maniaControl->chat->sendError("Invalid player login.", $player->login); + return; + } + $message = ''; + if (isset($params[2])) { + $message = $params[2]; + } + $success = $this->maniaControl->client->query('Kick', $target->login, $message); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> kicked $<' . $target->nickname . '$>!'); + } + + /** + * Handle //forcespec command + * + * @param array $chat + * @param Player $player + */ + public function command_ForceSpectator(array $chat, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $params = explode(' ', $chat[1][2]); + if (!isset($params[1])) { + $this->maniaControl->chat->sendUsageInfo('Usage example: //forcespec login', $player->login); + return; + } + $target = $params[1]; + $target = $this->maniaControl->playerManager->getPlayer($target); + if (!$target) { + $this->maniaControl->chat->sendError("Invalid player login.", $player->login); + return; + } + $type = 3; + if (isset($params[2]) && is_numeric($params[2])) { + $type = intval($params[2]); + } + $success = $this->maniaControl->client->query('ForceSpectator', $target->login, $type); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + if ($type != 1) { + $this->maniaControl->client->query('ForceSpectator', $target->login, 0); + } + $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> forced $<' . $target->nickname . '$> to spectator!'); + } + + /** + * Handle //forceplay command + * + * @param array $chat + * @param Player $player + */ + public function command_ForcePlayer(array $chat, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $params = explode(' ', $chat[1][2]); + if (!isset($params[1])) { + $this->maniaControl->chat->sendUsageInfo('Usage example: //forceplay login', $player->login); + return; + } + $target = $params[1]; + $target = $this->maniaControl->playerManager->getPlayer($target); + if (!$target) { + $this->maniaControl->chat->sendError("Invalid player login.", $player->login); + return; + } + $type = 2; + if (isset($params[2]) && is_numeric($params[2])) { + $type = intval($params[2]); + } + $success = $this->maniaControl->client->query('ForceSpectator', $target->login, 2); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + if ($type != 1) { + $this->maniaControl->client->query('ForceSpectator', $target->login, 0); + } + $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> forced $<' . $target->nickname . '$> to player!'); + } + + /** + * Handle //addfakeplayers command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_AddFakePlayers(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $amount = 1; + $messageParts = explode(' ', $chatCallback[1][2]); + if (isset($messageParts[1]) && is_numeric($messageParts[1])) { + $amount = intval($messageParts[1]); + } + $success = true; + for ($i = 0; $i < $amount; $i++) { + if (!$this->maniaControl->client->query('ConnectFakePlayer')) { + $success = false; + } + } + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Fake players connected!', $player->login); + } + + /** + * Handle //removefakeplayers command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_RemoveFakePlayers(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('DisconnectFakePlayer', '*'); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Fake players disconnected!', $player->login); + } +} + +?> diff --git a/application/core/Players/PlayerManager.php b/application/core/Players/PlayerManager.php index ce50ce48..f6a0af3d 100644 --- a/application/core/Players/PlayerManager.php +++ b/application/core/Players/PlayerManager.php @@ -3,6 +3,7 @@ namespace ManiaControl\Players; require_once __DIR__ . '/Player.php'; +require_once __DIR__ . '/PlayerCommands.php'; use ManiaControl\Formatter; use ManiaControl\ManiaControl; @@ -27,6 +28,7 @@ class PlayerManager implements CallbackListener { * Private properties */ private $maniaControl = null; + private $playerCommands = null; private $playerList = array(); /** @@ -38,6 +40,8 @@ class PlayerManager implements CallbackListener { $this->maniaControl = $maniaControl; $this->initTables(); + $this->playerCommands = new PlayerCommands($maniaControl); + // Init settings $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, false); diff --git a/application/core/Server/ServerCommands.php b/application/core/Server/ServerCommands.php index 030884d3..b97f5c4d 100644 --- a/application/core/Server/ServerCommands.php +++ b/application/core/Server/ServerCommands.php @@ -30,9 +30,10 @@ class ServerCommands implements CallbackListener, CommandListener { public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; + // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds'); - $this->maniaControl->commandManager->registerCommandListener('/kick', $this, 'command_Kick'); + // Register for commands $this->maniaControl->commandManager->registerCommandListener('/setpwd', $this, 'command_SetPwd'); $this->maniaControl->commandManager->registerCommandListener('/setservername', $this, 'command_SetServerName'); $this->maniaControl->commandManager->registerCommandListener('/setmaxplayers', $this, 'command_SetMaxPlayers'); @@ -41,6 +42,12 @@ class ServerCommands implements CallbackListener, CommandListener { $this->maniaControl->commandManager->registerCommandListener('/shutdown', $this, 'command_Shutdown'); $this->maniaControl->commandManager->registerCommandListener('/shutdownserver', $this, 'command_ShutdownServer'); $this->maniaControl->commandManager->registerCommandListener('/systeminfo', $this, 'command_SystemInfo'); + $this->maniaControl->commandManager->registerCommandListener('/hideserver', $this, 'command_HideServer'); + $this->maniaControl->commandManager->registerCommandListener('/showserver', $this, 'command_ShowServer'); + $this->maniaControl->commandManager->registerCommandListener('/enablemapdownload', $this, 'command_EnableMapDownload'); + $this->maniaControl->commandManager->registerCommandListener('/disablemapdownload', $this, 'command_DisableMapDownload'); + $this->maniaControl->commandManager->registerCommandListener('/enablehorns', $this, 'command_EnableHorns'); + $this->maniaControl->commandManager->registerCommandListener('/disablehorns', $this, 'command_DisableHorns'); } /** @@ -54,14 +61,14 @@ class ServerCommands implements CallbackListener, CommandListener { if ($this->serverShutdownEmpty) { $players = $this->maniaControl->server->getPlayers(); if (count($players) <= 0) { - return $this->shutdownServer('empty'); + $this->shutdownServer('empty'); } } // Delayed shutdown if ($this->serverShutdownTime > 0) { if (time() >= $this->serverShutdownTime) { - return $this->shutdownServer('delayed'); + $this->shutdownServer('delayed'); } } } @@ -71,17 +78,16 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chat * @param Player $player - * @return bool */ public function command_SystemInfo(array $chat, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $systemInfo = $this->maniaControl->server->getSystemInfo(); $message = 'SystemInfo: ip=' . $systemInfo['PublishedIp'] . ', port=' . $systemInfo['Port'] . ', p2pPort=' . $systemInfo['P2PPort'] . ', title=' . $systemInfo['TitleId'] . ', login=' . $systemInfo['ServerLogin'] . '.'; - return $this->maniaControl->chat->sendInformation($message, $player->login); + $this->maniaControl->chat->sendInformation($message, $player->login); } /** @@ -89,14 +95,13 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chat * @param Player $player - * @return bool */ public function command_Shutdown(array $chat, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } - return $this->maniaControl->quit("ManiaControl shutdown requested by '{$player->login}'"); + $this->maniaControl->quit("ManiaControl shutdown requested by '{$player->login}'"); } /** @@ -104,12 +109,11 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chat * @param Player $player - * @return bool */ public function command_ShutdownServer(array $chat, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } // Check for delayed shutdown $params = explode(' ', $chat[1][2]); @@ -119,54 +123,24 @@ class ServerCommands implements CallbackListener, CommandListener { $this->serverShutdownEmpty = !$this->serverShutdownEmpty; if ($this->serverShutdownEmpty) { $this->maniaControl->chat->sendInformation("The server will shutdown as soon as it's empty!", $player->login); - return true; + return; } $this->maniaControl->chat->sendInformation("Empty-shutdown cancelled!", $player->login); - return true; + return; } $delay = (int) $param; if ($delay <= 0) { // Cancel shutdown $this->serverShutdownTime = -1; $this->maniaControl->chat->sendInformation("Delayed shutdown cancelled!", $player->login); - return true; + return; } // Trigger delayed shutdown $this->serverShutdownTime = time() + $delay * 60.; $this->maniaControl->chat->sendInformation("The server will shut down in {$delay} minutes!", $player->login); - return true; + return; } - return $this->shutdownServer($player->login); - } - - /** - * Handle //kick command - * - * @param array $chat - * @param Player $player - * @return bool - */ - public function command_Kick(array $chat, Player $player) { - if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { - $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; - } - $params = explode(' ', $chat[1][2], 3); - if (!isset($params[1])) { - $this->maniaControl->chat->sendUsageInfo('Usage example: //kick login', $player->login); - return false; - } - $target = $params[1]; - $target = $this->maniaControl->playerManager->getPlayer($target); - if (!$target) { - $this->maniaControl->chat->sendError("Invalid player login.", $player->login); - return false; - } - $message = ''; - if (isset($params[2])) { - $message = $params[2]; - } - return $this->maniaControl->client->query('Kick', $target->login, $message); + $this->shutdownServer($player->login); } /** @@ -174,25 +148,23 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chat * @param Player $player - * @return bool */ public function command_SetServerName(array $chat, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $params = explode(' ', $chat[1][2], 2); if (count($params) < 2) { $this->maniaControl->chat->sendUsageInfo('Usage example: //setservername ManiaPlanet Server', $player->login); - return false; + return; } $serverName = $params[1]; if (!$this->maniaControl->client->query('SetServerName', $serverName)) { $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); - return false; + return; } $this->maniaControl->chat->sendSuccess("Server name changed to: '{$serverName}'!", $player->login); - return true; } /** @@ -200,12 +172,11 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chatCallback * @param Player $player - * @return bool */ public function command_SetPwd(array $chatCallback, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $messageParts = explode(' ', $chatCallback[1][2], 2); $password = ''; @@ -217,10 +188,9 @@ class ServerCommands implements CallbackListener, CommandListener { $success = $this->maniaControl->client->query('SetServerPassword', $password); if (!$success) { $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); - return false; + return; } $this->maniaControl->chat->sendSuccess($successMessage, $player->login); - return true; } /** @@ -228,12 +198,11 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chatCallback * @param Player $player - * @return bool */ public function command_SetSpecPwd(array $chatCallback, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $messageParts = explode(' ', $chatCallback[1][2], 2); $password = ''; @@ -245,10 +214,9 @@ class ServerCommands implements CallbackListener, CommandListener { $success = $this->maniaControl->client->query('SetServerPasswordForSpectator', $password); if (!$success) { $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); - return false; + return; } $this->maniaControl->chat->sendSuccess($successMessage, $player->login); - return true; } /** @@ -256,22 +224,21 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chatCallback * @param Player $player - * @return bool */ public function command_SetMaxPlayers(array $chatCallback, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $messageParts = explode(' ', $chatCallback[1][2], 2); if (!isset($messageParts[1])) { $this->maniaControl->chat->sendUsageInfo('Usage example: //setmaxplayers 16', $player->login); - return false; + return; } $amount = $messageParts[1]; if (!is_numeric($amount)) { $this->maniaControl->chat->sendUsageInfo('Usage example: //setmaxplayers 16', $player->login); - return false; + return; } $amount = (int) $amount; if ($amount < 0) { @@ -280,10 +247,9 @@ class ServerCommands implements CallbackListener, CommandListener { $success = $this->maniaControl->client->query('SetMaxPlayers', $amount); if (!$success) { $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); - return false; + return; } $this->maniaControl->chat->sendSuccess("Changed max players to: {$amount}", $player->login); - return true; } /** @@ -291,22 +257,21 @@ class ServerCommands implements CallbackListener, CommandListener { * * @param array $chatCallback * @param Player $player - * @return bool */ public function command_SetMaxSpectators(array $chatCallback, Player $player) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { $this->maniaControl->authenticationManager->sendNotAllowed($player); - return false; + return; } $messageParts = explode(' ', $chatCallback[1][2], 2); if (!isset($messageParts[1])) { $this->maniaControl->chat->sendUsageInfo('Usage example: //setmaxspectators 16', $player->login); - return false; + return; } $amount = $messageParts[1]; if (!is_numeric($amount)) { $this->maniaControl->chat->sendUsageInfo('Usage example: //setmaxspectators 16', $player->login); - return false; + return; } $amount = (int) $amount; if ($amount < 0) { @@ -315,10 +280,123 @@ class ServerCommands implements CallbackListener, CommandListener { $success = $this->maniaControl->client->query('SetMaxSpectators', $amount); if (!$success) { $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); - return false; + return; } $this->maniaControl->chat->sendSuccess("Changed max spectators to: {$amount}", $player->login); - return true; + } + + /** + * Handle //hideserver command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_HideServer(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('SetHideServer', 1); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Server is now hidden!', $player->login); + } + + /** + * Handle //showserver command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_ShowServer(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('SetHideServer', 0); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Server is now visible!', $player->login); + } + + /** + * Handle //enablemapdownload command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_EnableMapDownload(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('AllowMapDownload', true); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Map Download is now enabled!', $player->login); + } + + /** + * Handle //disablemapdownload command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_DisableMapDownload(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('AllowMapDownload', false); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Map Download is now disabled!', $player->login); + } + + /** + * Handle //enablehorns command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_EnableHorns(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('DisableHorns', false); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Horns enabled!', $player->login); + } + + /** + * Handle //disablehorns command + * + * @param array $chatCallback + * @param Player $player + */ + public function command_DisableHorns(array $chatCallback, Player $player) { + if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $success = $this->maniaControl->client->query('DisableHorns', true); + if (!$success) { + $this->maniaControl->chat->sendError('Error occured: ' . $this->maniaControl->getClientErrorText(), $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Horns disabled!', $player->login); } /**