TrackManiaControl/core/Script/ModeScriptEventManager.php

555 lines
18 KiB
PHP
Raw Normal View History

2017-03-22 17:57:21 +01:00
<?php
namespace ManiaControl\Script;
2017-03-22 17:57:21 +01:00
use ManiaControl\Callbacks\Callbacks;
2017-04-16 14:43:13 +02:00
use ManiaControl\Callbacks\Structures\XmlRpc\CallbackListStructure;
use ManiaControl\Callbacks\Structures\XmlRpc\DocumentationStructure;
use ManiaControl\Callbacks\Structures\XmlRpc\MethodListStructure;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
2017-05-13 23:05:47 +02:00
use ManiaControl\Logger;
2017-03-22 17:57:21 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2017-05-13 23:05:47 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
2017-03-22 17:57:21 +01:00
2017-04-07 17:34:57 +02:00
/**
* Manager for Mode Script Events
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2017 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ModeScriptEventManager implements UsageInformationAble {
use UsageInformationTrait;
2017-03-23 20:39:32 +01:00
const API_VERSION = "2.0.0";
2017-03-22 17:57:21 +01:00
/** @var ManiaControl $maniaControl */
private $maniaControl;
/**
* Construct a new ranking manager instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Enables XmlRpc Callbacks
*/
public function enableCallbacks() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.EnableCallbacks', array('true'));
2017-03-22 18:21:33 +01:00
2017-03-23 20:39:32 +01:00
$this->setApiVersion(self::API_VERSION);
2017-04-16 14:43:13 +02:00
$this->unBlockAllCallbacks();
2017-03-22 17:57:21 +01:00
}
/**
* Disables XmlRpc Callbacks
*/
public function disableCallbacks() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.EnableCallbacks', array('false'));
2017-03-22 17:57:21 +01:00
}
2017-03-22 18:21:33 +01:00
2017-03-22 18:21:33 +01:00
/**
* Request a list of all available callbacks. This method will trigger the "XmlRpc.CallbacksList" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
2017-03-22 18:21:33 +01:00
*/
public function getCallbacksList() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetCallbacksList', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_CALLBACKSLIST, $responseId);
2017-03-22 18:21:33 +01:00
}
/**
* Prints the List of XMLRPC Callbacks in the Console
*
* @api
*/
public function printCallbacksList() {
$this->getCallbacksList()->setCallable(function (CallbackListStructure $structure) {
var_dump($structure->getCallbacks());
});
}
2017-04-07 17:34:57 +02:00
/**
* Provide a Array of Callbacks you want to Block
*
* @api
2017-04-07 17:34:57 +02:00
* @param array $callbackNames
*/
public function blockCallbacks($callbackNames) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.BlockCallbacks', $callbackNames);
2017-04-07 17:34:57 +02:00
}
/**
* Block a Single Callback
*
* @api
2017-04-07 17:34:57 +02:00
* @param $callbackName
*/
public function blockCallback($callbackName) {
$this->blockCallbacks(array($callbackName));
}
/**
2017-04-16 14:43:13 +02:00
* UnBlocks All Callbacks
*
* @api
*/
public function unBlockAllCallbacks() {
$this->getListOfDisabledCallbacks()->setCallable(function (CallbackListStructure $structure) {
$this->unBlockCallbacks($structure->getCallbacks());
});
}
/**
* Provide a Array of Callbacks you want to UnBlock
2017-04-07 17:34:57 +02:00
*
* @api
2017-04-07 17:34:57 +02:00
* @param array $callbackNames
*/
public function unBlockCallbacks($callbackNames) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.UnblockCallbacks', $callbackNames);
2017-04-07 17:34:57 +02:00
}
/**
* Block a Single Callback
*
* @api
2017-04-07 17:34:57 +02:00
* @param $callbackName
*/
public function unBlockCallback($callbackName) {
$this->unBlockCallbacks(array($callbackName));
}
/**
* Request a list of all enabled callbacks. This method will trigger the "XmlRpc.CallbacksList_Enabled" callback.
*
2017-04-07 18:37:36 +02:00
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getListOfEnabledCallbacks() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetCallbacksList_Enabled', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_ENABLEDCALLBACKS, $responseId);
}
/**
* Request a list of all disabled callbacks. This method will trigger the "XmlRpc.CallbacksList_Enabled" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getListOfDisabledCallbacks() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetCallbacksList_Disabled', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_DISABLEDCALLBACKS, $responseId);
}
/**
* Description: Request help about a callback. This method will trigger the "XmlRpc.CallbackHelp" callback.
*
* @api
* @param $callbackName
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getCallbackHelp($callbackName) {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetCallbackHelp', array($callbackName, $responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_CALLBACKHELP, $responseId);
}
/**
* Request a list of all available methods. This method will trigger the "XmlRpc.MethodsList" callback.s
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getMethodsList() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetMethodsList', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_METHODSLIST, $responseId);
}
/**
* Prints the List of XMLRPC Methods in the Console
*
* @api
*/
public function printMethodsList() {
$this->getMethodsList()->setCallable(function (MethodListStructure $structure) {
var_dump($structure->getMethods());
});
}
2017-03-22 18:21:33 +01:00
/**
* Sets the Api Version
*
2017-03-22 18:21:33 +01:00
* @param string $version
*/
public function setApiVersion($version = self::API_VERSION) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.SetApiVersion', array($version));
2017-03-22 18:21:33 +01:00
}
/**
* Gets the Api Version
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getApiVersion() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetApiVersion', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_APIVERSION, $responseId);
}
/**
* Request help about a method. This method will trigger the "XmlRpc.MethodHelp" callback.
*
* @api
* @param $methodName
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getMethodHelp($methodName) {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetMethodHelp', array($methodName, $responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_METHODHELP, $responseId);
}
/**
* Request the current game mode xmlrpc callbacks and methods documentation. This method will trigger the "XmlRpc.Documentation" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getDocumentation() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetDocumentation', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_DOCUMENTATION, $responseId);
}
/**
* Printes the XMLRPC Documentation in the Console
*
* @api
*/
public function printDocumentation() {
$this->getDocumentation()->setCallable(function (DocumentationStructure $structure) {
var_dump($structure->getDocumentation());
});
}
/**
* Gets a List of All Api Version
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getAllApiVersions() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('XmlRpc.GetAllApiVersions', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::XMLRPC_ALLAPIVERSIONS, $responseId);
2017-03-22 18:21:33 +01:00
}
/**
* Hides the Scoreboard on Pressing Alt
*
* @api
* @param \ManiaControl\Players\Player $player
*/
public function hideScoreBoardOnAlt(Player $player) {
$login = Player::parseLogin($player);
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.UI.SetAltScoresTableVisibility', array($login, "False"));
}
/**
* Displays the Scoreboard on Pressing Alt
*
* @api
* @param \ManiaControl\Players\Player $player
*/
public function displayScoreBoardOnAlt(Player $player) {
$login = Player::parseLogin($player);
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.UI.SetAltScoresTableVisibility', array($login, "True"));
}
2017-04-07 17:34:57 +02:00
/**
* Extend the duration of any ongoing warmup.
*
* @api
2017-04-13 20:46:23 +02:00
* @param $seconds < the duration of the extension in seconds.
2017-04-07 17:34:57 +02:00
*/
2017-04-13 20:46:23 +02:00
public function extendManiaPlanetWarmup($seconds) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.Extend', array(strval($seconds * 1000)));
2017-04-07 17:34:57 +02:00
}
/**
* Stop any ongoing warmup.
2017-04-07 18:37:36 +02:00
*
* @api
2017-04-07 17:34:57 +02:00
*/
public function stopManiaPlanetWarmup() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.Stop');
2017-05-15 21:25:16 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.ForceStop');
2017-04-07 17:34:57 +02:00
}
/**
* Blocks the End of the Warmup,
*
* @param int $time Timer before the end of the warmup when all players are ready. Use a negative value to prevent the warmup from ending even if all players are ready.
*/
public function blockEndWarmUp($time = -1) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.BlockEndWarmUp', array("True", strval($time)));
}
/**
* Blocks the End of the Warmup,
*
* @param int $time Timer before the end of the warmup when all players are ready. Use a negative value to prevent the warmup from ending even if all players are ready.
*/
public function unBlockEndWarmUp($time = -1) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.BlockEndWarmUp', array("False", strval($time)));
}
2017-04-07 17:34:57 +02:00
/**
* Get the status of the warmup.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
2017-04-07 17:34:57 +02:00
*/
public function getWarmupStatus() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.WarmUp.GetStatus', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::MP_WARMUP_STATUS, $responseId);
2017-04-07 17:34:57 +02:00
}
/**
* Get the status of the pause.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
2017-04-07 17:34:57 +02:00
*/
public function getPauseStatus() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.Pause.GetStatus', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::MP_PAUSE_STATUS, $responseId);
2017-04-07 17:34:57 +02:00
}
/**
* Start a Pause and triggers a Callback for the Pause Status
2017-04-07 17:34:57 +02:00
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback To get The Pause Status You can directly set a callable on it via setCallable()
2017-04-07 17:34:57 +02:00
*/
public function startPause() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.Pause.SetActive', array("True", $responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::MP_PAUSE_STATUS, $responseId);
2017-04-07 17:34:57 +02:00
}
/**
* End a Pause and triggers a Callback for the Pause Status
2017-04-07 17:34:57 +02:00
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback To get The Pause Status You can directly set a callable on it via setCallable()
2017-04-07 17:34:57 +02:00
*/
public function endPause() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.Pause.SetActive', array("False", $responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::MP_PAUSE_STATUS, $responseId);
}
/**
* Returns if the GameMode is a TeamMode or not
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback To get The TeamMode Status You can directly set a callable on it via setCallable()
*/
public function isTeamMode() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Maniaplanet.Mode.GetUseTeams', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::MP_USES_TEAMMODE, $responseId);
2017-04-07 17:34:57 +02:00
}
/**
* Move the spectators' timers UI.
*
* @api
2017-04-07 17:34:57 +02:00
* @param $x
* @param $y
* @param $z
*/
2017-05-12 19:43:25 +02:00
public function setComboTimerPosition($x, $y, $z) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Shootmania.Combo.SetTimersPosition', array(strval(floatval($x)), strval(floatval($y)), strval(floatval($z))));
2017-04-07 17:34:57 +02:00
}
/**
* Move the progression UI.
*
* @api
2017-04-07 17:34:57 +02:00
* @param $x
* @param $y
* @param $z
*/
2017-05-12 19:43:25 +02:00
public function setSiegeProgressionUIPosition($x, $y, $z) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Shootmania.Siege.SetProgressionUIPosition', array(strval(floatval($x)), strval(floatval($y)), strval(floatval($z))));
2017-04-07 17:34:57 +02:00
}
/**
* Request the current scores. This method will trigger the "Shootmania.Scores" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getShootmaniaScores() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Shootmania.GetScores', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::SM_SCORES, $responseId);
}
/**
* Request the current ui properties. This method will trigger the "Shootmania.UIProperties" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getShootmaniaUIProperties() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Shootmania.UI.GetProperties', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::SM_UIPROPERTIES, $responseId);
}
/**
* Update the ui properties.
*
* @api
* @param string Json-Encoded Xml UI Property String
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable() to get the updated Properties
*/
public function setShootmaniaUIProperties($properties) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Shootmania.UI.SetProperties', array($properties));
return $this->getShootmaniaUIProperties();
}
/**
* Request the current scores. This method will trigger the "Trackmania.Scores" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getTrackmaniaScores() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.GetScores', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::TM_SCORES, $responseId);
}
/**
* Request the current points repartition. This method will trigger the "Trackmania.PointsRepartition" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getTrackmaniaPointsRepartition() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.GetPointsRepartition', array($responseId));
return new InvokeScriptCallback($this->maniaControl, Callbacks::TM_POINTSREPARTITION, $responseId);
}
/**
* Update the points repartition.
*
* @api
* @param array String Array of Points
*/
public function setTrackmaniaPointsRepartition($pointArray) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.SetPointsRepartition', $pointArray);
}
/**
* Request the current ui properties. This method will trigger the "Shootmania.UIProperties" callback.
*
* @api
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable()
*/
public function getTrackmaniaUIProperties() {
$responseId = $this->generateResponseId();
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.UI.GetProperties', array($responseId));
2017-05-08 20:26:28 +02:00
return new InvokeScriptCallback($this->maniaControl, Callbacks::TM_UIPROPERTIES, $responseId);
}
/**
* Update the ui properties.
2017-04-07 18:37:36 +02:00
*
* @api
* @param string Json-Encoded Xml UI Property String
* @return \ManiaControl\Script\InvokeScriptCallback You can directly set a callable on it via setCallable() to get the updated Properties
*/
public function setTrackmaniaUIProperties($properties) {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.UI.SetProperties', array($properties));
return $this->getTrackmaniaUIProperties();
}
/**
* Stop the whole warm up sequence.
2017-04-07 18:37:36 +02:00
*
* @api
*/
public function stopTrackmaniaWarmup() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.WarmUp.Stop');
}
/**
* Stop the current warm up round.
2017-04-07 18:37:36 +02:00
*
* @api
*/
public function stopTrackmaniaRound() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.WarmUp.StopRound');
}
/**
* Stop the current round. Only available in Cup, Rounds and Team modes.
2017-04-07 18:37:36 +02:00
*
* @api
*/
public function forceTrackmaniaRoundEnd() {
2017-05-13 23:05:47 +02:00
$this->triggerModeScriptEvent('Trackmania.ForceEndRound');
}
/**
* Triggers a ModeScript Event
*
* @api
* @param $eventName
* @param string $data
*/
public function triggerModeScriptEvent($eventName, $data = '') {
2017-05-14 09:25:52 +02:00
$this->maniaControl->getClient()->triggerModeScriptEvent($eventName, $data, function ($exception) use ($eventName) {
if ($exception instanceof GameModeException) {
if ($exception->getMessage() != 'Not in script mode.') {
throw $exception;
}
Logger::logWarning($eventName . " can't be triggered because you are not in Scriptmode, start your server in Scriptmode!");
2017-05-13 23:05:47 +02:00
}
2017-05-14 09:25:52 +02:00
});
}
/**
* Generates the needed Unique ResponseId
2017-04-07 18:37:36 +02:00
*
* @return string
*/
private function generateResponseId() {
return uniqid("ManiaControl.");
}
2017-03-22 17:57:21 +01:00
}