94 lines
2.9 KiB
Lua
94 lines
2.9 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|
local __TS__ArrayIsArray = ____lualib.__TS__ArrayIsArray
|
|
local ____exports = {}
|
|
local ____api_helper = require("api_helper")
|
|
local encodeApiBody = ____api_helper.encodeApiBody
|
|
local setApiHeaders = ____api_helper.setApiHeaders
|
|
local ____server_config = require("server_config")
|
|
local SERVER_CONFIG = ____server_config.SERVER_CONFIG
|
|
local function decodeBody(self, body, fallback)
|
|
do
|
|
local ____try, ____hasReturned, ____returnValue = pcall(function()
|
|
local parsed = {json.decode(body)}
|
|
if __TS__ArrayIsArray(parsed) and #parsed > 0 then
|
|
return true, parsed[1]
|
|
end
|
|
if parsed and type(parsed) == "table" then
|
|
return true, parsed
|
|
end
|
|
end)
|
|
if ____try and ____hasReturned then
|
|
return ____returnValue
|
|
end
|
|
end
|
|
return fallback
|
|
end
|
|
function ____exports.getSteamIdForApi(self, playerId)
|
|
local steamId = PlayerResource:GetSteamAccountID(playerId)
|
|
if not steamId or steamId <= 0 then
|
|
return nil
|
|
end
|
|
return steamId
|
|
end
|
|
function ____exports.loadEquipmentStateFromApi(self, playerId, onDone)
|
|
local steamId = ____exports.getSteamIdForApi(nil, playerId)
|
|
if not steamId then
|
|
onDone(nil, nil)
|
|
return
|
|
end
|
|
local req = CreateHTTPRequest(
|
|
"GET",
|
|
((SERVER_CONFIG.API_URL .. "/player/") .. tostring(steamId)) .. "/equipment"
|
|
)
|
|
setApiHeaders(nil, req)
|
|
req:Send(function(res)
|
|
if res.StatusCode >= 200 and res.StatusCode < 300 then
|
|
local parsed = decodeBody(
|
|
nil,
|
|
tostring(res.Body or ""),
|
|
{}
|
|
)
|
|
if parsed.equipment then
|
|
onDone(nil, parsed.equipment)
|
|
else
|
|
onDone(nil, parsed)
|
|
end
|
|
return
|
|
end
|
|
onDone(nil, nil)
|
|
end)
|
|
end
|
|
function ____exports.saveEquipmentStateToApi(self, playerId, state)
|
|
local steamId = ____exports.getSteamIdForApi(nil, playerId)
|
|
if not steamId then
|
|
return
|
|
end
|
|
local req = CreateHTTPRequest(
|
|
"PUT",
|
|
((SERVER_CONFIG.API_URL .. "/player/") .. tostring(steamId)) .. "/equipment"
|
|
)
|
|
setApiHeaders(nil, req)
|
|
req:SetHTTPRequestRawPostBody(
|
|
"application/json",
|
|
encodeApiBody(nil, {equipment = state})
|
|
)
|
|
req:Send(function() return nil end)
|
|
end
|
|
function ____exports.postEquipmentDropToApi(self, playerId, item)
|
|
local steamId = ____exports.getSteamIdForApi(nil, playerId)
|
|
if not steamId then
|
|
return
|
|
end
|
|
local req = CreateHTTPRequest(
|
|
"POST",
|
|
((SERVER_CONFIG.API_URL .. "/player/") .. tostring(steamId)) .. "/equipment/drop"
|
|
)
|
|
setApiHeaders(nil, req)
|
|
req:SetHTTPRequestRawPostBody(
|
|
"application/json",
|
|
encodeApiBody(nil, {item = item})
|
|
)
|
|
req:Send(function() return nil end)
|
|
end
|
|
return ____exports
|