feat: 2.3.2
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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
|
||||
Reference in New Issue
Block a user