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

17 lines
872 B
TypeScript

// Building copies: the server sends each instance's `instance_share` (1 for the first of a
// type, 0.5, 0.25, … for the copies); the client only shows it as a percentage.
/** A copy works below full output. Missing shares (older servers) count as full. */
export const isCopy = (share: number | undefined) => share !== undefined && share < 1
/** 0.5 → 50, 0.125 → 12.5. */
export const sharePercent = (share: number) => Math.round(share * 1000) / 10
/**
* The instances of one type in the server's rank: by share (the first works in full), then
* level, then age. The share decides, so the order needs no copy rule in the client.
*/
export function ranked<B extends { id: number; level: number; instance_share: number }>(instances: B[]): B[] {
return [...instances].sort((a, b) => b.instance_share - a.instance_share || b.level - a.level || a.id - b.id)
}