- extra shootmania callback class for parsing script callbacks

- timeattack local records support
This commit is contained in:
Steffen Schröder 2014-03-19 21:58:11 +01:00
parent 06907825ed
commit 87549fdf9b
3 changed files with 186 additions and 84 deletions

View File

@ -14,37 +14,42 @@ class CallbackManager {
* Constants * Constants
*/ */
// ManiaControl callbacks // ManiaControl callbacks
const CB_ONINIT = 'ManiaControl.OnInit'; const CB_ONINIT = 'ManiaControl.OnInit';
const CB_AFTERINIT = 'ManiaControl.AfterInit'; const CB_AFTERINIT = 'ManiaControl.AfterInit';
const CB_ONSHUTDOWN = 'ManiaControl.OnShutdown'; const CB_ONSHUTDOWN = 'ManiaControl.OnShutdown';
// ManiaPlanet callbacks // ManiaPlanet callbacks
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart'; const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop'; const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch'; const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch'; const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified'; const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
const CB_MP_ECHO = 'ManiaPlanet.Echo'; const CB_MP_ECHO = 'ManiaPlanet.Echo';
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated'; const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat'; const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat';
const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect'; const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect';
const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect'; const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect';
const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer'; const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer';
const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged'; const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged';
const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged'; const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged';
const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated'; const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated';
const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged'; const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged';
const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback'; const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback';
const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray'; const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray';
const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived'; const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived';
// TrackMania callbacks // TrackMania callbacks
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint'; const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish'; const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence'; const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
/** /*
* Private Properties * Public proeprties
*/
public $shootManiaCallbacks = null;
/*
* Private properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $callbackListeners = array(); private $callbackListeners = array();
@ -57,14 +62,16 @@ class CallbackManager {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->shootManiaCallbacks = new ShootManiaCallbacks($maniaControl, $this);
} }
/** /**
* Register a new Callback Listener * Register a new Callback Listener
* *
* @param string $callbackName * @param string $callbackName
* @param \ManiaControl\Callbacks\CallbackListener $listener * @param \ManiaControl\Callbacks\CallbackListener $listener
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
@ -82,9 +89,9 @@ class CallbackManager {
/** /**
* Register a new Script Callback Listener * Register a new Script Callback Listener
* *
* @param string $callbackName * @param string $callbackName
* @param CallbackListener $listener * @param CallbackListener $listener
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
@ -107,8 +114,8 @@ class CallbackManager {
*/ */
public function unregisterCallbackListener(CallbackListener $listener) { public function unregisterCallbackListener(CallbackListener $listener) {
$removed = false; $removed = false;
foreach($this->callbackListeners as &$listeners) { foreach ($this->callbackListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) { foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) { if ($listenerCallback[0] != $listener) {
continue; continue;
} }
@ -127,8 +134,8 @@ class CallbackManager {
*/ */
public function unregisterScriptCallbackListener(CallbackListener $listener) { public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false; $removed = false;
foreach($this->scriptCallbackListener as &$listeners) { foreach ($this->scriptCallbackListener as &$listeners) {
foreach($listeners as $key => &$listenerCallback) { foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) { if ($listenerCallback[0] != $listener) {
continue; continue;
} }
@ -150,7 +157,7 @@ class CallbackManager {
} }
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, count($params), true); $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); call_user_func_array(array($listener[0], $listener[1]), $params);
} }
} }
@ -166,7 +173,7 @@ class CallbackManager {
} }
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, count($params), true); $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); call_user_func_array(array($listener[0], $listener[1]), $params);
} }
} }
@ -186,39 +193,48 @@ class CallbackManager {
$callbacks = $this->maniaControl->client->executeCallbacks(); $callbacks = $this->maniaControl->client->executeCallbacks();
// Handle callbacks // Handle callbacks
foreach($callbacks as $callback) { foreach ($callbacks as $callback) {
$callbackName = $callback[0]; $this->handleCallback($callback);
switch($callbackName) { }
case 'ManiaPlanet.BeginMatch': }
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
$this->triggerCallback($callbackName, $callback); /**
break; * Handle the given Callback
} *
case 'ManiaPlanet.BeginMap': * @param array $callback
$this->maniaControl->mapManager->handleBeginMap($callback); */
private function handleCallback(array $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
case 'ManiaPlanet.BeginMatch':
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
$this->triggerCallback($callbackName, $callback); $this->triggerCallback($callbackName, $callback);
break; break;
case 'ManiaPlanet.EndMatch': //TODO temporary fix }
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') { case 'ManiaPlanet.BeginMap':
$this->triggerCallback($callbackName, $callback); $this->maniaControl->mapManager->handleBeginMap($callback);
break; $this->triggerCallback($callbackName, $callback);
} break;
case 'ManiaPlanet.EndMap': case 'ManiaPlanet.EndMatch': // TODO temporary fix
$this->maniaControl->mapManager->handleEndMap($callback); if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
$this->triggerCallback($callbackName, $callback); $this->triggerCallback($callbackName, $callback);
break; break;
case self::CB_MP_MODESCRIPTCALLBACK: }
$this->handleScriptCallback($callback); case 'ManiaPlanet.EndMap':
$this->triggerCallback($callbackName, $callback); $this->maniaControl->mapManager->handleEndMap($callback);
break; $this->triggerCallback($callbackName, $callback);
case self::CB_MP_MODESCRIPTCALLBACKARRAY: break;
$this->handleScriptCallback($callback); case self::CB_MP_MODESCRIPTCALLBACK:
$this->triggerCallback($callbackName, $callback); $this->handleScriptCallback($callback);
break; $this->triggerCallback($callbackName, $callback);
default: break;
$this->triggerCallback($callbackName, $callback); case self::CB_MP_MODESCRIPTCALLBACKARRAY:
break; $this->handleScriptCallback($callback);
} $this->triggerCallback($callbackName, $callback);
break;
default:
$this->triggerCallback($callbackName, $callback);
break;
} }
} }
@ -230,20 +246,6 @@ class CallbackManager {
private function handleScriptCallback(array $callback) { private function handleScriptCallback(array $callback) {
$scriptCallbackData = $callback[1]; $scriptCallbackData = $callback[1];
$scriptCallbackName = $scriptCallbackData[0]; $scriptCallbackName = $scriptCallbackData[0];
switch($scriptCallbackName) { $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
case 'BeginMap':
case 'LibXmlRpc_BeginMap':
$this->maniaControl->mapManager->handleScriptBeginMap($callback);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
break;
case 'EndMap':
case 'LibXmlRpc_EndMap':
$this->maniaControl->mapManager->handleScriptEndMap($callback);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
break;
default:
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
break;
}
} }
} }

View File

@ -0,0 +1,101 @@
<?php
namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl;
/**
* Class handling and parsing ShootMania Callbacks
*
* @author steeffeen
*/
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_ONCHECKPOINT = 'TimeAttack_OnCheckpoint';
const SCB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
/*
* Private properties
*/
private $maniaControl = null;
/**
* Create a new ShootMania Callbacks Instance
*
* @param ManiaControl $maniaControl
*/
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];
$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));
}
/**
* Handle TimeAttack OnFinish Script Callback
*
* @param array $callback
*/
public function callback_TimeAttack_OnFinish(array $callback) {
$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));
}
}

View File

@ -32,7 +32,6 @@ require_once __DIR__ . '/Libs/FML/autoload.php';
require_once __DIR__ . '/Libs/Symfony/autoload.php'; require_once __DIR__ . '/Libs/Symfony/autoload.php';
require_once __DIR__ . '/Libs/curl-easy/autoload.php'; require_once __DIR__ . '/Libs/curl-easy/autoload.php';
/** /**
* ManiaControl Server Controller for ManiaPlanet Server * ManiaControl Server Controller for ManiaPlanet Server
* *