28
[irbot.git] / sources / tick-class.inc.php
blobcb8688127f4c1c5f54ec1efb1ef3889272dfbb24
1 <?php
2 /**
3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 class tick {
21 private $ticks = array();
22 private $ircmain;
24 static public $instance = false;
26 public static function GetInstance() {
27 if (!self::$instance) {
28 self::$instance = new tick();
30 return self::$instance;
33 private function __construct() {
34 $this->ircmain = bot::GetInstance();
37 /**
38 * Adding a job to the tick list $tickname
40 * @param string $tickname
41 * @param string $jobname
42 * @param string $function Function to call
43 * @param array $params Parameters send to the $function
44 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
45 * @param object $object Reference to an object if $function is a method
46 * @return boolean
48 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
49 if (isset($this->ticks[$tickname])) {
50 if (!is_null($object)) {
51 $this->ticks[$tickname]['jobs'][] = array(
52 'jname' => $jobname,
53 'function' => $function,
54 'params' => $params,
55 'temporary' => $temporary,
56 'objectRef' => &$object
58 } else {
59 $this->ticks[$tickname]['jobs'][] = array(
60 'jname' => $jobname,
61 'function' => $function,
62 'params' => $params,
63 'temporary' => $temporary
66 return true;
68 return false;
71 /**
72 * Create a tick if not exist
74 * @param string $name
75 * @param int $timer Number of seconds before the next tick
76 * @return boolean
78 public function setTick($name,$timer) {
79 if (!isset($this->ticks[$name])) {
80 $this->ticks[$name] = array(
81 'timer' => $timer,
82 'reference' => time(),
83 'tickOn' => strtotime("+$timer seconds"),
84 'jobs' => array()
86 return true;
88 return false;
91 /**
92 * Delete a tick
94 * @param string $name
95 * @return boolean
97 public function delTick($name) {
98 if (isset($this->ticks[$name])) {
99 unset($this->ticks[$name]);
100 return true;
102 return false;
105 public function doAllTicks() {
106 $curtime = time();
108 foreach($this->ticks as $name => $options) {
110 if ($options['tickOn']<=$curtime) {
111 echo $options['tickOn']," - $curtime\n";
113 foreach ($options['jobs'] as $functions) {
115 if (!$functions['temporary']) {
116 $jlist[] = $functions;
119 if (!$this->ircmain->connected) {
120 continue;
123 switch (isset($functions['objectRef'])) {
124 case true:
125 if (!is_object($functions['objectRef'])) {
126 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
128 //print_r($functions);break;
129 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
130 break;
132 case false:
133 call_user_func_array($functions['function'],$functions['params']);
134 break;
139 $options['jobs'] = $jlist;
141 $this->ticks[$name] = array(
142 'timer' => $options['timer'],
143 'reference' => time(),
144 'tickOn' => strtotime("+{$options['timer']} seconds"),
145 'jobs' => $options['jobs']
148 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";
154 return true;