File Coverage

blib/lib/App/TeleGramma/Plugin/Core/YearProgress.pm
Criterion Covered Total %
statement 14 70 20.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 5 14 35.7
pod 3 7 42.8
total 22 100 22.0


line stmt bran cond sub pod time code
1             package App::TeleGramma::Plugin::Core::YearProgress;
2             $App::TeleGramma::Plugin::Core::YearProgress::VERSION = '0.12';
3             # ABSTRACT: TeleGramma plugin to tell you how the year is progressing
4              
5 1     1   947 use Mojo::Base 'App::TeleGramma::Plugin::Base';
  1         3  
  1         8  
6 1     1   187 use App::TeleGramma::BotAction::Listen;
  1         2  
  1         7  
7 1     1   25 use App::TeleGramma::Constants qw/:const/;
  1         3  
  1         164  
8 1     1   797 use DateTime;
  1         397518  
  1         742  
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 4 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(1 => 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 0           $self->app->send_message_to_chat_id($id, "Hey $username, the year is progressing: $bar");
82             }
83             }
84              
85             sub yp_start {
86 0     0 0   my $self = shift;
87 0           my $msg = shift;
88              
89             # We don't really care about anything from here. They typed /ypstart
90             # and that's all that matters.
91 0           my $user_id = $msg->from->id;
92 0           my $username = $msg->from->username;
93              
94             # Check the config to see if they are already registered.
95 0 0         if (defined $self->store->hash('registered')->{$user_id}) {
96             # already registered
97 0           $self->reply_to($msg, "Hey $username, you're already down for regular updates on the depressingly fast progress of the year.");
98 0           return PLUGIN_RESPONDED_LAST;
99             }
100             else {
101 0           $self->store->hash('registered')->{$user_id} = {
102             ts => time(),
103             username => $username,
104             user_id => $user_id,
105             };
106 0           $self->store->save('registered');
107 0           $self->reply_to($msg, "OK $username, I will be sure to keep you informed of the year's progress.");
108 0           return PLUGIN_RESPONDED_LAST;
109             }
110              
111             }
112              
113             sub yp_stop {
114 0     0 0   my $self = shift;
115 0           my $msg = shift;
116              
117             # We don't really care about anything from here. They typed /ypstart
118             # and that's all that matters.
119 0           my $user_id = $msg->from->id;
120 0           my $username = $msg->from->username;
121              
122             # Check the config to see if they are registered.
123 0 0         if (defined $self->store->hash('registered')->{$user_id}) {
124             # already registered, get rid of them
125 0           delete $self->store->hash('registered')->{$user_id};
126 0           $self->store->save('registered');
127 0           $self->reply_to($msg, "OK $username, I'll no longer give you updates on the progress of the year.");
128 0           return PLUGIN_RESPONDED_LAST;
129             }
130             else {
131 0           $self->reply_to($msg, "Nice idea $username, but you aren't registered. Maybe try /ypstart ?");
132 0           return PLUGIN_RESPONDED_LAST;
133             }
134             }
135              
136             1;
137              
138             __END__
139              
140             =pod
141              
142             =encoding UTF-8
143              
144             =head1 NAME
145              
146             App::TeleGramma::Plugin::Core::YearProgress - TeleGramma plugin to tell you how the year is progressing
147              
148             =head1 VERSION
149              
150             version 0.12
151              
152             =head1 AUTHOR
153              
154             Justin Hawkins <justin@eatmorecode.com>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2017 by Justin Hawkins <justin@eatmorecode.com>.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut