diff --git a/packages/local-service/src/char/actions/action-table.ts b/packages/local-service/src/char/actions/action-table.ts index 9c348a7..4650f22 100644 --- a/packages/local-service/src/char/actions/action-table.ts +++ b/packages/local-service/src/char/actions/action-table.ts @@ -7,6 +7,7 @@ import { acceptEula } from './agreement-actions.ts'; import { bulkTrainCrews, trainCrew } from './crew-actions.ts'; import { buyAllModificationsAction, buyModificationAction, buySparesAction, enableModificationsAction } from './modification-actions.ts'; import { purchaseSkillPointsAction, specializeCrewAction, upgradeCrewAction } from './crew-skill-actions.ts'; +import { savePilotAppearanceAction, selectTitleAction, usersTerseInfoAction } from './player-card-actions.ts'; import { buyResourceAction } from './decor-actions.ts'; import { saveDecorLayoutsAction } from './decor-layout-actions.ts'; import { entitlementsFull, initialMeta, legacyUserData } from './online-init-actions.ts'; @@ -43,6 +44,9 @@ export function actionTable(deps: ActionDependencies): ReadonlyMap{ nick:t pilotIcon:t pilotId:t title:t clanTag:t clanName:t background:t frame:t } + * or { failed:b=yes } for unknown users + */ +import type { BlkBlock, BlkParam } from '@wto/blk'; +import type { AccountStore } from '../../accounts/account-store.ts'; +import { chooseTitle, saveAppearance } from '../../player-card/player-card-rules.ts'; +import { appearance, chosenTitle } from '../../profile/profile-appearance.ts'; +import type { PlayerProfiles } from '../../profile/player-profiles.ts'; +import type { CharActionHandler } from '../char-action.ts'; +import { stringParam } from './payload-params.ts'; +import { ruleAction } from './rule-answer.ts'; +import { withAccount } from './account-context.ts'; + +export function selectTitleAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { + return ruleAction(profiles, accounts, (payload) => { + const title = stringParam(payload, 'name'); + if (title === undefined) return undefined; + return (profile) => chooseTitle(profile, title); + }); +} + +export function savePilotAppearanceAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { + return ruleAction(profiles, accounts, (payload) => { + const choices = payload.params.flatMap((param) => param.value.type === 'string' ? [{ field: param.name, image: param.value.value }] : []); + return (profile) => saveAppearance(profile, choices); + }); +} + +export function usersTerseInfoAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { + return (context) => withAccount(context, accounts, () => { + const list = context.payload ? stringParam(context.payload, 'usersList') : undefined; + if (list === undefined) return { kind: 'error', code: 'BAD_PARAMS' }; + const blocks = list.split(';').filter((uid) => uid !== '').map((uid): BlkBlock => { + const account = accounts.byUserId(uid); + if (!account) return { name: uid, params: [{ name: 'failed', value: { type: 'bool', value: true } }], blocks: [] }; + const profile = profiles.profileOf(account.userId); + const looks = appearance(profile); + const fields: [string, string][] = [ + ['nick', account.nick], ['pilotIcon', looks.pilotIcon ?? ''], ['title', chosenTitle(profile)], + ['background', looks.background ?? ''], ['frame', looks.frame ?? ''], + ]; + return { name: uid, params: fields.map(([name, value]): BlkParam => ({ name, value: { type: 'string', value } })), blocks: [] }; + }); + return { kind: 'data', blk: { name: '', params: [], blocks } }; + }); +} diff --git a/packages/local-service/src/player-card/player-card-rules.ts b/packages/local-service/src/player-card/player-card-rules.ts new file mode 100644 index 0000000..98d5535 --- /dev/null +++ b/packages/local-service/src/player-card/player-card-rules.ts @@ -0,0 +1,24 @@ +/** + * Player card actions: + * cln_select_title (select_current_title, aces.exe 0x1555b60): name:t; a title has to be an + * earned unlock (titles are unlocks), "" clears it + * cln_save_pilot_appearance (scripts user/profileAppearance.nut): :t; + * the choices are unlock images the client already filters to the available ones + */ +import type { BlkBlock } from '@wto/blk'; +import { APPEARANCE_FIELDS, setAppearance, setChosenTitle, type AppearanceField } from '../profile/profile-appearance.ts'; +import { earnedUnlocks } from '../profile/profile-unlocks.ts'; +import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; + +export function chooseTitle(profile: BlkBlock, title: string): RuleOutcome { + if (title !== '' && !earnedUnlocks(profile).has(title)) return refused('NOT_AVAILABLE'); + setChosenTitle(profile, title); + return ACCEPTED; +} + +export function saveAppearance(profile: BlkBlock, choices: readonly { field: string; image: string }[]): RuleOutcome { + const known = choices.filter((choice): choice is { field: AppearanceField; image: string } => (APPEARANCE_FIELDS as readonly string[]).includes(choice.field)); + if (known.length === 0) return refused('BAD_PARAMS'); + for (const { field, image } of known) setAppearance(profile, field, image); + return ACCEPTED; +} diff --git a/packages/local-service/src/profile/profile-appearance.ts b/packages/local-service/src/profile/profile-appearance.ts new file mode 100644 index 0000000..9645354 --- /dev/null +++ b/packages/local-service/src/profile/profile-appearance.ts @@ -0,0 +1,38 @@ +/** + * 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 service's own record + */ +import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; + +const TITLE = 'title'; +const APPEARANCE = 'pilotAppearance'; +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> { + const block = findBlock(profile, APPEARANCE); + const result: Partial> = {}; + for (const field of APPEARANCE_FIELDS) { + const value = block && findParam(block, field)?.value; + if (value?.type === 'string') result[field] = value.value; + } + return result; +} + +export function setAppearance(profile: BlkBlock, field: AppearanceField, image: string): void { + setParam(ensureBlock(profile, APPEARANCE), field, { type: 'string', value: image }); +}