Skip to content

Instantly share code, notes, and snippets.

@samyk
Created November 23, 2018 21:09
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 samyk/66148f77ef073384112123be8944e6cb to your computer and use it in GitHub Desktop.
Save samyk/66148f77ef073384112123be8944e6cb to your computer and use it in GitHub Desktop.
Convert Supercell gamer tags / avatar IDs / account IDs between each other
#!/usr/bin/perl
#
# Convert Supercell gamer tags to avatar/account IDs and vice versa
# Tested on Crash Royale and Boom Beach
# - supports new Boom Beach avatar_id format
#
# -samy kamkar - https://samy.pl
# 2018/11/23
my @tagchrs = qw/0 2 8 9 P Y L Q G R J C U V/;
my %tagh = map { $_ => $i++ } @tagchrs;
if ($ARGV[0] =~ /^#/)
{
print idfromtag(@ARGV) . $/;
}
elsif ($ARGV[0] =~ /^\d+$/)
{
print tagfromid(@ARGV) . $/;
}
else
{
die "usage: $0 <#tag | hi lo | avatar_id>
examples:
$0 '#GGY0GUVQ'
$0 55838114918
$0 3540070 13\n";
}
sub idfromtag
{
my ($tag) = @_;
$tag =~ s/#//;
my $i = 0;
my $id;
while (length($tag))
{
my $c = $tagh{substr($tag, 0, 1, '')};
$id *= @tagchrs;
$id += $c;
}
print "(" . ($id & 0xFF) . " " . ($id >> 8) . ")\n";
print (((($id & 0xFF) << 32) | $id >> 8) . "\n");
return $id;
}
sub tagfromid
{
my ($lo, $hi) = @_;
if ($lo > 2 ** 32)
{
$hi = $lo >> 32;
$lo &= 0xFFFFFFFF;
}
my $id = (($lo << 8) + $hi);
print "$id\n";
my $res;
while ($id)
{
my $rem = int($id % @tagchrs);
#print "rem = $rem (id=$id)\n";
$res = $tagchrs[$rem] . $res;
$id -= $rem;
$id /= @tagchrs;
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment