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();
}
/**
@ -186,6 +187,7 @@ class CallbackManager {
*/
public function manageCallbacks() {
// Timed callbacks
$this->maniaControl->timerManager->manageTimings();
$this->manageTimedCallbacks();
// Server Callbacks
@ -197,6 +199,7 @@ class CallbackManager {
$callbacks = $this->maniaControl->client->executeCallbacks();
} catch(Exception $e) {
trigger_error("Error reading server callbacks. " . $e->getMessage());
return;
}
// Handle callbacks

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) {