game-village-politics-frontend/app/types/game.ts

139 lines
3.7 KiB
TypeScript
Raw Normal View History

// Payloads exchanged with the server. Every number in here is computed server-side;
// the client only projects resources and countdowns forward from the timestamps.
export type Resource = 'wood' | 'clay' | 'iron' | 'food'
export type Resources = Record<Resource, number>
export type Cost = Record<Exclude<Resource, 'food'>, number>
export type BuildingType =
| 'headquarters'
| 'storage'
| 'farm'
| 'timber_camp'
| 'clay_pit'
| 'iron_mine'
| 'hiding_place'
| 'wall'
| 'rally_point'
/** Why the server refuses an offer. The client shows `village.blocked.<reason>`; no_space means not enough free building space. */
export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires'
export interface AuthUser {
id: number
name: string
email: string
}
/** `POST auth/login` and `POST auth/register`. */
export interface AuthResult {
token: string
user: AuthUser
}
/** One row of `world.index`. */
export interface WorldSummary {
id: number
name: string
slug: string
speed: number
village_id: number | null
}
/** `village.show`, and the reply to every `village.*` mutation. */
export interface VillageState {
id: number
world_id: number
name: string
x: number
y: number
/** Map continent, e.g. K55 for 500|500. */
continent: string
/** Sum of the buildings' level points, as shown on the map. */
points: number
/** Building space: what buildings and queued new buildings take, out of the village maximum. */
space: { used: number; max: number }
/** Amounts stored at `resources_at`. */
resources: Resources
resources_at: string
/** Net change per hour. Food is farm output minus upkeep, so it can be negative. */
rates: Resources
storage_cap: number
population: Population
builders: number
buildings: Building[]
queue: BuildOrder[]
buildable: BuildOption[]
/** Lets the client correct for its own clock being off. */
server_time: string
}
export interface Population {
/** Food per hour the village can feed. */
food_budget: number
/** Food per hour eaten by villagers and soldiers. */
upkeep: number
villagers: number
/** Villagers assigned to buildings. */
workers: number
soldiers: number
}
export interface Building {
id: number
type: BuildingType
level: number
/** Building space this instance takes, fixed per type (0 for the wall). */
space: number
priority: number
workers: { assigned: number; required: number }
/** assigned / required, 0..1. */
staffing: number
production: { resource: Resource; per_hour: number } | null
/** Next level after everything already queued; null at max level. */
upgrade: Offer | null
/** null when the building cannot go lower (core buildings at level 0). */
demolish: { refund: Cost; seconds: number } | null
}
/** Whether it is affordable is left to the client, which compares the cost to projected resources. */
export interface Offer {
target_level: number
cost: Cost
seconds: number
blocked: BlockedReason | null
}
export interface BuildOption extends Offer {
type: BuildingType
space: number
}
export interface BuildOrder {
id: number
/** null for a new instance that does not exist yet. */
building_id: number | null
type: BuildingType
action: 'build' | 'upgrade' | 'demolish'
/** -1 means the instance is removed. */
target_level: number
space: number
cost: Cost
position: number
/** Builder slot working on it, or null while it waits. */
builder: number | null
starts_at: string | null
finishes_at: string | null
}
/** Push `village:updated` after the server processed an event for this village. */
export interface VillageUpdated {
village_id: number
}
/** What a rejected `ws.send` carries. */
export interface GameError {
message: string
code?: string
}