43 lines
1.7 KiB
Lua
43 lines
1.7 KiB
Lua
local nk = require("nakama")
|
|
|
|
-- =============================================================================
|
|
-- Steam Auth Hook
|
|
-- =============================================================================
|
|
-- On first Steam login: set display_name from Steam username, default role to "player"
|
|
|
|
local function after_authenticate_steam(context, output, input)
|
|
if not context.user_id then return end
|
|
|
|
local status, account = pcall(nk.account_get_id, context.user_id)
|
|
if not status or not account then return end
|
|
|
|
-- On first login (no display name set), use Steam username
|
|
if not account.user.display_name or account.user.display_name == "" then
|
|
local steamName = input.username or "SteamPlayer"
|
|
pcall(nk.account_update_id, context.user_id, nil, steamName, nil, nil, nil, nil, nil)
|
|
nk.logger_info("Steam user " .. context.user_id .. " display name set to: " .. steamName)
|
|
end
|
|
|
|
-- Set default role if not set
|
|
local metadata = {}
|
|
if type(account.user.metadata) == "string" then
|
|
local s, m = pcall(nk.json_decode, account.user.metadata or "{}")
|
|
if s then metadata = m end
|
|
else
|
|
metadata = account.user.metadata or {}
|
|
end
|
|
|
|
if not metadata.role or metadata.role == "" then
|
|
metadata.role = "player"
|
|
pcall(nk.account_update_id, context.user_id, nil, nil, nil, nil, nil, nil, nk.json_encode(metadata))
|
|
end
|
|
end
|
|
|
|
-- Register the Steam auth after-hook
|
|
nk.register_req_after(after_authenticate_steam, "AuthenticateSteam")
|
|
|
|
-- Create default native leaderboard on startup
|
|
pcall(nk.leaderboard_create, "global_high_score", true, "desc", "best", nil, {})
|
|
|
|
nk.logger_info("LUA TEST: core module loaded successfully")
|