MC/website/node/claim-registry.mjs
2026-11-27 23:43:05 +00:00

30 lines
3.7 KiB
JavaScript

import {createHash} from 'node:crypto';
import {worldGuardHomeRegion} from './claim-worldguard.mjs';
// Web registry. Development locations are persisted on the owner's instruction;
// source stays internal so a later game writer cannot mistake them for imports.
export const CLAIM_REGISTRY_SCHEMA=`CREATE TABLE IF NOT EXISTS site_claim_registry (organisation_kind VARCHAR(12) NOT NULL,organisation_id CHAR(36) NOT NULL,snapshot_json TEXT NOT NULL,revision INTEGER NOT NULL,updated_at BIGINT NOT NULL,PRIMARY KEY(organisation_kind,organisation_id))`;
export async function readClaimRegistry(db,kind,id){
if(!['clan','company','protected'].includes(kind))throw new TypeError('Unsupported claim organisation.');
const row=await db.prepare('SELECT snapshot_json,revision,updated_at FROM site_claim_registry WHERE organisation_kind=? AND organisation_id=?').bind(kind,id).first();
if(!row)return null;
if(!Number.isSafeInteger(Number(row.revision))||Number(row.revision)<1||!Number.isSafeInteger(Number(row.updated_at)))throw Error('Claim registry revision is invalid.');
const snapshot=JSON.parse(row.snapshot_json);
if(snapshot.organisationId!==id||snapshot.simulation===true)throw Error('The registry must contain a real organisation projection.');
return snapshot;
}
function stableUuid(value){const h=createHash('sha256').update(value).digest('hex');return h.slice(0,8)+'-'+h.slice(8,12)+'-4'+h.slice(13,16)+'-a'+h.slice(17,20)+'-'+h.slice(20,32);}
export function webClanClaimSnapshot(clan,{kind='clan'}={}){
if(!['clan','company','protected'].includes(kind))throw TypeError('Unsupported Home Block kind');const prefix=kind==='clan'?'web':'web-'+kind;
const worlds={kaladam:'Kaladam',giedermark:'Giedermark',kaiterhof:'Kaiterhof',sigmadraken_iv:'Sigmadraken IV',arrakduin:'Arrakduin'},world=worlds[clan.home_world]?clan.home_world:'arrakduin',hash=createHash('sha256').update(kind==='clan'?clan.id:kind+':'+clan.id).digest();
const x=(hash.readUInt16BE(0)-32768)*32+8,z=(hash.readUInt16BE(2)-32768)*32+8;
const position={x,y:64,z},worldguard=worldGuardHomeRegion(clan.id,position,{ownerUuid:clan.owner_uuid,memberUuids:clan.member_uuids||[],organisationKind:kind});
return {organisationId:clan.id,source:'web-development',tier:kind==='protected'?'PBC_T0':kind==='company'?'BCO_T0':clan.institution_key?'PBC_T0':'BC_T0',usedChunks:1,homeBlock:{id:stableUuid(prefix+'-home:'+clan.id),worldUuid:stableUuid('web-world:'+world),worldName:worlds[world],position},estates:[{id:stableUuid(prefix+'-estate:'+clan.id),name:clan.name+' · Home Estate',worldName:worlds[world],chunks:1,state:'active',worldguard}]};
}
export async function seedWebClanClaimLocations(db,{clock=Date.now}={}){
const {results:clans}=await db.prepare("SELECT c.id,c.name,c.home_world,i.institution_key,m.minecraft_uuid owner_uuid FROM site_clans c LEFT JOIN site_clan_institutions i ON i.clan_id=c.id LEFT JOIN site_clan_members m ON m.clan_id=c.id AND m.user_id=c.owner_id AND m.clan_role='owner' WHERE c.active=1 ORDER BY c.id").all();
let inserted=0;
for(const clan of clans){const {results:members}=await db.prepare('SELECT minecraft_uuid FROM site_clan_members WHERE clan_id=? AND minecraft_uuid IS NOT NULL LIMIT 500').bind(clan.id).all();const snapshot=webClanClaimSnapshot({...clan,member_uuids:members.map(m=>m.minecraft_uuid)});try{
const result=await db.prepare("INSERT INTO site_claim_registry(organisation_kind,organisation_id,snapshot_json,revision,updated_at) SELECT 'clan',?,?,1,? WHERE NOT EXISTS (SELECT 1 FROM site_claim_registry WHERE organisation_kind='clan' AND organisation_id=?)").bind(clan.id,JSON.stringify(snapshot),clock(),clan.id).run();inserted+=Number(result.meta.changes||0);
}catch(error){if(error.code!=='ER_DUP_ENTRY'&&!String(error.message).includes('UNIQUE constraint'))throw error;}}
return {inserted};
}