Mercurial > hg > skillbot
annotate timers.pl @ 73:ee8acca852f4
Merging 0.03 release
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Thu, 11 Feb 2010 00:15:15 +0000 |
parents | 2f9832b6dede |
children |
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 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
18 my $genid = 0; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
19 my @timers_once; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
20 my @timers_repeat; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
21 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
22 sub add_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
23 my ($offset, $coderef) = @_; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
24 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
25 my $one_shot = {}; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
26 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
27 $one_shot->{id} = $genid++; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
28 $one_shot->{stamp} = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
29 $one_shot->{offset} = $offset; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
30 $one_shot->{coderef} = $coderef; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
31 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
32 push(@timers_once, $one_shot); |
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
33 return $one_shot->{id}; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
34 } |
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 sub del_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
37 my ($timer_id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
38 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
39 @timers_once = grep { $_->{id} ne $timer_id } @timers_once; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
40 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
41 |
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 sub add_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
44 my ($every, $coderef) = @_; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
45 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
46 my $repeat = {}; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
47 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
48 $repeat->{id} = $genid++; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
49 $repeat->{last} = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
50 $repeat->{every} = $every; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
51 $repeat->{coderef} = $coderef; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
52 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
53 push(@timers_repeat, $repeat); |
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
54 return $repeat->{id}; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
55 } |
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 sub del_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
58 my ($timer_id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
59 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
60 @timers_repeat = grep { $_->{id} ne $timer_id } @timers_repeat; |
0
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_one_shot_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
64 my ($id) = shift; |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
65 |
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
66 return grep { $_->{id} eq $id } @timers_once; |
0
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 get_repeat_timer { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
70 my ($id) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
71 |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
72 return grep { $_->{id} eq $id } @timers_repeat; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
73 } |
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 sub do_timers_once { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
76 my ($self) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
77 my ($timer_id, $timer); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
78 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
79 return unless @timers_once; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
80 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
81 my $now = time(); |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
82 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
83 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
|
84 $timer = $timers_once[$timer_id]; |
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 if ($now >= $timer->{stamp} + $timer->{offset}) { |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
87 &{ $timer->{coderef} }($timer->{id}, $self); |
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
88 splice(@timers_once, $timer_id, 1); |
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
89 $timer_id--; |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
90 } |
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 } |
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 sub do_timers_repeat { |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
95 my ($self) = shift; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
96 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
97 my ($timer_id, $timer); |
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 return unless @timers_repeat; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
100 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
101 my $now = time(); |
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 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
|
104 $timer = $timers_repeat[$timer_id]; |
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 if ($now >= $timer->{last} + $timer->{every}) { |
41
2f9832b6dede
Changing IDs to be assigned incrementally instead of array indexes
Dominic Cleal <dominic@computerkb.co.uk>
parents:
0
diff
changeset
|
107 &{ $timer->{coderef} }($timer->{id}, $self); |
0
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
108 $timer->{last} = $now; |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
109 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
110 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
111 } |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
112 |
d6521d5ea884
Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff
changeset
|
113 1; |