Skip to content

Instantly share code, notes, and snippets.

@frodwith
Last active December 18, 2015 03:58
Show Gist options
  • Save frodwith/5721666 to your computer and use it in GitHub Desktop.
Save frodwith/5721666 to your computer and use it in GitHub Desktop.
use warnings;
use strict;
use DBI;
my $dbname = $ARGV[0] || die 'No database file name';
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname");
my $games = $dbh->selectall_arrayref('SELECT * FROM t_gibo', { Slice => {} });
for my $game (@$games) {
my %sgf;
# some fields have the same tags as sgf
my @copy = qw(PB PW BR WR);
@sgf{@copy} = @{$game}{@copy};
# let's get the date right...
my ($y, $m, $d, $h, $min) = $game->{RD} =~
/(\d{4})-(\d\d)-(\d\d)\s+(\d\d):(\d\d)/;
$sgf{DT} = "$y-$m-$d";
# Now we know what to call the file.
my $fn = "$sgf{PB} vs $sgf{PW} ($sgf{DT} $h:$min).sgf";
open my $fh, '>', $fn;
# SGF starts like this
print {$fh} '(;';
# SGF tags look like this: TN[VALUE]
print {$fh} "$_\[$sgf{$_}\]" for keys %sgf;
# moves are already in sgf format. Well, almost.
my @moves = split /;/, $game->{GIBO};
shift(@moves); # first one'll be empty, cause the string starts with ;
@moves = map {
my ($color, $coord) = /(W|B)\[(..)\]/;
# gibo records passes as ``. oy ve.
$coord = '' if $coord eq '``';
";$color\[$coord\]";
} @moves;
print {$fh} "\n";
print {$fh} join("\n", @moves);
# aaaand we're done.
print {$fh} ')';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment