- Moved FML into core folder
- Endurance plugin - map class nbCheckpoints - added script callbacks registration
This commit is contained in:
parent
65394e165e
commit
7f1646f25d
@ -53,6 +53,7 @@ class CallbackManager {
|
||||
*/
|
||||
private $maniaControl = null;
|
||||
private $callbackListeners = array();
|
||||
private $scriptCallbackListener = array();
|
||||
private $last1Second = -1;
|
||||
private $last5Second = -1;
|
||||
private $last1Minute = -1;
|
||||
@ -92,6 +93,28 @@ class CallbackManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new script callback listener
|
||||
*
|
||||
* @param string $callbackName
|
||||
* @param CallbackListener $listener
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
|
||||
if (!method_exists($listener, $method)) {
|
||||
trigger_error(
|
||||
"Given listener (" . get_class($listener) .
|
||||
") can't handle script callback '{$callbackName}' (no method '{$method}')!");
|
||||
return false;
|
||||
}
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
||||
$this->scriptCallbackListener[$callbackName] = array();
|
||||
}
|
||||
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a specific callback
|
||||
*
|
||||
@ -107,6 +130,21 @@ class CallbackManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a specific script callback
|
||||
*
|
||||
* @param string $callbackName
|
||||
* @param array $callback
|
||||
*/
|
||||
public function triggerScriptCallback($callbackName, array $callback) {
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
||||
return;
|
||||
}
|
||||
foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
|
||||
call_user_func(array($listener[0], $listener[1]), $callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger internal and manage server callbacks
|
||||
*/
|
||||
@ -141,6 +179,7 @@ class CallbackManager {
|
||||
case self::CB_MP_MODESCRIPTCALLBACK:
|
||||
{
|
||||
$this->handleScriptCallback($callback);
|
||||
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -158,22 +197,24 @@ class CallbackManager {
|
||||
* @param array $callback
|
||||
*/
|
||||
private function handleScriptCallback(array $callback) {
|
||||
$scriptCallbackName = $callback[1][0];
|
||||
$scriptCallbackData = $callback[1];
|
||||
$scriptCallbackName = $scriptCallbackData[0];
|
||||
switch ($scriptCallbackName) {
|
||||
case 'EndMap':
|
||||
{
|
||||
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
|
||||
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
||||
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
|
||||
break;
|
||||
}
|
||||
case 'LibXmlRpc_EndMap':
|
||||
{
|
||||
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
|
||||
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
||||
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace ManiaControl\Manialinks;
|
||||
|
||||
require_once __DIR__ . '/../../FML/autoload.php';
|
||||
require_once __DIR__ . '/../FML/autoload.php';
|
||||
|
||||
/**
|
||||
* Manialink utility class
|
||||
|
@ -23,6 +23,7 @@ class Map {
|
||||
public $copperPrice = 0;
|
||||
public $mapType = '';
|
||||
public $mapStyle = '';
|
||||
public $nbCheckpoints = -1;
|
||||
public $mx = null;
|
||||
public $authorLogin = '';
|
||||
public $authorNick = '';
|
||||
@ -60,6 +61,7 @@ class Map {
|
||||
$this->copperPrice = $rpc_infos['CopperPrice'];
|
||||
$this->mapType = $rpc_infos['MapType'];
|
||||
$this->mapStyle = $rpc_infos['MapStyle'];
|
||||
$this->nbCheckpoints = $rpc_infos['NbCheckpoints'];
|
||||
|
||||
$mapsDirectory = $this->maniaControl->server->getMapsDirectory();
|
||||
if ($this->maniaControl->server->checkAccess($mapsDirectory)) {
|
||||
|
94
application/plugins/Endurance.php
Normal file
94
application/plugins/Endurance.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Plugins\Plugin;
|
||||
|
||||
/**
|
||||
* Plugin for the TM Game Mode 'Endurance' by TGYoshi
|
||||
*
|
||||
* @author steeffeen
|
||||
*/
|
||||
class Endurance_Plugin extends Plugin implements CallbackListener {
|
||||
/**
|
||||
* Constants
|
||||
*/
|
||||
const VERSION = '1.0';
|
||||
const CB_CHECKPOINT = 'Endurance.Checkpoint';
|
||||
|
||||
/**
|
||||
* Private properties
|
||||
*/
|
||||
private $currentMap = null;
|
||||
private $playerLapTimes = array();
|
||||
|
||||
/**
|
||||
* Create a new endurance plugin instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Plugin information
|
||||
$this->name = 'Endurance Plugin';
|
||||
$this->version = self::VERSION;
|
||||
$this->author = 'steeffeen';
|
||||
$this->description = "Plugin enabling Support for the TM Game Mode 'Endurance' by TGYoshi.";
|
||||
|
||||
// Register for callbacks
|
||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'callback_OnInit');
|
||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'callback_BeginMap');
|
||||
$this->maniaControl->callbackManager->registerScriptCallbackListener(self::CB_CHECKPOINT, $this, 'callback_Checkpoint');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ManiaControl OnInit callback
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function callback_OnInit(array $callback) {
|
||||
$this->currentMap = $this->maniaControl->mapManager->getCurrentMap();
|
||||
$this->playerLapTimes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle BeginMap callback
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function callback_BeginMap(array $callback) {
|
||||
$this->currentMap = $this->maniaControl->mapManager->getCurrentMap();
|
||||
$this->playerLapTimes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Endurance Checkpoint callback
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function callback_Checkpoint(array $callback) {
|
||||
$callbackData = json_decode($callback[1]);
|
||||
if ($callbackData->Checkpoint % $this->currentMap->nbCheckpoints != 0) {
|
||||
return;
|
||||
}
|
||||
$player = $this->maniaControl->playerManager->getPlayer($callbackData->Login);
|
||||
if (!$player) {
|
||||
return;
|
||||
}
|
||||
$time = $callbackData->Time;
|
||||
if ($time <= 0) {
|
||||
return;
|
||||
}
|
||||
if (isset($this->playerLapTimes[$player->login])) {
|
||||
$time -= $this->playerLapTimes[$player->login];
|
||||
}
|
||||
$this->playerLapTimes[$player->login] = $callbackData->Time;
|
||||
// Trigger trackmania player finish callback
|
||||
$finishCallback = array($player->pid, $player->login, $time);
|
||||
$finishCallback = array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback);
|
||||
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user