'finished' implementing ping support for server browser, not tested on windows

This commit is contained in:
2022-09-11 15:55:57 -05:00
parent 50ec9fc1da
commit ae3720d119
3 changed files with 62 additions and 27 deletions

View File

@@ -16,8 +16,8 @@ class W3DHub
@status = @data[:status] ? Status.new(@data[:status]) : nil @status = @data[:status] ? Status.new(@data[:status]) : nil
@last_pinged = -1 @ping_interval = 30_000
@ping_interval = 30 @last_pinged = Gosu.milliseconds + @ping_interval + 1
end end
def update(hash) def update(hash)
@@ -33,16 +33,7 @@ class W3DHub
@status.instance_variable_set(:@teams, hash[:teams]&.map { |t| Team.new(t) }) if hash[:teams] @status.instance_variable_set(:@teams, hash[:teams]&.map { |t| Team.new(t) }) if hash[:teams]
@status.instance_variable_set(:@players, hash[:players]&.select { |t| t[:nick] != "Nod" && t[:nick] != "GDI" }&.map { |t| Player.new(t) }) if hash[:players] @status.instance_variable_set(:@players, hash[:players]&.select { |t| t[:nick] != "Nod" && t[:nick] != "GDI" }&.map { |t| Player.new(t) }) if hash[:players]
if Gosu.milliseconds - @last_pinged >= @ping_interval send_ping
@last_pinged = Gosu.milliseconds
Thread.new do
ping = Net::Ping::External.new(@address)
@ping = (ping.duration * 1000.0).round if ping.ping?
States::Interface.instance&.update_server_ping(self)
end
end
return true return true
end end
@@ -50,6 +41,19 @@ class W3DHub
false false
end end
def send_ping(force_ping = false)
if force_ping || Gosu.milliseconds - @last_pinged >= @ping_interval
@last_pinged = Gosu.milliseconds
Thread.new do
ping = Net::Ping::External.new(@address)
@ping = (ping.duration * 1000.0).round if ping.ping?
States::Interface.instance&.update_server_ping(self)
end
end
end
class Status class Status
attr_reader :name, :password, :map, :max_players, :player_count, :started, :remaining, :teams, :players attr_reader :name, :password, :map, :max_players, :player_count, :started, :remaining, :teams, :players

View File

@@ -198,8 +198,8 @@ class W3DHub
@ping_icons[:unknown] = unknown @ping_icons[:unknown] = unknown
end end
def ping_icon(ping) def ping_icon(server)
case ping case server.ping
when 0..160 when 0..160
@ping_icons[:good] @ping_icons[:good]
when 161..250 when 161..250
@@ -213,24 +213,53 @@ class W3DHub
end end
end end
def ping_tip(server)
server.ping.negative? ? "Ping failed" : "Ping #{server.ping}ms"
end
def find_element_by_tag(container, tag, list = [])
container.children.each do |child|
list << child if child.style.tag == tag
find_element_by_tag(child, tag, list) if child.is_a?(CyberarmEngine::Element::Container)
end
return list.first
end
def refresh_server_list(server) def refresh_server_list(server)
@refresh_server_list = Gosu.milliseconds + 3_000 @refresh_server_list = Gosu.milliseconds + 3_000
@refresh_server = server if @selected_server&.id == server.id @refresh_server = server if @selected_server&.id == server.id
server_container = find_element_by_tag(@server_list_container, server.id)
return unless server_container
game_icon = find_element_by_tag(server_container, :game_icon)
server_name = find_element_by_tag(server_container, :server_name)
server_channel = find_element_by_tag(server_container, :server_channel)
server_region = find_element_by_tag(server_container, :server_region)
server_map = find_element_by_tag(server_container, :server_map)
player_count = find_element_by_tag(server_container, :player_count)
server_ping = find_element_by_tag(server_container, :ping)
server_name.value = "<b>#{server&.status&.name}</b>"
server_channel.value = server.channel
server_region.value = server.region
server_map.value = server&.status&.map
player_count.value = "#{server&.status&.player_count}/#{server&.status&.max_players}"
server_ping.value = ping_icon(server)
end end
def update_server_ping(server) def update_server_ping(server)
container = @server_list_container.children.find do |child| container = find_element_by_tag(@server_list_container, server.id)
child.style.tag == server.id
end
if container if container
ping_image = container.children.map { |c| c.children }.flatten.find do |child| ping_image = find_element_by_tag(container, :ping)
child.style.tag == :ping
end
if ping_image if ping_image
ping_image.value = ping_icon(server.ping) ping_image.value = ping_icon(server)
ping_image.tip = "#{server.ping}ms" ping_image.parent.parent.tip = ping_tip(server)
end end
end end
end end
@@ -273,7 +302,7 @@ class W3DHub
i += 1 i += 1
server_container = flow(width: 1.0, height: 48, hover: { background: 0xff_555566 }, active: { background: 0xff_555588 }, tag: server.id) do server_container = flow(width: 1.0, height: 48, hover: { background: 0xff_555566 }, active: { background: 0xff_555588 }, tag: server.id, tip: ping_tip(server)) do
background 0xff_333333 if i.even? background 0xff_333333 if i.even?
flow(width: 48, height: 1.0, padding: 4) do flow(width: 48, height: 1.0, padding: 4) do
@@ -290,7 +319,7 @@ class W3DHub
end end
flow(fill: true, height: 1.0) do flow(fill: true, height: 1.0) do
inscription "#{server&.status&.map}", tag: :map_name inscription "#{server&.status&.map}", tag: :server_map
end end
flow(width: 0.11, height: 1.0) do flow(width: 0.11, height: 1.0) do
@@ -298,9 +327,7 @@ class W3DHub
end end
flow(width: 48, height: 1.0, padding: 4) do flow(width: 48, height: 1.0, padding: 4) do
puts "#{server&.status&.name}#{server.ping}" image ping_icon(server), height: 1.0, tag: :ping
image ping_icon(server.ping), height: 1.0, tip: "#{server.ping}ms", tag: :ping
end end
end end

View File

@@ -187,6 +187,10 @@ class W3DHub
Api::ServerListUpdater.instance Api::ServerListUpdater.instance
list.each do |server|
server.send_ping(true)
end
@tasks[:server_list][:complete] = true @tasks[:server_list][:complete] = true
end end
end end