TrackManiaControl/application/core/Players/PlayerCommands.php

359 lines
13 KiB
PHP
Raw Normal View History

2013-12-05 00:30:10 +01:00
<?php
namespace ManiaControl\Players;
2014-01-05 00:02:13 +01:00
use FML\Controls\Quads\Quad_Icons128x32_1;
2014-01-05 00:43:46 +01:00
use FML\Controls\Quads\Quad_UIConstruction_Buttons;
2013-12-05 00:30:10 +01:00
use ManiaControl\Admin\AuthenticationManager;
2014-02-01 19:06:21 +01:00
use ManiaControl\Callbacks\CallbackListener;
2013-12-05 00:30:10 +01:00
use ManiaControl\Commands\CommandListener;
2014-01-04 18:49:33 +01:00
use ManiaControl\ManiaControl;
2014-01-04 18:18:46 +01:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Server\Server;
2014-06-22 18:42:40 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
use Maniaplanet\DedicatedServer\Xmlrpc\UnavailableFeatureException;
2013-12-05 00:30:10 +01:00
/**
* Class offering various Admin Commands related to Players
2013-12-05 00:30:10 +01:00
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-12-05 00:30:10 +01:00
*/
2014-02-01 19:06:21 +01:00
class PlayerCommands implements CommandListener, ManialinkPageAnswerListener, CallbackListener {
/*
2014-01-04 18:18:46 +01:00
* Constants
*/
2014-01-31 16:55:01 +01:00
const ACTION_BALANCE_TEAMS = 'PlayerCommands.BalanceTeams';
const ACTION_OPEN_PLAYERLIST = 'PlayerCommands.OpenPlayerList';
const SETTING_PERMISSION_ADD_BOT = 'Add Bot';
const SETTING_PERMISSION_TEAM_BALANCE = 'Balance Teams';
2014-01-10 13:28:48 +01:00
/*
* Private Properties
2013-12-05 00:30:10 +01:00
*/
private $maniaControl = null;
2014-01-04 18:49:33 +01:00
2013-12-05 00:30:10 +01:00
/**
* Create a new Player Commands Instance
2013-12-05 00:30:10 +01:00
*
2014-01-04 18:49:33 +01:00
* @param ManiaControl $maniaControl
2013-12-05 00:30:10 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-10 13:28:48 +01:00
2013-12-14 22:00:59 +01:00
// Register for admin commands
2014-05-01 01:41:19 +02:00
$this->maniaControl->commandManager->registerCommandListener(array('balance', 'teambalance', 'autoteambalance'), $this, 'command_TeamBalance', true, 'Balances the teams.');
$this->maniaControl->commandManager->registerCommandListener('kick', $this, 'command_Kick', true, 'Kicks player from the server.');
$this->maniaControl->commandManager->registerCommandListener('ban', $this, 'command_Ban', true, 'Bans player from the server.');
$this->maniaControl->commandManager->registerCommandListener(array('forcespec', 'forcespectator'), $this, 'command_ForceSpectator', true, 'Forces player into spectator.');
2014-05-18 16:26:52 +02:00
$this->maniaControl->commandManager->registerCommandListener('forceplay', $this, 'command_ForcePlay', true, 'Forces player into Play mode.');
2014-05-01 01:41:19 +02:00
$this->maniaControl->commandManager->registerCommandListener('forceblue', $this, 'command_ForceBlue', true, 'Forces player into blue team.');
$this->maniaControl->commandManager->registerCommandListener('forcered', $this, 'command_ForceRed', true, 'Forces player into red team.');
$this->maniaControl->commandManager->registerCommandListener(array('addbots', 'addbot'), $this, 'command_AddFakePlayers', true, 'Adds bots to the game.');
$this->maniaControl->commandManager->registerCommandListener(array('removebot', 'removebots'), $this, 'command_RemoveFakePlayers', true, 'Removes bots from the game.');
$this->maniaControl->commandManager->registerCommandListener('mute', $this, 'command_MutePlayer', true, 'Mutes a player (prevents player from chatting).');
$this->maniaControl->commandManager->registerCommandListener('unmute', $this, 'command_UnmutePlayer', true, 'Unmute a player (enables player to chat again).');
2014-01-10 13:28:48 +01:00
2013-12-18 15:48:33 +01:00
// Register for player chat commands
2014-05-01 01:41:19 +02:00
$this->maniaControl->commandManager->registerCommandListener(array('player', 'players'), $this, 'command_playerList', false, 'Shows players currently on the server.');
2014-01-10 13:28:48 +01:00
2014-01-31 16:55:01 +01:00
//Define Rights
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_BOT, AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_TEAM_BALANCE, AuthenticationManager::AUTH_LEVEL_MODERATOR);
2014-02-01 19:06:21 +01:00
//CallbackManager
$this->maniaControl->callbackManager->registerCallbackListener(Server::CB_TEAM_MODE_CHANGED, $this, 'teamStatusChanged');
2014-01-10 13:28:48 +01:00
// Action Open PlayerList
2014-01-04 18:49:33 +01:00
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_PLAYERLIST, $this, 'command_playerList');
2014-01-05 00:43:46 +01:00
$itemQuad = new Quad_UIConstruction_Buttons();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Author);
2014-01-04 18:49:33 +01:00
$itemQuad->setAction(self::ACTION_OPEN_PLAYERLIST);
2014-05-18 16:26:52 +02:00
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 15, 'Open PlayerList');
2014-01-04 18:49:33 +01:00
}
2014-02-01 19:06:21 +01:00
/**
* Handle TeamStatusChanged
2014-05-02 17:50:30 +02:00
*
* @param bool $teamMode
2014-02-01 19:06:21 +01:00
*/
public function teamStatusChanged($teamMode) {
2014-02-01 19:06:21 +01:00
//Add Balance Team Icon if it's a teamMode
if ($teamMode) {
2014-02-01 19:06:21 +01:00
// Action Balance Teams
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_BALANCE_TEAMS, $this, 'command_TeamBalance');
$itemQuad = new Quad_Icons128x32_1();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_RT_Team);
$itemQuad->setAction(self::ACTION_BALANCE_TEAMS);
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, false, 40, 'Balance Teams');
}
}
2013-12-05 00:30:10 +01:00
/**
* Handle //teambalance command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
public function command_TeamBalance(array $chatCallback, Player $player) {
2014-01-31 16:55:01 +01:00
if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_TEAM_BALANCE)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->autoTeamBalance();
2014-06-22 18:42:40 +02:00
} catch (GameModeException $exception) {
$this->maniaControl->chat->sendException($exception, $player);
2013-12-05 00:30:10 +01:00
return;
}
2014-01-20 20:51:03 +01:00
2014-06-22 18:42:40 +02:00
$this->maniaControl->chat->sendInformation($player->getEscapedNickname() . ' balanced Teams!');
2013-12-05 00:30:10 +01:00
}
/**
* Handle //kick command
*
2014-01-10 13:28:48 +01:00
* @param array $chat
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
public function command_Kick(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_KICK_PLAYER)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2], 3);
2014-01-31 16:55:01 +01:00
if (count($params) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//kick login'", $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$targetLogin = $params[1];
2014-01-10 13:28:48 +01:00
$message = '';
2014-01-31 16:55:01 +01:00
if (isset($params[2])) {
2013-12-05 00:30:10 +01:00
$message = $params[2];
}
$this->maniaControl->playerManager->playerActions->kickPlayer($player->login, $targetLogin, $message);
}
2014-04-27 01:56:23 +02:00
/**
* Handle //ban command
*
* @param array $chat
* @param Player $player
*/
public function command_Ban(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_BAN_PLAYER)) {
2014-04-27 01:56:23 +02:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2], 3);
if (count($params) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//ban login'", $player->login);
return;
}
$targetLogin = $params[1];
$message = '';
if (isset($params[2])) {
$message = $params[2];
}
$this->maniaControl->playerManager->playerActions->banPlayer($player->login, $targetLogin, $message);
2014-04-27 01:56:23 +02:00
}
/**
* Handle //warn Command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
* @param Player $player
*/
public function command_Warn(array $chatCallback, Player $player) {
$params = explode(' ', $chatCallback[1][2], 3);
2014-01-31 16:55:01 +01:00
if (count($params) <= 1) {
2014-05-09 13:00:04 +02:00
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//warn login'", $player->login);
return;
}
$targetLogin = $params[1];
$this->maniaControl->playerManager->playerActions->warnPlayer($player->login, $targetLogin);
2013-12-05 00:30:10 +01:00
}
/**
* Handle //forcespec command
*
2014-01-10 13:28:48 +01:00
* @param array $chat
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
public function command_ForceSpectator(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_FORCE_PLAYER_SPEC)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
2014-01-31 16:55:01 +01:00
if (count($params) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//forcespec login'", $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$targetLogin = $params[1];
2014-01-10 13:28:48 +01:00
2014-01-31 16:55:01 +01:00
if (isset($params[2]) && is_numeric($params[2])) {
2014-01-10 13:28:48 +01:00
$type = (int)$params[2];
$this->maniaControl->playerManager->playerActions->forcePlayerToSpectator($player->login, $targetLogin, $type);
2014-01-10 13:28:48 +01:00
} else {
$this->maniaControl->playerManager->playerActions->forcePlayerToSpectator($player->login, $targetLogin);
2013-12-05 00:30:10 +01:00
}
}
/**
* Handle //forceplay command
*
2014-01-10 13:28:48 +01:00
* @param array $chat
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
2014-01-06 18:09:47 +01:00
public function command_ForcePlay(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_FORCE_PLAYER_PLAY)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
2014-01-31 16:55:01 +01:00
if (!isset($params[1])) {
2014-01-06 18:09:47 +01:00
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//forceplay login'", $player->login);
2013-12-05 00:30:10 +01:00
return;
}
2014-01-06 18:09:47 +01:00
$targetLogin = $params[1];
2014-01-10 13:28:48 +01:00
2013-12-05 00:30:10 +01:00
$type = 2;
2014-01-31 16:55:01 +01:00
if (isset($params[2]) && is_numeric($params[2])) {
$type = (int)$params[2];
2014-01-14 14:37:31 +01:00
}
$selectable = ($type === 2);
2014-01-14 14:37:31 +01:00
$this->maniaControl->playerManager->playerActions->forcePlayerToPlay($player->login, $targetLogin, $selectable);
2014-01-06 18:09:47 +01:00
}
/**
* Handle //forceblue command
*
2014-01-10 13:28:48 +01:00
* @param array $chat
2014-01-06 18:09:47 +01:00
* @param Player $player
*/
public function command_ForceBlue(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_FORCE_PLAYER_TEAM)) {
2014-01-06 18:09:47 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
2013-12-05 00:30:10 +01:00
return;
}
2014-01-06 18:09:47 +01:00
$params = explode(' ', $chat[1][2]);
2014-01-31 16:55:01 +01:00
if (!isset($params[1])) {
2014-01-06 18:09:47 +01:00
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//forceblue login'", $player->login);
return;
2013-12-05 00:30:10 +01:00
}
2014-01-06 18:09:47 +01:00
$targetLogin = $params[1];
2014-01-10 13:28:48 +01:00
$this->maniaControl->playerManager->playerActions->forcePlayerToTeam($player->login, $targetLogin, PlayerActions::TEAM_BLUE);
2014-01-06 18:09:47 +01:00
}
/**
* Handle //forcered command
*
2014-01-10 13:28:48 +01:00
* @param array $chat
2014-01-06 18:09:47 +01:00
* @param Player $player
*/
public function command_ForceRed(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, PlayerActions::SETTING_PERMISSION_FORCE_PLAYER_TEAM)) {
2014-01-06 18:09:47 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
2014-01-31 16:55:01 +01:00
if (!isset($params[1])) {
2014-01-06 18:09:47 +01:00
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//forcered login'", $player->login);
return;
}
$targetLogin = $params[1];
2014-01-10 13:28:48 +01:00
$this->maniaControl->playerManager->playerActions->forcePlayerToTeam($player->login, $targetLogin, PlayerActions::TEAM_RED);
2013-12-05 00:30:10 +01:00
}
/**
* Handle //addfakeplayers command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
public function command_AddFakePlayers(array $chatCallback, Player $player) {
2014-01-31 16:55:01 +01:00
if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_ADD_BOT)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
2014-01-10 13:28:48 +01:00
$amount = 1;
2013-12-05 00:30:10 +01:00
$messageParts = explode(' ', $chatCallback[1][2]);
2014-01-31 16:55:01 +01:00
if (isset($messageParts[1]) && is_numeric($messageParts[1])) {
2013-12-05 00:30:10 +01:00
$amount = intval($messageParts[1]);
}
try {
for ($i = 0; $i < $amount; $i++) {
$this->maniaControl->client->connectFakePlayer();
}
$this->maniaControl->chat->sendSuccess('Fake players connected!', $player->login);
} catch (UnavailableFeatureException $e) {
$this->maniaControl->chat->sendSuccess('Error while connecting a Fake-Player.', $player->login);
2013-12-05 00:30:10 +01:00
}
}
/**
* Handle //removefakeplayers command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
2014-01-04 18:49:33 +01:00
* @param Player $player
2013-12-05 00:30:10 +01:00
*/
public function command_RemoveFakePlayers(array $chatCallback, Player $player) {
2014-01-31 16:55:01 +01:00
if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_ADD_BOT)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$this->maniaControl->client->disconnectFakePlayer('*');
2013-12-05 00:30:10 +01:00
$this->maniaControl->chat->sendSuccess('Fake players disconnected!', $player->login);
}
2013-12-18 15:48:33 +01:00
/**
* Handle //mute Command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
* @param Player $admin
*/
public function command_MutePlayer(array $chatCallback, Player $admin) {
$commandParts = explode(' ', $chatCallback[1][2]);
2014-01-31 16:55:01 +01:00
if (count($commandParts) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No login specified! Example: '//mute login'");
return;
}
$targetLogin = $commandParts[1];
$this->maniaControl->playerManager->playerActions->mutePlayer($admin->login, $targetLogin);
}
/**
* Handle //unmute Command
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
* @param Player $admin
*/
public function command_UnmutePlayer(array $chatCallback, Player $admin) {
$commandParts = explode(' ', $chatCallback[1][2]);
2014-01-31 16:55:01 +01:00
if (count($commandParts) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No login specified! Example: '//unmute login'");
return;
}
$targetLogin = $commandParts[1];
$this->maniaControl->playerManager->playerActions->unMutePlayer($admin->login, $targetLogin);
}
2013-12-18 15:48:33 +01:00
/**
* Handle Player list command
2014-01-04 18:49:33 +01:00
*
2014-01-10 13:28:48 +01:00
* @param array $chatCallback
2013-12-18 15:48:33 +01:00
* @param Player $player
*/
public function command_playerList(array $chatCallback, Player $player) {
$this->maniaControl->playerManager->playerList->addPlayerToShownList($player, PlayerList::SHOWN_MAIN_WINDOW);
$this->maniaControl->playerManager->playerList->showPlayerList($player);
2013-12-18 15:48:33 +01:00
}
2013-12-05 00:30:10 +01:00
}