-
-
Save chandeeland/9355140 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 $clock = $svg->text( id=>'clock',x=>10,y=>10); | |
# $clock->animate( | |
# -method => 'attribute', | |
# attributeName => '' | |
# ); | |
my $pass = ''; | |
my $user = ''; | |
my $host = 'db.schemaverse.com'; | |
my $dbh = DBI->connect('dbi:Pg:host='.$host.';database=schemaverse',$user,$pass) or die "$!"; | |
my $dur = .1; | |
my $max_tic; | |
my $players; | |
{ | |
my $dbh = DBI->connect('dbi:Pg:host='.$host.';database=schemaverse',$user,$pass) or die "$!"; | |
$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; | |
/); | |
} | |
for my $player (@$players) { | |
# my $dbh = DBI->connect('dbi:Pg:host='.$host.';database=schemaverse',$user,$pass) 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[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 | |
; | |
\); | |
$sth->execute( $w, $h, $player ); | |
my $r = $sth->fetchall_arrayref({}); | |
# dd $r;die; | |
my $p = $dbh->selectcol_arrayref(q/select get_player_username(?)/, undef, $player)->[0]; | |
my $player_rgb = $dbh->selectcol_arrayref(q/select coalesce(( select get_player_rgb(?) ),'ffffff');/, undef, $player )->[0]; | |
# $player_rgb ||= 'ffffff'; | |
say qq/$player - $p - $player_rgb/; | |
my $svgg = $svg->group( id => 'p'.$player ); | |
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