-- @outline // RUNNERS


function Ping.Update()
	if tick() - LastUpdate < REQUEST_INTERVAL then return end
	
	LastUpdate = tick()
	for _,v in pairs(game.Players:GetChildren()) do
		Ping.UpdatePlayer(v)
	end
end

function Ping.Init()
	System.Remotes.ConnectEvent("Ping", function(plr, Code)
		if type(Code) ~= "number" or not Pool[plr] or Pool[plr].Code == -1 or Pool[plr].Code ~= Code then return end
		
		Pool[plr].Value = tick() - Pool[plr].Time
		Pool[plr].Code = -1
	end)
	
	game.Players.PlayerAdded:Connect(function(plr)
		Pool[plr] = {
			Code = -1,
			Value = 0,
			Time = tick()
		}
	end)
	
	game.Players.PlayerRemoving:Connect(function(plr)
		Pool[plr] = nil
	end)
end

function Ping.ClientInit()
	Pool[game.Players.LocalPlayer] = {Value = 0}
	System.Remotes.ConnectEvent("Ping", function(tbl)
		Pool[game.Players.LocalPlayer] = {Value = tbl.Value}
		
		System.Remotes.FireServer("Ping", tbl.Code)
	end)
end

return Ping
