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