Keep company labels under political ownership
All checks were successful
DuneRealms Web CI / web (push) Successful in 5m20s
All checks were successful
DuneRealms Web CI / web (push) Successful in 5m20s
This commit is contained in:
parent
03b7332653
commit
577d975787
2 changed files with 32 additions and 4 deletions
|
|
@ -9,7 +9,9 @@ const donorRanks=new Set(['vip','patron_caravan','patron_guild','patron_grand','
|
|||
const clean=value=>String(value||'').replace(/[&§%<>\x00-\x1f\x7f]/g,'').trim().slice(0,64);
|
||||
export function publicIdentity({ranks=[],roots=[],positions=[]}={}){
|
||||
const unique=kind=>[...new Map(roots.filter(r=>r.kind===kind&&!r.hidden).map(r=>[r.id,{name:clean(r.name),...(kind==='clan'?{tag:clean(r.tag).slice(0,6)}:{})}])).values()].filter(r=>r.name).slice(0,5);
|
||||
return {staff:currentStaffRanks(ranks),vip:ranks.some(r=>donorRanks.has(r)),positions:[...new Set(positions.map(clean).filter(Boolean))],clans:unique('clan'),companies:unique('company'),pols:unique('politics')};
|
||||
const titles=[...new Set(positions.map(clean).filter(Boolean))];
|
||||
const titleNames=new Set(titles.map(name=>name.toLowerCase()));
|
||||
return {staff:currentStaffRanks(ranks),vip:ranks.some(r=>donorRanks.has(r)),positions:titles,clans:unique('clan'),companies:unique('company'),pols:unique('politics').filter(({name})=>!titleNames.has(name.toLowerCase()))};
|
||||
}
|
||||
// Public labels only. Deed relations are reused without changing rosters or authority.
|
||||
export async function readGamePublicIdentity(db,actor,{clock=Date.now}={}){
|
||||
|
|
@ -31,9 +33,15 @@ export async function readGamePublicIdentity(db,actor,{clock=Date.now}={}){
|
|||
const value=parent?await root(parent.parent_kind,parent.parent_id,seen):{kind,id,name:row.name,tag:row.tag};memo.set(key,value);return value;
|
||||
}
|
||||
const clans=await store.query('SELECT DISTINCT c.id FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE m.user_id=? AND c.active=1 ORDER BY c.id LIMIT 21',[actor.id]);
|
||||
const companies=await store.query("SELECT DISTINCT c.id FROM corrino_companies c LEFT JOIN site_company_members m ON m.company_id=c.id WHERE c.status IN ('active','converted-order') AND (m.user_id=? OR c.ceo_account_id=?) ORDER BY c.id LIMIT 21",[actor.id,actor.id]);
|
||||
const companies=await store.query("SELECT id,name,status,direct_member FROM (SELECT c.id,c.name,c.status,c.ceo_account_id,EXISTS(SELECT 1 FROM site_company_members m WHERE m.company_id=c.id AND m.user_id=?) AS direct_member FROM corrino_companies c WHERE c.status IN ('active','converted-order')) AS companies WHERE direct_member=1 OR ceo_account_id=? ORDER BY id LIMIT 21",[actor.id,actor.id]);
|
||||
if(clans.length>20||companies.length>20)throw Error('Public membership registry requires review');
|
||||
for(const [kind,rows]of [['clan',clans],['company',companies]])for(const row of rows){const value=await root(kind,row.id);if(value)roots.push(value);}
|
||||
for(const [kind,rows]of [['clan',clans],['company',companies]])for(const row of rows){
|
||||
const value=await root(kind,row.id);
|
||||
if(value)roots.push(value);
|
||||
if(kind==='company'&&row.status==='active'&&Number(row.direct_member)===1&&value?.kind==='politics'&&!value.hidden){
|
||||
roots.push({kind,id:row.id,name:row.name});
|
||||
}
|
||||
}
|
||||
for(const order of await store.query('SELECT DISTINCT o.parent_office FROM political_passive_orders o LEFT JOIN political_passive_order_members m ON m.order_id=o.id WHERE m.user_id=? OR o.leader_account_id=?',[actor.id,actor.id])){const value=await root('politics',order.parent_office);if(value)roots.push(value);}
|
||||
for(const office of PUBLIC_OFFICES){const holder=await currentOfficeController(store,office.key,clock());if(holder?.userId===actor.id){positions.push(office.name);const value=await root('politics',office.key);if(value)roots.push(value);}}
|
||||
const access=await accessFor(db,actor);return publicIdentity({ranks:access?.ranks||[],roots,positions});
|
||||
|
|
|
|||
|
|
@ -1,11 +1,31 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';
|
||||
import {LocalD1} from './d1-adapter.mjs';import {readGamePublicIdentity,publicIdentity} from '../node/game-public-identity.mjs';import {PUBLIC_OFFICES} from '../node/public-government.mjs';
|
||||
import {SMUGGLER_COMPANY_ID} from '../node/clandestine-identity.mjs';
|
||||
test('public labels use mother entities, current offices and current ranks without changing membership',async()=>{
|
||||
const db=new LocalD1(`CREATE TABLE site_clans(id TEXT,name TEXT,tag TEXT,active INT);CREATE TABLE site_clan_members(clan_id TEXT,user_id TEXT,minecraft_uuid TEXT);CREATE TABLE site_game_links(user_id TEXT,minecraft_uuid TEXT);CREATE TABLE site_clan_deeds(clan_id TEXT,parent_clan_id TEXT);CREATE TABLE corrino_companies(id TEXT,name TEXT,status TEXT,ceo_account_id TEXT);CREATE TABLE site_company_members(company_id TEXT,user_id TEXT);CREATE TABLE site_company_relations(company_id TEXT,parent_company_id TEXT);CREATE TABLE jarvis_entity_relations(child_kind TEXT,child_id TEXT,parent_kind TEXT,parent_id TEXT);CREATE TABLE political_passive_orders(id TEXT,name TEXT,parent_office TEXT,legacy_company_id TEXT,leader_account_id TEXT);CREATE TABLE political_passive_order_members(order_id TEXT,user_id TEXT);CREATE TABLE political_office_terms(id TEXT,user_id TEXT,kind TEXT,faction TEXT,office TEXT,outcome TEXT,starts_at INT,expires_at INT);CREATE TABLE site_accounts(id TEXT,password_hash TEXT);CREATE TABLE political_office_charters(office_key TEXT,staff_key_id TEXT);CREATE TABLE organisation_keys(id TEXT,revoked_at INT);CREATE TABLE cp_ranks(rank_key TEXT,label TEXT,rank_level INT,kind TEXT,permissions_json TEXT,version INT);
|
||||
INSERT INTO site_clans VALUES('child','Child clan','CH',1),('mother','Mother clan','MOM',1);INSERT INTO site_clan_deeds VALUES('child','mother'),('mother',NULL);INSERT INTO site_clan_members VALUES('child','one','minecraft-one');INSERT INTO site_game_links VALUES('one','minecraft-one');INSERT INTO corrino_companies VALUES('childco','Child company','active','other'),('motherco','Mother company','active','other');INSERT INTO site_company_members VALUES('childco','one');INSERT INTO jarvis_entity_relations VALUES('company','childco','company','motherco');INSERT INTO site_accounts VALUES('one','hash');`);
|
||||
const store={query:async(sql,args=[]) =>(await db.prepare(sql).bind(...args).all()).results,prepare:sql=>db.prepare(sql)};
|
||||
try{const office=PUBLIC_OFFICES.find(o=>o.kind==='imperial');await db.prepare('INSERT INTO political_office_terms VALUES(?,?,?,?,?,?,?,?)').bind('term','one',office.kind,office.faction,office.office,'winner',1000,3000).run();await db.prepare('INSERT INTO political_office_charters VALUES(?,?)').bind(office.key,'charter').run();await db.prepare('INSERT INTO organisation_keys VALUES(?,NULL)').bind('charter').run();
|
||||
const read=()=>readGamePublicIdentity(store,{id:'one',role:'player',ranks:['vip']},{clock:()=>2000});let result=await read();assert.deepEqual(result.clans,[{name:'Mother clan',tag:'MOM'}]);assert.deepEqual(result.companies,[{name:'Mother company'}]);assert.deepEqual(result.pols,[{name:office.scope}]);assert.deepEqual(result.positions,[office.name]);assert.equal(result.vip,true);assert.equal(result.staff,false);assert.equal((await store.query('SELECT clan_id FROM site_clan_members'))[0].clan_id,'child');
|
||||
const read=()=>readGamePublicIdentity(store,{id:'one',role:'player',ranks:['vip']},{clock:()=>2000});let result=await read();assert.deepEqual(result.clans,[{name:'Mother clan',tag:'MOM'}]);assert.deepEqual(result.companies,[{name:'Mother company'}]);assert.deepEqual(result.pols,[]);assert.deepEqual(result.positions,[office.name]);assert.equal(result.vip,true);assert.equal(result.staff,false);assert.equal((await store.query('SELECT clan_id FROM site_clan_members'))[0].clan_id,'child');
|
||||
for(const [id,name,status,member,ceo] of [
|
||||
['techno','Technoblade','active',true,'other'],
|
||||
['converted','Converted order','converted-order',true,'other'],
|
||||
[SMUGGLER_COMPANY_ID,'Secret company','active',true,'other'],
|
||||
['ceo-only','CEO only company','active',false,'one']
|
||||
]){
|
||||
await db.prepare('INSERT INTO corrino_companies VALUES(?,?,?,?)').bind(id,name,status,ceo).run();
|
||||
if(member)await db.prepare("INSERT INTO site_company_members VALUES(?,'one')").bind(id).run();
|
||||
await db.prepare("INSERT INTO jarvis_entity_relations VALUES('company',?,'politics',?)").bind(id,office.key).run();
|
||||
}
|
||||
const tables=['corrino_companies','site_company_members','jarvis_entity_relations','political_office_terms'];
|
||||
const snapshot=()=>Promise.all(tables.map(table=>store.query('SELECT * FROM '+table)));
|
||||
const before=await snapshot();
|
||||
result=await read();
|
||||
assert.deepEqual(result.companies,[{name:'Mother company'},{name:'Technoblade'}]);
|
||||
assert.deepEqual(result.positions,[office.name]);
|
||||
assert.deepEqual(result.pols,[]);
|
||||
assert.deepEqual(await snapshot(),before);
|
||||
await db.prepare("DELETE FROM jarvis_entity_relations WHERE parent_kind='politics'").run();
|
||||
await db.prepare('UPDATE political_office_terms SET expires_at=1500').run();result=await read();assert.deepEqual(result.positions,[]);assert.deepEqual(result.pols,[]);
|
||||
await db.prepare("INSERT INTO site_clan_deeds VALUES('loop','child')").run();await db.prepare("UPDATE site_clan_deeds SET parent_clan_id='child' WHERE clan_id='mother'").run();await assert.rejects(read(),/hierarchy requires review/);
|
||||
}finally{db.close();}
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff