| 1 | 1 | ▶ | use v5.40; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON::MaybeXS qw(encode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use PAGI::Nano; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # *** Real-time multiplayer Conway's Game of Life, in one small file. *** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # A background ticker evolves one shared world on the event loop. Every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # generation is broadcast over Server-Sent Events to all connected browsers at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # once, and anyone can POST to bring a cell to life — which everyone then sees in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # the very next frame. Open it in two tabs and watch them stay in lockstep; click | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # in one and life appears in the other. A live, multiplayer cellular automaton | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # with no database, no message broker, no JS framework — just PAGI::Nano. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # pagi-server app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # # then open http://127.0.0.1:5000/ in two browser windows and click around | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # # or watch the raw frames stream by: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # curl -N http://127.0.0.1:5000/live | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # # and inject a cell from another terminal: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # curl -X POST http://127.0.0.1:5000/cell/10/10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | my ($W, $H) = (48, 28); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # --- the world: a toroidal grid evolving by Conway's rules -------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | package GoL::World { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | sub new ($class, $w, $h) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | bless { w => $w, h => $h, gen => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | cells => [ map { [ (0) x $w ] } 1 .. $h ] }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 19 | ▶ | sub toggle ($self, $x, $y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toggle: called CC=5 SCAR=21.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 19 | ▶ | return unless $x >= 0 && $x < $self->{w} && $y >= 0 && $y < $self->{h}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$x >= 0 and $x < $self->{"w"} and $y >= 0 and $y < $self->{"h"}
$x >= 0 and $x < $self->{"w"} and $y >= 0
$x >= 0 and $x < $self->{"w"}
MC/DC0%0/4
$x >= 0 and $x < $self->{"w"} and $y >= 0 and $y < $self->{"h"}
$x >= 0
$x < $self->{"w"}
$y >= 0
$y < $self->{"h"}
Truth table20%1/5
A and B and C and D
A= $x >= 0B= $x < $self->{"w"}C= $y >= 0D= $y < $self->{"h"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 19 | $self->{cells}[$y][$x] ^= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1600 | ▶ | sub _alive ($self, $x, $y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_alive: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1600 | $self->{cells}[ ($y + $self->{h}) % $self->{h} ][ ($x + $self->{w}) % $self->{w} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub step ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
step: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | my @next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | for my $y (0 .. $self->{h} - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 20 | my @row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 20 | for my $x (0 .. $self->{w} - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 200 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 200 | for my $dy (-1, 0, 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 600 | for my $dx (-1, 0, 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1800 | ▶ | next if $dx == 0 && $dy == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dx == 0 and $dy == 0
MC/DC100%2/2
$dx == 0 and $dy == 0
$dx == 0
$dy == 0
Truth table100%3/3
A and B
A= $dx == 0B= $dy == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1600 | $n += $self->_alive($x + $dx, $y + $dy); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 200 | my $live = $self->{cells}[$y][$x]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 200 | ▶ | push @row, (($live ? ($n == 2 || $n == 3) : ($n == 3)) ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$n == 2 or $n == 3
MC/DC50%1/2
$n == 2 or $n == 3
$n == 2
$n == 3
Truth table66%2/3
A or B
A= $n == 2B= $n == 3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | push @next, \@row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | $self->{cells} = \@next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | $self->{gen}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # Drop a classic glider with its top-left at ($ox, $oy). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | sub seed_glider ($self, $ox, $oy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seed_glider: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $self->toggle($ox + $_->[0], $oy + $_->[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | for ([1, 0], [2, 1], [0, 2], [1, 2], [2, 2]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # A wire-friendly snapshot: each row is a string of 0/1 characters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | sub frame ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frame: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | generation => $self->{gen}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | w => $self->{w}, h => $self->{h}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | rows => [ map { join '', @$_ } @{ $self->{cells} } ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # --- a broadcast hub: one Future per waiting client, all resolved per tick ----- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | package GoL::Hub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub new ($class) { bless { waiters => [] }, $class } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub next_frame ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_frame: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | my $f = Future->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | push @{ $self->{waiters} }, $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub publish ($self, $frame) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
publish: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my @waiters = @{ $self->{waiters} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $self->{waiters} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $_->done($frame) for @waiters; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | our $HTML; # the live client, defined at the foot of the file | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $app = app { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | startup async sub ($state) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | my $world = GoL::World->new($W, $H); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | $world->seed_glider(2, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | $world->seed_glider(20, 10); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | $world->seed_glider(35, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | my $hub = GoL::Hub->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | $state->{world} = $world; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | $state->{hub} = $hub; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # The single source of motion: step the world and broadcast, forever. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # Retained (not awaited) so it runs on the loop for the app's lifetime. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | $state->{ticker} = (async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | await Future::IO->sleep(0.3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $world->step; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $hub->publish($world->frame); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | })->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | get '/' => sub ($c) { $c->html($HTML) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # A plain JSON snapshot — for curl, tests, or a non-streaming client. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | get '/grid' => sub ($c) { $c->state->{world}->frame }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # Bring a cell to life (or kill it). The change rides out on the next frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # that every connected client receives — that is the whole multiplayer trick. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | post '/cell/:x/:y' => sub ($c, $x, $y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | $c->state->{world}->toggle(int($x), int($y)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | $c->json({ ok => 1 }, status => 202); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # The live feed: send the current frame now, then one per generation until | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # the client goes away. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sse '/live' => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my $hub = $c->state->{hub}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | my $s = $c->sse; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | await $s->send_event(event => 'frame', data => encode_json($c->state->{world}->frame)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | while (!$c->is_disconnected) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | my $frame = await $hub->next_frame; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | last if $c->is_disconnected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | await $s->send_event(event => 'frame', data => encode_json($frame)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # --- the live client: EventSource in, canvas out, clicks back out ------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | $HTML = <<'HTML'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | <!doctype html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | <meta charset="utf-8"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | <title>PAGI::Nano — Multiplayer Life</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | <style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | body { background:#0b0e14; color:#cdd9e5; font:14px/1.5 ui-monospace,monospace; text-align:center; margin:2rem } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | h1 { font-weight:600; letter-spacing:.04em } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | canvas { background:#11151c; border:1px solid #2a3550; border-radius:6px; cursor:crosshair; image-rendering:pixelated } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | .meta { margin:.6rem; color:#7b8aa8 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | b { color:#6cf09a } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | </style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | <h1>PAGI::Nano · Multiplayer Game of Life</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | <div class="meta">generation <b id="gen">0</b> — click to seed life; open another tab to play along</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | <canvas id="c"></canvas> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | <script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | const cell = 14; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | const cv = document.getElementById('c'), ctx = cv.getContext('2d'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | let W = 0, H = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | function draw(frame) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | W = frame.w; H = frame.h; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | if (cv.width !== W*cell) { cv.width = W*cell; cv.height = H*cell; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | ctx.fillStyle = '#11151c'; ctx.fillRect(0, 0, cv.width, cv.height); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | ctx.fillStyle = '#6cf09a'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | for (let y = 0; y < H; y++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | const row = frame.rows[y]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | for (let x = 0; x < W; x++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | if (row[x] === '1') ctx.fillRect(x*cell+1, y*cell+1, cell-2, cell-2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | document.getElementById('gen').textContent = frame.generation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | const es = new EventSource('/live'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | es.addEventListener('frame', e => draw(JSON.parse(e.data))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | cv.addEventListener('click', e => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | const r = cv.getBoundingClientRect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | const x = Math.floor((e.clientX - r.left) / cell); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | const y = Math.floor((e.clientY - r.top) / cell); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | fetch(`/cell/${x}/${y}`, { method: 'POST' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | </script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | HTML | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | $app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||