refactor a bit, add post class in prep for testing
[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
38efa3f1 8sub check_post_gap ($aperture, $days, @posts) {
9 return @posts if @posts <= $aperture;
10 my @next_post = splice(@posts, 0, $aperture);
a51e36e5 11 return 0 if DateTime->now - $next_post[-1]->at > $days->days;
38efa3f1 12 my $success = $aperture;
67e9aef2 13 foreach my $post (@posts) {
38efa3f1 14 return $success if $next_post[0]->at - $post->at > $days->days;
67e9aef2 15 $success++;
16 shift(@next_post);
17 push(@next_post, $post);
18 }
19 return $success;
20}
38efa3f1 21
a51e36e5 22sub 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
38efa3f1 27sub 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}
ac04ccd9 33
341;