import {createHash} from 'node:crypto'; const serviceId='f7b88778-f0bc-41cc-9227-d8ef2f52264d'; const blocked=node=>node.includes('*')||/(^|\.)op($|\.)/.test(node)||node.startsWith('luckperms.')||node.startsWith('minecraft.command.op'); export async function projectOrganisationGameUsers(accounts,{access,citizenGameNodes,gameNodes,bankMap}){ const groups=new Map(),users=[],seen=new Set(); for(const account of accounts){ if(account.id===serviceId||account.game_link_exempt)continue; const uuid=account.minecraft_uuid?.toLowerCase(); if(!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(uuid||'')||seen.has(uuid))throw Error('Game projection requires unique verified player identities.'); seen.add(uuid); const rights=await access(account); if(rights.organisation?.mode!=='active')throw Error('Central game projection requires active organisation authority.'); const nodes=new Set(rights.nodes.filter(n=>gameNodes.has(n)&&!blocked(n))); for(const n of await citizenGameNodes(rights.ranks))if(gameNodes.has(n)&&!blocked(n)&&!Object.hasOwn(bankMap,n))nodes.add(n); for(const [game,web]of Object.entries(bankMap))if(rights.nodes.includes(web)&&gameNodes.has(game)&&!blocked(game))nodes.add(game); const permissions=Object.fromEntries([...nodes].sort().map(n=>[n,true])); const id='org_'+createHash('sha256').update(JSON.stringify(permissions)).digest('hex').slice(0,20); if(!groups.has(id))groups.set(id,{id,permissions}); users.push({uuid,ranks:[id]}); } return {ranks:[...groups.values()].sort((a,b)=>a.id.localeCompare(b.id)),users}; }