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 ;
2017-04-03 21:30:31 +02:00
use FML\Controls\Quads\Quad_UIConstruction_Buttons ;
2014-05-01 01:41:19 +02:00
use FML\ManiaLink ;
use FML\Script\Features\Paging ;
2014-01-14 20:02:35 +01:00
use ManiaControl\Callbacks\CallbackListener ;
2014-05-24 16:39:12 +02:00
use ManiaControl\Callbacks\Callbacks ;
2014-01-14 19:47:40 +01:00
use ManiaControl\ManiaControl ;
2017-04-01 18:48:36 +02:00
use ManiaControl\Manialinks\LabelLine ;
2014-05-02 17:50:30 +02:00
use ManiaControl\Manialinks\ManialinkManager ;
2017-04-03 21:30:31 +02:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener ;
2014-01-14 19:47:40 +01:00
use ManiaControl\Players\Player ;
2014-01-14 20:13:52 +01:00
/**
2014-07-25 16:28:47 +02:00
* ManiaControl Help Manager Class
2014-01-14 20:13:52 +01:00
*
2014-05-02 17:50:30 +02:00
* @ author ManiaControl Team < mail @ maniacontrol . com >
2017-02-04 11:49:23 +01:00
* @ copyright 2014 - 2017 ManiaControl Team
2014-05-02 17:50:30 +02:00
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
2014-01-14 20:13:52 +01:00
*/
2017-04-03 21:30:31 +02:00
class HelpManager implements CommandListener , CallbackListener , ManialinkPageAnswerListener {
2017-04-10 20:50:52 +02:00
const ACTION_OPEN_HELP_ALL = 'HelpManager.OpenHelpAll' ;
const ACTION_OPEN_ADMIN_HELP_ALL = 'HelpManager.OpenAdminHelpAll' ;
2017-04-03 21:30:31 +02:00
2014-04-12 12:14:37 +02:00
/*
2014-08-02 22:31:46 +02:00
* Private properties
2014-01-14 19:47:40 +01:00
*/
2014-08-02 22:31:46 +02:00
/** @var ManiaControl $maniaControl */
2015-01-19 19:18:26 +01:00
private $maniaControl = null ;
2014-01-14 19:47:40 +01:00
private $playerCommands = array ();
2015-01-19 19:18:26 +01:00
private $adminCommands = array ();
2014-01-14 19:47:40 +01:00
/**
* Construct a new Commands Manager
*
2014-07-25 16:28:47 +02:00
* @ param ManiaControl $maniaControl
2014-01-14 19:47:40 +01:00
*/
2014-01-14 20:02:35 +01:00
public function __construct ( ManiaControl $maniaControl ) {
2014-01-14 19:47:40 +01:00
$this -> maniaControl = $maniaControl ;
2017-04-03 21:30:31 +02:00
// Action Open StatsList
$this -> maniaControl -> getManialinkManager () -> registerManialinkPageAnswerListener ( self :: ACTION_OPEN_HELP_ALL , $this , 'maniaLink_helpAll' );
2017-04-10 20:50:52 +02:00
$this -> maniaControl -> getManialinkManager () -> registerManialinkPageAnswerListener ( self :: ACTION_OPEN_ADMIN_HELP_ALL , $this , 'maniaLink_adminHelpAll' );
2017-04-03 21:30:31 +02:00
$itemQuad = new Quad_UIConstruction_Buttons ();
$itemQuad -> setSubStyle ( $itemQuad :: SUBSTYLE_Help );
$itemQuad -> setAction ( self :: ACTION_OPEN_HELP_ALL );
2017-04-10 20:50:52 +02:00
$this -> maniaControl -> getActionsMenu () -> addMenuItem ( $itemQuad , true , 0 , 'Available commands' );
$itemQuad = clone $itemQuad ;
$itemQuad -> setAction ( self :: ACTION_OPEN_ADMIN_HELP_ALL );
$this -> maniaControl -> getActionsMenu () -> addAdminMenuItem ( $itemQuad , 0 , 'Available admin commands' );
2017-04-03 21:30:31 +02:00
2014-07-25 16:28:47 +02:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: ONINIT , $this , 'handleOnInit' );
2014-01-14 20:02:35 +01:00
}
2014-02-19 15:44:00 +01:00
/**
* Handle ManiaControl OnInit Callback
*/
public function handleOnInit () {
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( 'help' , $this , 'command_playerHelp' , false , 'Shows all commands in chat.' );
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( 'helpall' , $this , 'command_playerHelpAll' , false , 'Shows all commands in ManiaLink with description.' );
$this -> maniaControl -> getCommandManager () -> registerCommandListener ( 'help' , $this , 'command_adminHelp' , true , 'Shows all admin commands in chat.' );
$this -> maniaControl -> getCommandManager () -> 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
/**
2014-07-25 16:28:47 +02:00
* Show a list of Admin Commands
2014-01-14 20:02:35 +01:00
*
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 ) {
2015-01-19 19:18:26 +01:00
// Parse list from array
$message = $this -> parseHelpList ( $this -> adminCommands );
// Show message when it's not empty
if ( $message != null ) {
$message = 'Supported Admin Commands: ' . $message ;
$this -> maniaControl -> getChat () -> sendChat ( $message , $player );
}
2014-01-14 20:02:35 +01:00
}
2014-01-14 19:47:40 +01:00
2014-01-14 20:02:35 +01:00
/**
2014-07-25 16:28:47 +02:00
* Show a list of Player Commands
2014-01-14 20:02:35 +01:00
*
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 ) {
2015-01-19 19:18:26 +01:00
// Parse list from array
2015-01-19 19:04:43 +01:00
$message = $this -> parseHelpList ( $this -> playerCommands );
2015-01-19 19:18:26 +01:00
// Show message when it's not empty
if ( $message != null ) {
$message = 'Supported Player Commands: ' . $message ;
$this -> maniaControl -> getChat () -> sendChat ( $message , $player );
}
2014-01-14 19:47:40 +01:00
}
2014-05-01 01:41:19 +02:00
/**
2014-07-25 16:28:47 +02:00
* Show a ManiaLink list of Player Commands
2014-05-01 01:41:19 +02:00
*
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 ) {
2015-01-19 19:18:26 +01:00
$this -> parseHelpList ( $this -> playerCommands , true , $player );
2014-05-01 01:41:19 +02:00
}
2017-04-03 21:30:31 +02:00
/**
2017-04-10 20:50:52 +02:00
* Method for ManiaLink answer
2017-04-03 21:30:31 +02:00
*
* @ param array $callback
* @ param \ManiaControl\Players\Player $player
2017-04-10 20:50:52 +02:00
* @ internal
2017-04-03 21:30:31 +02:00
*/
public function maniaLink_helpAll ( array $callback , Player $player ) {
$this -> parseHelpList ( $this -> playerCommands , true , $player );
}
2017-04-10 20:50:52 +02:00
/**
* Method for ManiaLink answer
*
* @ param array $callback
* @ param \ManiaControl\Players\Player $player
* @ internal
*/
public function maniaLink_adminHelpAll ( array $callback , Player $player ){
$this -> parseHelpList ( $this -> adminCommands , true , $player );
}
2014-05-01 01:58:57 +02:00
/**
2015-01-19 19:18:26 +01:00
* Parse list with commands from array
*
* @ param array $commands
* @ param bool $isHelpAll
* @ param Player $player
2017-04-03 21:30:31 +02:00
* @ return string
2015-01-19 19:18:26 +01:00
*/
2015-01-19 19:04:43 +01:00
private function parseHelpList ( array $commands , $isHelpAll = false , Player $player = null ) {
2014-05-02 17:50:30 +02:00
$showCommands = array ();
2014-05-01 01:41:19 +02:00
$registeredMethods = array ();
2015-01-19 19:18:26 +01:00
$message = '' ;
2014-05-02 17:50:30 +02:00
foreach ( array_reverse ( $commands ) as $command ) {
if ( array_key_exists ( $command [ 'Method' ], $registeredMethods )) {
2015-01-19 19:18:26 +01:00
if ( $showCommands [ $registeredMethods [ $command [ 'Method' ]]][ 'Description' ] === $command [ 'Description' ]) {
2017-04-01 18:48:36 +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' ];
}
}
2014-06-14 15:48:27 +02:00
usort ( $showCommands , function ( $commandA , $commandB ) {
return strcmp ( $commandA [ 'Name' ], $commandB [ 'Name' ]);
2014-05-01 01:41:19 +02:00
});
2015-01-19 19:18:26 +01:00
if ( ! $isHelpAll ) {
2015-01-19 19:04:43 +01:00
foreach ( $showCommands as $command ) {
$message .= $command [ 'Name' ] . ',' ;
}
$message = substr ( $message , 0 , - 1 );
2015-01-19 19:18:26 +01:00
} else {
if ( $player != null ) {
2015-01-19 19:04:43 +01:00
$this -> showHelpAllList ( $showCommands , $player );
}
}
2015-01-19 19:18:26 +01:00
return $message ;
2014-05-01 01:41:19 +02:00
}
2014-05-01 01:58:57 +02:00
/**
2014-07-25 16:28:47 +02:00
* Show the HelpAll list to the player .
2014-05-01 01:58:57 +02:00
*
2014-05-13 16:40:05 +02:00
* @ param array $commands
* @ param mixed $player
2014-05-01 01:58:57 +02:00
*/
2017-04-10 20:50:52 +02:00
public function showHelpAllList ( array $commands , $player ) {
2014-08-13 11:05:52 +02:00
$width = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsWidth ();
$height = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getListWidgetsHeight ();
2014-05-01 01:41:19 +02:00
// 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
2014-08-13 11:05:52 +02:00
$frame = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultListFrame ( $script , $paging );
2017-03-25 19:15:50 +01:00
$maniaLink -> addChild ( $frame );
2014-05-01 01:41:19 +02:00
// Start offsets
2014-06-14 15:48:27 +02:00
$posX = - $width / 2 ;
$posY = $height / 2 ;
2014-05-01 01:41:19 +02:00
//Predefine description Label
2014-08-13 11:05:52 +02:00
$descriptionLabel = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultDescriptionLabel ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $descriptionLabel );
2014-05-01 01:41:19 +02:00
// Headline
$headFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $headFrame );
2014-06-14 15:48:27 +02:00
$headFrame -> setY ( $posY - 5 );
2014-05-01 01:41:19 +02:00
2017-04-01 18:48:36 +02:00
$labelLine = new LabelLine ( $headFrame );
$labelLine -> addLabelEntryText ( 'Command' , $posX + 5 );
$labelLine -> addLabelEntryText ( 'Description' , $posX + 50 );
$labelLine -> render ();
$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 ) {
2014-06-14 15:48:27 +02:00
if ( $index % 15 === 1 ) {
2014-05-01 01:41:19 +02:00
$pageFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $pageFrame );
2014-06-14 15:48:27 +02:00
$posY = $height / 2 - 10 ;
2017-04-03 21:30:31 +02:00
$paging -> addPageControl ( $pageFrame );
2014-05-01 01:41:19 +02:00
}
$playerFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$pageFrame -> addChild ( $playerFrame );
2014-06-14 15:48:27 +02:00
$playerFrame -> setY ( $posY );
2014-05-01 01:41:19 +02:00
2014-06-14 15:48:27 +02:00
if ( $index % 2 !== 0 ) {
2014-05-01 01:41:19 +02:00
$lineQuad = new Quad_BgsPlayerCard ();
2017-03-25 19:15:50 +01:00
$playerFrame -> addChild ( $lineQuad );
2014-05-01 01:41:19 +02:00
$lineQuad -> setSize ( $width , 4 );
$lineQuad -> setSubStyle ( $lineQuad :: SUBSTYLE_BgPlayerCardBig );
$lineQuad -> setZ ( 0.001 );
}
2017-04-01 18:48:36 +02:00
$labelLine = new LabelLine ( $playerFrame );
$labelLine -> addLabelEntryText ( $command [ 'Name' ], $posX + 5 , 45 );
$labelLine -> addLabelEntryText ( $command [ 'Description' ], $posX + 50 , $width / 2 - $posX + 50 );
$labelLine -> render ();
2014-05-01 01:41:19 +02:00
2014-06-14 15:48:27 +02:00
$posY -= 4 ;
$index ++ ;
2014-05-01 01:41:19 +02:00
}
// Render and display xml
2014-08-13 11:05:52 +02:00
$this -> maniaControl -> getManialinkManager () -> displayWidget ( $maniaLink , $player , 'HelpAllList' );
2014-05-01 01:41:19 +02:00
}
2014-05-02 17:50:30 +02:00
/**
2014-07-25 16:28:47 +02:00
* Show a ManiaLink list of Admin Commands
2014-05-02 17:50:30 +02:00
*
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 ) {
2015-01-19 19:18:26 +01:00
$this -> parseHelpList ( $this -> adminCommands , true , $player );
2014-05-02 17:50:30 +02:00
}
2014-01-14 19:47:40 +01:00
/**
2014-07-25 16:28:47 +02:00
* Register a new Command
2014-01-14 19:47:40 +01:00
*
2014-07-25 16:28:47 +02:00
* @ param string $name
2014-05-01 01:41:19 +02:00
* @ param bool $adminCommand
* @ param string $description
2014-07-25 16:28:47 +02:00
* @ param string $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
}
}
2014-04-12 12:14:37 +02:00
}