2013-11-25 19:46:29 +01:00
|
|
|
<?php
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2014-02-08 15:00:21 +01:00
|
|
|
use ManiaControl\Maps\Map;
|
2013-11-25 19:46:29 +01:00
|
|
|
use ManiaControl\Plugins\Plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin for the TM Game Mode 'Endurance' by TGYoshi
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
2013-12-03 22:21:17 +01:00
|
|
|
class EndurancePlugin implements CallbackListener, Plugin {
|
2013-11-25 19:46:29 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-12-09 10:04:22 +01:00
|
|
|
const ID = 4;
|
|
|
|
const VERSION = 0.1;
|
2013-11-25 19:46:29 +01:00
|
|
|
const CB_CHECKPOINT = 'Endurance.Checkpoint';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-12-30 16:52:26 +01:00
|
|
|
/** @var maniaControl $maniaControl */
|
2013-12-14 23:29:17 +01:00
|
|
|
private $maniaControl = null;
|
2014-02-08 15:00:21 +01:00
|
|
|
/** @var Map $currentMap */
|
2013-11-25 19:46:29 +01:00
|
|
|
private $currentMap = null;
|
|
|
|
private $playerLapTimes = array();
|
|
|
|
|
2014-01-27 20:39:10 +01:00
|
|
|
/**
|
|
|
|
* Prepares the Plugin
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function prepare(ManiaControl $maniaControl) {
|
2014-01-27 21:05:02 +01:00
|
|
|
//do nothing
|
2014-01-27 20:39:10 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:46:29 +01:00
|
|
|
/**
|
|
|
|
*
|
2013-12-14 23:29:17 +01:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::load()
|
2013-11-25 19:46:29 +01:00
|
|
|
*/
|
2013-12-14 23:29:17 +01:00
|
|
|
public function load(ManiaControl $maniaControl) {
|
2013-11-25 19:46:29 +01:00
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
|
|
|
// Register for callbacks
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'callback_OnInit');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'callback_BeginMap');
|
2013-11-25 19:46:29 +01:00
|
|
|
$this->maniaControl->callbackManager->registerScriptCallbackListener(self::CB_CHECKPOINT, $this, 'callback_Checkpoint');
|
2013-12-14 23:29:17 +01:00
|
|
|
|
|
|
|
return true;
|
2013-11-25 19:46:29 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 23:29:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::unload()
|
|
|
|
*/
|
|
|
|
public function unload() {
|
|
|
|
$this->maniaControl->callbackManager->unregisterCallbackListener($this);
|
|
|
|
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($this);
|
|
|
|
unset($this->maniaControl);
|
|
|
|
}
|
2013-12-09 10:04:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getId()
|
|
|
|
*/
|
|
|
|
public static function getId() {
|
|
|
|
return self::ID;
|
|
|
|
}
|
2013-12-14 23:29:17 +01:00
|
|
|
|
2013-12-03 22:21:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getName()
|
|
|
|
*/
|
|
|
|
public static function getName() {
|
|
|
|
return 'Endurance Plugin';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getVersion()
|
|
|
|
*/
|
|
|
|
public static function getVersion() {
|
|
|
|
return self::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getAuthor()
|
|
|
|
*/
|
|
|
|
public static function getAuthor() {
|
|
|
|
return 'steeffeen';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getDescription()
|
|
|
|
*/
|
|
|
|
public static function getDescription() {
|
|
|
|
return "Plugin enabling Support for the TM Game Mode 'Endurance' by TGYoshi.";
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:46:29 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|