Mercurial > hg > skillbot
annotate timers.pl @ 25:1d57373d1c04
Merging in df's branch
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Sat, 06 Dec 2008 22:54:58 +0000 |
parents | d6521d5ea884 |
children | 2f9832b6dede |
rev | line source |
---|---|
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
2 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
3 =pod |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
4 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
5 Braindead timers thing. |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
6 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
7 Copyright ©2000 Andy Smith <andy+timers.pl@strugglers.net> |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
8 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
9 Artistic license as perl. |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
10 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
11 $Id: timers.pl 776 2008-11-03 18:20:04Z andy $ |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
12 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
13 =cut |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
14 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
15 use warnings; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
16 use strict; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
17 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
18 my @timers_once; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
19 my @timers_repeat; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
20 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
21 sub add_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
22 my ($offset, $coderef) = @_; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
23 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
24 my $one_shot = {}; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
25 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
26 $one_shot->{stamp} = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
27 $one_shot->{offset} = $offset; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
28 $one_shot->{coderef} = $coderef; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
29 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
30 return push(@timers_once, $one_shot) - 1; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
31 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
32 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
33 sub del_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
34 my ($timer_id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
35 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
36 splice(@timers_once, $timer_id, 1); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
37 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
38 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
39 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
40 sub add_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
41 my ($every, $coderef) = @_; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
42 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
43 my $repeat = {}; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
44 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
45 $repeat->{last} = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
46 $repeat->{every} = $every; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
47 $repeat->{coderef} = $coderef; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
48 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
49 return push(@timers_repeat, $repeat) - 1; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
50 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
51 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
52 sub del_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
53 my ($timer_id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
54 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
55 splice(@timers_repeat, $timer_id, 1); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
56 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
57 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
58 sub get_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
59 my ($id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
60 return $timers_once[$id]; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
61 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
62 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
63 sub get_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
64 my ($id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
65 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
66 return $timers_repeat[$id]; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
67 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
68 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
69 sub do_timers_once { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
70 my ($self) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
71 my ($timer_id, $timer); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
72 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
73 return unless @timers_once; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
74 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
75 my $now = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
76 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
77 for ($timer_id = 0; $timer_id <= $#timers_once; $timer_id++) { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
78 $timer = $timers_once[$timer_id]; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
79 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
80 if ($now >= $timer->{stamp} + $timer->{offset}) { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
81 &{ $timer->{coderef} }($timer_id, $self); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
82 del_one_shot_timer($timer_id); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
83 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
84 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
85 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
86 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
87 sub do_timers_repeat { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
88 my ($self) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
89 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
90 my ($timer_id, $timer); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
91 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
92 return unless @timers_repeat; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
93 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
94 my $now = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
95 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
96 for ($timer_id = 0; $timer_id <= $#timers_repeat; $timer_id++) { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
97 $timer = $timers_repeat[$timer_id]; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
98 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
99 if ($now >= $timer->{last} + $timer->{every}) { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
100 &{ $timer->{coderef} }($timer_id, $self); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
101 $timer->{last} = $now; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
102 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
103 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
104 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
105 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
106 1; |