File Coverage

blib/lib/App/TeleGramma/Plugin/Core/YearProgress.pm
Criterion Covered Total %
statement 14 72 19.4
branch 0 8 0.0
condition 0 3 0.0
subroutine 5 14 35.7
pod 3 7 42.8
total 22 104 21.1


line stmt bran cond sub pod time code
1             package App::TeleGramma::Plugin::Core::YearProgress;
2             $App::TeleGramma::Plugin::Core::YearProgress::VERSION = '0.13';
3             # ABSTRACT: TeleGramma plugin to tell you how the year is progressing
4              
5 1     1   847 use Mojo::Base 'App::TeleGramma::Plugin::Base';
  1         3  
  1         8  
6 1     1   181 use App::TeleGramma::BotAction::Listen;
  1         2  
  1         7  
7 1     1   24 use App::TeleGramma::Constants qw/:const/;
  1         2  
  1         125  
8 1     1   714 use DateTime;
  1         391942  
  1         794  
9              
10             sub synopsis {
11 0     0 1 0 "Periodically remind you how depressingly fast the year is passing"
12             }
13              
14             sub default_config {
15 1     1 1 5 my $self = shift;
16 1         5 return { };
17             }
18              
19             sub register {
20 0     0 1   my $self = shift;
21              
22             my $start_yp = App::TeleGramma::BotAction::Listen->new(
23             command => qr{/ypstart}i,
24 0     0     response => sub { $self->yp_start(@_) }
25 0           );
26              
27             my $stop_yp = App::TeleGramma::BotAction::Listen->new(
28             command => qr{/ypstop}i,
29 0     0     response => sub { $self->yp_stop(@_) }
30 0           );
31              
32             Mojo::IOLoop->recurring(60 => sub {
33 0     0     my $loop = shift;
34 0           $self->send_out_progress_if_necessary;
35 0           });
36              
37 0           return ($start_yp, $stop_yp);
38             }
39              
40             sub send_out_progress_if_necessary {
41 0     0 0   my $self = shift;
42              
43 0           my $decimals = 0;
44              
45             # what was the last progress report percentage?
46 0   0       my $last_pc = $self->store->hash('last')->{percentage} || sprintf("%0.${decimals}f", 0);
47              
48             # how many seconds in a year?
49 0           my $year_seconds = 365 * 86400; # no such thing as leap year la la la
50              
51             # how many seconds are we into this year?
52 0           my $year_start_ts = DateTime->now->truncate(to => 'year')->epoch;
53 0           my $year_seconds_so_far = time() - $year_start_ts;
54              
55             # as a percent?
56 0           my $percent = sprintf("%0.${decimals}f", 100 * ($year_seconds_so_far / $year_seconds));
57              
58             # has it rolled?
59 0 0         if ($percent ne $last_pc) {
60             # store it
61 0           $self->store->hash('last')->{percentage} = $percent;
62 0           $self->store->save('last');
63             # send out the alerts
64 0           $self->send_alerts($percent);
65             }
66             }
67              
68             sub send_alerts {
69 0     0 0   my $self = shift;
70 0           my $percent = shift;
71 0           my @alertees = keys %{ $self->store->hash('registered') };
  0            
72 0           my $done_num = int($percent / 10);
73 0           my $todo_num = 10 - $done_num;
74              
75 0           my $done = "â–“";
76 0           my $todo = "â–‘";
77 0           my $bar = $done x $done_num . $todo x $todo_num;
78 0           $bar .= " ${percent}%";
79 0           foreach my $id (@alertees) {
80 0           my $username = $self->store->hash('registered')->{$id}->{username};
81             eval {
82 0           $self->app->send_message_to_chat_id($id, "Hey $username, the year is progressing: $bar");
83 0 0         } or do {
84 0           warn "Could not send message to $username - $@";
85             };
86             }
87             }
88              
89             sub yp_start {
90 0     0 0   my $self = shift;
91 0           my $msg = shift;
92              
93             # We don't really care about anything from here. They typed /ypstart
94             # and that's all that matters.
95 0           my $user_id = $msg->from->id;
96 0           my $username = $msg->from->username;
97              
98             # Check the config to see if they are already registered.
99 0 0         if (defined $self->store->hash('registered')->{$user_id}) {
100             # already registered
101 0           $self->reply_to($msg, "Hey $username, you're already down for regular updates on the depressingly fast progress of the year.");
102 0           return PLUGIN_RESPONDED_LAST;
103             }
104             else {
105 0           $self->store->hash('registered')->{$user_id} = {
106             ts => time(),
107             username => $username,
108             user_id => $user_id,
109             };
110 0           $self->store->save('registered');
111 0           $self->reply_to($msg, "OK $username, I will be sure to keep you informed of the year's progress.");
112 0           return PLUGIN_RESPONDED_LAST;
113             }
114              
115             }
116              
117             sub yp_stop {
118 0     0 0   my $self = shift;
119 0           my $msg = shift;
120              
121             # We don't really care about anything from here. They typed /ypstart
122             # and that's all that matters.
123 0           my $user_id = $msg->from->id;
124 0           my $username = $msg->from->username;
125              
126             # Check the config to see if they are registered.
127 0 0         if (defined $self->store->hash('registered')->{$user_id}) {
128             # already registered, get rid of them
129 0           delete $self->store->hash('registered')->{$user_id};
130 0           $self->store->save('registered');
131 0           $self->reply_to($msg, "OK $username, I'll no longer give you updates on the progress of the year.");
132 0           return PLUGIN_RESPONDED_LAST;
133             }
134             else {
135 0           $self->reply_to($msg, "Nice idea $username, but you aren't registered. Maybe try /ypstart ?");
136 0           return PLUGIN_RESPONDED_LAST;
137             }
138             }
139              
140             1;
141              
142             __END__
143              
144             =pod
145              
146             =encoding UTF-8
147              
148             =head1 NAME
149              
150             App::TeleGramma::Plugin::Core::YearProgress - TeleGramma plugin to tell you how the year is progressing
151              
152             =head1 VERSION
153              
154             version 0.13
155              
156             =head1 AUTHOR
157              
158             Justin Hawkins <justin@eatmorecode.com>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is copyright (c) 2017 by Justin Hawkins <justin@eatmorecode.com>.
163              
164             This is free software; you can redistribute it and/or modify it under
165             the same terms as the Perl 5 programming language system itself.
166              
167             =cut