improve error messages of failed password include
[sc2-widget] / getsc2clan
index 94ca435551adcd62807fca213ea08464c60938ae..1fbdd02ee3ada748cd8ac69992f2f408d11cfa89 100755 (executable)
@@ -6,7 +6,7 @@ use utf8;
 use Data::Dump qw( pp );
 use LWP::Authen::OAuth2;
 use JSON qw( decode_json );
-use List::MoreUtils qw( all part nsort_by );
+use List::MoreUtils qw( all part sort_by nsort_by );
 
 if (@ARGV and all { m[/] } @ARGV) {
        say pp blizget($_) for @ARGV;
@@ -20,7 +20,9 @@ my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches || []};
 
 sub blizget {
        state $bliz = do {
-               my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
+               my @authdata = do './.blizzard.passwd.pl' and not $@ || $!
+                       or die "No auth setup: ", $@ || $!, "\n";
+               my %auth = @authdata;
                my $bliz = LWP::Authen::OAuth2->new(%auth,
                        token_endpoint          => 'https://eu.battle.net/oauth/token',
                        request_required_params => [qw( client_id client_secret grant_type )],
@@ -40,13 +42,19 @@ sub blizget {
 my @ladderdata = map {
        blizget(legacy => profile => 2 => 1 => $_ => 'ladders')
 } @{$profiles};
-my %ladders = (
-       map { $_->{ladder}->[0]->{ladderId} => $_ } # unique
-       grep { $_->{ladder}->[0]->{division} }
-       map { $_->{previousSeason}->@*, $_->{currentSeason}->@* }
-       @ladderdata
-);
+
+# merge relevant ladder data of all users
+my %ladders;
+for my $season (qw[ currentSeason previousSeason ]) {
+       for my $row (map { $_->{$season}->@* } @ladderdata) {
+               $row->{ladder}->[0]->{division} or next;
+               $row->{season} = $season;
+               $ladders{ $row->{ladder}->[0]->{ladderId} } //= $row;
+       }
+}
+
 my @ladders = (
+       sort_by { $_->{season} } # season
        nsort_by {
                -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
        } # activity desc
@@ -72,6 +80,7 @@ say JSON->new->canonical->pretty->encode({
                members  => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
                wins     => $_->{ladder}->[0]->{wins},
                losses   => $_->{ladder}->[0]->{losses},
+               (season  => -1) x ($_->{season} eq 'previousSeason'),
        }} @ladders],
        members  => [map {
                blizget(metadata => profile => 2 => 1 => $_->{id})