Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active February 2, 2025 19:04
Show Gist options
  • Select an option

  • Save julianrubisch/686fddcd703ca4b9e756716c9f049bca to your computer and use it in GitHub Desktop.

Select an option

Save julianrubisch/686fddcd703ca4b9e756716c9f049bca to your computer and use it in GitHub Desktop.

Revisions

  1. julianrubisch revised this gist Feb 2, 2025. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,8 @@ def initialize(players: [])
    end

    def tick
    super

    poll_progress unless @current_scene_name == :host_scene
    end

  2. julianrubisch revised this gist Feb 2, 2025. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions router.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    class HttpRouter
    VERSION = "0.1.0"

    attr_gtk

    def initialize
    @_routes = Routes.new
    @_request = {}
    @_response = {}
    end

    def tick
    process_http
    end

    def route
    yield @_routes
    end

    private

    def process_http
    inputs.http_requests.each do |r|
    @_routes.match(r.uri, r.method).call(r)
    end
    end
    end

    class Routes
    def initialize
    @_routes = {}
    end

    def match(path, method)
    @_routes[path][method]
    end

    def get(path, &block)
    @_routes[path] ||= {}
    @_routes[path]["GET"] = block
    end

    def post(path, &block)
    @_routes[path] ||= {}
    @_routes[path]["POST"] = block
    end
    end
  3. julianrubisch created this gist Feb 2, 2025.
    49 changes: 49 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    class Game < HttpRouter
    def initialize(players: [])
    @players = players
    @http_debounce = 0

    route do |routes|
    @players.each do |player|
    routes.get "/#{player.id}/progress" do |req|
    if @current_scene_name == :host_scene
    req.respond 200, "{ \"progress\": #{player.progress} }"
    end
    end

    routes.post "/#{player.id}/step" do |req|
    if @current_scene_name == :host_scene
    player.progress += 1
    player.progress = player.progress % player.course.coords.size

    req.respond 200
    end
    end
    end
    end
    end

    def tick
    poll_progress unless @current_scene_name == :host_scene
    end

    private

    def poll_progress
    return if @http_debounce.elapsed_time < 30

    @http_debounce = Kernel.tick_count

    @players.each do |player|
    @progress_result[player.id] ||= GTK.http_get "http://localhost:3000/#{player.id}/progress"

    # wait until response is complete
    if @progress_result[player.id] && @progress_result[player.id][:complete]
    if @progress_result[player.id][:http_response_code] == 200
    new_progress = GTK.parse_json(@progress_result[player.id][:response_data])["progress"]
    # ... handle state update in "player" scenes
    end
    end
    end
    end
    end