Persist the accepted EULA version before responding and return both agreement fields in the profile. Respond to matching heartbeats.
35 lines
1.1 KiB
TypeScript
35 lines
1.1 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.
|
|
*/
|
|
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';
|
|
|
|
export class PlayerProfiles {
|
|
readonly #saves: SaveStore;
|
|
readonly #gameFiles: GameFiles;
|
|
|
|
constructor(saves: SaveStore, gameFiles: GameFiles) {
|
|
this.#saves = saves;
|
|
this.#gameFiles = gameFiles;
|
|
}
|
|
|
|
profileOf(userId: string): BlkBlock {
|
|
const saved = this.#saves.loadProfile(userId);
|
|
if (saved) return saved;
|
|
const created = newProfile(this.#gameFiles.newAccountTemplate());
|
|
this.#saves.saveProfile(userId, created);
|
|
return created;
|
|
}
|
|
|
|
/** Change the player's profile and write it to the save before returning it. */
|
|
update(userId: string, change: (profile: BlkBlock) => void): BlkBlock {
|
|
const profile = this.profileOf(userId);
|
|
change(profile);
|
|
this.#saves.saveProfile(userId, profile);
|
|
return profile;
|
|
}
|
|
}
|