355 lines
12 KiB
Lua
355 lines
12 KiB
Lua
--[[
|
|
Imperial Export for FiveM
|
|
DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOUR DOING!
|
|
]]--
|
|
|
|
local function checkConvar(name, description)
|
|
local value = GetConvar(name, "")
|
|
if not value or value == "" then
|
|
error(string.format("Could not find required Convar '%s' for %s.", name, description))
|
|
end
|
|
end
|
|
|
|
checkConvar("imperial_community_id", "ImperialCAD Community ID")
|
|
checkConvar("imperialAPI", "Imperial API key")
|
|
|
|
local function performAPIRequest(url, method, data, headers, callback)
|
|
method = method:upper()
|
|
|
|
if method == "GET" then
|
|
data = ""
|
|
else
|
|
data = json.encode(data)
|
|
end
|
|
|
|
PerformHttpRequest(url, function(errorCode, resultData, resultHeaders, errorData)
|
|
if errorCode ~= 200 then
|
|
|
|
if errorCode == 500 then
|
|
callback(false, "^1[IMPERIAL_API_CALLBACK]^7 request failed: Temporary Internal Error, Unable to retreive CAD data.")
|
|
return
|
|
end
|
|
|
|
if Config.debug then
|
|
print("^1[IMPERIAL_API_ERROR]^7 HTTP Error Code:", errorCode)
|
|
if errorData and errorData ~= "" then
|
|
print("^1[IMPERIAL_API_ERROR]^7 Response: " .. errorData)
|
|
elseif resultData and resultData ~= "" then
|
|
print("^1[IMPERIAL_API_ERROR]^7 Result Data (fallback): " .. resultData)
|
|
end
|
|
end
|
|
|
|
if callback then
|
|
if errorData and errorData ~= "" then
|
|
local jsonPart = errorData:match("{.*}")
|
|
if jsonPart then
|
|
callback(false, jsonPart)
|
|
else
|
|
callback(false, errorData)
|
|
end
|
|
else
|
|
callback(false, "^1[IMPERIAL_API_CALLBACK]^7 request failed: No response data")
|
|
end
|
|
end
|
|
|
|
return
|
|
end
|
|
|
|
local status
|
|
if resultData and resultData ~= "" then
|
|
local ok, body = pcall(json.decode, resultData)
|
|
if ok and type(body) == "table" then
|
|
status = body.status -- "error", "RATE_LIMIT", "success", "BUSY", "LIMIT_REACHED", "USER_LIMIT", "RESERVED"
|
|
end
|
|
end
|
|
|
|
if status and status ~= "success" then
|
|
if Config.debug then
|
|
print("^1[IMPERIAL_API_STATUS]^7 Non-success status in 200 response:", status)
|
|
print("^1[IMPERIAL_API_STATUS]^7 Raw Result Data:", resultData)
|
|
end
|
|
|
|
if callback then
|
|
callback(false, resultData or ("Status: " .. tostring(status)))
|
|
end
|
|
|
|
return
|
|
end
|
|
|
|
if callback then
|
|
if resultData and resultData ~= "" then
|
|
callback(true, resultData)
|
|
else
|
|
callback(true, "^1[IMPERIAL_API_CALLBACK]^7 request succeeded, But No response data was returned")
|
|
end
|
|
end
|
|
|
|
if Config.debug then
|
|
print("^1[IMPERIAL_API_DEBUG]^7 Result Data: " .. tostring(resultData))
|
|
end
|
|
|
|
end, method, data, headers)
|
|
end
|
|
|
|
function NewCharacter(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
users_discordID = data.users_discordID,
|
|
Fname = data.Fname,
|
|
Mname = data.Mname,
|
|
Lname = data.Lname,
|
|
Birthdate = data.Birthdate,
|
|
gender = data.gender,
|
|
race = data.race,
|
|
hairC = data.hairC,
|
|
eyeC = data.eyeC,
|
|
height = data.height,
|
|
weight = data.weight,
|
|
postal = data.postal,
|
|
address = data.address,
|
|
city = data.city,
|
|
county = data.county,
|
|
phonenum = data.phonenum,
|
|
dlstatus = data.dlstatus,
|
|
citizenid = data.citizenid
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/NewCharacter", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print("[ImperialExport] Attemping to create a new civilian CAD character!")
|
|
end
|
|
end
|
|
|
|
function NewCharacterAdvanced(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
Fname = data.Fname,
|
|
Mname = data.Mname,
|
|
Lname = data.Lname,
|
|
Birthdate = data.Birthdate,
|
|
gender = data.gender,
|
|
race = data.race,
|
|
hairC = data.hairC,
|
|
eyeC = data.eyeC,
|
|
height = data.height,
|
|
weight = data.weight,
|
|
postal = data.postal,
|
|
address = data.address,
|
|
city = data.city,
|
|
county = data.county,
|
|
state = data.state,
|
|
phonenum = data.phonenum,
|
|
licensedetails = {
|
|
hasBoatLic = data.licensedetails.hasBoatLic,
|
|
hasCDL = data.licensedetails.hasCDL,
|
|
CDLNumber = data.licensedetails.CDLNumber,
|
|
CDLStatus = data.licensedetails.CDLStatus,
|
|
hasDL = data.licensedetails.hasDL,
|
|
DLNumber = data.licensedetails.DLNumber,
|
|
DLStatus = data.licensedetails.DLStatus,
|
|
hasFirearmsCertification = data.licensedetails.hasFirearmsCertification,
|
|
hasFishLic = data.licensedetails.hasFishLic,
|
|
hasHuntLic = data.licensedetails.hasHuntLic
|
|
},
|
|
misc = {
|
|
missing = data.misc.missing
|
|
}
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/NewAdvancedCharacter", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print("[ImperialExport] Attemping to create a new civilian CAD character!")
|
|
end
|
|
end
|
|
|
|
function DeleteCharacter(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
users_discordID = data.users_discordID,
|
|
citizenid = data.citizenid
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/DeleteCharacter", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print("[ImperialExport] Attemping to delete civilian CAD character!")
|
|
end
|
|
end
|
|
|
|
function GetCharacter(charid, commId, callback)
|
|
|
|
if not charid or charid == "" then
|
|
print("Invalid Character ID. It must not be empty.")
|
|
if callback then callback(false, "Invalid Character ID") end
|
|
return
|
|
end
|
|
|
|
if not commId or commId == "" then
|
|
print("Invalid Community ID. It must not be empty.")
|
|
if callback then callback(false, "Invalid Community ID") end
|
|
return
|
|
end
|
|
|
|
local url = string.format(
|
|
"http://imperialcad.app/api/1.1/wf/GetCharacter?charid=%s&commId=%s",
|
|
charid,
|
|
commId
|
|
)
|
|
|
|
performAPIRequest(url, "GET", nil, nil, function(success, response)
|
|
if response then
|
|
local data = json.decode(response)
|
|
if data.status == "success" then
|
|
print("Character retrieved: " .. json.encode(data))
|
|
if callback then callback(true, data) end
|
|
else
|
|
print("Character not found.")
|
|
if callback then callback(false, "Character not found.") end
|
|
end
|
|
else
|
|
print("API Request Failed: " .. response)
|
|
if callback then callback(false, response) end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function GetCharacterAdvanced(Character, callback)
|
|
|
|
local firstname = Character.firstname
|
|
local lastname = Character.lastname
|
|
local commId = GetConvar("imperial_community_id", "")
|
|
|
|
if not firstname or not lastname then
|
|
print("Invalid Character table. It must not be empty and must contain a firstname and lastname.")
|
|
if callback then callback(false, "Invalid table") end
|
|
return
|
|
end
|
|
|
|
if not commId or commId == "" then
|
|
print("Invalid Community ID. It must not be empty.")
|
|
if callback then callback(false, "Invalid Community ID") end
|
|
return
|
|
end
|
|
|
|
local url = string.format(
|
|
"http://imperialcad.app/api/1.1/wf/GetCharacterAdvanced?firstname=%s&lastname=%s&commId=%s",
|
|
firstname,
|
|
lastname,
|
|
commId
|
|
)
|
|
|
|
performAPIRequest(url, "GET", nil, nil, function(success, response)
|
|
if response then
|
|
local data = json.decode(response)
|
|
if data.status == "success" then
|
|
print("[ADVANCED] Character retrieved: " .. json.encode(data.response))
|
|
if callback then callback(true, data) end
|
|
elseif data.status == "error" then
|
|
print("[ADVANCED] Character not found.")
|
|
if callback then callback(true, data) end
|
|
else
|
|
print("[ADVANCED] Character not found, Not an expected result")
|
|
if callback then callback(false, "Character not found.") end
|
|
end
|
|
else
|
|
print("API Request Failed: " .. response)
|
|
if callback then callback(false, response) end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function CreateVehicle(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
ssn = data.ssn,
|
|
vehicleModel = data.model,
|
|
plate = data.plate,
|
|
year = data.year or "2015",
|
|
make = data.make or "UNKNOWN",
|
|
color = data.color
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/registerVehicle", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print("[ImperialExport] Attemping to reigster " .. data.plate .. " to CAD!")
|
|
end
|
|
end
|
|
|
|
function SetActiveCiv(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
users_discordID = data.users_discordID,
|
|
ssn = data.ssn
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/setActiveCivilian", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print(string.format("[ImperialExport] Attemping to set ssn %s as active civ for %s in CAD!", data.ssn, data.users_discordID))
|
|
end
|
|
end
|
|
|
|
function CreateVehicleAdvanced(data, callback)
|
|
local requestData = {
|
|
commId = GetConvar("imperial_community_id", ""),
|
|
vehicleData = {
|
|
plate = data.vehicleData.plate,
|
|
model = data.vehicleData.model,
|
|
Make = data.vehicleData.Make,
|
|
color = data.vehicleData.color,
|
|
year = data.vehicleData.year,
|
|
regState = data.vehicleData.regState,
|
|
regStatus = data.vehicleData.regStatus,
|
|
regExpDate = data.vehicleData.regExpDate,
|
|
vin = data.vehicleData.vin,
|
|
stolen = data.vehicleData.stolen
|
|
},
|
|
vehicleInsurance = {
|
|
hasInsurance = data.vehicleInsurance.hasInsurance,
|
|
insuranceStatus = data.vehicleInsurance.insuranceStatus,
|
|
insurancePolicyNum = data.vehicleInsurance.insurancePolicyNum
|
|
},
|
|
vehicleOwner = {
|
|
ownerSSN = data.vehicleOwner.ownerSSN,
|
|
ownerFirstName = data.vehicleOwner.ownerFirstName,
|
|
ownerLastName = data.vehicleOwner.ownerLastName,
|
|
ownerGender = data.vehicleOwner.ownerGender,
|
|
ownerAddress = data.vehicleOwner.ownerAddress,
|
|
ownerCity = data.vehicleOwner.ownerCity
|
|
}
|
|
}
|
|
local headers = {
|
|
["Content-Type"] = "application/json",
|
|
["APIKEY"] = GetConvar("imperialAPI", "")
|
|
}
|
|
performAPIRequest("https://imperialcad.app/api/1.1/wf/registeradvancedvehicle", "POST", requestData, headers, callback)
|
|
|
|
if Config.debug then
|
|
print("[ImperialExport_AdvancedVehReg] Attemping to register " .. requestData.vehicleData.plate .. " to CAD!")
|
|
end
|
|
end
|
|
|
|
exports('GetCharacter', GetCharacter)
|
|
exports('NewCharacter', NewCharacter)
|
|
exports('DeleteCharacter', DeleteCharacter)
|
|
exports('CreateVehicle', CreateVehicle)
|
|
exports('CreateVehicleAdvanced', CreateVehicleAdvanced)
|
|
exports('SetActiveCiv', SetActiveCiv) |