game-village-politics-frontend/app/utils/market.ts

19 lines
923 B
TypeScript

import type { Cost } from '~/types/game'
type Translate = (key: string, values?: Record<string, unknown>) => string
/** A route's interval in real time: "2 h 30 min", "45 min", "1 d 2 h". */
export function formatInterval(seconds: number, t: Translate) {
const m = Math.round(Math.max(0, seconds) / 60)
const days = Math.floor(m / 1440)
const hours = Math.floor((m % 1440) / 60)
const minutes = m % 60
if (days) return t('market.interval.dh', { d: days, h: hours })
if (hours && minutes) return t('market.interval.hm', { h: hours, m: minutes })
if (hours) return t('market.interval.h', { h: hours })
return t('market.interval.m', { m: minutes })
}
/** The resources of a cargo with an amount above zero, in wood, clay, iron order. */
export const cargoRows = (cargo: Cost | null | undefined) => (['wood', 'clay', 'iron'] as const).filter((r) => (cargo?.[r] ?? 0) > 0).map((r) => [r, cargo![r]] as const)