rendered paste body<?php
//Each bot has its own "timer" class, minimally this runs every what 100th of a second.
//Timers are processed on there own :P
class timers {
//Local variables
public $timerStack = array(); //list of all timers indexed by name
function __construct() {
echo "YO I started!!";
}
function handle() {
if (count($this->timerStack) > 0) {
foreach($this->timerStack as $timerid => $timer) {
if ($timer['nextrun'] <= time()) {
$this->processTimer($timerid);
}
}
}
}
function processTimer($timerid) {
if (isset($this->timerStack[$timerid])) {
$timer = $this->timerStack[$timerid];
$timer['lastrun'] = time();
$timer['nextrun'] = time()+$timer['duration'];
$timer['ran']++;
$this->timerStack[$timerid] = $timer;
if (method_exists($timer['class'], $timer['function'])) {
$value = call_user_method($timer['function'], $timer['class'], $timer['args']);
} else {
$this->stop($timerid);
}
if ($timer['times'] != 0 && $timer['ran'] >= $timer['times']) { $this->stop($timerid); }
if ($value == HALT_TIMER) { $this->stop($timerid); }
}
}
/**
* Add/Updates a timer. Note when you re-add the timer gets reset.
*
* @param string $name
* @param int $times
* @param mixed $duration
* @param object $class
* @param function $function
* @param parmeters $args
* @param bool $runNow
* @return bool
*/
function add($name, $times, $duration, $class, $function, $args, $runNow = false) {
if (!method_exists($class,$function)) {
return false;
}
if (!ctype_digit($duration)) {
$duration = getDuration($duration);
}
if ($duration <= 0 || !$duration) {
return false;
}
$this->timerStack[$name] = array(
'lastrun' => time(),
'nextrun' => time()+$duration,
'class' => $class,
'function' => $function,
'args' => $args,
'times' => $times,
'duration' => $duration,
'ran' => 0);
if ($runNow == true) {
$this->processTimer($name);
}
}
function stop($timer) {
if (key_exists($timer, $this->timerStack)) {
unset($this->timerStack[$timer]);
return true;
} else {
return false;
}
}
function timer($timer) {
if (!key_exists($timer,$this->timerStack)) {
return false;
}
return $this->timerStack[$timer];
}
}
?>