From 9f4ca51af8556698facebeaa227eccabf8e54b0b Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sat, 11 Jun 2022 18:18:04 -0500 Subject: [PATCH] Updated gems, added server events to games page --- Gemfile.lock | 24 +++++----- lib/api.rb | 15 +++++++ lib/api/event.rb | 33 ++++++++++++++ lib/pages/games_redesign.rb | 88 ++++++++++++++++++++++++++++++------- w3d_hub_linux_launcher.rb | 1 + 5 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 lib/api/event.rb diff --git a/Gemfile.lock b/Gemfile.lock index 3e78819..0ea19d6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,42 +3,43 @@ GEM specs: addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) - async (1.30.1) + async (1.30.2) console (~> 1.10) nio4r (~> 2.3) timers (~> 4.1) - async-http (0.56.5) + async-http (0.56.6) async (>= 1.25) async-io (>= 1.28) async-pool (>= 0.2) protocol-http (~> 0.22.0) protocol-http1 (~> 0.14.0) protocol-http2 (~> 0.14.0) + traces (~> 0.4.0) async-io (1.33.0) async - async-pool (0.3.9) + async-pool (0.3.10) async (>= 1.25) async-websocket (0.19.0) async-http (~> 0.54) async-io (~> 1.23) protocol-websocket (~> 0.7.0) clipboard (1.3.6) - concurrent-ruby (1.1.9) - console (1.14.0) + concurrent-ruby (1.1.10) + console (1.15.3) fiber-local - cyberarm_engine (0.20.0) + cyberarm_engine (0.21.0) clipboard (~> 1.3) excon (~> 0.88) gosu (~> 1.1) gosu_more_drawables (~> 0.3) digest-crc (0.6.4) rake (>= 12.0.0, < 14.0.0) - excon (0.92.0) + excon (0.92.3) ffi (1.15.5) ffi (1.15.5-x64-mingw-ucrt) ffi (1.15.5-x64-mingw32) fiber-local (1.0.0) - gosu (1.4.1) + gosu (1.4.3) gosu_more_drawables (0.3.1) i18n (1.10.0) concurrent-ruby (~> 1.0) @@ -46,8 +47,8 @@ GEM addressable (~> 2.7) nio4r (2.5.8) protocol-hpack (1.4.2) - protocol-http (0.22.5) - protocol-http1 (0.14.2) + protocol-http (0.22.6) + protocol-http1 (0.14.4) protocol-http (~> 0.22) protocol-http2 (0.14.2) protocol-hpack (~> 1.4) @@ -55,11 +56,12 @@ GEM protocol-websocket (0.7.5) protocol-http (~> 0.2) protocol-http1 (~> 0.2) - public_suffix (4.0.6) + public_suffix (4.0.7) rake (13.0.6) rexml (3.2.5) thread-local (1.1.0) timers (4.3.3) + traces (0.4.1) PLATFORMS x64-mingw-ucrt diff --git a/lib/api.rb b/lib/api.rb index f0478e2..a2027ce 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -207,6 +207,21 @@ class W3DHub Cache.fetch_package(package, block) end + # /apis/w3dhub/1/get-events + # + # clients requests events: data={"serverPath":"apb"} + def self.events(app_id) + body = URI.encode_www_form("data": JSON.dump({ serverPath: app_id })) + response = post("#{ENDPOINT}/apis/w3dhub/1/get-server-events", FORM_ENCODED_HEADERS, body) + + if response.success? + array = JSON.parse(response.read, symbolize_names: true) + array.map { |e| Event.new(e) } + else + false + end + end + #! === Server List API === !# SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze diff --git a/lib/api/event.rb b/lib/api/event.rb new file mode 100644 index 0000000..a379f6a --- /dev/null +++ b/lib/api/event.rb @@ -0,0 +1,33 @@ +class W3DHub + class Api + class Event + def initialize(data) + @data = data + end + + def server + @data[:server] + end + + def title + @data[:title] + end + + def start_time + @start_time ||= Time.parse(@data[:starttime]).localtime + end + + def end_time + @end_time ||= Time.parse(@data[:endtime]).localtime + end + + def date_time + @data[:dateTime] + end + + def image + @data[:image] + end + end + end +end diff --git a/lib/pages/games_redesign.rb b/lib/pages/games_redesign.rb index ce2efcc..cc21445 100644 --- a/lib/pages/games_redesign.rb +++ b/lib/pages/games_redesign.rb @@ -3,6 +3,7 @@ class W3DHub class Games < Page def setup @game_news ||= {} + @game_events ||= {} # unless Store.offline_mode @focused_game ||= Store.applications.games.find { |g| g.id == Store.settings[:last_selected_app] } @@ -212,6 +213,10 @@ class W3DHub end end + # Game Events + @game_events_container = flow(width: 1.0, height: 128, padding: 8, visible: false) do + end + # Game News @game_news_container = flow(width: 1.0, fill: true, padding: 8, scroll: true) do # background 0xff_005500 @@ -220,24 +225,40 @@ class W3DHub end end - return if Cache.net_lock?("game_news_#{game.id}") - - if @game_news[game.id] - populate_game_news(game) - else - @game_news_container.clear do - title I18n.t(:"games.fetching_news"), padding: 8 - end - - BackgroundWorker.foreground_job( - -> { fetch_game_news(game) }, - lambda do |result| - if result - populate_game_news(game) - Cache.release_net_lock(result) + unless Cache.net_lock?("game_news_#{game.id}") + if @game_events[game.id] + populate_game_events(game) + else + BackgroundWorker.foreground_job( + -> { fetch_game_events(game) }, + lambda do |result| + if result + populate_game_events(game) + Cache.release_net_lock(result) + end end + ) + end + end + + unless Cache.net_lock?("game_events_#{game.id}") + if @game_news[game.id] + populate_game_news(game) + else + @game_news_container.clear do + title I18n.t(:"games.fetching_news"), padding: 8 end - ) + + BackgroundWorker.foreground_job( + -> { fetch_game_news(game) }, + lambda do |result| + if result + populate_game_news(game) + Cache.release_net_lock(result) + end + end + ) + end end end @@ -344,6 +365,41 @@ class W3DHub end end + def fetch_game_events(game) + lock = Cache.acquire_net_lock("game_events_#{game.id}") + return false unless lock + + events = Api.events(game.id) + Cache.release_net_lock("game_events_#{game.id}") unless events + + return false unless events + + @game_events[game.id] = events + + "game_events_#{game.id}" + end + + def populate_game_events(game) + return unless @focused_game == game + + if (events = @game_events[game.id]) + @game_events_container.show unless events.empty? + @game_events_container.hide if events.empty? + + @game_events_container.clear do + events.flatten.each do |event| + stack(width: 300, height: 1.0, margin_left: 8, margin_right: 8, border_thickness: 1, border_color: lighten(Gosu::Color.new(game.color))) do + background 0xaa_222222 + + title event.title, width: 1.0, text_align: :center + tagline event.start_time.strftime("%A"), text_size: 36, width: 1.0, text_align: :center + caption event.start_time.strftime("%B %e, %Y %l:%M %p"), width: 1.0, text_align: :center + end + end + end + end + end + def populate_game_modifications(application, channel) @game_news_container.clear do ([ diff --git a/w3d_hub_linux_launcher.rb b/w3d_hub_linux_launcher.rb index 011d1eb..75d65eb 100644 --- a/w3d_hub_linux_launcher.rb +++ b/w3d_hub_linux_launcher.rb @@ -96,6 +96,7 @@ require_relative "lib/api/server_list_server" require_relative "lib/api/server_list_updater" require_relative "lib/api/account" require_relative "lib/api/package" +require_relative "lib/api/event" require_relative "lib/page" # require_relative "lib/pages/games"