diff --git a/website/node/game-guild.mjs b/website/node/game-guild.mjs index 0811e4fc..175c8e9c 100644 --- a/website/node/game-guild.mjs +++ b/website/node/game-guild.mjs @@ -8,7 +8,7 @@ import {PostbankPayments} from './postbank-payments.mjs'; import {BankError} from './postbank-service.mjs'; import {createPersonalActivityEvents} from './personal-activity-events.mjs'; -export const GUILD_SCHEMA=`CREATE TABLE IF NOT EXISTS pb_guild_passages(id CHAR(36) PRIMARY KEY,actor_id CHAR(36) NOT NULL,minecraft_uuid CHAR(36) NOT NULL,house_key CHAR(36) NOT NULL,house_version INT NOT NULL,destination VARCHAR(32) NOT NULL,fare_minor BIGINT NOT NULL,source_id BIGINT UNSIGNED NOT NULL,target_id BIGINT UNSIGNED NOT NULL,fingerprint CHAR(64) NOT NULL,state VARCHAR(16) NOT NULL,created_at BIGINT NOT NULL,expires_at BIGINT NOT NULL,arrived_at BIGINT NULL,INDEX guild_pending(minecraft_uuid,state,expires_at)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`; +export const GUILD_SCHEMA=`CREATE TABLE IF NOT EXISTS pb_guild_passages(id CHAR(36) PRIMARY KEY,actor_id CHAR(36) NOT NULL,minecraft_uuid CHAR(36) NOT NULL,house_key CHAR(36) NOT NULL,house_version INT NOT NULL,destination VARCHAR(32) NOT NULL,fare_minor BIGINT NOT NULL,source_id BIGINT UNSIGNED NOT NULL,target_id BIGINT UNSIGNED NOT NULL,fingerprint CHAR(64) NOT NULL,state VARCHAR(16) NOT NULL,created_at BIGINT NOT NULL,expires_at BIGINT NOT NULL,arrived_at BIGINT NULL,reported_at BIGINT NULL,INDEX guild_pending(minecraft_uuid,state,expires_at)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`; const routes={atreides:'caladan',harkonnen:'giedi_prime',corrino:'kaitain',ordos:'sigma_draconis_iv',fremen:'arrakis'}; const accountSQL=`SELECT a.*,c.entity_id,e.external_key,e.active entity_active,p.state package_state FROM pb_accounts a LEFT JOIN pb_clients c ON c.client_number=a.client_number LEFT JOIN pb_entities e ON e.id=c.entity_id LEFT JOIN pb_packages p ON p.id=a.package_id`; const fail=(message,status=409)=>{throw new BankError(message,status);}; @@ -38,7 +38,7 @@ export function createGuildService(bank,{authority,resolve,recordArrival=async() async function arrive(uuid,server){ await expire();const user=await resolve(uuid);if(!user)fail('A current linked personal account is required.',403); const row=await bank.transaction(async tx=>{ - const r=await tx.first("SELECT * FROM pb_guild_passages WHERE minecraft_uuid=? AND actor_id=? AND destination=? AND state IN ('pending','arrived') ORDER BY created_at DESC LIMIT 1 FOR UPDATE",[uuid,user.id,server]);if(!r)return null; + const r=await tx.first("SELECT * FROM pb_guild_passages WHERE minecraft_uuid=? AND actor_id=? AND destination=? AND (state='pending' OR state='arrived' AND reported_at IS NULL) ORDER BY created_at DESC LIMIT 1 FOR UPDATE",[uuid,user.id,server]);if(!r)return null; if(r.state==='arrived')return r; if(Number(r.expires_at)<=clock()){await release(tx,r);return null;} const accounts=await tx.query(accountSQL+' WHERE a.id IN (?,?) ORDER BY a.id FOR UPDATE',[r.source_id,r.target_id]),source=accounts.find(a=>String(a.id)===String(r.source_id)),target=accounts.find(a=>String(a.id)===String(r.target_id)); @@ -48,7 +48,7 @@ export function createGuildService(bank,{authority,resolve,recordArrival=async() await payments.post(tx,{id:r.id,actor_id:user.id,amount_minor:String(r.fare_minor)},source,target,true); r.state='arrived';r.arrived_at=clock();await tx.query("UPDATE pb_guild_passages SET state='arrived',arrived_at=? WHERE id=?",[r.arrived_at,r.id]);return r; }); - if(row)await recordArrival(user,row);return{ok:true,passage:row?view(row):null}; + if(row){await recordArrival(user,row);await bank.query("UPDATE pb_guild_passages SET reported_at=? WHERE id=? AND state='arrived'",[clock(),row.id]);}return{ok:true,passage:row?view(row):null}; } return{quote,board,arrive,expire}; }