App-TeleGramma

 view release on metacpan or  search on metacpan

lib/App/TeleGramma/Plugin/Core/YearProgress.pm  view on Meta::CPAN

  });

  return ($start_yp, $stop_yp);
}

sub send_out_progress_if_necessary {
  my $self = shift;

  my $decimals = 0;

  # what was the last progress report percentage?
  my $last_pc = $self->store->hash('last')->{percentage} || sprintf("%0.${decimals}f", 0);

  # how many seconds in a year?
  my $year_seconds = 365 * 86400; # no such thing as leap year la la la

  # how many seconds are we into this year?
  my $year_start_ts = DateTime->now->truncate(to => 'year')->epoch;
  my $year_seconds_so_far = time() - $year_start_ts;

  # as a percent?
  my $percent = sprintf("%0.${decimals}f", 100 * ($year_seconds_so_far / $year_seconds));

  # has it rolled?
  if ($percent ne $last_pc) {
    # store it
    $self->store->hash('last')->{percentage} = $percent;
    $self->store->save('last');
    # send out the alerts
    $self->send_alerts($percent);
  }
}

sub send_alerts {
  my $self     = shift;
  my $percent  = shift;
  my @alertees = keys %{ $self->store->hash('registered') };
  my $done_num = int($percent / 10);
  my $todo_num = 10 - $done_num;

  my $done = "â–“";
  my $todo = "â–‘";
  my $bar = $done x $done_num . $todo x $todo_num;
  $bar .= " ${percent}%";
  foreach my $id (@alertees) {
    my $username = $self->store->hash('registered')->{$id}->{username};
    eval {
      $self->app->send_message_to_chat_id($id, "Hey $username, the year is progressing: $bar");
    } or do {
      warn "Could not send message to $username - $@";
    };
  }
}



( run in 0.322 second using v1.01-cache-2.11-cpan-05162d3a2b1 )