Skip to content

Commit

Permalink
Update csrp_server.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
harleykradovill authored Sep 12, 2024
1 parent 16653e9 commit b5d241d
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions csrp_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Make a GET request to receive user's currently selected call, and set a GPS rout
it's postal code.
--]]
RegisterServerEvent('fetchIncidentDetails')
AddEventHandler('fetchIncidentDetails', function()
local playerSource = source
local playerIdentifiers = GetPlayerIdentifiers(source)
AddEventHandler('fetchIncidentDetails', function(playerid)
local playerSource = playerid or source
local playerIdentifiers = GetPlayerIdentifiers(playerSource)
local discordId = nil

for _, identifier in pairs(playerIdentifiers) do
Expand Down Expand Up @@ -212,6 +212,38 @@ SetHttpHandler(function(request, response)
response.send(json.encode({ status = "error", message = "'discordid' or 'codes' key not found" }))
end
end)

elseif request.path == "/initiateRti" then
request.setDataHandler(function(data)
local success, parsedData = pcall(json.decode, data)
if not success then
print("Error decoding JSON: " .. parsedData)
response.writeHead(400, { ['Content-Type'] = 'application/json' })
response.send(json.encode({ status = "error", message = "Invalid JSON" }))
return
end

if parsedData and parsedData.discordid then
local discordid = parsedData.discordid

for _, playerId in ipairs(GetPlayers()) do
local identifiers = GetPlayerIdentifiers(playerId)
for _, id in pairs(identifiers) do
if id:find("discord:") and id:sub(9) == discordid then
TriggerEvent('fetchIncidentDetails', playerId)
break
end
end
end

response.writeHead(200, { ['Content-Type'] = 'application/json' })
response.send(json.encode({ status = "success", message = "RTI initiated successfully." }))
else
print("Error: 'discordid' key not found in the data.")
response.writeHead(400, { ['Content-Type'] = 'application/json' })
response.send(json.encode({ status = "error", message = "'discordid' key not found" }))
end
end)
else
response.writeHead(404, { ['Content-Type'] = 'application/json' })
response.send(json.encode({ status = "error", message = "Route not found" }))
Expand Down

0 comments on commit b5d241d

Please sign in to comment.