TrackManiaControl/application/core/Players/PlayerCommands.php

348 lines
13 KiB
PHP
Raw Normal View History

2013-12-05 00:30:10 +01:00
<?php
namespace ManiaControl\Players;
2014-01-04 18:18:46 +01:00
use FML\Controls\Quads\Quad_Icons128x128_1;
2014-01-05 00:02:13 +01:00
use FML\Controls\Quads\Quad_Icons128x32_1;
2014-01-04 18:18:46 +01:00
use FML\Controls\Quads\Quad_Icons64x64_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;
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;
2013-12-05 00:30:10 +01:00
/**
* Class offering various Admin Commands related to Players
2013-12-05 00:30:10 +01:00
*
* @author steeffeen & kremsy
*/
2014-01-04 18:18:46 +01:00
class PlayerCommands implements CommandListener, ManialinkPageAnswerListener {
/**
* Constants
*/
const ACTION_BALANCE_TEAMS = 'PlayerCommands.BalanceTeams';
2014-01-04 18:49:33 +01:00
const ACTION_OPEN_PLAYERLIST = 'PlayerCommands.OpenPlayerList';
const ACTION_CANCEL_VOTE = 'PlayerCommands.CancelVote';
2013-12-05 00:30:10 +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;
2013-12-14 22:00:59 +01:00
// Register for admin commands
2014-01-04 18:49:33 +01:00
$this->maniaControl->commandManager->registerCommandListener('balance', $this, 'command_TeamBalance', true);
$this->maniaControl->commandManager->registerCommandListener('teambalance', $this, 'command_TeamBalance', true);
$this->maniaControl->commandManager->registerCommandListener('autoteambalance', $this, 'command_TeamBalance', true);
$this->maniaControl->commandManager->registerCommandListener('kick', $this, 'command_Kick', true);
$this->maniaControl->commandManager->registerCommandListener('forcespec', $this, 'command_ForceSpectator', true);
$this->maniaControl->commandManager->registerCommandListener('forcespectator', $this, 'command_ForceSpectator', true);
2014-01-06 18:09:47 +01:00
$this->maniaControl->commandManager->registerCommandListener('forceplay', $this, 'command_ForcePlay', true);
$this->maniaControl->commandManager->registerCommandListener('forceblue', $this, 'command_ForceBlue', true);
$this->maniaControl->commandManager->registerCommandListener('forcered', $this, 'command_ForceRed', true);
2014-01-04 18:49:33 +01:00
$this->maniaControl->commandManager->registerCommandListener('addbot', $this, 'command_AddFakePlayers', true);
$this->maniaControl->commandManager->registerCommandListener('removebot', $this, 'command_RemoveFakePlayers', true);
$this->maniaControl->commandManager->registerCommandListener('addbots', $this, 'command_AddFakePlayers', true);
$this->maniaControl->commandManager->registerCommandListener('removebots', $this, 'command_RemoveFakePlayers', true);
$this->maniaControl->commandManager->registerCommandListener('mute', $this, 'command_MutePlayer', true);
$this->maniaControl->commandManager->registerCommandListener('unmute', $this, 'command_UnmutePlayer', true);
// TODO: Move into another class
$this->maniaControl->commandManager->registerCommandListener('cancel', $this, 'command_CancelVote', true);
2013-12-18 15:48:33 +01:00
// Register for player chat commands
$this->maniaControl->commandManager->registerCommandListener('player', $this, 'command_playerList');
$this->maniaControl->commandManager->registerCommandListener('players', $this, 'command_playerList');
// Action Balance Teams
2014-01-04 18:18:46 +01:00
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_BALANCE_TEAMS, $this, 'command_TeamBalance');
2014-01-05 00:02:13 +01:00
$itemQuad = new Quad_Icons128x32_1();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_RT_Team);
2014-01-04 18:18:46 +01:00
$itemQuad->setAction(self::ACTION_BALANCE_TEAMS);
2014-01-05 01:29:49 +01:00
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, false, 9, 'Balance Teams');
// Action cancel Vote
2014-01-04 18:49:33 +01:00
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_CANCEL_VOTE, $this, 'command_cancelVote');
$itemQuad = new Quad_Icons64x64_1();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_ArrowRed);
$itemQuad->setAction(self::ACTION_CANCEL_VOTE);
2014-01-05 01:29:49 +01:00
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, false, 6, 'Cancel Vote');
// 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-01-05 01:29:49 +01:00
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 15, 'Open Playerlist');
2014-01-04 18:49:33 +01:00
}
/**
* Handle //cancelvote command
*
* @param array $chatCallback
2014-01-04 18:49:33 +01:00
* @param Player $player
*/
public function command_CancelVote(array $chatCallback, Player $player) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2014-01-04 18:49:33 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$success = $this->maniaControl->client->query('CancelVote');
if (!$success) {
2014-01-04 18:49:33 +01:00
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
return;
}
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> canceled the Vote!');
2013-12-05 00:30:10 +01:00
}
/**
* Handle //teambalance command
*
* @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) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$success = $this->maniaControl->client->query('AutoTeamBalance');
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> balanced Teams!');
}
/**
* Handle //kick command
*
* @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->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2013-12-05 00:30:10 +01: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: '//kick login'", $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$targetLogin = $params[1];
2013-12-05 00:30:10 +01:00
$message = '';
if (isset($params[2])) {
2013-12-05 00:30:10 +01:00
$message = $params[2];
}
$this->maniaControl->playerManager->playerActions->kickPlayer($player->login, $targetLogin, $message);
}
/**
* Handle //warn Command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_Warn(array $chatCallback, Player $player) {
$params = explode(' ', $chat[1][2], 3);
if (count($params) <= 1) {
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//kick 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
*
* @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->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
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];
if (isset($params[2]) && is_numeric($params[2])) {
$type = (int) $params[2];
$this->maniaControl->playerManager->playerActions->forcePlayerToSpectator($player->login, $targetLogin, $type);
2013-12-05 00:30:10 +01:00
}
else {
$this->maniaControl->playerManager->playerActions->forcePlayerToSpectator($player->login, $targetLogin);
2013-12-05 00:30:10 +01:00
}
}
/**
* Handle //forceplay command
*
* @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->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
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];
2013-12-05 00:30:10 +01:00
$type = 2;
if (isset($params[2]) && is_numeric($params[2])) {
2013-12-05 00:30:10 +01:00
$type = intval($params[2]);
}
2014-01-06 18:09:47 +01:00
$this->maniaControl->playerManager->playerActions->forcePlayerToPlay($player->login, $targetLogin, $type);
}
/**
* Handle //forceblue command
*
* @param array $chat
* @param Player $player
*/
public function command_ForceBlue(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
$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]);
if (!isset($params[1])) {
$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];
$this->maniaControl->playerManager->playerActions->forcePlayerToTeam($player->login, $targetLogin, PlayerActions::TEAM_BLUE);
}
/**
* Handle //forcered command
*
* @param array $chat
* @param Player $player
*/
public function command_ForceRed(array $chat, Player $player) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$params = explode(' ', $chat[1][2]);
if (!isset($params[1])) {
$this->maniaControl->chat->sendUsageInfo("No Login given! Example: '//forcered login'", $player->login);
return;
}
$targetLogin = $params[1];
$this->maniaControl->playerManager->playerActions->forcePlayerToTeam($player->login, $targetLogin, PlayerActions::TEAM_RED);
2013-12-05 00:30:10 +01:00
}
/**
* Handle //addfakeplayers command
*
* @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) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$amount = 1;
2013-12-05 00:30:10 +01:00
$messageParts = explode(' ', $chatCallback[1][2]);
if (isset($messageParts[1]) && is_numeric($messageParts[1])) {
2013-12-05 00:30:10 +01:00
$amount = intval($messageParts[1]);
}
$success = true;
for ($i = 0; $i < $amount; $i++) {
if (!$this->maniaControl->client->query('ConnectFakePlayer')) {
2013-12-05 00:30:10 +01:00
$success = false;
}
}
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$this->maniaControl->chat->sendSuccess('Fake players connected!', $player->login);
}
/**
* Handle //removefakeplayers command
*
* @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) {
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
2013-12-05 00:30:10 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$success = $this->maniaControl->client->query('DisconnectFakePlayer', '*');
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
2013-12-05 00:30:10 +01:00
return;
}
$this->maniaControl->chat->sendSuccess('Fake players disconnected!', $player->login);
}
2013-12-18 15:48:33 +01:00
/**
* Handle //mute Command
*
* @param array $chatCallback
* @param Player $admin
*/
public function command_MutePlayer(array $chatCallback, Player $admin) {
$commandParts = explode(' ', $chatCallback[1][2]);
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
*
* @param array $chatCallback
* @param Player $admin
*/
public function command_UnmutePlayer(array $chatCallback, Player $admin) {
$commandParts = explode(' ', $chatCallback[1][2]);
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
*
* @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
}