Skip to content

Instantly share code, notes, and snippets.

@DemmyDemon
Created December 15, 2023 19:50
Show Gist options
  • Save DemmyDemon/db29108fdad5a9869ca84155f3f94745 to your computer and use it in GitHub Desktop.
Save DemmyDemon/db29108fdad5a9869ca84155f3f94745 to your computer and use it in GitHub Desktop.
HTTP request handler to get player locations
local function handler(request, response)
local key = GetConvar("playerLocationKey", "")
if key ~= "" then
if request.headers.Authorization == nil or request.headers.Authorization ~= "Bearer " .. key then
response.writeHead(403, {["Content-Type"] = "text/plain"})
response.send("Invalid Bearer Token")
return
end
end
local peerMatch = GetConvar("playerLocationPeer", "")
if peerMatch ~= "" then
local mustMatch = "^" .. peerMatch .. ":[0-9]+$"
local peer = request.address
print(mustMatch, peer)
if not string.match(peer, mustMatch) then
response.writeHead(403, {["Content-Type"] = "text/plain"})
response.send("Invalid Peer")
return
end
end
local locations = {}
for _, playerId in ipairs(GetPlayers()) do
local coords = GetEntityCoords(GetPlayerPed(playerId))
table.insert(locations, {coords.x, coords.y})
end
response.writeHead(200, {["Content-Type"] = "application/json"})
response.send(json.encode(locations))
end
SetHttpHandler(handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment