Include the default pilot icon in client profile replies when a save has no selected icon. Preserve chosen and legacy icons without modifying stored saves.
44 lines
1.9 KiB
TypeScript
44 lines
1.9 KiB
TypeScript
/**
|
|
* The player's card in the profile:
|
|
* title:t the chosen title (profile top level, read by the client's
|
|
* profile serializer)
|
|
* pilotAppearance{ pilotIcon:t background:t frame:t }
|
|
* what cln_save_pilot_appearance sets; the client itself reads
|
|
* it back through cln_get_users_terse_info, so the block is the
|
|
* local server's own record
|
|
*/
|
|
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk';
|
|
|
|
const TITLE = 'title';
|
|
const APPEARANCE = 'pilotAppearance';
|
|
export const DEFAULT_PILOT_ICON = 'cardicon_default';
|
|
export const APPEARANCE_FIELDS = ['pilotIcon', 'background', 'frame'] as const;
|
|
export type AppearanceField = (typeof APPEARANCE_FIELDS)[number];
|
|
|
|
export function chosenTitle(profile: BlkBlock): string {
|
|
const value = findParam(profile, TITLE)?.value;
|
|
return value?.type === 'string' ? value.value : '';
|
|
}
|
|
|
|
export function setChosenTitle(profile: BlkBlock, title: string): void {
|
|
setParam(profile, TITLE, { type: 'string', value: title });
|
|
}
|
|
|
|
export function appearance(profile: BlkBlock): Partial<Record<AppearanceField, string>> {
|
|
const block = findBlock(profile, APPEARANCE);
|
|
const result: Partial<Record<AppearanceField, string>> = {};
|
|
for (const field of APPEARANCE_FIELDS) {
|
|
const value = block && findParam(block, field)?.value;
|
|
if (value?.type === 'string') result[field] = value.value;
|
|
}
|
|
if (result.pilotIcon === undefined) {
|
|
const legacy = findParam(profile, 'pilotIcon')?.value;
|
|
if (legacy?.type === 'string') result.pilotIcon = legacy.value;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function setAppearance(profile: BlkBlock, field: AppearanceField, image: string): void {
|
|
setParam(ensureBlock(profile, APPEARANCE), field, { type: 'string', value: image });
|
|
}
|