missing use autobox, die to spot sub name error, code now runs and explodes rather...
[engit/Iron-Munger.git] / lib / IronMunger / Calculate.pm
CommitLineData
ac04ccd9 1package IronMunger::Calculate;
2
3use strict;
4use warnings;
38827ef4 5use autobox;
38efa3f1 6use autobox::DateTime::Duration;
f79d4b5c 7use signatures;
67e9aef2 8
a7b549f9 9use Sub::Exporter -setup => {
10 exports => [
11 qw(successful_sequential_posts time_remaining_to_post)
12 ]
13};
14
38efa3f1 15sub check_post_gap ($aperture, $days, @posts) {
16 return @posts if @posts <= $aperture;
17 my @next_post = splice(@posts, 0, $aperture);
a51e36e5 18 return 0 if DateTime->now - $next_post[-1]->at > $days->days;
38efa3f1 19 my $success = $aperture;
67e9aef2 20 foreach my $post (@posts) {
38efa3f1 21 return $success if $next_post[0]->at - $post->at > $days->days;
67e9aef2 22 $success++;
23 shift(@next_post);
24 push(@next_post, $post);
25 }
26 return $success;
27}
38efa3f1 28
a51e36e5 29sub check_time_remaining ($aperture, $days, @posts) {
30 my $cand = (@posts > $aperture ? $posts[$aperture - 1] : $posts[-1]);
31 return $days->days - (DateTime->now - $cand->at)->in_units('days');
32}
33
a7b549f9 34sub check_both ($check, @posts) {
38efa3f1 35 return max(
a7b549f9 36 $check->(1, 10, @posts), # 10 days between posts
37 $check->(4, 32, @posts), # 4 posts within any given 32 days
38efa3f1 38 );
39}
ac04ccd9 40
a7b549f9 41sub successful_sequential_posts (@posts) {
42 return check_both(\&check_post_gap, @posts);
43}
44
38827ef4 45sub days_remaining_to_post (@posts) {
a7b549f9 46 return check_both(\&check_time_remaining, @posts);
47}
48
ac04ccd9 491;