Merge pull request #31 from tomvlk/master
Code refactoring, replaced 3 code blocks with one function.
This commit is contained in:
commit
f0a33ac5a2
@ -19,7 +19,7 @@ use ManiaControl\Players\Player;
|
||||
* @copyright 2014-2015 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
// TODO: refactor code - i see duplicated code all over the place..
|
||||
|
||||
class HelpManager implements CommandListener, CallbackListener {
|
||||
/*
|
||||
* Private properties
|
||||
@ -58,29 +58,15 @@ class HelpManager implements CommandListener, CallbackListener {
|
||||
* @param Player $player
|
||||
*/
|
||||
public function command_adminHelp(array $chatCallback, Player $player) {
|
||||
$showCommands = array();
|
||||
$registeredMethods = array();
|
||||
foreach (array_reverse($this->adminCommands) as $command) {
|
||||
if (array_key_exists($command['Method'], $registeredMethods) && $showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
|
||||
$name = $registeredMethods[$command['Method']];
|
||||
$showCommands[$name]['Name'] .= '|' . $command['Name'];
|
||||
} else {
|
||||
$showCommands[$command['Name']] = $command;
|
||||
$registeredMethods[$command['Method']] = $command['Name'];
|
||||
}
|
||||
}
|
||||
// Parse list from array
|
||||
$message = $this->parseHelpList($this->adminCommands);
|
||||
|
||||
usort($showCommands, function ($commandA, $commandB) {
|
||||
return strcmp($commandA['Name'], $commandB['Name']);
|
||||
});
|
||||
|
||||
$message = 'Supported Admin Commands: ';
|
||||
foreach ($showCommands as $command) {
|
||||
$message .= $command['Name'] . ',';
|
||||
}
|
||||
$message = substr($message, 0, -1);
|
||||
// Show message when it's not empty
|
||||
if($message != NULL){
|
||||
$message = 'Supported Admin Commands: ' . $message;
|
||||
$this->maniaControl->getChat()->sendChat($message, $player);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a list of Player Commands
|
||||
@ -89,29 +75,15 @@ class HelpManager implements CommandListener, CallbackListener {
|
||||
* @param Player $player
|
||||
*/
|
||||
public function command_playerHelp(array $chatCallback, Player $player) {
|
||||
$showCommands = array();
|
||||
$registeredMethods = array();
|
||||
foreach (array_reverse($this->playerCommands) as $command) {
|
||||
if (array_key_exists($command['Method'], $registeredMethods) && $showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
|
||||
$name = $registeredMethods[$command['Method']];
|
||||
$showCommands[$name]['Name'] .= '|' . $command['Name'];
|
||||
} else {
|
||||
$showCommands[$command['Name']] = $command;
|
||||
$registeredMethods[$command['Method']] = $command['Name'];
|
||||
}
|
||||
}
|
||||
// Parse list from array
|
||||
$message = $this->parseHelpList($this->playerCommands);
|
||||
|
||||
usort($showCommands, function ($commandA, $commandB) {
|
||||
return strcmp($commandA['Name'], $commandB['Name']);
|
||||
});
|
||||
|
||||
$message = 'Supported Player Commands: ';
|
||||
foreach ($showCommands as $command) {
|
||||
$message .= $command['Name'] . ',';
|
||||
}
|
||||
$message = substr($message, 0, -1);
|
||||
// Show message when it's not empty
|
||||
if($message != NULL){
|
||||
$message = 'Supported Player Commands: ' . $message;
|
||||
$this->maniaControl->getChat()->sendChat($message, $player);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a ManiaLink list of Player Commands
|
||||
@ -120,18 +92,20 @@ class HelpManager implements CommandListener, CallbackListener {
|
||||
* @param Player $player
|
||||
*/
|
||||
public function command_playerHelpAll(array $chatCallback, Player $player) {
|
||||
$this->prepareHelpAll($this->playerCommands, $player);
|
||||
$this->parseHelpList($this->playerCommands, true, $player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the commands for the HelpAll ManiaLink.
|
||||
*
|
||||
* Parse list with commands from array
|
||||
* @param array $commands
|
||||
* @param mixed $player
|
||||
* @param bool $isHelpAll
|
||||
* @param Player $player
|
||||
* @return string|void
|
||||
*/
|
||||
private function prepareHelpAll(array $commands, $player) {
|
||||
private function parseHelpList(array $commands, $isHelpAll = false, Player $player = null) {
|
||||
$showCommands = array();
|
||||
$registeredMethods = array();
|
||||
$message = '';
|
||||
foreach (array_reverse($commands) as $command) {
|
||||
if (array_key_exists($command['Method'], $registeredMethods)) {
|
||||
if($showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
|
||||
@ -151,8 +125,21 @@ class HelpManager implements CommandListener, CallbackListener {
|
||||
return strcmp($commandA['Name'], $commandB['Name']);
|
||||
});
|
||||
|
||||
if(!$isHelpAll){
|
||||
foreach ($showCommands as $command) {
|
||||
$message .= $command['Name'] . ',';
|
||||
}
|
||||
$message = substr($message, 0, -1);
|
||||
|
||||
return $message;
|
||||
}else{
|
||||
if($player != NULL){
|
||||
$this->showHelpAllList($showCommands, $player);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the HelpAll list to the player.
|
||||
@ -234,7 +221,7 @@ class HelpManager implements CommandListener, CallbackListener {
|
||||
* @param Player $player
|
||||
*/
|
||||
public function command_adminHelpAll(array $chatCallback, Player $player) {
|
||||
$this->prepareHelpAll($this->adminCommands, $player);
|
||||
$this->parseHelpList($this->adminCommands, true, $player);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user