use
5.006;
our
$VERSION
=
'0.01'
;
sub
new {
my
$class
=
shift
;
my
$self
= { };
$self
->{config} =
$_
[0];
croak
"Missing data"
unless
exists
$self
->{config};
croak
"data is not a hashref"
unless
ref
$self
->{config} eq
'HASH'
;
my
$config
=
$self
->{config};
my
@intervals
;
for
my
$interval
(
keys
%{
$config
}) {
croak
"$config->{$interval} is not an even divisor of 86400"
if
(86400 %
$config
->{
$interval
} != 0);
push
(
@intervals
,
$config
->{
$interval
});
}
my
$max
= max(
@intervals
);
$self
->{max} =
$max
;
my
%groups
= ( );
for
my
$each
(
keys
%{
$config
}) {
push
( @{
$groups
{
$config
->{
$each
} } },
$each
);
}
my
%schedule
;
undef
@intervals
;
undef
$config
;
undef
$self
->{config};
for
my
$interval
(
keys
%groups
) {
my
$members
=
scalar
@{
$groups
{
$interval
}};
my
$iter
= 1;
if
(
$members
/
$interval
>= 1) {
my
$sets
=
$max
/
$interval
;
while
(
$members
>= 1) {
$iter
= 1
if
$iter
>
$interval
;
my
$set
= 0;
while
(
$set
<
$sets
) {
my
$slot
;
if
(
$set
== 0) {
$slot
=
$iter
;
}
else
{
$slot
= (
$set
*
$interval
) +
$iter
;
}
push
( @{
$schedule
{
$slot
}},
$groups
{
$interval
}[
$members
-1]);
$set
++;
}
$iter
++;
$members
--;
}
}
else
{
my
$rate
= floor 1/(
$members
/
$interval
);
my
$sets
=
$max
/
$interval
;
while
(
$members
>=1) {
$iter
= 1
if
$iter
>=
$interval
;
my
$set
= 0;
while
(
$set
<
$sets
) {
my
$slot
;
if
(
$set
== 0) {
$slot
=
$iter
+ (
$rate
-1);
}
else
{
$slot
= (
$set
*
$interval
) + (
$iter
+ (
$rate
-1));
}
push
( @{
$schedule
{
$slot
}},
$groups
{
$interval
}[
$members
-1]);
$set
++;
}
$iter
+=
$rate
;
$members
--;
}
}
}
$self
->{schedule} = \
%schedule
;
bless
$self
,
$class
;
};
sub
current {
my
$self
=
shift
;
my
$max
=
$self
->{max};
my
@time
=
localtime
();
my
$second
= (
$time
[2] * 3600) + (
$time
[1] * 60) +
$time
[0];
my
$y
=0;
if
(
$second
>
$max
) {
while
(
$y
<
$second
) {
$y
+=
$max
;
}
return
$second
- (
$y
-
$max
);
}
else
{
return
$second
;
}
}
sub
which {
my
$self
=
shift
;
my
$current
=
$self
->current;
my
@who
;
if
(
exists
$self
->{schedule}{
$current
}) {
for
my
$each
(@{
$self
->{schedule}{
$current
}}) {
push
(
@who
,
$each
);
}
return
\
@who
;
}
return
0;
}
1;