Skip to content

Instantly share code, notes, and snippets.

@jsnell
Created March 26, 2018 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsnell/8d257b19c5cd1ec71e820c538e6e98ab to your computer and use it in GitHub Desktop.
Save jsnell/8d257b19c5cd1ec71e820c538e6e98ab to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -lw
use strict;
use JSON;
my %data = ();
sub buckets {
my $trophies = shift;
my @buckets = 'all';
if (defined $trophies) {
my $low = $trophies - $trophies % 500;
my $high = $low + 500;
push @buckets, "$low-$high";
}
map {
$data{$_} ||= {};
} @buckets;
}
while (<>) {
my $data = decode_json $_;
my @team = @{$data->{team}};
my @other = @{$data->{opponent}};
next if @team != 1;
next if $data->{mode} ne 'Ladder';
for my $bucket (buckets $team[0]{startingTrophies}) {
my @cards = map { $_->{key} } @{$team[0]{cards}};
my @ocards = map { $_->{key} } @{$other[0]{cards}};
$bucket->{total} += 2;
for my $card (@cards) {
$bucket->{card_count}{$card}++;
}
for my $ocard (@ocards) {
$bucket->{card_count}{$ocard}++;
}
for my $card (@cards) {
for my $ocard (@ocards) {
$bucket->{matchups}{$card}{$ocard}++;
$bucket->{matchups}{$ocard}{$card}++;
}
}
}
}
sub print_stats {
my ($bucket_name, $bucket) = @_;
my @cards = keys %{$bucket->{card_count}};
my $total = $bucket->{total};
for my $a (@cards) {
my $arate = $bucket->{card_count}{$a} / $total;
for my $b (@cards) {
my $brate = $bucket->{card_count}{$b} / $total;
my $expected = $arate*$brate;
my $actual = ($bucket->{matchups}{$a}{$b} // 0) / $total;
print join ",", ($bucket_name,
$a, $b,
$arate, $brate,
$bucket->{card_count}{$a},
$bucket->{card_count}{$b},
$total,
$expected,
$actual,
$actual / $expected)
}
}
}
print join ",", qw(bucket a b a_rate b_rate a_count b_count
total expected actual ratio);
for (keys %data) {
print_stats $_, $data{$_};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment