TrackManiaControl/application/core/Commands/HelpManager.php

256 lines
8.1 KiB
PHP
Raw Normal View History

2014-01-14 19:47:40 +01:00
<?php
namespace ManiaControl\Commands;
2014-05-01 01:41:19 +02:00
use FML\Controls\Frame;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\ManiaLink;
use FML\Script\Features\Paging;
2014-01-14 20:02:35 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
2014-01-14 19:47:40 +01:00
use ManiaControl\ManiaControl;
2014-05-02 17:50:30 +02:00
use ManiaControl\Manialinks\ManialinkManager;
2014-01-14 19:47:40 +01:00
use ManiaControl\Players\Player;
2014-01-14 20:13:52 +01:00
/**
* Help Manager
2014-01-14 20:13:52 +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
2014-01-14 20:13:52 +01:00
*/
// TODO: refactor code - i see duplicated code all over the place..
2014-01-14 20:02:35 +01:00
class HelpManager implements CommandListener, CallbackListener {
/*
2014-01-14 19:47:40 +01:00
* Private Properties
*/
private $maniaControl = null;
private $playerCommands = array();
private $adminCommands = array();
/**
* Construct a new Commands Manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
2014-01-14 20:02:35 +01:00
public function __construct(ManiaControl $maniaControl) {
2014-01-14 19:47:40 +01:00
$this->maniaControl = $maniaControl;
2014-01-14 20:02:35 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit');
2014-01-14 20:02:35 +01:00
}
/**
* Handle ManiaControl OnInit Callback
*/
public function handleOnInit() {
2014-01-14 19:47:40 +01:00
//Register the help command
2014-05-01 01:41:19 +02:00
$this->maniaControl->commandManager->registerCommandListener('help', $this, 'command_playerHelp', false, 'Shows all commands in chat.');
$this->maniaControl->commandManager->registerCommandListener('helpall', $this, 'command_playerHelpAll', false, 'Shows all commands in ManiaLink with description.');
$this->maniaControl->commandManager->registerCommandListener('help', $this, 'command_adminHelp', true, 'Shows all admin commands in chat.');
$this->maniaControl->commandManager->registerCommandListener('helpall', $this, 'command_adminHelpAll', true, 'Shows all admin commands in ManiaLink with description.');
2014-01-14 19:47:40 +01:00
}
2014-01-14 20:02:35 +01:00
/**
* Shows a list of Admin Commands
*
2014-05-02 18:21:38 +02:00
* @param array $chatCallback
2014-01-14 20:02:35 +01:00
* @param Player $player
*/
2014-05-02 18:21:38 +02:00
public function command_adminHelp(array $chatCallback, Player $player) {
2014-05-02 17:50:30 +02:00
$showCommands = array();
2014-05-01 01:41:19 +02:00
$registeredMethods = array();
2014-05-02 17:50:30 +02:00
foreach (array_reverse($this->adminCommands) as $command) {
if (array_key_exists($command['Method'], $registeredMethods) && $showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
2014-05-01 01:41:19 +02:00
$name = $registeredMethods[$command['Method']];
2014-05-02 17:50:30 +02:00
$showCommands[$name]['Name'] .= '|' . $command['Name'];
2014-05-01 01:41:19 +02:00
} else {
2014-05-02 17:50:30 +02:00
$showCommands[$command['Name']] = $command;
2014-05-01 01:41:19 +02:00
$registeredMethods[$command['Method']] = $command['Name'];
}
}
usort($showCommands, function ($commandA, $commandB) {
return strcmp($commandA['Name'], $commandB['Name']);
2014-05-01 01:41:19 +02:00
});
$message = 'Supported Admin Commands: ';
2014-05-02 17:50:30 +02:00
foreach ($showCommands as $command) {
2014-01-14 20:02:35 +01:00
$message .= $command['Name'] . ',';
}
$message = substr($message, 0, -1);
$this->maniaControl->chat->sendChat($message, $player->login);
}
2014-01-14 19:47:40 +01:00
2014-01-14 20:02:35 +01:00
/**
* Shows a list of Player Commands
*
2014-05-02 18:21:38 +02:00
* @param array $chatCallback
2014-01-14 20:02:35 +01:00
* @param Player $player
*/
2014-05-02 18:21:38 +02:00
public function command_playerHelp(array $chatCallback, Player $player) {
2014-05-02 17:50:30 +02:00
$showCommands = array();
2014-05-01 01:41:19 +02:00
$registeredMethods = array();
2014-05-02 17:50:30 +02:00
foreach (array_reverse($this->playerCommands) as $command) {
if (array_key_exists($command['Method'], $registeredMethods) && $showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
2014-05-01 01:41:19 +02:00
$name = $registeredMethods[$command['Method']];
2014-05-02 17:50:30 +02:00
$showCommands[$name]['Name'] .= '|' . $command['Name'];
2014-05-01 01:41:19 +02:00
} else {
2014-05-02 17:50:30 +02:00
$showCommands[$command['Name']] = $command;
2014-05-01 01:41:19 +02:00
$registeredMethods[$command['Method']] = $command['Name'];
}
}
usort($showCommands, function ($commandA, $commandB) {
return strcmp($commandA['Name'], $commandB['Name']);
2014-05-01 01:41:19 +02:00
});
$message = 'Supported Player Commands: ';
2014-05-02 17:50:30 +02:00
foreach ($showCommands as $command) {
2014-01-14 20:02:35 +01:00
$message .= $command['Name'] . ',';
2014-01-14 19:47:40 +01:00
}
2014-01-14 20:02:35 +01:00
$message = substr($message, 0, -1);
$this->maniaControl->chat->sendChat($message, $player->login);
2014-01-14 19:47:40 +01:00
}
2014-05-01 01:41:19 +02:00
/**
* Shows a ManiaLink list of Player Commands
*
2014-05-02 18:21:38 +02:00
* @param array $chatCallback
2014-05-01 01:41:19 +02:00
* @param Player $player
*/
2014-05-02 18:21:38 +02:00
public function command_playerHelpAll(array $chatCallback, Player $player) {
2014-05-01 01:41:19 +02:00
$this->prepareHelpAll($this->playerCommands, $player);
}
/**
* Prepare the commands for the HelpAll ManiaLink.
*
* @param array $commands
* @param mixed $player
*/
private function prepareHelpAll(array $commands, $player) {
2014-05-02 17:50:30 +02:00
$showCommands = array();
2014-05-01 01:41:19 +02:00
$registeredMethods = array();
2014-05-02 17:50:30 +02:00
foreach (array_reverse($commands) as $command) {
if (array_key_exists($command['Method'], $registeredMethods)) {
if ($showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
2014-05-01 01:41:19 +02:00
$name = $registeredMethods[$command['Method']];
2014-05-02 17:50:30 +02:00
$showCommands[$name]['Name'] .= '|' . $command['Name'];
2014-05-01 01:41:19 +02:00
} else {
2014-05-02 17:50:30 +02:00
$showCommands[$command['Name']] = $command;
2014-05-01 01:41:19 +02:00
$registeredMethods[$command['Method']] = $command['Name'];
}
} else {
2014-05-02 17:50:30 +02:00
$showCommands[$command['Name']] = $command;
2014-05-01 01:41:19 +02:00
$registeredMethods[$command['Method']] = $command['Name'];
}
}
usort($showCommands, function ($commandA, $commandB) {
return strcmp($commandA['Name'], $commandB['Name']);
2014-05-01 01:41:19 +02:00
});
$this->showHelpAllList($showCommands, $player);
}
/**
* Shows the HelpAll list to the player.
*
* @param array $commands
* @param mixed $player
*/
private function showHelpAllList(array $commands, $player) {
2014-05-01 01:41:19 +02:00
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
// create manialink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
2014-05-02 17:50:30 +02:00
$script = $maniaLink->getScript();
$paging = new Paging();
2014-05-01 01:41:19 +02:00
$script->addFeature($paging);
// Main frame
$frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging);
$maniaLink->add($frame);
// Start offsets
$posX = -$width / 2;
$posY = $height / 2;
2014-05-01 01:41:19 +02:00
//Predefine description Label
$descriptionLabel = $this->maniaControl->manialinkManager->styleManager->getDefaultDescriptionLabel();
$frame->add($descriptionLabel);
// Headline
$headFrame = new Frame();
$frame->add($headFrame);
$headFrame->setY($posY - 5);
$array = array('Command' => $posX + 5, 'Description' => $posX + 50);
2014-05-01 01:41:19 +02:00
$this->maniaControl->manialinkManager->labelLine($headFrame, $array);
$index = 1;
$posY -= 10;
2014-05-15 17:45:08 +02:00
$pageFrame = null;
2014-05-02 17:50:30 +02:00
foreach ($commands as $command) {
if ($index % 15 === 1) {
2014-05-01 01:41:19 +02:00
$pageFrame = new Frame();
$frame->add($pageFrame);
$posY = $height / 2 - 10;
2014-05-01 01:41:19 +02:00
$paging->addPage($pageFrame);
}
$playerFrame = new Frame();
$pageFrame->add($playerFrame);
$playerFrame->setY($posY);
2014-05-01 01:41:19 +02:00
if ($index % 2 !== 0) {
2014-05-01 01:41:19 +02:00
$lineQuad = new Quad_BgsPlayerCard();
$playerFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
}
$array = array($command['Name'] => $posX + 5, $command['Description'] => $posX + 50);
2014-05-01 01:41:19 +02:00
$labels = $this->maniaControl->manialinkManager->labelLine($playerFrame, $array);
2014-05-02 15:35:52 +02:00
$label = $labels[0];
$label->setWidth(40);
2014-05-01 01:41:19 +02:00
$posY -= 4;
$index++;
2014-05-01 01:41:19 +02:00
}
// Render and display xml
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'HelpAllList');
}
2014-05-02 17:50:30 +02:00
/**
* Shows a ManiaLink list of Admin Commands
*
2014-05-02 18:21:38 +02:00
* @param array $chatCallback
2014-05-02 17:50:30 +02:00
* @param Player $player
*/
2014-05-02 18:21:38 +02:00
public function command_adminHelpAll(array $chatCallback, Player $player) {
2014-05-02 17:50:30 +02:00
$this->prepareHelpAll($this->adminCommands, $player);
}
2014-01-14 19:47:40 +01:00
/**
* Registers a new Command
*
2014-05-01 01:41:19 +02:00
* @param $name
* @param bool $adminCommand
* @param string $description
* @param $method
2014-01-14 19:47:40 +01:00
*/
2014-05-01 01:41:19 +02:00
public function registerCommand($name, $adminCommand = false, $description = '', $method) {
2014-05-02 17:50:30 +02:00
if ($adminCommand) {
2014-05-01 01:41:19 +02:00
array_push($this->adminCommands, array("Name" => $name, "Description" => $description, "Method" => $method));
2014-01-14 19:47:40 +01:00
} else {
2014-05-01 01:41:19 +02:00
array_push($this->playerCommands, array("Name" => $name, "Description" => $description, "Method" => $method));
2014-01-14 19:47:40 +01:00
}
}
}