| @@ -7,7 +7,7 @@ |
| 7 | 7 | * that are absent — a plain-language reason. Unknown actions and fields fall back to a |
| 8 | 8 | * humanised name and a generic input, so new API features still appear. |
| 9 | 9 | */ |
| 10 | | −import type { ActiveShip, WaypointKind } from '../types'; |
| 10 | +import type { ActiveShip, GameAction, WaypointKind } from '../types'; |
| 11 | 11 | import { isDisabled } from '../types'; |
| 12 | 12 | |
| 13 | 13 | export type ActionGroup = 'flight' | 'trade' | 'ship' | 'social'; |
| @@ -144,25 +144,84 @@ export const ACTIONS: Record<string, ActionSpec> = { |
| 144 | 144 | }, |
| 145 | 145 | // social |
| 146 | 146 | accept: { label: 'Accept', group: 'social', confirm: true, help: 'Take on this contract.' }, |
| 147 | | − leave: { label: 'Leave', group: 'social', confirm: true, help: 'Leave.' }, |
| 147 | + leave: { label: 'Leave federation', group: 'social', confirm: true, help: 'Leave your federation.' }, |
| 148 | 148 | deposit: { label: 'Deposit', group: 'social', confirm: true, help: 'Move credits to the treasury.', fields: { amount: { label: 'Credits', input: 'quantity', min: 1 } } }, |
| 149 | | − withdraw: { label: 'Withdraw', group: 'social', confirm: true, help: 'Withdraw.', fields: { amount: { label: 'Credits', input: 'quantity', min: 1 } } }, |
| 149 | + withdraw: { label: 'Withdraw', group: 'social', confirm: true, help: 'Move credits out of the treasury.', fields: { amount: { label: 'Credits', input: 'quantity', min: 1 } } }, |
| 150 | 150 | send: { label: 'Send', group: 'social', help: 'Send a message.', fields: { text: { label: 'Message', input: 'longtext' } } }, |
| 151 | 151 | reply: { label: 'Reply', group: 'social', help: 'Reply.', fields: { text: { label: 'Message', input: 'longtext' } } }, |
| 152 | | − post: { label: 'Post', group: 'social', help: 'Post.', fields: { text: { label: 'Message', input: 'longtext' } } } |
| 152 | + post: { label: 'Post', group: 'social', help: 'Post to the federation channel.', fields: { text: { label: 'Message', input: 'longtext' } } } |
| 153 | 153 | }; |
| 154 | 154 | |
| 155 | +/** |
| 156 | + * The API reuses some action names for unrelated endpoints (`leave` is both leaving the |
| 157 | + * federation and dropping a note). For those, the endpoint decides; `ACTIONS` holds the |
| 158 | + * meaning used when no variant matches. |
| 159 | + */ |
| 160 | +const VARIANTS: { name: string; href: RegExp; spec: ActionSpec }[] = [ |
| 161 | + { |
| 162 | + name: 'leave', |
| 163 | + href: /\/ships\/[^/]+\/drops$/, |
| 164 | + spec: { |
| 165 | + label: 'Leave a note', |
| 166 | + group: 'social', |
| 167 | + help: 'Leave a note at this waypoint for other captains to find.', |
| 168 | + fields: { text: { label: 'Note', input: 'longtext' } } |
| 169 | + } |
| 170 | + }, |
| 171 | + { |
| 172 | + name: 'withdraw', |
| 173 | + href: /\/charts\/offers\/[^/]+\/withdraw$/, |
| 174 | + spec: { label: 'Withdraw offer', group: 'social', confirm: true, help: 'Take your chart offer off the market.' } |
| 175 | + }, |
| 176 | + { |
| 177 | + name: 'withdraw', |
| 178 | + href: /\/postings\/[^/]+\/withdraw$/, |
| 179 | + spec: { label: 'Withdraw posting', group: 'social', confirm: true, help: 'Take the posting down.' } |
| 180 | + }, |
| 181 | + { |
| 182 | + name: 'accept', |
| 183 | + href: /\/postings\/[^/]+\/accept$/, |
| 184 | + spec: { label: 'Take job', group: 'social', confirm: true, help: 'Take on this posting and haul the cargo.' } |
| 185 | + }, |
| 186 | + { |
| 187 | + name: 'post', |
| 188 | + href: /\/postings$/, |
| 189 | + spec: { |
| 190 | + label: 'Post a job', |
| 191 | + group: 'social', |
| 192 | + help: 'Offer a reward to whoever hauls cargo to a station.', |
| 193 | + fields: { |
| 194 | + good: { label: 'Good', input: 'good' }, |
| 195 | + units: units('How many units to deliver.'), |
| 196 | + reward: { label: 'Reward (credits)', input: 'quantity', min: 1 }, |
| 197 | + station: { label: 'Station' }, |
| 198 | + hours: { label: 'Hours open', input: 'quantity', min: 1 } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +]; |
| 203 | + |
| 204 | +/** An action by name alone, or with its endpoint so reused names resolve to the right meaning. */ |
| 205 | +export type ActionRef = string | Pick<GameAction, 'name' | 'href'>; |
| 206 | + |
| 155 | 207 | export function humanize(name: string): string { |
| 156 | 208 | const s = name.replaceAll('_', ' '); |
| 157 | 209 | return s.charAt(0).toUpperCase() + s.slice(1); |
| 158 | 210 | } |
| 159 | 211 | |
| 160 | | −export function specFor(name: string): ActionSpec { |
| 161 | | − return ACTIONS[name] ?? { label: humanize(name), group: 'ship', help: '' }; |
| 212 | +function lookup(action: ActionRef): ActionSpec | undefined { |
| 213 | + if (typeof action === 'string') return ACTIONS[action]; |
| 214 | + const variant = VARIANTS.find((v) => v.name === action.name && v.href.test(action.href.split('?')[0])); |
| 215 | + return variant?.spec ?? ACTIONS[action.name]; |
| 216 | +} |
| 217 | + |
| 218 | +export function specFor(action: ActionRef): ActionSpec { |
| 219 | + const name = typeof action === 'string' ? action : action.name; |
| 220 | + return lookup(action) ?? { label: humanize(name), group: 'ship', help: '' }; |
| 162 | 221 | } |
| 163 | 222 | |
| 164 | | −export function fieldSpec(action: string, field: string): FieldSpec { |
| 165 | | − return ACTIONS[action]?.fields?.[field] ?? { label: humanize(field) }; |
| 223 | +export function fieldSpec(action: ActionRef, field: string): FieldSpec { |
| 224 | + return lookup(action)?.fields?.[field] ?? { label: humanize(field) }; |
| 166 | 225 | } |
| 167 | 226 | |
| 168 | 227 | /** Ship actions always shown in the command dock; when absent, `whyUnavailable` says why. */ |