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