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-paths8.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 $pass = '3Igoad3Kp54Q';
my $user = 'kwksilver';
my $host = 'db.schemaverse.com';
my $dbh = DBI->connect('dbi:Pg:host='.$host.';database=schemaverse',$user,$pass) or die "$!";
my $colors = [qw/1f77b4 aec7e8 ff7f0e ffbb78 2ca02c 98df8a d62728 ff9896 9467bd c5b0d5 8c564b c49c94 e377c2 f7b6d2 7f7f7f c7c7c7 bcbd22 dbdb8d 17becf 9edae5/];
my $dur = .1;
my $max_tic;
my $players;
$max_tic = $dbh->selectcol_arrayref(q/select last_value from tic_seq;/)->[0];
$players = $dbh->selectcol_arrayref(q/
select player_id from (
select count(ship_id),ship_id,player_id
from ship_flight_recorder
group by ship_id,player_id
having count(ship_id) > 1
)a
group by player_id;
/);
my $clock_g = $svg->group( id => 'clock' );
for( 0 .. $max_tic ) {
my $clock = $clock_g->text(
id=> 'clock'.$_,
x=> 5,
y=> 20,
visibility => 'hidden',
style=> { fill => 'white' },
)->cdata($_);
$clock->animate(
-method => 'attribute',
attributeName => 'visibility',
to => 'visible',
begin => ( $_ * $dur ),
dur => $dur,
fill => ( $_ == $max_tic ? 'freeze' : 'remove' ),
);
}
my $paths_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[0]+1) + 1e7 ) ) )::numeric, 4 )::text x,
round( ( ? / ( 2e7 / ( (location[1]+1) + 1e7 ) ) )::numeric, 4 )::text y
from ship_flight_recorder
where player_id = ?
order by ship_id,tic
)a
group by ship_id
having count(*) > 1
;
\);
my $explode_sth = $dbh->prepare(q\
select ship_id_1 s, tic
from my_events
where 1=1
and action = 'EXPLODE'
and player_id_1 = ?
;
\);
for my $player (@$players) {
$paths_sth->execute( $w, $h, $player );
my $r = $paths_sth->fetchall_arrayref({});
# $explode_sth->execute( $player );
# my $explode = $explode_sth->fetchall_hashref('s');
# dd $explode;
my $p = $dbh->selectcol_arrayref(q/select get_player_username(?);/, undef, $player)->[0];
my $player_rgb = $dbh->selectcol_arrayref(q/select get_player_rgb(?::int);/, undef, $player )->[0];
unless ( defined $player_rgb ) {
my $scaled = $player % scalar @$colors;
$player_rgb = $colors->[$scaled];
}
my $svgg = $svg->group( id => 'p'.$player );
for(@$r) {
my $ship = $svgg->circle(
id => 's'.$_->{s},
r => 1.125,
style => {
'fill' => '#'.$player_rgb,
}
);
my $begin = ( $dur * $_->{start_tic} ) . 's';
my $ship_dur = ( $dur * ( $_->{end_tic} - $_->{start_tic} ) ) . 's';
$ship->animate(
-method => 'attribute',
attributeName => 'cx',
values => $_->{x},
begin=> $begin,
dur => $ship_dur,
calcMode => 'linear',
);
$ship->animate(
-method => 'attribute',
attributeName => 'cy',
values => $_->{y},
begin=> $begin,
dur => $ship_dur,
calcMode => 'linear',
);
unless( $_->{end_tic} eq $max_tic ) {
$ship->animate(
-method => 'attribute',
attributeName => 'visibility',
to => 'hidden',
begin => ( $dur * ( $_->{end_tic} + 1 ) ),
);
}
}
}
say $svg->render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment