Files
tekton/server/nakama/lua/utils.lua
T
2026-05-19 17:30:29 +08:00

53 lines
1.6 KiB
Lua

local nk = require("nakama")
local utils = {}
utils.ADMIN_ROLES = { ["admin"] = true, ["moderator"] = true, ["owner"] = true }
function utils.is_admin(context)
if not context.user_id then return false end
local status, account = pcall(nk.account_get_id, context.user_id)
if not status or not account then return false end
local metadata = {}
if type(account.user.metadata) == "string" then
status, metadata = pcall(nk.json_decode, account.user.metadata)
if not status then metadata = {} end
else
metadata = account.user.metadata or {}
end
local role = metadata.role or ""
return utils.ADMIN_ROLES[role] == true
end
function utils.is_match_host(context, match_id)
if not context.user_id or not match_id then return false end
local status, match = pcall(nk.match_get, match_id)
if not status or not match then return false end
-- Needs to decode match.state if you're using authoritative matches
-- Simplified for lua translation:
local state = {}
if match.state then
status, state = pcall(nk.json_decode, match.state)
if not status then state = {} end
end
return state.hostUserId == context.user_id
end
function utils.require_admin(context)
if not utils.is_admin(context) then
error("Admin privileges required")
end
end
function utils.require_admin_or_host(context, match_id)
if not utils.is_admin(context) and not utils.is_match_host(context, match_id) then
error("Admin or host privileges required")
end
end
return utils