Skip to content

Instantly share code, notes, and snippets.

@chandeeland
Forked from KWKdesign/schemaverse-svg.pl
Created March 4, 2014 20:39
Show Gist options
  • Save chandeeland/9355140 to your computer and use it in GitHub Desktop.
Save chandeeland/9355140 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
use 5.010;
use strict;
use warnings;
use Data::Dump;
use DBI;
use SVG;
# https://dl.dropboxusercontent.com/u/94229093/Schemaverse/animate-paths.svg
my $h = 800;
my $w = $h;
my $svg = SVG->new(width=>$w,height=>$h);
$svg->rect( x=>1, y=>1, width=>$w, height=>$h );
my $dur = .1;
my $max_tic;
{
my $dbh = DBI->connect('dbi:Pg:host=localhost;database=schemaverse','schemaverse','') or die "$!";
$max_tic = $dbh->selectcol_arrayref(q/select last_value from tic_seq;/)->[0];
}
my $players = [qw/kwksilver philly deadlock/];
for my $player (@$players) {
my $dbh = DBI->connect('dbi:Pg:host=localhost;database=schemaverse',$player,'') or die "$!";
my $sth = $dbh->prepare(q\
select ship_id s, min(tic) start_tic, max(tic) end_tic,
string_agg( tic::text, ';') t,
string_agg( x, ';' ) x, string_agg( y , ';' ) y
from (
select ship_id, tic,
round( ( ? / ( 2e7 / ( (location_x+1) + 1e7 ) ) )::numeric, 4 )::text x,
round( ( ? / ( 2e7 / ( (location_y+1) + 1e7 ) ) )::numeric, 4 )::text y
from my_ships_flight_recorder
order by ship_id,tic
)a
group by ship_id
having count(*) > 1
;
\);
$sth->execute( $w, $h );
my $r = $sth->fetchall_arrayref({});
my $p = $dbh->selectcol_arrayref(q/select get_player_id(?)/, undef, $player)->[0];
my $player_rgb = $dbh->selectcol_arrayref(q/select get_player_rgb(get_player_id(?));/, undef, $player )->[0];
my $svgg = $svg->group( id => 'p'.$p );
for(@$r) {
# $_ = { # sample row
# s => 2287,
# t => "124;126;127;128;129;130;131;132;133;134;135",
# x => "470.0404;470.0379;467.5728;462.6475;459.8958;459.6714;455.5044;447.6514;442.3548;440.9802;435.8663",
# y => "283.0890;283.0922;286.2473;292.5514;295.9496;296.2473;297.2093;298.7358;299.7653;300.0077;301.6693",
# };
my $xv = [ split ';', $_->{x} ];
my $yv = [ split ';', $_->{y} ];
my $ship = $svgg->circle(
id=>'s'.$_->{s},
# cx => $xv->[0],
# cy => $yv->[0],
r => 1.125,
style=>{
'fill' => '#'.$player_rgb,
}
);
$ship->animate(
-method => 'attribute',
attributeName => 'cx',
values => $_->{x},
begin=>( $dur * $_->{start_tic} ) . 's',
dur => ( $dur * scalar @$xv ) . 's',
calcMode => 'linear',
fill => 'freeze',
# repeatCount => 'indefinite'
);
$ship->animate(
-method => 'attribute',
attributeName => 'cy',
values => $_->{y},
begin=> ( $dur * $_->{start_tic} ) . 's',
dur => ( $dur * scalar @$yv ) . 's',
calcMode => 'linear',
fill => 'freeze',
# repeatCount => 'indefinite'
);
}
}
say $svg->render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment