libxmlrpc callbacks

This commit is contained in:
kremsy 2014-04-24 21:55:47 +02:00 committed by Steffen Schröder
parent 8f1b5ae791
commit 335901edd8
5 changed files with 136 additions and 130 deletions

View File

@ -7,44 +7,44 @@ use ManiaControl\ManiaControl;
/**
* Class for managing Server and ManiaControl Callbacks
*
* @author steeffeen & kremsy
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 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
*/
class CallbackManager {
/*
* Constants
*/
// ManiaControl callbacks
const CB_ONINIT = 'ManiaControl.OnInit';
const CB_AFTERINIT = 'ManiaControl.AfterInit';
const CB_ONINIT = 'ManiaControl.OnInit';
const CB_AFTERINIT = 'ManiaControl.AfterInit';
const CB_ONSHUTDOWN = 'ManiaControl.OnShutdown';
// ManiaPlanet callbacks
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
const CB_MP_ECHO = 'ManiaPlanet.Echo';
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat';
const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect';
const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect';
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
const CB_MP_ECHO = 'ManiaPlanet.Echo';
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat';
const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect';
const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect';
const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer';
const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged';
const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged';
const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated';
const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged';
const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback';
const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray';
const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived';
const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged';
const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged';
const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated';
const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged';
const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback';
const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray';
const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived';
// TrackMania callbacks
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
/*
* Public Properties
*/
@ -65,17 +65,17 @@ class CallbackManager {
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->shootManiaCallbacks = new ShootManiaCallbacks($maniaControl, $this);
$this->libXmlRpcCallbacks = new LibXmlRpcCallbackManager($maniaControl, $this);
$this->libXmlRpcCallbacks = new LibXmlRpcCallbackManager($maniaControl, $this);
}
/**
* Register a new Callback Listener
*
* @param string $callbackName
* @param string $callbackName
* @param \ManiaControl\Callbacks\CallbackListener $listener
* @param string $method
* @param string $method
* @return bool
*/
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
@ -93,9 +93,9 @@ class CallbackManager {
/**
* Register a new Script Callback Listener
*
* @param string $callbackName
* @param string $callbackName
* @param CallbackListener $listener
* @param string $method
* @param string $method
* @return bool
*/
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
@ -118,8 +118,8 @@ class CallbackManager {
*/
public function unregisterCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->callbackListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
foreach($this->callbackListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) {
continue;
}
@ -138,8 +138,8 @@ class CallbackManager {
*/
public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->scriptCallbackListener as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
foreach($this->scriptCallbackListener as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) {
continue;
}
@ -161,7 +161,7 @@ class CallbackManager {
}
$params = func_get_args();
$params = array_slice($params, 1, count($params), true);
foreach ($this->callbackListeners[$callbackName] as $listener) {
foreach($this->callbackListeners[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params);
}
}
@ -177,7 +177,7 @@ class CallbackManager {
}
$params = func_get_args();
$params = array_slice($params, 1, count($params), true);
foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params);
}
}
@ -188,16 +188,16 @@ class CallbackManager {
public function manageCallbacks() {
// Manage Timings
$this->maniaControl->timerManager->manageTimings();
// Server Callbacks
if (!$this->maniaControl->client) {
return;
}
$callbacks = $this->maniaControl->client->executeCallbacks();
// Handle callbacks
foreach ($callbacks as $callback) {
foreach($callbacks as $callback) {
$this->handleCallback($callback);
}
}
@ -209,7 +209,7 @@ class CallbackManager {
*/
private function handleCallback(array $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
switch($callbackName) {
case 'ManiaPlanet.BeginMatch':
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
$this->triggerCallback($callbackName, $callback);
@ -251,6 +251,6 @@ class CallbackManager {
$scriptCallbackData = $callback[1];
$scriptCallbackName = $scriptCallbackData[0];
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerCallback(Callbacks::ScriptCallback, $scriptCallbackName, $scriptCallbackData[1]);
$this->triggerCallback(Callbacks::SCRIPTCALLBACK, $scriptCallbackName, $scriptCallbackData[1]);
}
}

View File

@ -9,11 +9,31 @@ namespace ManiaControl\Callbacks;
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
interface Callbacks {
const ScriptCallback = 'Callbacks.ScriptCallback';
/** BeginMatch Callback, param1 MapNumber */
const BeginMatch = "Callbacks.BeginMatch";
/** LoadingMap Callback, param1 MapNumber */
const LoadingMap = "Callbacks.LoadingMap";
/** BeginMap Callback, param1 MapNumber */
const BeginMap = "Callbacks.BeginMap";
const SCRIPTCALLBACK = 'Callbacks.ScriptCallback';
/** BeginMatch Callback, param1 MatchNumber */
const BEGINMATCH = "Callbacks.BeginMatch";
/** LoadingMap Callback, Number of Map */
const LOADINGMAP = "Callbacks.LoadingMap";
/** BeginMap Callback, triggered by MapManager, param1 Map Object */
const BEGINMAP = "Callbacks.BeginMap";
/** BeginSubMatch Callback, param1 Number of Submatch */
const BEGINSUBMATCH = "Callbacks.BeginSubmatch";
/** BeginRound Callback, param1 Number of Round */
const BEGINROUND = "Callbacks.BeginRound";
/** BeginTurn Callback, param1 Number of Turn */
const BEGINTURN = "Callbacks.BeginTurn";
/** EndTurn Callback, param1 Number of Turn */
const ENDTURN = "Callbacks.EndTurn";
/** EndRound Callback, param1 Number of Round */
const ENDROUND = "Callbacks.EndRound";
/** EndSubMatch Callback, param1 Number of Submatch */
const ENDSUBMATCH = "Callbacks.EndSubmatch";
/** BeginMap Callback, triggered by MapManager, param1 Map Object */
const ENDMAP = "Callbacks.EndMap";
/** EndMatch Callback, param1 MatchNumber */
const ENDMATCH = "Callbacks.EndMatch";
/** BeginWarmup Callback, no parameters */
const BEGINWARMUP = "Callbacks.BeginWarmUp";
/** EndWarmup Callback, no parameters */
const ENDWARMUP = "Callbacks.EndWarmUp";
}

View File

@ -1,51 +1,66 @@
<?php
/**
* Created by PhpStorm.
* User: Lukas
* Date: 24.04.14
* Time: 16:57
*/
namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl;
class LibXmlRpcCallbackManager implements CallbackListener{
class LibXmlRpcCallbackManager implements CallbackListener {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new ShootMania Callbacks Instance
* Create a new LibXmlRpc Callbacks Instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
$this->maniaControl = $maniaControl;
$callbackManager->registerCallbackListener(Callbacks::ScriptCallback, $this, 'handleScriptCallbacks');
$callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallbacks');
}
public function handleScriptCallbacks($name, $data){
switch($name){
public function handleScriptCallbacks($name, $data) {
switch($name) {
case 'LibXmlRpc_BeginMatch':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BeginMatch, $data[0]);
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINMATCH, $data[0]);
break;
case 'LibXmlRpc_LoadingMap':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::LoadingMap, $data[0]);
$this->maniaControl->callbackManager->triggerCallback(Callbacks::LOADINGMAP, $data[0]);
break;
case 'BeginMap':
case 'LibXmlRpc_BeginMap':
//$this->maniaControl->callbackManager->triggerCallback(Callbacks::BeginMap, $data[0]);
$this->maniaControl->mapManager->handleScriptBeginMap($data[0]);
break;
case 'LibXmlRpc_BeginSubmatch':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINSUBMATCH, $data[0]);
break;
case 'LibXmlRpc_BeginTurn':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINTURN, $data[0]);
break;
case 'LibXmlRpc_EndTurn':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDTURN, $data[0]);
break;
case 'LibXmlRpc_EndRound':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDROUND, $data[0]);
break;
case 'LibXmlRpc_EndSubmatch':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDSUBMATCH, $data[0]);
break;
case 'EndMap':
case 'LibXmlRpc_EndMap':
$this->maniaControl->mapManager->handleScriptEndMap($data[0]);
break;
case 'LibXmlRpc_EndMatch':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDMATCH, $data[0]);
break;
case 'LibXmlRpc_BeginWarmUp':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINWARMUP);
break;
case 'LibXmlRpc_EndWarmUp':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDWARMUP);
break;
}
var_dump($name);
var_dump($data);
}
}
}

