From 01f16d45273ee2549f8fdf899683c409c03c2f46 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 15 May 2024 16:19:21 +0900 Subject: [PATCH] Update examples. --- examples/chatbot/gems.locked | 20 ++-- examples/flappy-bird/application.rb | 16 +--- examples/tanks/application.rb | 70 ++++++++++++++ examples/tanks/gems.locked | 138 ++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+), 22 deletions(-) create mode 100755 examples/tanks/application.rb create mode 100644 examples/tanks/gems.locked diff --git a/examples/chatbot/gems.locked b/examples/chatbot/gems.locked index 666165e..828bdf9 100644 --- a/examples/chatbot/gems.locked +++ b/examples/chatbot/gems.locked @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - lively (0.6.0) + lively (0.7.0) falcon (~> 0.47) live (~> 0.9) xrb @@ -32,10 +32,10 @@ GEM timers (~> 4.1) async-container (0.18.2) async (~> 2.10) - async-http (0.66.2) + async-http (0.66.3) async (>= 2.10.2) async-pool (>= 0.6.1) - io-endpoint (~> 0.10) + io-endpoint (~> 0.10, >= 0.10.3) io-stream (~> 0.4) protocol-http (~> 0.26.0) protocol-http1 (~> 0.19.0) @@ -59,7 +59,7 @@ GEM protocol-rack (~> 0.5) protocol-websocket (~> 0.11) base64 (0.2.0) - bigdecimal (3.1.7) + bigdecimal (3.1.8) concurrent-ruby (1.2.3) connection_pool (2.4.1) console (1.25.2) @@ -67,10 +67,10 @@ GEM fiber-local (~> 1.1) json drb (2.2.1) - falcon (0.47.1) + falcon (0.47.6) async async-container (~> 0.18) - async-http (~> 0.66, >= 0.66.2) + async-http (~> 0.66, >= 0.66.3) async-http-cache (~> 0.4.0) async-service (~> 0.10) bundler @@ -85,11 +85,11 @@ GEM fiber-storage (0.1.0) i18n (1.14.5) concurrent-ruby (~> 1.0) - io-endpoint (0.10.2) + io-endpoint (0.10.3) io-event (1.5.1) io-stream (0.4.0) json (2.7.2) - live (0.9.0) + live (0.11.0) async-websocket (~> 0.23) xrb localhost (1.3.1) @@ -114,7 +114,7 @@ GEM rack (>= 1.0) protocol-websocket (0.12.1) protocol-http (~> 0.2) - rack (3.0.10) + rack (3.0.11) samovar (2.3.0) console (~> 1.0) mapping (~> 1.0) @@ -141,4 +141,4 @@ DEPENDENCIES sqlite3 (~> 1.4) BUNDLED WITH - 2.5.9 + 2.5.5 diff --git a/examples/flappy-bird/application.rb b/examples/flappy-bird/application.rb index 303a7a7..5061917 100755 --- a/examples/flappy-bird/application.rb +++ b/examples/flappy-bird/application.rb @@ -18,11 +18,7 @@ def initialize(x, y, width, height) @height = height end - attr :x - attr :y - - attr :width - attr :height + attr_accessor :x, :y, :width, :height def right @x + @width @@ -86,9 +82,7 @@ def initialize(x, y, offset = 100, width: 44, height: 700) @scored = false end - attr_accessor :x - attr_accessor :y - attr_accessor :offset + attr_accessor :x, :y, :offset # Whether the bird has passed through the pipe. attr_accessor :scored @@ -165,18 +159,16 @@ def initialize(...) def handle(event) case event[:type] when "keypress" - detail = event[:detail] - if @game.nil? start_game! - elsif detail[:key] == " " + elsif event.dig(:detail, :key) == " " @bird&.jump end end end def forward_keypress - "live.forwardEvent(#{JSON.dump(@id)}, event, {value: event.target.value, key: event.key})" + "live.forwardEvent(#{JSON.dump(@id)}, event, {key: event.key})" end def reset! diff --git a/examples/tanks/application.rb b/examples/tanks/application.rb new file mode 100755 index 0000000..7bd8c30 --- /dev/null +++ b/examples/tanks/application.rb @@ -0,0 +1,70 @@ +#!/usr/bin/env lively + +require_relative 'png' +require 'base64' + +class Map + def initialize(width = 128, height = 128) + @width = width + @height = height + + @buffer = IO::Buffer.new(width * height) + + @buffer.slice(0, width * (height / 2)).clear(128) + end + + attr :width + attr :height + attr :buffer + + def step + x = rand(@width) + y = rand(@height) + value = rand(256) + + @buffer.set_value(:U8, x + y * @width, value) + end +end + +class TanksView < Live::View + def initialize(...) + super + + @map = Map.new + @loop = nil + end + + def bind(page) + super + + @loop ||= Async do + while true + @map.step + self.update! + + sleep 0.001 + end + end + end + + def close + if @loop + @loop.stop + @loop = nil + end + + super + end + + def map_data + data = PNG.greyscale(@map.width, @map.height, @map.buffer) + + return "data:image/png;base64,#{Base64.strict_encode64(data)}" + end + + def render(builder) + builder.tag('img', src: map_data) + end +end + +Application = Lively::Application[TanksView] diff --git a/examples/tanks/gems.locked b/examples/tanks/gems.locked new file mode 100644 index 0000000..f0af121 --- /dev/null +++ b/examples/tanks/gems.locked @@ -0,0 +1,138 @@ +PATH + remote: ../.. + specs: + lively (0.7.0) + falcon (~> 0.47) + live (~> 0.9) + xrb + +GEM + remote: https://rubygems.org/ + specs: + activemodel (7.1.3.2) + activesupport (= 7.1.3.2) + activerecord (7.1.3.2) + activemodel (= 7.1.3.2) + activesupport (= 7.1.3.2) + timeout (>= 0.4.0) + activesupport (7.1.3.2) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + async (2.11.0) + console (~> 1.25, >= 1.25.2) + fiber-annotation + io-event (~> 1.5, >= 1.5.1) + timers (~> 4.1) + async-container (0.18.2) + async (~> 2.10) + async-http (0.66.3) + async (>= 2.10.2) + async-pool (>= 0.6.1) + io-endpoint (~> 0.10, >= 0.10.3) + io-stream (~> 0.4) + protocol-http (~> 0.26.0) + protocol-http1 (~> 0.19.0) + protocol-http2 (~> 0.17.0) + traces (>= 0.10.0) + async-http-cache (0.4.3) + async-http (~> 0.56) + async-pool (0.6.1) + async (>= 1.25) + async-service (0.12.0) + async + async-container (~> 0.16) + async-websocket (0.26.1) + async-http (~> 0.54) + protocol-rack (~> 0.5) + protocol-websocket (~> 0.11) + base64 (0.2.0) + bigdecimal (3.1.8) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) + console (1.25.2) + fiber-annotation + fiber-local (~> 1.1) + json + drb (2.2.1) + falcon (0.47.6) + async + async-container (~> 0.18) + async-http (~> 0.66, >= 0.66.3) + async-http-cache (~> 0.4.0) + async-service (~> 0.10) + bundler + localhost (~> 1.1) + openssl (~> 3.0) + process-metrics (~> 0.2.0) + protocol-rack (~> 0.5) + samovar (~> 2.3) + fiber-annotation (0.2.0) + fiber-local (1.1.0) + fiber-storage + fiber-storage (0.1.0) + i18n (1.14.5) + concurrent-ruby (~> 1.0) + io-endpoint (0.10.3) + io-event (1.5.1) + io-stream (0.4.0) + json (2.7.2) + live (0.11.0) + async-websocket (~> 0.23) + xrb + localhost (1.3.1) + mapping (1.1.1) + markly (0.10.0) + mini_portile2 (2.8.6) + minitest (5.22.3) + mutex_m (0.2.0) + openssl (3.2.0) + process-metrics (0.2.1) + console (~> 1.8) + samovar (~> 2.1) + protocol-hpack (1.4.3) + protocol-http (0.26.5) + protocol-http1 (0.19.1) + protocol-http (~> 0.22) + protocol-http2 (0.17.0) + protocol-hpack (~> 1.4) + protocol-http (~> 0.18) + protocol-rack (0.5.1) + protocol-http (~> 0.23) + rack (>= 1.0) + protocol-websocket (0.12.1) + protocol-http (~> 0.2) + rack (3.0.11) + samovar (2.3.0) + console (~> 1.0) + mapping (~> 1.0) + sqlite3 (1.7.3) + mini_portile2 (~> 2.8.0) + sqlite3 (1.7.3-arm64-darwin) + timeout (0.4.1) + timers (4.3.5) + traces (0.11.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + xrb (0.6.1) + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + activerecord (~> 7.1) + base64 + live + lively! + markly + sqlite3 (~> 1.4) + +BUNDLED WITH + 2.5.5