web/website/node/travel-tariff.mjs

47 lines
2.4 KiB
JavaScript

// Open-travel tariff. All amounts are minor units (1 DM = 100). Pure functions, no I/O.
// Race world and Lobby are free. Any other world costs 150 DM for the first trip and 10% more
// (compounding) for each later paid trip, capped at 5000 DM. The counter resets after 30 days
// without a paid trip. A clan or company registered on the destination world halves the fare.
// The bounty placed on an unsanctioned arrival is five times the fare actually paid.
export const TRAVEL_BASE_MINOR=15000;
export const TRAVEL_CAP_MINOR=500000;
export const TRAVEL_STEP_PERCENT=10;
export const TRAVEL_RESET_MS=30*24*60*60*1000;
export const BOUNTY_MULTIPLIER=5;
export const FREE_WORLDS=Object.freeze(['lobby']);
const whole=(value,max)=>Number.isSafeInteger(value)&&value>=0&&value<=max;
/** Fare in minor units for the trip number `trips` (0 = first paid trip), before any reduction. */
export function ladderFare(trips){
if(!whole(trips,1e9))throw RangeError('Invalid trip count');
let fare=TRAVEL_BASE_MINOR;
for(let i=0;i<trips&&fare<TRAVEL_CAP_MINOR;i++)fare=Math.min(TRAVEL_CAP_MINOR,Math.ceil(fare*(100+TRAVEL_STEP_PERCENT)/100));
return fare;
}
/** The counter that applies now: it falls back to zero after 30 days without a paid trip. */
export function effectiveTrips(count,lastPaidAt,now){
if(!whole(Number(count),1e9)||!Number.isSafeInteger(now))throw RangeError('Invalid travel history');
return lastPaidAt!=null&&now-Number(lastPaidAt)<TRAVEL_RESET_MS?Number(count):0;
}
/** Half price, rounded up to a whole minor unit so a fare is never rounded to zero. */
export const reducedFare=fare=>Math.ceil(fare/2);
/** Fare and bounty for a paid route. `reduced` is true when the traveller has a clan or company on that world. */
export function paidTariff({trips,reduced}){
const base=ladderFare(trips),fare=reduced?reducedFare(base):base;
return{fareMinor:fare,bountyMinor:fare*BOUNTY_MULTIPLIER,reduced:!!reduced,trip:trips+1};
}
/**
* Classifies a destination for a traveller. Free routes carry no fare and no bounty:
* Lobby, the traveller's own race world, and a destination covered by an Emperor free-passage decree.
*/
export function classifyRoute({destination,raceWorld,decree=false}){
if(FREE_WORLDS.includes(destination))return{free:true,reason:'lobby'};
if(destination===raceWorld)return{free:true,reason:'race-world'};
if(decree)return{free:true,reason:'decree'};
return{free:false,reason:'paid'};
}