View File

@ -7,23 +7,19 @@ use ManiaControl\ManiaControl;
/**
* Class handling and parsing ShootMania Callbacks
*
* @author steeffeen
* @author steeffeen
* @copyright ManiaControl Copyright © 2014 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
*/
class ShootManiaCallbacks implements CallbackListener {
/*
* Constants
*/
const SCB_LIBXMLRPC_BEGINMAP = 'LibXmlRpc_BeginMap';
const SCB_LIBXMLRPC_ENDMAP = 'LibXmlRpc_EndMap';
const SCB_OLD_BEGINMAP = 'BeginMap';
const SCB_OLD_ENDMAP = 'EndMap';
const SCB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart';
const SCB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart';
const SCB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart';
const SCB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart';
const SCB_TIMEATTACK_ONCHECKPOINT = 'TimeAttack_OnCheckpoint';
const SCB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
const SCB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
/*
* Private Properties
*/
@ -36,51 +32,27 @@ class ShootManiaCallbacks implements CallbackListener {
*/
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
$this->maniaControl = $maniaControl;
// Register for script callbacks
$callbackManager->registerScriptCallbackListener(self::SCB_LIBXMLRPC_BEGINMAP, $this, 'callback_LibXmlRpc_BeginMap');
$callbackManager->registerScriptCallbackListener(self::SCB_OLD_BEGINMAP, $this, 'callback_LibXmlRpc_BeginMap');
$callbackManager->registerScriptCallbackListener(self::SCB_LIBXMLRPC_ENDMAP, $this, 'callback_LibXmlRpc_EndMap');
$callbackManager->registerScriptCallbackListener(self::SCB_OLD_ENDMAP, $this, 'callback_LibXmlRpc_EndMap');
$callbackManager->registerScriptCallbackListener(self::SCB_TIMEATTACK_ONCHECKPOINT, $this, 'callback_TimeAttack_OnCheckpoint');
$callbackManager->registerScriptCallbackListener(self::SCB_TIMEATTACK_ONFINISH, $this, 'callback_TimeAttack_OnFinish');
}
/**
* Handle LibXmlRpc BeginMap Script Callback
*
* @param array $callback
*/
public function callback_LibXmlRpc_BeginMap(array $callback) {
$this->maniaControl->mapManager->handleScriptBeginMap($callback);
}
/**
* Handle LibXmlRpc EndMap Script Callback
*
* @param array $callback
*/
public function callback_LibXmlRpc_EndMap(array $callback) {
$this->maniaControl->mapManager->handleScriptEndMap($callback);
}
/**
* Handle TimeAttack OnCheckpoint Script Callback
*
* @param array $callback
*/
public function callback_TimeAttack_OnCheckpoint(array $callback) {
$login = $callback[1][0];
$time = (int) $callback[1][1];
$login = $callback[1][0];
$time = (int)$callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) {
return;
}
// Trigger trackmania player checkpoint callback
$checkpointCallback = array($player->pid, $player->login, $time, 0, 0);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT,
array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback));
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT, array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback));
}
/**
@ -89,15 +61,14 @@ class ShootManiaCallbacks implements CallbackListener {
* @param array $callback
*/
public function callback_TimeAttack_OnFinish(array $callback) {
$login = $callback[1][0];
$time = (int) $callback[1][1];
$login = $callback[1][0];
$time = (int)$callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) {
return;
}
// Trigger trackmania player finish callback
$finishCallback = array($player->pid, $player->login, $time);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH,
array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback));
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback));
}
}

