comparison skillbot.pl @ 56:c604d8d6ccc5

Caching skill info in friend hash, instead of calling the API
author Dominic Cleal <dominic@computerkb.co.uk>
date Sun, 28 Dec 2008 16:38:57 +0000
parents c5ce2fff9b85
children fabae7870b52
comparison
equal deleted inserted replaced
55:c5ce2fff9b85 56:c604d8d6ccc5
223 my $found = 0; 223 my $found = 0;
224 224
225 my @filtered = 225 my @filtered =
226 sort { 226 sort {
227 (defined $a->{skill} && defined $b->{skill}) 227 (defined $a->{skill} && defined $b->{skill})
228 ? $a->{skill}->finish_time <=> $b->{skill}->finish_time 228 ? $a->{skill_finish} <=> $b->{skill_finish}
229 : 0 229 : 0
230 } 230 }
231 grep { 231 grep {
232 ((defined $2 && (lc($_->{char}->name) eq lc($2) 232 ((defined $2 && (lc($_->{char}) eq lc($2)
233 || lc($2) eq 'all' || lc($2) eq '*')) 233 || lc($2) eq 'all' || lc($2) eq '*'))
234 || (!defined $2 && $_->{nick} eq $their_nick)) 234 || (!defined $2 && $_->{nick} eq $their_nick))
235 } @friends; 235 } @friends;
236 236
237 for my $f (@filtered) 237 for my $f (@filtered)
238 { 238 {
239 check_friend($self, $f); 239 check_friend($self, $f);
240 my $text; 240 my $text;
241 if (defined $f->{skill}) { 241 if (defined $f->{skill}) {
242 my $downtime = ''; 242 my $downtime = '';
243 if (skill_finish_in_downtime($f->{skill})) { 243 if (skill_finish_in_downtime($f->{skill_finish})) {
244 $downtime = ", coincides with downtime"; 244 $downtime = ", coincides with downtime";
245 } 245 }
246 246
247 my $finish = DateTime->from_epoch( 247 my $finish = DateTime->from_epoch(
248 epoch => $f->{skill}->finish_time, 248 epoch => $f->{skill_finish},
249 time_zone => $f->{tz}); 249 time_zone => $f->{tz});
250 250
251 $text = sprintf("Currently training %s %s ". 251 $text = sprintf("Currently training %s ".
252 "(finish in %s, %s %s%s)", 252 "(finish in %s, %s %s%s)",
253 $f->{skill}->name, $f->{skill}->level, 253 $f->{skill_name}, $f->{skill_countdown},
254 $f->{skill}->time_remaining,
255 $finish->strftime("%A %R"), 254 $finish->strftime("%A %R"),
256 $finish->time_zone_short_name(), 255 $finish->time_zone_short_name(),
257 $downtime); 256 $downtime);
258 } else { 257 } else {
259 $text = "No skill currently training"; 258 $text = "No skill currently training";
260 } 259 }
261 260
262 $self->privmsg('#' . $config{channel}, sprintf( 261 $self->privmsg('#' . $config{channel}, sprintf(
263 "\002%s:\002 %s", $f->{char}->name, 262 "\002%s:\002 %s", $f->{char},
264 encode("utf8", $text))); 263 encode("utf8", $text)));
265 $found++; 264 $found++;
266 } 265 }
267 266
268 if ($found == 0) { 267 if ($found == 0) {
274 elsif ($msg =~ /^\!next/i) { 273 elsif ($msg =~ /^\!next/i) {
275 my $nreply = "Training: "; 274 my $nreply = "Training: ";
276 275
277 check_training($self); 276 check_training($self);
278 my @training = sort { 277 my @training = sort {
279 $a->{skill}->finish_time <=> $b->{skill}->finish_time 278 $a->{skill_finish} <=> $b->{skill_finish}
280 } grep { defined $_->{skill} } @friends; 279 } grep { defined $_->{skill} } @friends;
281 280
282 for my $f (@training) { 281 for my $f (@training) {
283 $nreply .= sprintf("\002%s\002 (%s) | ", 282 $nreply .= sprintf("\002%s\002 (%s) | ",
284 $f->{char}->name, $f->{skill}->time_remaining); 283 $f->{char}, $f->{skill_countdown});
285 } 284 }
286 285
287 $self->privmsg('#' . $config{channel}, substr($nreply, 0, -3)); 286 $self->privmsg('#' . $config{channel}, substr($nreply, 0, -3));
288 } 287 }
289 } 288 }
434 433
435 my $loaded = 0; 434 my $loaded = 0;
436 foreach my $character ($api->characters) { 435 foreach my $character ($api->characters) {
437 next if defined $c && $c ne $character->name; 436 next if defined $c && $c ne $character->name;
438 my $f = { 437 my $f = {
439 char => $character, 438 char_api => $character,
439 char => $character->name,
440 api => $api, 440 api => $api,
441 nick => $nick, 441 nick => $nick,
442 tz => $tz 442 tz => $tz
443 }; 443 };
444 push @friends, $f; 444 push @friends, $f;
479 sub check_friend 479 sub check_friend
480 { 480 {
481 my $self = shift; 481 my $self = shift;
482 my $f = shift; 482 my $f = shift;
483 483
484 my $skill = $f->{char}->skill->in_training; 484 my $skill = $f->{char_api}->skill->in_training;
485 485
486 # Nothing training 486 # Nothing training
487 unless (defined $skill) 487 unless (defined $skill)
488 { 488 {
489 # If the char was training before and now has stopped then cancel 489 # If the char was training before and now has stopped then cancel
495 return; 495 return;
496 } 496 }
497 497
498 # Check for changes in the skill, skip or cancel announcement 498 # Check for changes in the skill, skip or cancel announcement
499 if (defined $f->{skill}) { 499 if (defined $f->{skill}) {
500 return if $skill->id eq $f->{skill}->id; 500 return if $skill->id eq $f->{skill};
501 501
502 del_one_shot_timer($f->{timer}); 502 del_one_shot_timer($f->{timer});
503 delete $f->{timer}; 503 delete $f->{timer};
504 } 504 }
505 505
506 my $finish = $skill->finish_time - time(); 506 my $finish = $skill->finish_time - time();
507 my $prefinish = $finish - 60; 507 my $prefinish = $finish - 60;
508 508
509 irc_debug("Character %s is training %s %d (%s == %d sec)", 509 irc_debug("Character %s is training %s %d (%s == %d sec)",
510 $f->{char}->name, $skill->name, $skill->level, 510 $f->{char}, $skill->name, $skill->level,
511 $skill->time_remaining, $finish); 511 $skill->time_remaining, $finish);
512 512
513 # Ignore bad data 513 # Ignore bad data
514 if ($finish < 0) 514 if ($finish < 0)
515 { 515 {
516 irc_debug("Unreasonable finish time given of %s seconds, ignoring", 516 irc_debug("Unreasonable finish time given of %s seconds, ignoring",
517 $finish); 517 $finish);
518 return; 518 return;
519 } 519 }
520 520
521 $f->{skill} = $skill; 521 $f->{skill} = $skill->id;
522 $f->{skill_finish} = $skill->finish_time;
523 $f->{skill_countdown} = $skill->time_remaining;
524 $f->{skill_name} = sprintf("%s %d", $skill->name, $skill->level);
522 525
523 # Check if the finish time is within scheduled EVE downtime 526 # Check if the finish time is within scheduled EVE downtime
524 if (skill_finish_in_downtime($f->{skill})) 527 if (skill_finish_in_downtime($f->{skill_finish}))
525 { 528 {
526 $self->privmsg('#' . $config{channel}, sprintf( 529 $self->privmsg('#' . $config{channel}, sprintf(
527 "\002%s:\002 Completion time of %s's %s %lu " . 530 "\002%s:\002 Completion time of %s's %s " .
528 "skill will coincide with scheduled downtime at %s", 531 "skill will coincide with scheduled downtime at %s",
529 $f->{nick}, $f->{char}->name, 532 $f->{nick}, $f->{char}, $f->{skill_name},
530 $f->{skill}->name, $f->{skill}->level, 533 scalar(gmtime($f->{skill_finish}))));
531 scalar(gmtime($f->{skill}->finish_time)))); 534 }
532 } 535
533 536 my $text = sprintf("%s has completed training skill %s",
534 my $text = sprintf("%s has completed training skill %s %s", 537 $f->{char}, $f->{skill_name});
535 $f->{char}->name, $skill->name, $skill->level);
536 538
537 if ($text =~ /[\n\r]/) { 539 if ($text =~ /[\n\r]/) {
538 $text =~ s/[\n\r]/ /g; 540 $text =~ s/[\n\r]/ /g;
539 } 541 }
540 542
549 $f->{timer} = add_one_shot_timer($skill->finish_time - time(), sub { 551 $f->{timer} = add_one_shot_timer($skill->finish_time - time(), sub {
550 $self->privmsg('#' . $config{channel}, sprintf( 552 $self->privmsg('#' . $config{channel}, sprintf(
551 "\002%s:\002 %s", $f->{nick}, 553 "\002%s:\002 %s", $f->{nick},
552 encode("utf8", $text))); 554 encode("utf8", $text)));
553 delete $f->{skill}; 555 delete $f->{skill};
556 delete $f->{skill_finish};
557 delete $f->{skill_countdown};
558 delete $f->{skill_name};
554 }); 559 });
555 } 560 }
556 561
557 =pod 562 =pod
558 563
563 sub skill_finish_in_downtime 568 sub skill_finish_in_downtime
564 { 569 {
565 my $skill = shift; 570 my $skill = shift;
566 571
567 # Check if the finish time is within 11:00-11:59 (EVE downtime) 572 # Check if the finish time is within 11:00-11:59 (EVE downtime)
568 return ([gmtime($skill->finish_time)]->[2] == 11); 573 return ([gmtime($skill)]->[2] == 11);
569 } 574 }
570 575
571 END { 576 END {
572 cleanup_and_die(); 577 cleanup_and_die();
573 } 578 }