Websocket based server list updater now works, gui needs a bit of work to not flash when refreshing list

This commit is contained in:
2021-12-27 20:48:30 -06:00
parent 92f6a76381
commit 0f943252c5
4 changed files with 60 additions and 24 deletions

View File

@@ -16,6 +16,18 @@ class W3DHub
@status = @data[:status] ? Status.new(@data[:status]) : nil
end
def update(hash)
if @status
@status.instance_variable_set(:@name, hash[:name])
@status.instance_variable_set(:@password, hash[:password] || false)
@status.instance_variable_set(:@map, hash[:map])
@status.instance_variable_set(:@max_players, hash[:maxplayers])
@status.instance_variable_set(:@player_count, hash[:numplayers] || 0)
@status.instance_variable_set(:@started, hash[:started])
@status.instance_variable_set(:@remaining, hash[:remaining])
end
end
class Status
attr_reader :name, :password, :map, :max_players, :player_count, :started, :remaining, :teams, :players

View File

@@ -1,6 +1,8 @@
class W3DHub
class Api
class ServerListUpdater
include CyberarmEngine::Common
##!!! When this breaks update from: https://github.com/socketry/async-websocket/blob/master/lib/async/websocket/connection.rb
# refinements preserves super... 😢
class PatchedConnection < ::Protocol::WebSocket::Connection
@@ -71,7 +73,6 @@ class W3DHub
run
end
# TODO: Properly start up and monitor for updates to server list
def run
Async do |task|
internet = Async::HTTP::Internet.instance
@@ -88,12 +89,10 @@ class W3DHub
pp connection.read
connection.write({ "type": 6 })
puts "servers: #{Store.server_list&.count}"
Store.server_list.each_with_index do |server, i|
i += 1
mode = 1 # 2 full details, 1 basic details
out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, 1] }
out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, mode] }
connection.write(out)
end
@@ -105,10 +104,10 @@ class W3DHub
next unless rpc[:target] == "ServerStatusChanged"
id, data = rpc[:arguments]
pp [id, data.length, data]
server = Api::ServerListServer.new(data)
pp [server.player_count, server.max_players]
server = Store.server_list.find { |s| s.id == id }
server&.update(data)
state = window.current_state
state.update_server_browser(server) if state.is_a?(States::Interface) && server
end
end
end

View File

@@ -5,6 +5,7 @@ class W3DHub
@server_locked_icons = {}
@selected_server ||= nil
@selected_server_container ||= nil
@selected_color = 0xff_666655
@filters = Store.settings[:server_list_filters] || {}
@@ -119,6 +120,28 @@ class W3DHub
end
end
def update
super
populate_server_list if @refresh_server_list
@refresh_server_list = false
end
def refresh_server_list(server)
populate_server_info(server) if @selected_server&.id == server.id
@refresh_server_list = true
end
def stylize_selected_server(server_container)
server_container.style.server_item_background = server_container.style.default[:background]
server_container.style.server_item_hover_background = server_container.style.hover[:background]
server_container.style.server_item_active_background = server_container.style.active[:background]
server_container.style.background = @selected_color
server_container.style.default[:background] = @selected_color
server_container.style.hover[:background] = @selected_color
server_container.style.active[:background] = @selected_color
end
def populate_server_list
@server_list_container.scroll_top = 0
@@ -171,25 +194,23 @@ class W3DHub
end
server_container.subscribe(:clicked_left_mouse_button) do
if @selected_server
@selected_server.style.background = @selected_server.style.server_item_background
@selected_server.style.default[:background] = @selected_server.style.server_item_background
@selected_server.style.hover[:background] = @selected_server.style.server_item_hover_background
@selected_server.style.active[:background] = @selected_server.style.server_item_active_background
if @selected_server_container
@selected_server_container.style.background = @selected_server_container.style.server_item_background
@selected_server_container.style.default[:background] = @selected_server_container.style.server_item_background
@selected_server_container.style.hover[:background] = @selected_server_container.style.server_item_hover_background
@selected_server_container.style.active[:background] = @selected_server_container.style.server_item_active_background
end
server_container.style.server_item_background = server_container.style.default[:background]
server_container.style.server_item_hover_background = server_container.style.hover[:background]
server_container.style.server_item_active_background = server_container.style.active[:background]
server_container.style.background = @selected_color
server_container.style.default[:background] = @selected_color
server_container.style.hover[:background] = @selected_color
server_container.style.active[:background] = @selected_color
stylize_selected_server(server_container)
@selected_server = server_container
@selected_server_container = server_container
@selected_server = server
populate_server_info(server)
end
stylize_selected_server(server_container) if server.id == @selected_server&.id
end
end
end
@@ -313,7 +334,7 @@ class W3DHub
end
def fetch_server_list
unless Gosu.milliseconds - Store.server_list_last_fetch >= 30_000 # 30 seconds
unless Gosu.milliseconds - Store.server_list_last_fetch >= 3_000 # 3 seconds
populate_server_list # Fake it
return
end

View File

@@ -165,8 +165,6 @@ class W3DHub
end
def page(klass, options = {})
# @menu_bar.clear
# @status_bar.clear
body.clear
@page.blur if @page
@@ -179,6 +177,12 @@ class W3DHub
@page.focus
end
def update_server_browser(server)
return unless @page.is_a?(Pages::ServerBrowser)
@page.refresh_server_list(server)
end
def show_application_taskbar
@application_taskbar_container.show
end