Return the actual saved House key status

This commit is contained in:
unimatrixgit 2026-11-21 21:36:45 +01:00
commit 4fc5c0b482
2 changed files with 5 additions and 1 deletions

View file

@ -14,7 +14,10 @@ export function createCharacterChoice(db,{origin='https://cp.dunerealms.com',aut
const linked=await db.prepare('SELECT minecraft_uuid FROM site_game_links WHERE user_id=?').bind(user.id).first();
if(!linked)return request.method==='GET'?reply({linked:false,choices:CHARACTER_FACTIONS,selection:null}):reply({message:'Link Minecraft before choosing a faction.'},409);
const current=await db.prepare('SELECT faction,version,minecraft_uuid FROM site_character_choices WHERE user_id=?').bind(user.id).first();
if(request.method==='GET')return reply({linked:true,choices:CHARACTER_FACTIONS,selection:current?.minecraft_uuid===linked.minecraft_uuid?current.faction:null,version:Number(current?.version||0),gameSync:'not_connected'});
if(request.method==='GET'){
if(selectionService){try{const value=await selectionService.read(linked.minecraft_uuid);return reply({linked:true,choices:CHARACTER_FACTIONS,selection:value.selection,version:value.version,keyId:value.keyId,gameSync:value.keyId?'key_recorded':'key_pending'});}catch(error){if(error.status)return reply({message:error.message},error.status);throw error;}}
return reply({linked:true,choices:CHARACTER_FACTIONS,selection:current?.minecraft_uuid===linked.minecraft_uuid?current.faction:null,version:Number(current?.version||0),gameSync:'not_connected'});
}
if(request.headers.get('origin')!==origin||!/^application\/json(?:;|$)/i.test(request.headers.get('content-type')||''))return reply({message:'Request origin or format rejected.'},403);
const raw=await request.text();if(raw.length>1024)return reply({message:'Request too large.'},413);
let body;try{body=JSON.parse(raw);}catch{return reply({message:'Invalid selection.'},400);}

View file

@ -14,6 +14,7 @@ test('website confirmation uses the same HOUSE instrument and keeps its existing
const route=createCharacterChoice(f.db,{authenticate:async()=>f.people.Owner,access:async()=>({nodes:['web.menu.game']}),selectionService:webService});
const post=(faction,version)=>route(new Request('https://cp.dunerealms.com/api/account/character',{method:'POST',headers:{origin:'https://cp.dunerealms.com','content-type':'application/json'},body:JSON.stringify({faction,version,confirmed:true})}));
let response=await post('atreides',0);assert.equal(response.status,200);let saved=await response.json();assert.ok(saved.keyId);assert.equal((await f.service.read(f.uuid)).keyId,saved.keyId);
const webRead=await (await route(new Request('https://cp.dunerealms.com/api/account/character'))).json();assert.equal(webRead.keyId,saved.keyId);assert.equal(webRead.gameSync,'key_recorded');
response=await post('fremen',1);assert.equal(response.status,200);saved=await response.json();assert.equal((await f.service.read(f.uuid)).selection,'fremen');assert.equal((await f.service.read(f.uuid)).keyId,saved.keyId);
await assert.rejects(webService.choose({uuid:f.uuid,faction:'fremen',version:2,confirmed:true,requestId:randomUUID()},{expectedAccountId:f.people.Other.id}),/link changed/);
}finally{f.db.close();}});