Replace O(N^2) linear lookups and repeated array filtering across modifications, weapons, modsResearch and unitResearch with single-pass Map indexes, pre-sort catalogue equipment once on load, avoid redundant pre-change disk writes in PlayerProfiles.updateWhen, and shallow-copy only top-level lists in clientProfile.
69 lines
2.7 KiB
TypeScript
69 lines
2.7 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 (clan sync, 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 every action returns a consistent persisted profile.
|
|
*/
|
|
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, userId: string) => 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 loaded = this.#saves.loadProfile(userId);
|
|
const profile = loaded ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
|
|
this.#refresh(profile, userId);
|
|
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.
|
|
*
|
|
* Runs one pre-change refresh in memory (without an extra disk write) so handlers see current
|
|
* derived state, then refreshes and persists once when the change succeeds.
|
|
*/
|
|
updateWhen(userId: string, change: (profile: BlkBlock) => boolean): BlkBlock | undefined {
|
|
const loaded = this.#saves.loadProfile(userId);
|
|
const profile = loaded ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
|
|
if (!loaded) {
|
|
this.#refresh(profile, userId);
|
|
}
|
|
if (!change(profile)) {
|
|
if (!loaded) this.#saves.saveProfile(userId, profile);
|
|
return undefined;
|
|
}
|
|
this.#refresh(profile, userId);
|
|
this.#saves.saveProfile(userId, profile);
|
|
return profile;
|
|
}
|
|
}
|