27 lines
3.5 KiB
JavaScript
27 lines
3.5 KiB
JavaScript
import {identity} from './auth.mjs';import {accessFor} from './permissions.mjs';
|
|
import {PostbankPayments} from './postbank-payments.mjs';import {PUBLIC_OFFICES} from './public-government.mjs';
|
|
const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v);
|
|
export function createOfficeAuctionAPI(central,bank,runtime,settings={},deps={}){
|
|
const origin=settings.CP_ORIGIN||'https://cp.dunerealms.com',authenticate=deps.authenticate||(r=>identity(r,central)),access=deps.access||(u=>accessFor(central,u)),clock=deps.clock||Date.now;
|
|
const reply=(body,status=200,publicView=false)=>Response.json(body,{status,headers:{'Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff',...(publicView?{'Access-Control-Allow-Origin':'https://www.dunerealms.com'}:{})}});
|
|
const project=row=>{const office=PUBLIC_OFFICES.find(o=>o.key===row.office_key),highest=Number(row.highest_minor);return{id:row.id,type:'auctions',category:'politics',auctionType:'office',title:(office?.name||'Office')+' auction',owner:'Imperial Register',world:office?.scope||'Imperium',officeKey:row.office_key,image:'https://www.dunerealms.com/assets/seals/'+(office?.art||'emperor-v1.png'),opensAt:Number(row.opens_at),closesAt:Number(row.closes_at),highestMinor:highest,minimumBidMinor:highest?highest+1000:10000,revision:Number(row.revision),state:row.state};};
|
|
return async request=>{try{
|
|
if(!runtime)return reply({message:'Office auctions are unavailable.'},503);
|
|
const url=new URL(request.url);
|
|
if(request.method==='GET'&&url.pathname==='/api/jarvis/auctions'&&!url.search){const rows=await bank.query("SELECT * FROM jarvis_office_auctions WHERE state IN ('open','award_pending') ORDER BY closes_at,id LIMIT 500");return reply({auctions:rows.map(project)},200,true);}
|
|
const user=await authenticate(request);if(!user)return reply({message:'Sign in first.'},401);if(!(await access(user)).nodes.includes('web.menu.game'))return reply({message:'Player account required.'},403);
|
|
if(request.method==='GET'&&url.pathname==='/api/jarvis/auction'){
|
|
const id=url.searchParams.get('id');if(!uuid(id)||url.searchParams.size!==1)return reply({message:'Choose an auction.'},400);
|
|
const row=await bank.first('SELECT * FROM jarvis_office_auctions WHERE id=?',[id]);if(!row)return reply({message:'Auction not found.'},404);
|
|
const canBid=row.state==='open'&&Number(row.opens_at)<=clock()&&clock()<Number(row.closes_at)&&await runtime.eligible(null,user,row);
|
|
const accounts=canBid?(await new PostbankPayments(bank,runtime.authority).accounts(user)).filter(a=>a.canDebit):[];
|
|
return reply({auction:project(row),canBid,accounts,highestBidIsYours:row.highest_user_id===user.id});
|
|
}
|
|
if(request.method!=='POST'||url.pathname!=='/api/jarvis/auctions/bid'||url.search)return reply({message:'Not found.'},404);
|
|
if(request.headers.get('origin')!==origin)return reply({message:'Origin rejected.'},403);
|
|
if(!/^application\/json(?:;|$)/i.test(request.headers.get('content-type')||''))return reply({message:'JSON required.'},415);
|
|
const raw=await request.text();if(raw.length>2048)return reply({message:'Bid too large.'},413);let input;try{input=JSON.parse(raw);}catch{return reply({message:'Invalid bid.'},400);}
|
|
if(input.confirmed!==true)return reply({message:'Confirm the bid and reservation.'},400);
|
|
return reply(await runtime.service.bid(user,input));
|
|
}catch(error){if(error.status)return reply({message:error.message},error.status);return reply({message:'The auction service did not confirm this action. Refresh before retrying.'},503);}};
|
|
}
|

Staff