View File

@ -5,6 +5,7 @@ namespace ManiaControl\Maps;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Files\FileUtil;
use ManiaControl\ManiaControl;
use ManiaControl\ManiaExchange\ManiaExchangeList;
@ -29,8 +30,10 @@ class MapManager implements CallbackListener {
/*
* Constants
*/
const TABLE_MAPS = 'mc_maps';
const CB_BEGINMAP = 'MapManager.BeginMap';
const TABLE_MAPS = 'mc_maps';
/** @deprecated CB_BEGINMAP */
const CB_BEGINMAP = 'MapManager.BeginMap';
/** @deprecated CB_ENDMAP */
const CB_ENDMAP = 'MapManager.EndMap';
const CB_MAPS_UPDATED = 'MapManager.MapsUpdated';
const CB_KARMA_UPDATED = 'MapManager.KarmaUpdated';
@ -503,16 +506,18 @@ class MapManager implements CallbackListener {
// Update the mx of the map (for update checks, etc.)
$this->mxManager->fetchManiaExchangeMapInformations($this->currentMap);
// Trigger own BeginMap callback
// Trigger own BeginMap callback (
//TODO remove deprecated callback later
$this->maniaControl->callbackManager->triggerCallback(self::CB_BEGINMAP, $this->currentMap);
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINMAP, $this->currentMap);
}
/**
* Handle Script BeginMap callback
*
* @param array $callback
* @param int $mapNumber
*/
public function handleScriptBeginMap(array $callback) {
public function handleScriptBeginMap($mapNumber) {
$this->handleBeginMap(array());
}
@ -530,22 +535,17 @@ class MapManager implements CallbackListener {
// Trigger own EndMap callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_ENDMAP, $this->currentMap);
//TODO remove deprecated callback later
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDMAP, $this->currentMap);
}
/**
* Handle Script EndMap Callback
*
* @param array $callback
* @param int $mapNumber
*/
public function handleScriptEndMap(array $callback) {
if ($this->mapEnded) {
return;
}
$this->mapEnded = true;
$this->mapBegan = false;
// Trigger own EndMap callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_ENDMAP, $this->currentMap);
public function handleScriptEndMap($mapNumber) {
$this->handleEndMap(array());
}
/**