TrackManiaControl/application/core/callbacks.php

193 lines
5.7 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl;
/**
* Class for handling server and controller callbacks
2013-11-09 17:24:03 +01:00
*
* @author steeffeen
*/
class Callbacks {
/**
* Constants
*/
// ManiaControl callbacks
const CB_MC_1_SECOND = 'ManiaControl.1Second';
const CB_MC_5_SECOND = 'ManiaControl.5Second';
const CB_MC_1_MINUTE = 'ManiaControl.1Minute';
const CB_MC_3_MINUTE = 'ManiaControl.3Minute';
const CB_MC_ONINIT = 'ManiaControl.OnInit';
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';
const CB_MC_ENDMAP = 'ManiaControl.EndMap';
2013-11-09 17:24:03 +01:00
// ManiaPlanet callbacks
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMAP = 'ManiaPlanet.BeginMap';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_ENDMAP = 'ManiaPlanet.EndMap';
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';
// TrackMania callbacks
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
2013-11-09 17:24:03 +01:00
/**
* Private properties
*/
private $maniaControl = null;
2013-11-09 17:24:03 +01:00
private $callbackHandlers = array();
private $last1Second = -1;
private $last5Second = -1;
private $last1Minute = -1;
private $last3Minute = -1;
/**
* Construct callbacks handler
*
* @param ManiaControl $maniaControl
2013-11-09 17:24:03 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-11-09 17:24:03 +01:00
// Init values
$this->last1Second = time();
$this->last5Second = time();
$this->last1Minute = time();
$this->last3Minute = time();
}
/**
* Perform OnInit callback
*/
public function onInit() {
// On init callback
$this->triggerCallback(self::CB_MC_ONINIT, array(self::CB_MC_ONINIT));
2013-11-09 17:24:03 +01:00
// Simulate begin map
2013-11-10 20:09:08 +01:00
$map = $this->maniaControl->server->getCurrentMap();
2013-11-09 17:24:03 +01:00
if ($map) {
$this->triggerCallback(self::CB_MC_BEGINMAP, array(self::CB_MC_BEGINMAP, array($map)));
2013-11-09 17:24:03 +01:00
}
}
/**
* Handles the given array of callbacks
*/
public function handleCallbacks() {
// Perform ManiaControl callbacks
if ($this->last1Second <= time() - 1) {
$this->last1Second = time();
// 1 second
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
2013-11-09 17:24:03 +01:00
if ($this->last5Second <= time() - 5) {
$this->last5Second = time();
// 5 second
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
2013-11-09 17:24:03 +01:00
if ($this->last1Minute <= time() - 60) {
$this->last1Minute = time();
// 1 minute
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
2013-11-09 17:24:03 +01:00
if ($this->last3Minute <= time() - 180) {
$this->last3Minute = time();
// 3 minute
$this->triggerCallback(self::CB_MC_3_MINUTE, array(self::CB_MC_3_MINUTE));
2013-11-09 17:24:03 +01:00
}
}
}
}
// Get server callbacks
if (!$this->maniaControl->client) {
return;
}
$this->maniaControl->client->resetError();
$this->maniaControl->client->readCB();
$callbacks = $this->maniaControl->client->getCBResponses();
if (!is_array($callbacks) || $this->maniaControl->client->isError()) {
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
2013-11-09 17:24:03 +01:00
return;
}
// Handle callbacks
foreach ($callbacks as $index => $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
case self::CB_MP_BEGINMAP:
{
// Map begin
$this->triggerCallback($callbackName, $callback);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
2013-11-09 17:24:03 +01:00
break;
}
case self::CB_MP_ENDMAP:
{
// Map end
$this->triggerCallback($callbackName, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
2013-11-09 17:24:03 +01:00
break;
}
default:
{
$this->triggerCallback($callbackName, $callback);
break;
}
}
}
}
/**
* Trigger a specific callback
*
* @param string $callbackName
* @param array $data
2013-11-09 17:24:03 +01:00
*/
public function triggerCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->callbackHandlers)) {
return;
}
2013-11-09 17:24:03 +01:00
foreach ($this->callbackHandlers[$callbackName] as $handler) {
call_user_func(array($handler[0], $handler[1]), $callback);
2013-11-09 17:24:03 +01:00
}
}
/**
* Add a new callback handler
*/
public function registerCallbackHandler($callback, $handler, $method) {
if (!is_object($handler) || !method_exists($handler, $method)) {
trigger_error("Given handler can't handle callback '{$callback}' (no method '{$method}')!");
2013-11-09 17:24:03 +01:00
return;
}
if (!array_key_exists($callback, $this->callbackHandlers) || !is_array($this->callbackHandlers[$callback])) {
// Init callback handler array
$this->callbackHandlers[$callback] = array();
}
// Register callback handler
array_push($this->callbackHandlers[$callback], array($handler, $method));
}
}
?>