comparison skillbot.pl @ 9:c797330cd371

Handles multiple characters on one account
author Dominic Cleal <dominic@computerkb.co.uk>
date Sat, 06 Dec 2008 15:29:45 +0000
parents cb6e21f2d514
children 9cc6dbc803f6
comparison
equal deleted inserted replaced
8:cb6e21f2d514 9:c797330cd371
30 require 'timers.pl'; 30 require 'timers.pl';
31 31
32 # Config variables 32 # Config variables
33 my %config; 33 my %config;
34 34
35 my %friends; 35 my %frienduids;
36 my @friends;
36 37
37 open(CONFIG, "< skillbot.conf") or die "can't open skillbot.conf for reading: $!"; 38 open(CONFIG, "< skillbot.conf") or die "can't open skillbot.conf for reading: $!";
38 while(<CONFIG>) { 39 while(<CONFIG>) {
39 chomp; 40 chomp;
40 s/#.*//; 41 s/#.*//;
343 344
344 while (<FF>) { 345 while (<FF>) {
345 chomp; 346 chomp;
346 next if (/^#/); 347 next if (/^#/);
347 348
348 if (/^([^\t]+)\t+([0-9]+)\t+([A-F0-9]{64})\t+([^\t]+)/i) { 349 if (/^([0-9]+)\s+([a-f0-9]{64})\s+(\S+)(\s+(.+))?/i) {
349 my $c = $1; 350 my $uid = $1;
350 my $uid = $2; 351 my $key = uc($2);
351 my $key = uc($3); 352 my $nick = $3;
352 my $nick = $4; 353 my $c = $5;
353 354
354 if (! $friends{$c}) { 355 if (! $frienduids{$uid}) {
355 my $api = WebService::EveOnline->new( { user_id => $uid, 356 my $api = WebService::EveOnline->new( { user_id => $uid,
356 api_key => $key } ); 357 api_key => $key } );
357 358
359 my $loaded = 0;
358 foreach my $character ($api->characters) { 360 foreach my $character ($api->characters) {
359 next if $c ne $character->name; 361 next if defined $c && $c ne $character->name;
360 $friends{$c}->{char} = $character; 362 push @friends, {
361 last; 363 char => $character,
364 api => $api,
365 nick => $nick
366 };
367 $loaded++;
362 } 368 }
363 369
364 unless (defined $friends{$c}->{char}) 370 if (defined $c && $loaded == 0)
365 { 371 {
366 irc_debug("EVE: Unable to find character %s for ID %lu", 372 irc_debug("EVE: Unable to find character %s for ID %lu",
367 $c, $uid); 373 $c, $uid);
368 next; 374 next;
369 } 375 }
370 376
371 $friends{$c}->{api} = $api; 377 irc_debug("EVE: Adding new friend %s (%lu), %lu characters",
372 378 $nick, $uid, $loaded);
373 irc_debug("EVE: Adding new friend '%s', nick %s", $c, $nick);
374 } 379 }
375
376 $friends{$c}->{nick} = $nick;
377 } 380 }
378 } 381 }
379 382
380 close(FF) or warn "Something weird when closing friends_file: $!"; 383 close(FF) or warn "Something weird when closing friends_file: $!";
381 } 384 }
388 =cut 391 =cut
389 sub check_training 392 sub check_training
390 { 393 {
391 my $self = shift; 394 my $self = shift;
392 395
393 foreach my $f (keys %friends) { 396 foreach my $f (@friends) {
394 # Skip skills training that we've learnt about 397 # Skip skills training that we've learnt about
395 next if defined $friends{$f}->{skill}; 398 next if defined $f->{skill};
396 399
397 my $skill = $friends{$f}->{char}->skill->in_training; 400 my $skill = $f->{char}->skill->in_training;
398 # Nothing training 401 # Nothing training
399 next unless $skill; 402 next unless $skill;
400 403
401 irc_debug("Character %s is training %s (%s)", 404 irc_debug("Character %s is training %s (%s)",
402 $friends{$f}->{char}->name, $skill->name, 405 $f->{char}->name, $skill->name,
403 $skill->time_remaining); 406 $skill->time_remaining);
404 407
405 $friends{$f}->{skill} = $skill; 408 $f->{skill} = $skill;
406 409
407 my $text = sprintf("%s has completed training skill %s %s", 410 my $text = sprintf("%s has completed training skill %s %s",
408 $friends{$f}->{char}->name, 411 $f->{char}->name, $skill->name, $skill->level);
409 $skill->name, $skill->level);
410 412
411 if ($text =~ /[\n\r]/) { 413 if ($text =~ /[\n\r]/) {
412 $text =~ s/[\n\r]/ /g; 414 $text =~ s/[\n\r]/ /g;
413 } 415 }
414 416
415 add_one_shot_timer($skill->finish_time - time(), sub { 417 add_one_shot_timer($skill->finish_time - time(), sub {
416 $self->notice('#' . $config{channel}, sprintf("[%s] %s", 418 $self->notice('#' . $config{channel}, sprintf("[%s] %s",
417 $friends{$f}->{nick}, encode("utf8", $text))); 419 $f->{nick}, encode("utf8", $text)));
418 delete $friends{$f}->{skill}; 420 delete $f->{skill};
419 }); 421 });
420 } 422 }
421 } 423 }
422 424
423 END { 425 END {