fix a change name bug
[irbot.git] / sources / tick-class.inc.php
blobf8775e0e715486fea3cfcaecc463c95ddfc1b599
1 <?php
2 /**
3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007-2008 Bellière Ludovic
6 * LICENCE
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @category IrBot
22 * @package IrBot_Tick
23 * @copyright Copyright (c) 2007-2008 Bellière Ludovic
24 * @license http://www.gnu.org/licenses/gpl-3.0.html
28 /**
29 * Cron event emulation
31 * @category IrBot
32 * @package IrBot_Tick
33 * @copyright Copyright (c) 2007-2008 Bellière Ludovic
34 * @license http://www.gnu.org/licenses/gpl-3.0.html
36 class tick {
37 private $ticks = array();
38 /**
39 * IRCMain object
41 * @var IRCMain
43 private $ircmain;
45 static public $instance = false;
47 public static function GetInstance(IRCMain $ircmain) {
48 if (!self::$instance) {
49 self::$instance = new tick($ircmain);
51 return self::$instance;
54 private function __construct(IRCMain $ircmain) {
55 $this->ircmain = $ircmain;
58 /**
59 * Adding a job to the tick list $tickname
61 * @param string $tickname
62 * @param string $jobname
63 * @param string $function Function to call
64 * @param array $params Parameters send to the $function
65 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
66 * @param object $object Reference to an object if $function is a method
67 * @return boolean
69 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
70 if (isset($this->ticks[$tickname])) {
71 if (!is_null($object)) {
72 $this->ticks[$tickname]['jobs'][] = array(
73 'jname' => $jobname,
74 'function' => $function,
75 'params' => $params,
76 'temporary' => $temporary,
77 'objectRef' => &$object
79 } else {
80 $this->ticks[$tickname]['jobs'][] = array(
81 'jname' => $jobname,
82 'function' => $function,
83 'params' => $params,
84 'temporary' => $temporary
87 return true;
89 return false;
92 /**
93 * Create a tick if not exist
95 * @param string $name
96 * @param int $timer Number of seconds before the next tick
97 * @return boolean
99 public function setTick($name,$timer) {
100 if (!isset($this->ticks[$name])) {
101 $this->ticks[$name] = array(
102 'timer' => $timer,
103 'reference' => time(),
104 'tickOn' => strtotime("+$timer seconds"),
105 'jobs' => array()
107 return true;
109 return false;
113 * Delete a tick
115 * @param string $name
116 * @return boolean
118 public function delTick($name) {
119 if (isset($this->ticks[$name])) {
120 unset($this->ticks[$name]);
121 return true;
123 return false;
126 public function doAllTicks() {
127 $curtime = time();
129 foreach($this->ticks as $name => $options) {
131 if ($options['tickOn']<=$curtime) {
132 echo $options['tickOn']," - $curtime\n";
134 foreach ($options['jobs'] as $functions) {
136 if (!$functions['temporary']) {
137 $jlist[] = $functions;
140 if (!$this->ircmain->connected) {
141 continue;
144 switch (isset($functions['objectRef'])) {
145 case true:
146 if (!is_object($functions['objectRef'])) {
147 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
149 //print_r($functions);break;
150 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
151 break;
153 case false:
154 call_user_func_array($functions['function'],$functions['params']);
155 break;
160 $options['jobs'] = $jlist;
162 $this->ticks[$name] = array(
163 'timer' => $options['timer'],
164 'reference' => time(),
165 'tickOn' => strtotime("+{$options['timer']} seconds"),
166 'jobs' => $options['jobs']
169 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";
175 return true;