web/website/node/game-imperial-raids.mjs

12 lines
1.2 KiB
JavaScript

import {gameAuthorized,json} from './game-link.mjs';
import {validateImperialRaidInput} from './imperial-raid-protocol.mjs';
export function createGameImperialRaidsAPI(runtime,settings){return async request=>{
if(request.method!=='POST')return json({message:'Method not allowed'},405);
if(!gameAuthorized(request,settings))return json({message:'Unauthorized'},401);
if(new URL(request.url).search||!/^application\/json(?:;|$)/i.test(request.headers.get('content-type')||''))return json({message:'Invalid request'},400);
const raw=await request.text();if(Buffer.byteLength(raw,'utf8')>16384)return json({message:'Invalid request'},400);
let input;try{input=validateImperialRaidInput(JSON.parse(raw));}catch{return json({message:'Invalid Imperial raid request'},400);}
if(!runtime||settings.IMPERIAL_RAIDS_ENABLED!=='true'||settings.WAR_BRIDGE_ENABLED!=='true'||settings.POSTBANK_ENABLED!=='true')return json({message:'The Pillager Update is not enabled.'},503);
try{return json(await runtime.execute(input));}catch(error){return json({message:error.status?error.message:'Imperial raids are temporarily unavailable. Retain the original request reference.'},error.status||503);}
};}