mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-09-21 07:43:57 +00:00
Compare commits
3 Commits
7524d01347
...
b589a3dc1b
| Author | SHA1 | Date | |
|---|---|---|---|
| b589a3dc1b | |||
| 8333c7b00d | |||
| 7f716a57ca |
@@ -105,9 +105,9 @@ module W3DHubLauncher
|
||||
step = @steps.shift
|
||||
step&.call
|
||||
|
||||
pp (1.0 - (@steps.size / @step_count.to_f)), @steps.size, @step_count
|
||||
# pp (1.0 - (@steps.size / @step_count.to_f)), @steps.size, @step_count
|
||||
@progress_bar.value = (1.0 - (@steps.size / @step_count.to_f))
|
||||
puts
|
||||
|
||||
|
||||
if step.nil? && @initialization_acceptable
|
||||
after(100) do
|
||||
@@ -151,11 +151,20 @@ module W3DHubLauncher
|
||||
def step_server_list
|
||||
@initialization_container.append do
|
||||
tagline "Requesting server listing..."
|
||||
caption "Found 21 servers <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
||||
end
|
||||
|
||||
after(1000) do
|
||||
next_step
|
||||
Worker::Api.servers do |result|
|
||||
@initialization_container.append do
|
||||
if result.okay?
|
||||
MemCache[:servers] = JSON.parse(result.data).map { |s| Worker::Api::GameServer.new(s) }
|
||||
|
||||
caption "Received #{MemCache[:servers].size} servers <c=0f0>OK</c>", margin_left: LARGE_PADDING
|
||||
else
|
||||
caption "Failed to retrieve server listing.", color: 0xff_ff8800, margin_left: LARGE_PADDING
|
||||
end
|
||||
|
||||
next_step
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ module W3DHubLauncher
|
||||
def populate_game_info
|
||||
# FIXME: We should be able to query an api to figure out the installation state, instead of explictly writing it out like this...
|
||||
application = MemCache[:settings].applications.find { |app| app.id == @current_app.id }
|
||||
pp application
|
||||
# pp application
|
||||
|
||||
@game_info_container.clear do
|
||||
# logo
|
||||
@@ -117,7 +117,7 @@ module W3DHubLauncher
|
||||
button safe_get_image("#{ROOT_PATH}/media/icons/singleplayer.png"), tip: "Single player", image_height: 1.0, background_nine_slice: NINE_SLICE_SQUARE, **CTA_BUTTON_THEME
|
||||
button safe_get_image("#{ROOT_PATH}/media/icons/gear.png"), tip: "Options", image_height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED_RIGHT, **CTA_BUTTON_THEME
|
||||
elsif @current_app.servicable?
|
||||
pp @current_app
|
||||
# pp @current_app
|
||||
button "Import", enabled: false, tip: "Import existing application installation", fill: true, height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED_LEFT, **CTA_BUTTON_THEME
|
||||
button "Download", tip: "Download and install application", fill: true, height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED_RIGHT, **CTA_BUTTON_THEME
|
||||
else
|
||||
|
||||
@@ -4,45 +4,75 @@ module W3DHubLauncher
|
||||
include GuiExt
|
||||
|
||||
def setup
|
||||
@games_filter = []
|
||||
|
||||
# game bar container
|
||||
flow(width: 1.0, height: 60) do
|
||||
flow(width: 220, height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_GRAY) do
|
||||
widget(width: 220, height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_GRAY, hover: { background_nine_slice_color: ALPHA_BLACK }, active: { background_nine_slice_color: ALPHA_GRAY }) do |w|
|
||||
flow(width: 1.0, height: 40, margin_left: PADDING, v_align: :center, h_align: :center) do
|
||||
image safe_get_image("#{ROOT_PATH}/media/icons/menuGrid.png"), height: 40, color: 0xff_bbbbbb
|
||||
link "ALL SERVERS", text_size: 24, font: FONT_BLACK, height: 1.0, text_v_align: :center
|
||||
end
|
||||
|
||||
w.subscribe(:clicked_left_mouse_button) do
|
||||
@games_filter.clear
|
||||
populate_server_list
|
||||
end
|
||||
end
|
||||
|
||||
flow(fill: true, height: 1.0, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_GRAY, margin_left: PADDING) do
|
||||
image safe_get_image("#{ROOT_PATH}/data/cache/apb.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0x88_5e5c64, border_thickness_bottom: 3, border_color_bottom: 0xff_3584e4, tip: "Red Alert: A Path Beyond"
|
||||
image safe_get_image("#{ROOT_PATH}/data/cache/ren.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Command & Conquer: Renegade"
|
||||
image safe_get_image("#{ROOT_PATH}/data/cache/tsr.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Tiberian Sun: Reborn"
|
||||
image safe_get_image("#{ROOT_PATH}/data/cache/woa.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Battle for Dune: War of Assassins"
|
||||
MemCache[:applications].each do |app|
|
||||
next unless app.game?
|
||||
|
||||
image(safe_get_image("#{ROOT_PATH}/data/cache/#{app.id}.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0x88_5e5c64, border_thickness_bottom: 3, border_color_bottom: 0xff_3584e4, tip: app.name) do |btn|
|
||||
if shift_down?
|
||||
@games_filter << app.id
|
||||
@games_filter.uniq!
|
||||
else
|
||||
@games_filter.clear
|
||||
@games_filter << app.id
|
||||
end
|
||||
|
||||
populate_server_list
|
||||
end
|
||||
# image safe_get_image("#{ROOT_PATH}/data/cache/ren.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Command & Conquer: Renegade"
|
||||
# image safe_get_image("#{ROOT_PATH}/data/cache/tsr.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Tiberian Sun: Reborn"
|
||||
# image safe_get_image("#{ROOT_PATH}/data/cache/woa.png"), height: 1.0, padding: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED_TOP, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: 0, tip: "Battle for Dune: War of Assassins"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# game content container
|
||||
stack(width: 1.0, fill: true, margin_top: LARGE_PADDING, scroll: true) do
|
||||
10.times do
|
||||
widget(width: 1.0, height: 48, padding_top: HALF_PADDING, padding_bottom: HALF_PADDING, margin_bottom: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: ALPHA_GRAY, hover: { background_nine_slice_color: 0xff_5e5c64 } , active: { background_nine_slice_color: 0xaa_5e5c64 }) do
|
||||
@servers_container = stack(width: 1.0, fill: true, margin_top: LARGE_PADDING, scroll: true) do
|
||||
end
|
||||
|
||||
# FIXME: Remember and use last used filter
|
||||
populate_server_list
|
||||
end
|
||||
|
||||
def populate_server_list
|
||||
@servers_container.clear do
|
||||
(MemCache[:servers] || []).select { |s| @games_filter.empty? ? true : @games_filter.include?(s.game) }.sort_by { |s| s.player_count }.reverse.each do |server|
|
||||
app = MemCache[:applications].find { |a| a.id == server.game }
|
||||
|
||||
widget(width: 1.0, height: 48, padding_top: HALF_PADDING, padding_bottom: HALF_PADDING, margin_bottom: HALF_PADDING, background_nine_slice: NINE_SLICE_ROUNDED, background_nine_slice_from_edge: NINE_SLICE_EDGE, background_nine_slice_color: server.channel == "release" ? ALPHA_GRAY : 0xaa_c64600, hover: { background_nine_slice_color: 0xff_5e5c64 } , active: { background_nine_slice_color: 0xaa_5e5c64 }) do
|
||||
# app icon container
|
||||
image(safe_get_image("#{ROOT_PATH}/data/cache/apb.png"), tip: "Red Alert: A Path Beyond", width: 48, height: 1.0, margin_left: HALF_PADDING)
|
||||
# friend name and status container
|
||||
image(safe_get_image("#{ROOT_PATH}/data/cache/#{app.id}.png"), tip: app.name, width: 48, height: 1.0, margin_left: HALF_PADDING)
|
||||
|
||||
# server name, region, and times container
|
||||
stack(fill: true, height: 1.0, margin_left: HALF_PADDING) do
|
||||
stack(v_align: :center) do
|
||||
# server name
|
||||
server_name = ["Really Long Server Name Goes Here", "[US][W3D Hub] APB Game Night", "[US][W3D Hub] APB Co-op 3.7", "Really Long Server Name Goes Here Really Long Server Name Goes Here"].sample
|
||||
caption server_name, text_wrap: :none, tip: server_name
|
||||
caption server.name, text_wrap: :none, tip: server.name
|
||||
# server info
|
||||
a = ["North America", "South America", "Europe", "Asia", "Antarctica", "Arctica", "Oceania"]
|
||||
inscription "#{a.sample} • 13:52 / #{rand > 0.5 ? '∞' : '30:00'}", text_wrap: :none, margin_top: -HALF_PADDING
|
||||
inscription "#{server.channel} • #{server.region} • #{server.time_elapsed} / #{server.match_remaining_time}", text_wrap: :none, margin_top: -HALF_PADDING
|
||||
end
|
||||
end
|
||||
|
||||
# server map
|
||||
stack(width: 256, height: 1.0, margin_left: HALF_PADDING) do
|
||||
stack(width: 1.0, fill: true, v_align: :center) do
|
||||
server_map = ["RA_Under", "C&C Superduple Long Map Name Goes Here", "RA_NorthByNorthWest", "RA_HostileWatersParadox", "RA_PacificThreat"].sample
|
||||
caption server_map, tip: server_map, text_wrap: :none, width: 1.0, text_align: :center
|
||||
caption server.current_map, tip: server.current_map, text_wrap: :none, width: 1.0, text_align: :center
|
||||
inscription "map", text_wrap: :none, width: 1.0, text_align: :center, margin_top: -HALF_PADDING
|
||||
end
|
||||
end
|
||||
@@ -50,10 +80,11 @@ module W3DHubLauncher
|
||||
# server player count
|
||||
stack(width: 96, height: 1.0, margin_left: HALF_PADDING) do
|
||||
stack(width: 1.0, fill: true, v_align: :center) do
|
||||
caption format("%d / %d", rand(60), rand(60..127)), width: 1.0, text_align: :center
|
||||
caption format("%d / %d", server.player_count, server.max_players), width: 1.0, text_align: :center
|
||||
inscription "players", text_wrap: :none, width: 1.0, text_align: :center, margin_top: -HALF_PADDING
|
||||
end
|
||||
end
|
||||
|
||||
# server ping
|
||||
flow(width: 96, height: 1.0, margin_left: HALF_PADDING, margin_right: HALF_PADDING) do
|
||||
stack(fill: true, height: 1.0, v_align: :center) do
|
||||
|
||||
106
lib/worker.rb
106
lib/worker.rb
@@ -29,11 +29,15 @@ module W3DHubLauncher
|
||||
@requests = []
|
||||
|
||||
@settings = 0# Settings.new
|
||||
@game_servers = []
|
||||
@host_pings = {}
|
||||
|
||||
# connect to and monitor GSH web service
|
||||
@threads << Thread.new { game_server_hub_websocket }
|
||||
# connect to and monitor Backend web service
|
||||
@threads << Thread.new { backend_websocket }
|
||||
# poke game servers to ascertain their ping relative to the local machine
|
||||
@threads << Thread.new { ping_game_servers }
|
||||
|
||||
@w3dhub_api = W3DHubLauncher::W3DHubApi.new
|
||||
|
||||
@@ -45,11 +49,11 @@ module W3DHubLauncher
|
||||
json = JSON.parse(data)
|
||||
query = Request::Query.new(type: json["type"].to_sym, request_id: json["request_id"], data: json["data"])
|
||||
|
||||
pp [:server_incoming, data, query]
|
||||
# pp [:server_incoming, data, query]
|
||||
|
||||
if respond_to?(query.type)
|
||||
response = send(query.type, query)
|
||||
pp [:server_to_client, response]
|
||||
# pp [:server_to_client, response]
|
||||
payload = { status: response.status, request_id: response.request_id, data: response.result.data, error: response.result.error }.to_json
|
||||
socket.puts(payload)
|
||||
end
|
||||
@@ -68,9 +72,56 @@ module W3DHubLauncher
|
||||
def backend_websocket
|
||||
end
|
||||
|
||||
def ping_game_servers
|
||||
socket = Socket.open(Socket::AF_INET, Socket::SOCK_DGRAM, Socket::IPPROTO_ICMP)
|
||||
socket.setsockopt(:SOCKET, :TIMESTAMP, true)
|
||||
|
||||
Async do |task|
|
||||
while true
|
||||
echo_requests = {}
|
||||
|
||||
# Send pings to each unique server host
|
||||
@game_servers.map(&:address).uniq.each do |server_address|
|
||||
address = Socket.sockaddr_in(0, server_address)
|
||||
sequence_id = Digest::SHA256.hexdigest("#{server_address}-#{Time.now.iso8601}")
|
||||
|
||||
# ICMP Echo Request, ICMP Code, <checksum>, <identifier>, <sequence number>, Data
|
||||
msg = [8, 0, 0, 0, sequence_id].flatten.pack("C2n2A64")
|
||||
echo_requests[sequence_id] = { send_time: Time.now, address: server_address, replied: false }
|
||||
socket.send(msg, 0, address)
|
||||
end
|
||||
|
||||
# Receive replies until timeout
|
||||
task.with_timeout(3) do
|
||||
while(echo_requests.values.any? { |v| v[:replied] == false })
|
||||
response, sender_address, flags, *controls = socket.recvmsg(256)
|
||||
_type, _code, _identifier, _sequence, data = response.unpack("C2n2A64")
|
||||
|
||||
if (request = echo_requests[data])# && request[:address] == sender_address.
|
||||
request[:replied] = true
|
||||
round_trip_time = ((controls.last.timestamp - request[:send_time]) * 1000.0).round
|
||||
@host_pings[request[:address]] = round_trip_time
|
||||
|
||||
# puts "#{request[:address]}: #{round_trip_time}ms"
|
||||
# else
|
||||
# # packet not for us, or it got mangled in transit.
|
||||
end
|
||||
end
|
||||
rescue Async::TimeoutError
|
||||
# puts "Timed out waiting for: #{echo_requests.values.select { |v| v[:replied] == false }.map { |v| v[:address] }.join(', ')}"
|
||||
end
|
||||
|
||||
sleep 5
|
||||
end
|
||||
end
|
||||
|
||||
ensure
|
||||
socket&.close
|
||||
end
|
||||
|
||||
# Send request to server
|
||||
def request(query)
|
||||
pp [:client_request, query]
|
||||
# pp [:client_request, query]
|
||||
payload = { type: query.type, request_id: query.request_id, data: query.data }.to_json
|
||||
|
||||
if respond_to?(query.type)
|
||||
@@ -82,11 +133,11 @@ module W3DHubLauncher
|
||||
|
||||
def service
|
||||
data = @socket.read_nonblock(1_048_576) # 1 MB
|
||||
pp [:CLIENT, data]
|
||||
# pp [:CLIENT, data]
|
||||
json = JSON.parse(data)
|
||||
request = W3DHubLauncher::Worker::Request.requests.find { |r| r.request_id == json["request_id"] }
|
||||
|
||||
pp [json, request]
|
||||
# pp [json, request]
|
||||
return unless request
|
||||
|
||||
CyberarmEngine::Window.instance&.add_to_queue(proc {
|
||||
@@ -101,7 +152,7 @@ module W3DHubLauncher
|
||||
|
||||
def deliver_response(result, query)
|
||||
response = Response.new(result.okay? ? Request::STATUS_COMPLETE : Request::STATUS_ERROR, query.request_id, result)
|
||||
pp response
|
||||
# pp response
|
||||
|
||||
response
|
||||
end
|
||||
@@ -110,12 +161,33 @@ module W3DHubLauncher
|
||||
# ------------ query / request handlers ------------
|
||||
#
|
||||
def fetch_url(query)
|
||||
result = CyberarmEngine::Result.new
|
||||
|
||||
method = query.data["method"]
|
||||
url = query.data["url"]
|
||||
headers = query.data["headers"] || DEFAULT_HEADERS
|
||||
body = query.data["body"]
|
||||
|
||||
Sync do |task|
|
||||
task.with_timeout(DEFAULT_NETWORK_TIMEOUT) do
|
||||
Async::HTTP::Internet.send(method, url, headers, body) do |response|
|
||||
if response.success?
|
||||
result.data = response.read
|
||||
end
|
||||
# rescue StandardError => e
|
||||
# result.error = e
|
||||
end
|
||||
# rescue Async::TimeoutError
|
||||
# result.error = e
|
||||
end
|
||||
end
|
||||
|
||||
deliver_response(result, query)
|
||||
end
|
||||
|
||||
def download_url(query)
|
||||
result = CyberarmEngine::Result.new
|
||||
|
||||
|
||||
method = query.data["method"]
|
||||
url = query.data["url"]
|
||||
path = query.data["path"]
|
||||
@@ -218,6 +290,26 @@ module W3DHubLauncher
|
||||
deliver_response(result, query)
|
||||
end
|
||||
|
||||
def servers(query)
|
||||
hash = {
|
||||
"method" => "get",
|
||||
"url" => "https://gsh.w3d.cyberarm.dev/listings/getAll/v2?statusLevel=2",
|
||||
"headers" => DEFAULT_HEADERS,
|
||||
"body" => query.data["body"]
|
||||
}
|
||||
|
||||
# inject data into query
|
||||
query = Request::Query.new(query.type, query.request_id, hash)
|
||||
|
||||
response = fetch_url(query)
|
||||
|
||||
if response.result.okay?
|
||||
@game_servers = JSON.parse(response.result.data).map { |server| Worker::Api::GameServer.new(server) }
|
||||
end
|
||||
|
||||
deliver_response(response.result, query)
|
||||
end
|
||||
|
||||
def install_application(query)
|
||||
end
|
||||
|
||||
|
||||
@@ -44,7 +44,8 @@ module W3DHubLauncher
|
||||
end
|
||||
|
||||
# returns current list of servers as reported from GSH / cache
|
||||
def self.servers
|
||||
def self.servers(&block)
|
||||
Worker::Request.new(:servers, "", &block)
|
||||
end
|
||||
|
||||
# returns news for application
|
||||
|
||||
@@ -32,6 +32,10 @@ module W3DHubLauncher
|
||||
"ff_#{extended_data("colour","#000000").sub("#", "")}".to_i(16)
|
||||
end
|
||||
|
||||
def game?
|
||||
@type.include?("game")
|
||||
end
|
||||
|
||||
def servicable?
|
||||
@servicable
|
||||
end
|
||||
|
||||
@@ -3,7 +3,10 @@ module W3DHubLauncher
|
||||
class Api
|
||||
# Designed to work with cyberarm's Game Server Hub service data, may work with W3D Hub's service with degraded metadata.
|
||||
class GameServer
|
||||
attr_reader :id, :game, :channel, :address, :port, :region, :version
|
||||
attr_reader :id, :game, :channel, :address, :port, :region,
|
||||
:name, :password, :current_map, :player_count, :max_players,
|
||||
:match_start_time, :estimated_end_time, :match_remaining_time,
|
||||
:teams, :next_map, :version
|
||||
|
||||
def initialize(data)
|
||||
# main data
|
||||
@@ -43,6 +46,15 @@ module W3DHubLauncher
|
||||
@version = data["version"] || "838"
|
||||
@next_map = data["status"]["nextmap"] || ""
|
||||
end
|
||||
|
||||
# TODO: implement :)
|
||||
def time_elapsed
|
||||
"00:00"
|
||||
end
|
||||
|
||||
def password?
|
||||
@password
|
||||
end
|
||||
end
|
||||
|
||||
class Team
|
||||
|
||||
@@ -52,7 +52,7 @@ module W3DHubLauncher
|
||||
|
||||
# event from Worker received
|
||||
def handle_event(event, data)
|
||||
pp [event, data]
|
||||
# pp [event, data]
|
||||
|
||||
case event
|
||||
when STATUS_ERROR, STATUS_COMPLETE
|
||||
|
||||
@@ -36,7 +36,7 @@ module W3DHubLauncher
|
||||
if response.success?
|
||||
result.data = response.read
|
||||
else
|
||||
pp response
|
||||
# pp response
|
||||
result.error = true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ require "socket"
|
||||
require "rexml"
|
||||
require "base64"
|
||||
require "logger"
|
||||
require "digest"
|
||||
|
||||
require "async"
|
||||
require "async/http/internet/instance"
|
||||
|
||||
Reference in New Issue
Block a user