57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { content } from "@deadhead/content";
|
|
|
|
export function formatCredits(credits: number): string {
|
|
return `${Math.round(credits).toLocaleString("en-US")} cr`;
|
|
}
|
|
|
|
/** A duration as the two largest units: "2h 05m", "4m 09s", "45s". */
|
|
export function formatDuration(ms: number): string {
|
|
const totalSeconds = Math.max(0, Math.ceil(ms / 1000));
|
|
const hours = Math.floor(totalSeconds / 3600);
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
const seconds = totalSeconds % 60;
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|
if (hours > 0) return `${hours}h ${pad(minutes)}m`;
|
|
if (minutes > 0) return `${minutes}m ${pad(seconds)}s`;
|
|
return `${seconds}s`;
|
|
}
|
|
|
|
/** A countdown as "mm:ss", or "h:mm:ss" from an hour up. */
|
|
export function formatClock(ms: number): string {
|
|
const totalSeconds = Math.max(0, Math.ceil(ms / 1000));
|
|
const hours = Math.floor(totalSeconds / 3600);
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|
const mmss = `${pad(Math.floor((totalSeconds % 3600) / 60))}:${pad(totalSeconds % 60)}`;
|
|
return hours > 0 ? `${hours}:${mmss}` : mmss;
|
|
}
|
|
|
|
/** A game-hours duration, e.g. "5.4 h" or "250 h". */
|
|
export function formatGameHours(hours: number): string {
|
|
return hours < 10 ? `${hours.toFixed(1)} h` : `${Math.round(hours)} h`;
|
|
}
|
|
|
|
export function formatMillionKm(distance: number): string {
|
|
return distance < 10 ? `${distance.toFixed(2)}M km` : `${Math.round(distance)}M km`;
|
|
}
|
|
|
|
export function locationName(id: string): string {
|
|
return content.location(id)?.name ?? id;
|
|
}
|
|
|
|
/** A location as a destination in a sentence: "to the Moon", "to Mars". */
|
|
export function toLocation(id: string): string {
|
|
return id === "moon" ? "to the Moon" : `to ${locationName(id)}`;
|
|
}
|
|
|
|
export function shipModelName(id: string): string {
|
|
return content.shipModel(id)?.name ?? id;
|
|
}
|
|
|
|
export function cargoName(id: string): string {
|
|
return content.cargoType(id)?.name ?? id;
|
|
}
|
|
|
|
export function skillName(id: string): string {
|
|
return content.skill(id)?.name ?? id;
|
|
}
|