handle failure cases better
[engit/Iron-Munger.git] / lib / IronMunger / Calculate.pm
1 package IronMunger::Calculate;
2
3 use strict;
4 use warnings;
5 use List::Util qw(min);
6 use autobox;
7 use autobox::DateTime::Duration;
8 use signatures;
9
10 use Sub::Exporter -setup => {
11   exports => [
12     qw(successful_sequential_posts time_remaining_to_post)
13   ]
14 };
15
16 sub day_diff ($dt1, $dt2) {
17   $dt1 = $dt1->at if $dt1->isa('IronMunger::Post');
18   $dt2 = $dt2->at if $dt2->isa('IronMunger::Post');
19   $dt1->delta_days($dt2)->delta_days;
20 }
21
22 sub check_post_gap ($aperture, $days, @posts) {
23   return scalar @posts if @posts <= $aperture;
24   my @next_post = splice(@posts, 0, $aperture);
25   if (day_diff(DateTime->now, $next_post[-1]) > $days) {
26     while (@next_post && day_diff(DateTime->now, $next_post[-1]) > $days) {
27       pop @next_post;
28     }
29     return scalar(@next_post);
30   }
31   my $success = $aperture;
32   foreach my $post (@posts) {
33     if (day_diff($next_post[0],$post) > $days) {
34       return $success;
35     }
36     $success++;
37     shift(@next_post);
38     push(@next_post, $post);
39   }
40   return $success;
41 }
42
43 sub check_time_remaining ($aperture, $days, @posts) {
44   my @consider = @posts > $aperture ? @posts[0 .. $aperture - 1] : @posts;
45   foreach my $cand (reverse @consider) {
46     my $days_ago = day_diff(DateTime->now, $cand);
47     return $days - $days_ago if $days > $days_ago;
48   }
49   return $days;
50 }
51
52 sub check_both ($check, @posts) {
53   return min(
54     $check->(1, 10, @posts), # 10 days between posts
55     $check->(4, 32, @posts), # 4 posts within any given 32 days
56   );
57 }
58
59 sub successful_sequential_posts (@posts) {
60   return check_both(\&check_post_gap, @posts);
61 }
62
63 sub days_remaining_to_post (@posts) {
64   return check_both(\&check_time_remaining, @posts);
65 }
66
67 1;