const script = `
--[[
pcall(function() game:GetService("InsertService"):SetBaseSetsUrl("http://www.rowblx.xyz/stamper/inseasset.php?nsets=10&type=base%22") end)
pcall(function() game:GetService("InsertService"):SetUserSetsUrl("http://www.rowblx.xyz/stamper/inseasset.php?nsets=20&type=user&userid=%d%22") end)
pcall(function() game:GetService("InsertService"):SetCollectionUrl("http://www.rowblx.xyz/stamper/inseasset.php?sid=%d%22") end)
pcall(function() game:GetService("InsertService"):SetAssetUrl("http://www.rowblx.xyz/asset/?id=%d") end)
pcall(function() game:GetService("InsertService"):SetAssetVersionUrl("http://www.rowblx.xyz/asset/?id=%d") end)
pcall(function() game:GetService("SocialService"):SetGroupUrl("http://10char.xyz/stamper/groupurl.php?method=IsInGroup&playerid=%d&groupid=%d%22") end)
pcall(function() game:GetService("BadgeService"):SetPlaceId(-1) end)
pcall(function() game:GetService("BadgeService"):SetIsBadgeLegalUrl("") end)
pcall(function() game:GetService("ScriptInformationProvider"):SetAssetUrl("http://www.rowblx.xyz/asset/") end)
pcall(function() game:GetService("ContentProvider"):SetBaseUrl("http://www.rowblx.xyz/stamper/stampericons.php?%22) end)
]]--

-- functions --------------------------
function onPlayerAdded(player)
   -- override
end

pcall(function() game:SetPlaceID(-1, false) end)

local startTime = tick()
local connectResolved = false
local loadResolved = false
local joinResolved = false
local playResolved = true
local playStartTime = 0

local cdnSuccess = 0
local cdnFailure = 0

settings()["Game Options"].CollisionSoundEnabled = true
pcall(function() settings().Rendering.EnableFRM = true end)
pcall(function() settings().Physics.Is30FpsThrottleEnabled = false end)
pcall(function() settings()["Task Scheduler"].PriorityMethod = Enum.PriorityMethod.AccumulatedError end)
pcall(function() settings().Physics.PhysicsEnvironmentalThrottle = Enum.EnviromentalPhysicsThrottle.DefaultAuto end)

local threadSleepTime = ...

if threadSleepTime==nil then
   threadSleepTime = 15
end

local test = true

local closeConnection = game.Close:connect(function() 
   if 0 then
       if not connectResolved then
           local duration = tick() - startTime;
       elseif (not loadResolved) or (not joinResolved) then
           local duration = tick() - startTime;
           if not loadResolved then
               loadResolved = true
           end
           if not joinResolved then
               joinResolved = true
           end
       elseif not playResolved then
           playResolved = true
       end
   end
end)

game:GetService("ChangeHistoryService"):SetEnabled(false)
game:GetService("ContentProvider"):SetThreadPool(16)
game:GetService("InsertService"):SetBaseSetsUrl("http://www.rowblx.xyz/Game/Tools/InsertAsset.ashx?nsets=10&type=base")
game:GetService("InsertService"):SetUserSetsUrl("http://www.rowblx.xyz/Game/Tools/InsertAsset.ashx?nsets=20&type=user&userid=%d")
game:GetService("InsertService"):SetCollectionUrl("http://www.rowblx.xyz/Game/Tools/InsertAsset.ashx?sid=%d")
game:GetService("InsertService"):SetAssetUrl("http://www.rowblx.xyz/asset/?id=%d")
game:GetService("InsertService"):SetAssetVersionUrl("http://www.rowblx.xyz/Asset/?assetversionid=%d")

pcall(function() game:GetService("SocialService"):SetFriendUrl("http://www.rowblx.xyz/Game/LuaWebService/HandleSocialRequest.ashx?method=IsFriendsWith&playerid=%d&userid=%d") end)
pcall(function() game:GetService("SocialService"):SetBestFriendUrl("http://www.rowblx.xyz/Game/LuaWebService/HandleSocialRequest.ashx?method=IsBestFriendsWith&playerid=%d&userid=%d") end)
pcall(function() game:GetService("SocialService"):SetGroupUrl("http://www.rowblx.xyz/Game/LuaWebService/HandleSocialRequest.ashx?method=IsInGroup&playerid=%d&groupid=%d") end)
pcall(function() game:GetService("SocialService"):SetGroupRankUrl("http://www.rowblx.xyz/Game/LuaWebService/HandleSocialRequest.ashx?method=GetGroupRank&playerid=%d&groupid=%d") end)
pcall(function() game:GetService("SocialService"):SetGroupRoleUrl("http://www.rowblx.xyz/Game/LuaWebService/HandleSocialRequest.ashx?method=GetGroupRole&playerid=%d&groupid=%d") end)
pcall(function() game:GetService("GamePassService"):SetPlayerHasPassUrl("http://rowblx.xyz/Game/GamePass/GamePassHandler.ashx?Action=HasPass&UserID=%d&PassID=%d") end)
pcall(function() game:GetService("MarketplaceService"):SetProductInfoUrl("https://api.rowblx.xyz/marketplace/productinfo?assetId=%d") end)
pcall(function() game:GetService("MarketplaceService"):SetPlayerOwnsAssetUrl("https://api.rowblx.xyz/ownership/hasasset?userId=%d&assetId=%d") end)
pcall(function() game:SetCreatorID(0, Enum.CreatorType.User) end)

pcall(function() game:GetService("Players"):SetChatStyle(Enum.ChatStyle.ClassicAndBubble) end)

local waitingForCharacter = false

pcall( function()
   if settings().Network.MtuOverride == 0 then
     settings().Network.MtuOverride = 1400
   end
end)


client = game:GetService("NetworkClient")
visit = game:GetService("Visit")

function setMessage(message)
   -- todo: animated "..."
   if not false then
       game:SetMessage(message)
   else
       -- hack, good enought for now
       game:SetMessage("Teleporting ...")
   end
end

function showErrorWindow(message, errorType, errorCategory)
   game:SetMessage(message)
end

-- called when the client connection closes
function onDisconnection(peer, lostConnection)
   if lostConnection then
       showErrorWindow("You have lost connection", "LostConnection", "LostConnection")
   else
       showErrorWindow("This game has been shutdown", "Kick", "Kick")
   end
end

function requestCharacter(replicator)
   
   -- prepare code for when the Character appears
   local connection
   connection = player.Changed:connect(function (property)
       if property=="Character" then
           game:ClearMessage()
           waitingForCharacter = false
           
           connection:disconnect()
       
           if 0 then
               if not joinResolved then
                   local duration = tick() - startTime;
                   joinResolved = true
                   
                   playStartTime = tick()
                   playResolved = false
               end
           end
       end
   end)
   
   setMessage("Requesting character")
   
   local success, err = pcall(function()	
       replicator:RequestCharacter()
       setMessage("Waiting for character")
       waitingForCharacter = true
   end)
end

function onConnectionAccepted(url, replicator)
   connectResolved = true

   local waitingForMarker = true
   
   local success, err = pcall(function()	
       if not test then 
           visit:SetPing("", 300) 
       end
       
       if not false then
           game:SetMessageBrickCount()
       else
           setMessage("Teleporting ...")
       end

       replicator.Disconnection:connect(onDisconnection)
       
       local marker = replicator:SendMarker()
       
       marker.Received:connect(function()
           waitingForMarker = false
           requestCharacter(replicator)
       end)
   end)
   
   if not success then
       return
   end
   
   while waitingForMarker do
       workspace:ZoomToExtents()
       wait(0.5)
   end
end

-- called when the client connection fails
function onConnectionFailed(_, error)
   showErrorWindow("Failed to connect to the Game. (ID=" .. error .. ")", "ID" .. error, "Other")
end

-- called when the client connection is rejected
function onConnectionRejected()
   connectionFailed:disconnect()
   showErrorWindow("This game is not available. Please try another", "WrongVersion", "WrongVersion")
end

pcall(function() settings().Diagnostics:LegacyScriptMode() end)
local success, err = pcall(function()	

   game:SetRemoteBuildMode(true)
   
   setMessage("Connecting to Server")
   client.ConnectionAccepted:connect(onConnectionAccepted)
   client.ConnectionRejected:connect(onConnectionRejected)
   connectionFailed = client.ConnectionFailed:connect(onConnectionFailed)
   client.Ticket = ""
   
   playerConnectSucces, player = pcall(function() return client:PlayerConnect({id}, "{ip}", {port}, 0, threadSleepTime) end)

   player:SetSuperSafeChat(false)
   pcall(function() player:SetUnder13(false) end)
   pcall(function() player:SetMembershipType(Enum.MembershipType.{membership}) end)
   pcall(function() player:SetAccountAge(365) end)
   player.Idled:connect(onPlayerIdled)
   
   -- Overriden
   onPlayerAdded(player)
   
   -- player.CharacterAppearance = "http://www.rowblx.xyz/charapp/Custom.php?pants=<?php echo $pant; ?>&shirt=<?php echo $shirt; ?>&hat=<?php echo $hat; ?>&face=<?php echo $face; ?>"	
   if not test then visit:SetUploadUrl("")end
       player.Name = "{username}"
       
end)

pcall(function() game:SetScreenshotInfo("") end)`;

import signer from "./signer";

export default function (id, username, ip, port, membership) {
	let joinscript = script
		.replace("{id}", id)
		.replace("{username}", username)
		.replace("{ip}", ip)
		.replace("{port}", port)
		.replace("{membership}", membership);

	let sig = signer(joinscript);
	joinscript = `--rbxsig%${sig}%${joinscript}`;

	return joinscript;
}
