| 1 | package Bot::Cobalt::Timer; | ||
| 2 | $Bot::Cobalt::Timer::VERSION = '0.021002'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Bot::Cobalt::Common ':types'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | use Moo; | ||
| 9 | |||
| 10 | ## It's possible to pass in a different core. | ||
| 11 | ## (Allows timers to fire against different syndicators if needed) | ||
| 12 | has core => ( | ||
| 13 | lazy => 1, | ||
| 14 | is => 'rw', | ||
| 15 | isa => HasMethods['send_event'], | ||
| 16 | builder => sub { | ||
| 17 | require Bot::Cobalt::Core; | ||
| 18 | Bot::Cobalt::Core->instance | ||
| 19 | || die "Cannot find active Bot::Cobalt::Core instance" | ||
| 20 | }, | ||
| 21 | ); | ||
| 22 | |||
| 23 | ## May have a timer ID specified at construction for use by | ||
| 24 | ## timer pool managers; if not, creating IDs is up to them. | ||
| 25 | ## (See ::Core::Role::Timers) | ||
| 26 | has id => ( | ||
| 27 | lazy => 1, | ||
| 28 | is => 'rw', | ||
| 29 | isa => Str, | ||
| 30 | predicate => 'has_id' | ||
| 31 | ); | ||
| 32 | |||
| 33 | ## 'at' is set regardless of whether delay()/at() is used | ||
| 34 | ## (or 0 if none is ever set) | ||
| 35 | has at => ( | ||
| 36 | lazy => 1, | ||
| 37 | is => 'rw', | ||
| 38 | isa => Num, | ||
| 39 | builder => sub { 0 }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has delay => ( | ||
| 43 | lazy => 1, | ||
| 44 | is => 'rw', | ||
| 45 | isa => Num, | ||
| 46 | predicate => 'has_delay', | ||
| 47 | clearer => 'clear_delay', | ||
| 48 | builder => sub { 0 }, | ||
| 49 | trigger => sub { | ||
| 50 | my ($self, $value) = @_; | ||
| 51 | $self->at( time() + $value ); | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | has event => ( | ||
| 56 | lazy => 1, | ||
| 57 | is => 'rw', | ||
| 58 | isa => Str, | ||
| 59 | predicate => 'has_event', | ||
| 60 | ); | ||
| 61 | |||
| 62 | has args => ( | ||
| 63 | lazy => 1, | ||
| 64 | is => 'rw', | ||
| 65 | isa => ArrayObj, | ||
| 66 | coerce => 1, | ||
| 67 | builder => sub { [] }, | ||
| 68 | ); | ||
| 69 | |||
| 70 | has alias => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => Str, | ||
| 73 | builder => sub { scalar caller }, | ||
| 74 | ); | ||
| 75 | |||
| 76 | has context => ( | ||
| 77 | lazy => 1, | ||
| 78 | is => 'rw', | ||
| 79 | isa => Str, | ||
| 80 | predicate => 'has_context', | ||
| 81 | builder => sub { 'Main' }, | ||
| 82 | ); | ||
| 83 | |||
| 84 | has text => ( | ||
| 85 | lazy => 1, | ||
| 86 | is => 'rw', | ||
| 87 | isa => Str, | ||
| 88 | predicate => 'has_text' | ||
| 89 | ); | ||
| 90 | |||
| 91 | has target => ( | ||
| 92 | lazy => 1, | ||
| 93 | is => 'rw', | ||
| 94 | isa => Str, | ||
| 95 | predicate => 'has_target' | ||
| 96 | ); | ||
| 97 | |||
| 98 | has type => ( | ||
| 99 | lazy => 1, | ||
| 100 | is => 'rw', | ||
| 101 | isa => Str, | ||
| 102 | builder => sub { | ||
| 103 | my ($self) = @_; | ||
| 104 | # best guess: | ||
| 105 | $self->has_context && $self->has_target ? 'msg' : 'event' | ||
| 106 | }, | ||
| 107 | coerce => sub { | ||
| 108 | $_[0] =~ /message|privmsg/i ? 'msg' : lc($_[0]) ; | ||
| 109 | }, | ||
| 110 | ); | ||
| 111 | |||
| 112 | |||
| 113 | sub _process_type { | ||
| 114 | my ($self) = @_; | ||
| 115 | ## If this is a special type, set up event and args. | ||
| 116 | my $type = lc $self->type; | ||
| 117 | |||
| 118 | if (grep {; $_ eq $type } qw/msg message privmsg action/) { | ||
| 119 | my $ev_name = $type eq 'action' ? | ||
| 120 | 'action' : 'message' ; | ||
| 121 | $self->event( $ev_name ); | ||
| 122 | |||
| 123 | my @ev_args = ( $self->context, $self->target, $self->text ); | ||
| 124 | $self->args( \@ev_args ); | ||
| 125 | } | ||
| 126 | |||
| 127 | 1 | ||
| 128 | } | ||
| 129 | |||
| 130 | sub is_ready { | ||
| 131 | my ($self) = @_; | ||
| 132 | $self->at <= time ? 1 : () | ||
| 133 | } | ||
| 134 | |||
| 135 | sub execute { | ||
| 136 | my ($self) = @_; | ||
| 137 | $self->_process_type; | ||
| 138 | |||
| 139 | unless ( $self->event ) { | ||
| 140 | carp "timer execute called but no event specified"; | ||
| 141 | return | ||
| 142 | } | ||
| 143 | |||
| 144 | my $args = $self->args; | ||
| 145 | $self->core->send_event( $self->event, @$args ); | ||
| 146 | 1 | ||
| 147 | } | ||
| 148 | |||
| 149 | sub execute_if_ready { execute_ready(@_) } | ||
| 150 | sub execute_ready { | ||
| 151 | my ($self) = @_; | ||
| 152 | $self->is_ready ? $self->execute : () | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | 1; | ||
| 157 | __END__ | ||