Merge pull request 'Read organisation receipts through the production MySQL interface' (#472) from release/game-receipt-fix-20261121 into main
This commit is contained in:
commit
ee797cde52
2 changed files with 7 additions and 4 deletions
|
|
@ -15,6 +15,7 @@ import {boardingOpen,gameHomeLocation,transportDestination,gameOrganisationError
|
|||
export const GAME_ORGANISATION_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_game_organisation_actions(request_id CHAR(36) PRIMARY KEY,key_id CHAR(36) NOT NULL,account_id CHAR(36) NOT NULL,minecraft_uuid CHAR(36) NOT NULL,action_id VARCHAR(32) NOT NULL,input_hash CHAR(64) NOT NULL,result_json TEXT NOT NULL,state VARCHAR(16) NOT NULL,expires_at BIGINT NOT NULL,created_at BIGINT NOT NULL,INDEX game_arrival(minecraft_uuid,state,expires_at)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
|
||||
const arrays={clan:'clans',company:'companies',protected:'offices'};
|
||||
const hash=input=>createHash('sha256').update(JSON.stringify(input)).digest('hex');
|
||||
const first=(store,sql,args=[])=>store.first?store.first(sql,args):store.query(sql,args).then(rows=>rows[0]||null);
|
||||
export function createGameOrganisations(db,keys,{clock=Date.now,readState=()=>readGameNativeState(db)}={}){
|
||||
const reader=tx=>{const r=boardReadStore(tx,db);r.readSnapshot=undefined;return r;};
|
||||
async function actor(tx,uuid,lock=false){
|
||||
|
|
@ -50,7 +51,7 @@ export function createGameOrganisations(db,keys,{clock=Date.now,readState=()=>re
|
|||
}return out;
|
||||
}
|
||||
async function execute(input){
|
||||
const user=await actor(db,input.uuid),digest=hash(input),prior=await db.first('SELECT * FROM jarvis_game_organisation_actions WHERE request_id=?',[input.requestId]);
|
||||
const user=await actor(db,input.uuid),digest=hash(input),prior=await first(db,'SELECT * FROM jarvis_game_organisation_actions WHERE request_id=?',[input.requestId]);
|
||||
if(prior){if(prior.account_id!==user.id||prior.input_hash!==digest)throw fail('This action reference was already used.');return {...JSON.parse(prior.result_json),replayed:true};}
|
||||
const state=input.action==='set-home'?null:await readState();
|
||||
await keys.issue({accountId:user.id,keyType:'organisation-action',scopeId:'game:'+input.requestId,authorize:async(tx,{keyId})=>{
|
||||
|
|
@ -90,7 +91,7 @@ export function createGameOrganisations(db,keys,{clock=Date.now,readState=()=>re
|
|||
await tx.query('INSERT INTO jarvis_game_organisation_actions(request_id,key_id,account_id,minecraft_uuid,action_id,input_hash,result_json,state,expires_at,created_at) VALUES(?,?,?,?,?,?,?,?,?,?)',[input.requestId,keyId,user.id,input.uuid,input.action,digest,JSON.stringify(result),stateName,expires,clock()]);
|
||||
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),user.id,'game-organisation-'+input.action,JSON.stringify({keyId,requestId:input.requestId,kind:input.kind,id:input.id}),clock()]);
|
||||
}});
|
||||
const saved=await db.first('SELECT result_json FROM jarvis_game_organisation_actions WHERE request_id=?',[input.requestId]);if(!saved)throw fail('The recorded action requires review.');return JSON.parse(saved.result_json);
|
||||
const saved=await first(db,'SELECT result_json FROM jarvis_game_organisation_actions WHERE request_id=?',[input.requestId]);if(!saved)throw fail('The recorded action requires review.');return JSON.parse(saved.result_json);
|
||||
}
|
||||
async function arrival(input){
|
||||
const user=await actor(db,input.uuid),rows=await db.query("SELECT * FROM jarvis_game_organisation_actions WHERE minecraft_uuid=? AND account_id=? AND state='pending' AND expires_at>? ORDER BY created_at DESC LIMIT 10",[input.uuid,user.id,clock()]);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ test('company Home Block authority and receipt are atomic, idempotent and tied t
|
|||
const f=await companyFixture();try{
|
||||
const id=await f.create(),uuid=randomUUID();await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[uuid,f.people.Owner.id]);
|
||||
await f.db.query(GAME_ORGANISATION_SCHEMA);f.db.sqlite.exec('CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INT);CREATE TABLE jarvis_generated_instructions(key_id TEXT,action_id TEXT,created_at INT);');
|
||||
const service=createGameOrganisations(f.db,f.keys),input={action:'set-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1,expectedRevision:0,confirmed:true,location:{server:'caladan',worldName:'caladan',worldUuid:randomUUID(),position:{x:8,y:64,z:8}}};
|
||||
// Production exposes first() on transactions, not on the root MySQL adapter.
|
||||
const runtime={query:f.db.query.bind(f.db),prepare:f.db.prepare.bind(f.db),transaction:f.db.transaction.bind(f.db)};
|
||||
const service=createGameOrganisations(runtime,f.keys),input={action:'set-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1,expectedRevision:0,confirmed:true,location:{server:'caladan',worldName:'caladan',worldUuid:randomUUID(),position:{x:8,y:64,z:8}}};
|
||||
const saved=await service.execute(input);assert.equal(saved.homeBlock.source,'minecraft');assert.equal(saved.homeBlock.revision,1);assert.equal((await service.execute(input)).replayed,true);
|
||||
assert.equal((await f.db.query("SELECT * FROM organisation_keys WHERE key_type='organisation-action'")).length,1);
|
||||
await assert.rejects(service.execute({...input,requestId:randomUUID()}),/changed/);
|
||||
|
|
@ -16,7 +18,7 @@ test('company Home Block authority and receipt are atomic, idempotent and tied t
|
|||
f.db.sqlite.exec('DROP TRIGGER fail_game_audit');
|
||||
const snapshot=JSON.parse((await f.db.first('SELECT snapshot_json FROM site_claim_registry WHERE organisation_id=?',[id])).snapshot_json),home={...snapshot.homeBlock,revision:1,source:'minecraft'};
|
||||
let now=Date.UTC(2026,10,21,20,0);const state={clans:[],companies:[{id,lifecycle:'active',homeBlock:home}],offices:[],wars:[]};
|
||||
const travel=createGameOrganisations(f.db,f.keys,{clock:()=>now,readState:async()=>state}),journey={action:'travel-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1};
|
||||
const travel=createGameOrganisations(runtime,f.keys,{clock:()=>now,readState:async()=>state}),journey={action:'travel-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1};
|
||||
now+=5*60000;await assert.rejects(travel.execute(journey),/Boarding/);now-=5*60000;
|
||||
const ticket=await travel.execute(journey);assert.equal(ticket.destination.server,'caladan');
|
||||
assert.equal((await travel.arrival({action:'arrive',uuid,server:'arrakis'})).pending,false);
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff