Adding initial ads support.

This commit is contained in:
sergiotarxz 2023-08-20 13:51:32 +02:00
parent 529f5a1e43
commit c2e1b80e1a
3 changed files with 113 additions and 0 deletions

View File

@ -21,6 +21,10 @@ my $build = Module::Build->new(
'XML::Twig' => 0,
'JSON' => 0,
'IP::Geolocation::MMDB' => 0,
'Moo' => 0,
'Moo::Role' => 0,
'Module::Pluggable' => 0,
'List::AllUtils' => 0,
},
);
$build->create_build_script;

21
lib/BurguillosInfo/Ad.pm Normal file
View File

@ -0,0 +1,21 @@
package BurguillosInfo::Ad;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use Moo::Role;
sub order {
return 999;
}
sub seconds {
return 15;
}
requires 'id is_active img text';
1;

88
lib/BurguillosInfo/Ads.pm Normal file
View File

@ -0,0 +1,88 @@
package BurguillosInfo::Ads;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use List::AllUtils qw/none/;
use Moo;
use Module::Pluggable
search_path => ['BurguillosInfo::Ads'],
instantiate => 'instance',
on_require_error => sub ( $plugin, $error ) {
die $error;
};
{
my @array_ads;
sub _array ($self) {
if ( !scalar @array_ads ) {
$self->_populate_array;
}
return [@array_ads];
}
sub _populate_array ($self) {
@array_ads = $self->plugins();
for my $ad (@array_ads) {
$self->_check_ad_valid($ad);
}
@array_ads = sort { $self->_order_two_ads( $a, $b ); } @array_ads;
}
}
sub _order_two_ads ( $self, $a, $b ) {
my $by_order = $a->order <=> $b->order;
if ($by_order) {
return $by_order;
}
my $by_alpha = $a->id cmp $b->id;
return $by_alpha;
}
sub get_next ( $self, $current_ad_number = undef ) {
my $array = $self->_array;
if ( !scalar @$array
|| none { $_->is_active } @$array )
{
return { continue => 0, current_ad_number => undef };
}
if ( !defined $current_ad_number ) {
$current_ad_number = 0;
}
my $ad = $array->[$current_ad_number];
if ( !$ad->is_active ) {
return $self->get_next( $self->_get_next_number($current_ad_number) );
}
return {
ad => $ad,
continue => 1,
current_ad_number => $self->_get_next_number($current_ad_number),
};
}
sub _get_next_number ( $self, $current_ad_number = undef ) {
my $array = $self->_array;
if ( !scalar @$array ) {
return undef;
}
if ( !defined $current_ad_number ) {
return 0;
}
if ( ++$current_ad_number > ( scalar @$array ) - 1 ) {
return 0;
}
return $current_ad_number;
}
sub _check_ad_valid ( $self, $ad ) {
if ( !$ad->does('BurguillosInfo::Ad') ) {
die "$ad does not implement BurguillosInfo::Ad.";
}
}
1;