Files
Dota-Zombie-Invasion/scripts/vscripts/player_profile_manager.lua
T
2026-05-29 15:11:31 +07:00

67 lines
2.8 KiB
Lua

local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__New = ____lualib.__TS__New
local __TS__ArrayIsArray = ____lualib.__TS__ArrayIsArray
local ____exports = {}
local ____server_config = require("server_config")
local SERVER_CONFIG = ____server_config.SERVER_CONFIG
local ____api_helper = require("api_helper")
local encodeApiBody = ____api_helper.encodeApiBody
local setApiHeaders = ____api_helper.setApiHeaders
local ____store_manager = require("store_manager")
local StoreManager = ____store_manager.StoreManager
____exports.PlayerProfileManager = __TS__Class()
local PlayerProfileManager = ____exports.PlayerProfileManager
PlayerProfileManager.name = "PlayerProfileManager"
PlayerProfileManager.____file_path = "scripts/vscripts/player_profile_manager.lua"
function PlayerProfileManager.prototype.____constructor(self)
self:setupListeners()
end
function PlayerProfileManager.getInstance(self)
if not ____exports.PlayerProfileManager.instance then
____exports.PlayerProfileManager.instance = __TS__New(____exports.PlayerProfileManager)
end
return ____exports.PlayerProfileManager.instance
end
function PlayerProfileManager.prototype.setupListeners(self)
CustomGameEventManager:RegisterListener(
"create_player_profile",
function(source, event)
local playerId = event.player_id
local playerName = event.player_name
local steamId = tostring(PlayerResource:GetSteamAccountID(playerId))
self:createPlayerProfile(playerId, steamId, playerName)
end
)
end
function PlayerProfileManager.prototype.createPlayerProfile(self, playerId, steamId, playerName)
local request = CreateHTTPRequest("POST", SERVER_CONFIG.API_URL .. "/player")
setApiHeaders(nil, request)
local dataToSend = {steam_id = steamId, player_name = playerName}
request:SetHTTPRequestRawPostBody(
"application/json",
encodeApiBody(nil, dataToSend)
)
request:Send(function(result)
if result.StatusCode >= 200 and result.StatusCode < 300 then
do
pcall(function()
local decoded = {json.decode(result.Body)}
local responseData = nil
if __TS__ArrayIsArray(decoded) and #decoded > 0 then
responseData = decoded[1]
elseif decoded and type(decoded) == "table" then
responseData = decoded
end
if responseData then
StoreManager:getInstance():handleLoginPlayerApiResponse(playerId, responseData)
end
end)
end
elseif result.StatusCode == 409 then
else
end
end)
end
return ____exports