Merge pull request #31 from tomvlk/master

Code refactoring, replaced 3 code blocks with one function.
This commit is contained in:
Lukas Kremsmayr 2015-01-19 19:14:16 +01:00
commit f0a33ac5a2

View File

@ -19,7 +19,7 @@ use ManiaControl\Players\Player;
* @copyright 2014-2015 ManiaControl Team * @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @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 { class HelpManager implements CommandListener, CallbackListener {
/* /*
* Private properties * Private properties
@ -58,29 +58,15 @@ class HelpManager implements CommandListener, CallbackListener {
* @param Player $player * @param Player $player
*/ */
public function command_adminHelp(array $chatCallback, Player $player) { public function command_adminHelp(array $chatCallback, Player $player) {
$showCommands = array(); // Parse list from array
$registeredMethods = array(); $message = $this->parseHelpList($this->adminCommands);
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'];
}
}
usort($showCommands, function ($commandA, $commandB) { // Show message when it's not empty
return strcmp($commandA['Name'], $commandB['Name']); if($message != NULL){
}); $message = 'Supported Admin Commands: ' . $message;
$message = 'Supported Admin Commands: ';
foreach ($showCommands as $command) {
$message .= $command['Name'] . ',';
}
$message = substr($message, 0, -1);
$this->maniaControl->getChat()->sendChat($message, $player); $this->maniaControl->getChat()->sendChat($message, $player);
} }
}
/** /**
* Show a list of Player Commands * Show a list of Player Commands
@ -89,29 +75,15 @@ class HelpManager implements CommandListener, CallbackListener {
* @param Player $player * @param Player $player
*/ */
public function command_playerHelp(array $chatCallback, Player $player) { public function command_playerHelp(array $chatCallback, Player $player) {
$showCommands = array(); // Parse list from array
$registeredMethods = array(); $message = $this->parseHelpList($this->playerCommands);
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'];
}
}
usort($showCommands, function ($commandA, $commandB) { // Show message when it's not empty
return strcmp($commandA['Name'], $commandB['Name']); if($message != NULL){
}); $message = 'Supported Player Commands: ' . $message;
$message = 'Supported Player Commands: ';
foreach ($showCommands as $command) {
$message .= $command['Name'] . ',';
}
$message = substr($message, 0, -1);
$this->maniaControl->getChat()->sendChat($message, $player); $this->maniaControl->getChat()->sendChat($message, $player);
} }
}
/** /**
* Show a ManiaLink list of Player Commands * Show a ManiaLink list of Player Commands
@ -120,18 +92,20 @@ class HelpManager implements CommandListener, CallbackListener {
* @param Player $player * @param Player $player
*/ */
public function command_playerHelpAll(array $chatCallback, 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 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(); $showCommands = array();
$registeredMethods = array(); $registeredMethods = array();
$message = '';
foreach (array_reverse($commands) as $command) { foreach (array_reverse($commands) as $command) {
if (array_key_exists($command['Method'], $registeredMethods)) { if (array_key_exists($command['Method'], $registeredMethods)) {
if($showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) { if($showCommands[$registeredMethods[$command['Method']]]['Description'] === $command['Description']) {
@ -151,8 +125,21 @@ class HelpManager implements CommandListener, CallbackListener {
return strcmp($commandA['Name'], $commandB['Name']); 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); $this->showHelpAllList($showCommands, $player);
} }
}
return;
}
/** /**
* Show the HelpAll list to the player. * Show the HelpAll list to the player.
@ -234,7 +221,7 @@ class HelpManager implements CommandListener, CallbackListener {
* @param Player $player * @param Player $player
*/ */
public function command_adminHelpAll(array $chatCallback, Player $player) { public function command_adminHelpAll(array $chatCallback, Player $player) {
$this->prepareHelpAll($this->adminCommands, $player); $this->parseHelpList($this->adminCommands, true, $player);
} }
/** /**