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

47 lines
2.4 KiB
TypeScript
Raw Normal View History

import type { Building, BuildingType, VillageState } from '~/types/game'
/** A village's own screens, like the building links of Die Stämme: the bar's tabs and the phone's tab bar. */
export const VILLAGE_SCREENS: { name: 'overview' | 'build' | 'recruit' | 'rally' | 'workforce'; path: string; icon: BuildingType }[] = [
{ name: 'overview', path: '', icon: 'headquarters' },
{ name: 'build', path: '/build', icon: 'storage' },
{ name: 'recruit', path: '/recruit', icon: 'barracks' },
{ name: 'rally', path: '/rally', icon: 'rally_point' },
{ name: 'workforce', path: '/workforce', icon: 'farm' },
]
/**
* The screen a building opens when it is clicked: the headquarters its construction, the
* rally point its commands, a military building its recruiting, every other its own screen.
*/
export function buildingRoute(village: VillageState, building: Building) {
const base = `/village/${village.id}`
if (building.type === 'headquarters') return `${base}/build`
if (building.type === 'rally_point') return `${base}/rally`
if (village.military.some((m) => m.building_id === building.id)) return { path: `${base}/recruit`, query: { at: building.id } }
return `${base}/building/${building.id}`
}
/** The building whose screen the route shows, if any. */
export function buildingOfRoute(village: VillageState, path: string, query: Record<string, unknown>): Building | null {
const base = `/village/${village.id}`
const find = (pick: (b: Building) => boolean) => village.buildings.find(pick) ?? null
if (path.startsWith(`${base}/building/`)) return find((b) => b.id === Number(path.slice(`${base}/building/`.length)))
if (path === `${base}/build`) return find((b) => b.type === 'headquarters')
if (path === `${base}/rally`) return find((b) => b.type === 'rally_point')
if (path === `${base}/recruit`) {
const at = Number(query.at) || village.military[0]?.building_id
return find((b) => b.id === at)
}
return null
}
/**
* A building's name. Copies are counted in the server's rank ("Timber camp, copy 2"); the
* one that works in full is the plain name.
*/
export function buildingName(village: VillageState, building: Building, t: (key: string, params?: Record<string, unknown>) => string) {
const name = t(`buildings.${building.type}`)
const n = ranked(village.buildings.filter((b) => b.type === building.type)).findIndex((b) => b.id === building.id)
return n > 0 ? t('village.copy.name', { name, n: n + 1 }) : name
}