1178c816b446e0f94394d05cf63c5aa63ad48dc3
[engit/Iron-Munger.git] / lib / IronMunger / Calculate.pm
1 package IronMunger::Calculate;
2
3 use strict;
4 use warnings;
5 use signatures;
6 use autobox::DateTime::Duration;
7
8 sub check_post_gap ($aperture, $days, @posts) {
9   return @posts if @posts <= $aperture;
10   my @next_post = splice(@posts, 0, $aperture);
11   return 0 if DateTime->now - $next_post[-1]->at > $days->days;
12   my $success = $aperture;
13   foreach my $post (@posts) {
14     return $success if $next_post[0]->at - $post->at > $days->days;
15     $success++;
16     shift(@next_post);
17     push(@next_post, $post);
18   }
19   return $success;
20 }
21
22 sub check_time_remaining ($aperture, $days, @posts) {
23   my $cand = (@posts > $aperture ? $posts[$aperture - 1] : $posts[-1]);
24   return $days->days - (DateTime->now - $cand->at)->in_units('days');
25 }
26
27 sub successful_sequential_posts (@posts) {
28   return max(
29     check_post_gap(1, 10, @posts), # 10 days between posts
30     check_post_gap(3, 32, @posts), # 4 posts every 32 days
31   );
32 }
33
34 1;