begin timin

This commit is contained in:
kremsy 2014-01-30 22:35:32 +01:00 committed by Steffen Schröder
parent b751629dc5
commit 140c77441e
5 changed files with 102 additions and 20 deletions

View File

@ -69,6 +69,7 @@ class CallbackManager {
$this->last1Second = time();
$this->last5Second = time();
$this->last1Minute = time();
}
/**
@ -80,11 +81,11 @@ class CallbackManager {
* @return bool
*/
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
if(!method_exists($listener, $method)) {
if (!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
return false;
}
if(!array_key_exists($callbackName, $this->callbackListeners)) {
if (!array_key_exists($callbackName, $this->callbackListeners)) {
$this->callbackListeners[$callbackName] = array();
}
array_push($this->callbackListeners[$callbackName], array($listener, $method));
@ -100,11 +101,11 @@ class CallbackManager {
* @return bool
*/
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if(!method_exists($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)) {
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
$this->scriptCallbackListener[$callbackName] = array();
}
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
@ -121,7 +122,7 @@ class CallbackManager {
$removed = false;
foreach($this->callbackListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) {
if ($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]);
@ -141,7 +142,7 @@ class CallbackManager {
$removed = false;
foreach($this->scriptCallbackListener as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) {
if ($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]);
@ -158,7 +159,7 @@ class CallbackManager {
* @param array $callback
*/
public function triggerCallback($callbackName, array $callback) {
if(!array_key_exists($callbackName, $this->callbackListeners)) {
if (!array_key_exists($callbackName, $this->callbackListeners)) {
return;
}
foreach($this->callbackListeners[$callbackName] as $listener) {
@ -173,7 +174,7 @@ class CallbackManager {
* @param array $callback
*/
public function triggerScriptCallback($callbackName, array $callback) {
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
return;
}
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
@ -186,17 +187,19 @@ class CallbackManager {
*/
public function manageCallbacks() {
// Timed callbacks
$this->maniaControl->timerManager->manageTimings();
$this->manageTimedCallbacks();
// Server Callbacks
if(!$this->maniaControl->client) {
if (!$this->maniaControl->client) {
return;
}
try{
try {
$callbacks = $this->maniaControl->client->executeCallbacks();
}catch(Exception $e){
} catch(Exception $e) {
trigger_error("Error reading server callbacks. " . $e->getMessage());
return;
}
// Handle callbacks
@ -204,14 +207,14 @@ class CallbackManager {
$callbackName = $callback[0];
switch($callbackName) {
case 'ManiaPlanet.BeginMap':
if(!$this->mapBegan) {
if (!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true;
$this->mapEnded = false;
}
break;
case 'ManiaPlanet.EndMap':
if(!$this->mapEnded) {
if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
$this->mapBegan = false;
@ -244,7 +247,7 @@ class CallbackManager {
case 'BeginMap':
case 'LibXmlRpc_BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if(!$this->mapBegan) {
if (!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true;
$this->mapEnded = false;
@ -253,7 +256,7 @@ class CallbackManager {
case 'EndMap':
case 'LibXmlRpc_EndMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if(!$this->mapEnded) {
if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
$this->mapBegan = false;
@ -270,21 +273,21 @@ class CallbackManager {
*/
private function manageTimedCallbacks() {
// 1 second
if($this->last1Second > time() - 1) {
if ($this->last1Second > time() - 1) {
return;
}
$this->last1Second = time();
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
// 5 second
if($this->last5Second > time() - 5) {
if ($this->last5Second > time() - 5) {
return;
}
$this->last5Second = time();
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
// 1 minute
if($this->last1Minute > time() - 60) {
if ($this->last1Minute > time() - 60) {
return;
}
$this->last1Minute = time();

View File

@ -0,0 +1,15 @@
<?php
/**
* Interface for TimerListener
*
* @author steeffeen & kremsy
*/
namespace ManiaControl\Callbacks;
interface TimerListener {
/**
* Constants
*/
const TIMERLISTENER_INTERFACE = __CLASS__;
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Created by PhpStorm.
* User: Lukas
* Date: 30.01.14
* Time: 21:11
*/
namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl;
class TimerManager {
private $maniaControl = null;
private $timerListenings = array();
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Registers a Timing Listening, note < 10ms it can get inaccurate
*
* @param TimerListener $listener
* @param $method
* @param $time
* @return bool
*/
public function registerTimerListening(TimerListener $listener, $method, $time) {
if (!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle timer (no method '{$method}')!");
return false;
}
array_push($this->timerListenings, array("Listener" => $listener, "Method" => $method, "DeltaTime" => ($time / 1000), "LastTrigger" => microtime(true)));
return true;
}
/**
* Manage the Timings on every ms
*/
public function manageTimings() {
$time = microtime(true);
foreach($this->timerListenings as $key => $listening) {
if ($listening["LastTrigger"] + $listening["DeltaTime"] < $time) {
call_user_func(array($listening["Listener"], $listening["Method"]), $time);
$this->timerListenings[$key]["LastTrigger"] += ($listening["DeltaTime"]);
}
}
}
}

View File

@ -6,6 +6,7 @@ use ErrorHandler;
use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Commands\CommandManager;
use ManiaControl\Configurators\Configurator;
@ -62,6 +63,7 @@ class ManiaControl implements CommandListener {
public $statisticManager = null;
public $updateManager = null;
public $errorHandler = null;
public $timerManager = null;
/**
* Private properties
@ -83,6 +85,7 @@ class ManiaControl implements CommandListener {
// Load ManiaControl Modules
$this->database = new Database($this);
$this->callbackManager = new CallbackManager($this);
$this->timerManager = new TimerManager($this);
$this->settingManager = new SettingManager($this);
$this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this);
@ -277,7 +280,8 @@ class ManiaControl implements CommandListener {
// Yield for next tick
$loopEnd = microtime(true);
$sleepTime = 1000 - $loopEnd + $loopStart;
$sleepTime = (int) (1000 - $loopEnd + $loopStart);
if ($sleepTime > 0) {
usleep($sleepTime);
}

View File

@ -18,6 +18,7 @@ use FML\Script\Script;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
@ -28,7 +29,7 @@ use ManiaControl\Manialinks\ManialinkPageAnswerListener;
*
* @author steeffeen & kremsy
*/
class PlayerList implements ManialinkPageAnswerListener, CallbackListener {
class PlayerList implements ManialinkPageAnswerListener, CallbackListener, TimerListener {
/**
* Constants
*/
@ -75,6 +76,12 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'updateWidget');
$this->maniaControl->timerManager->registerTimerListening($this, 'test', 1); //TODo just a test
}
public function test($triggerTime){
var_dump($triggerTime);
}
public function addPlayerToShownList(Player $player, $showStatus = self::SHOWN_MAIN_WINDOW) {