Files
WT-Offline/packages/local-service/src/profile/player-profiles.ts
T
N0VAN0M4LY 7a08fff85e Implement vehicle research and purchases
Recompute unlocked ranks, prerequisites, and unit states before serving
profiles. Add research target selection, research conversion, vehicle
and slot purchases, and the price table. Support editing convertible RP
and restrict hidden vehicle visibility to eligible entries.

The client still displays zero purchase prices pending price table
integration.
2026-11-24 21:04:19 +08:00

58 lines
2.2 KiB
TypeScript

/**
* The one way services get a player's profile: the saved one, or a new one that is written
* to the save before it is handed out.
*
* State that follows from the rest of the profile (statuses, open ranks, earned unlocks:
* research/research-state.ts) is brought up to date every time a profile is handed out or
* changed, and saved with it, so it also follows a game update that changed the data.
*/
import type { BlkBlock } from '@wto/blk';
import type { GameFiles } from '../game-data/game-files.ts';
import type { SaveStore } from '../saves/save-store.ts';
import { newProfile } from './new-profile.ts';
import type { StartingContent } from './starting-content.ts';
/** Brings the derived state of a profile up to date, in place. */
export type ProfileRefresh = (profile: BlkBlock) => void;
export class PlayerProfiles {
readonly #saves: SaveStore;
readonly #gameFiles: GameFiles;
readonly #startingContent: StartingContent;
readonly #refresh: ProfileRefresh;
constructor(saves: SaveStore, gameFiles: GameFiles, startingContent: StartingContent, refresh: ProfileRefresh) {
this.#saves = saves;
this.#gameFiles = gameFiles;
this.#startingContent = startingContent;
this.#refresh = refresh;
}
profileOf(userId: string): BlkBlock {
const profile = this.#saves.loadProfile(userId) ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
this.#refresh(profile);
this.#saves.saveProfile(userId, profile);
return profile;
}
/** Change the player's profile and write it to the save before returning it. */
update(userId: string, change: (profile: BlkBlock) => void): BlkBlock {
return this.updateWhen(userId, (profile) => {
change(profile);
return true;
})!;
}
/**
* Change the player's profile if `change` accepts (returns true); the change must not touch
* the profile when it refuses. Returns the saved profile, or undefined when refused.
*/
updateWhen(userId: string, change: (profile: BlkBlock) => boolean): BlkBlock | undefined {
const profile = this.profileOf(userId);
if (!change(profile)) return undefined;
this.#refresh(profile);
this.#saves.saveProfile(userId, profile);
return profile;
}
}