burguillos.info/lib/BurguillosInfo/Ads.pm

105 lines
2.4 KiB
Perl
Raw Normal View History

2023-08-20 13:51:32 +02:00
package BurguillosInfo::Ads;
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use List::AllUtils qw/none/;
use Moo;
2023-08-21 00:58:11 +02:00
2023-08-20 13:51:32 +02:00
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);
}
}
}
sub get_next ( $self, $current_ad_number = undef ) {
my $array = $self->_array;
2023-08-21 00:58:11 +02:00
use Data::Dumper;
2023-08-20 13:51:32 +02:00
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) );
}
my $ad = $self->get_rand_ad($array);
2023-08-21 14:10:18 +02:00
$ad->regenerate_alternative;
2023-08-20 13:51:32 +02:00
return {
2023-08-21 00:58:11 +02:00
ad => $ad->serialize,
2023-08-20 13:51:32 +02:00
continue => 1,
current_ad_number => $self->_get_next_number($current_ad_number),
};
}
sub get_rand_ad($self, $array) {
my $valid_ads = [ grep { $_->is_active } @$array ];
my $max_weight = $self->sum_weights($array);
my $rand = int(rand() * $max_weight);
my $sum_weight = 0;
for my $ad (@$array) {
$sum_weight += $ad->weight;
if ($rand < $sum_weight) {
return $ad;
}
}
die "This should not happen, there should be always a corresponding ad.";
}
sub sum_weights($self, $array) {
my $sum = 0;
for my $ad (@$array) {
$sum += $ad->weight;
}
return $sum;
}
2023-08-20 13:51:32 +02:00
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;