local r=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local N=game:GetService("\080\108\097\121\101\114\115")local j=game:GetService("\084\101\108\101\112\111\114\116\083\101\114\118\105\099\101")local E=require(script.Parent.Parent.packages.fusion)local L=script.Parent.Parent.components local q=require(L.window.button)local v=require(script.Parent.Parent.components.window.window)local J=require(script.Parent.Parent.utils.request)local V=game.PlaceId return{name="\115\101\114\118\101\114\115",description="\086\105\101\119 \097 \108\105\115\116 \111\102 \115\101\114\118\101\114\115 \102\111\114 \116\104\101 \099\117\114\114\101\110\116 \103\097\109\101\046",arguments={},callback=function()local L={};((J("\104\116\116\112\115\058\047\047\103\097\109\101\115\046\114\111\098\108\111\120\046\099\111\109\047\118\049\047\103\097\109\101\115\047"..(V.."\047\115\101\114\118\101\114\115\047\080\117\098\108\105\099\063\115\111\114\116\079\114\100\101\114\061\065\115\099\038\108\105\109\105\116\061\053\048"))):andThen(function(N)local j=r:JSONDecode(N)for r,N in j.data do if type(N)=="\116\097\098\108\101"and N.playing<N.maxPlayers then table.insert(L,{id=N.id,ping=N.ping})end end end)):finally(function()v({title="\083\101\114\118\101\114\115",size=UDim2.fromOffset(345,394)},{q({text="\074\111\105\110 \082\097\110\100\111\109",secondary=true;callback=function()j:TeleportToPlaceInstance(V,L[math.random(1,#L)].id,N.LocalPlayer)end}),E.ForPairs(L,function(r,E)print(r,E)return r,q({text="\083\101\114\118\101\114 \073\068\058 "..E.id,subtext="\080\105\110\103\058 "..(E.ping.." \109\115"),clickable=true,callback=function()j:TeleportToPlaceInstance(V,E.id,N.LocalPlayer)end})end,E.cleanup)})end)end}
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")

local Fusion = require(script.Parent.Parent.packages.fusion)
local Components = script.Parent.Parent.components

local Button = require(Components.window.button)
local Window = require(script.Parent.Parent.components.window.window)

local request = require(script.Parent.Parent.utils.request)
local placeId = game.PlaceId

return {
	name = "servers",
	description = "View a list of servers for the current game.",
	arguments = {},

	callback = function()
		local servers = {}

		request("https://games.roblox.com/v1/games/" .. placeId .. "/servers/Public?sortOrder=Asc&limit=50")
			:andThen(function(response)
				local body = HttpService:JSONDecode(response)

				for _, server in body.data do
					if type(server) == "table" and server.playing < server.maxPlayers then
						table.insert(servers, {
							id = server.id,
							ping = server.ping,
						})
					end
				end
			end)
			:finally(function()
				Window({
					title = "Servers",
					size = UDim2.fromOffset(345, 394),
				}, {
					Button({
						text = "Join Random",
						secondary = true,
						callback = function()
							TeleportService:TeleportToPlaceInstance(
								placeId,
								servers[math.random(1, #servers)].id,
								Players.LocalPlayer
							)
						end,
					}),

					Fusion.ForPairs(servers, function(index, server)
						print(index, server)
						return index,
							Button({
								text = "Server ID: " .. server.id,
								subtext = "Ping: " .. server.ping .. " ms",
								clickable = true,
								callback = function()
									TeleportService:TeleportToPlaceInstance(placeId, server.id, Players.LocalPlayer)
								end,
							})
					end, Fusion.cleanup),
				})
			end)
	end,
}
