Add local save selection and editing
This commit is contained in:
1 parent
900eb90b42
commit
d19baec90b
18 files changed
+634
-4
No files matched your search
@@ -6,4 +6,5 @@ export { LOOPBACK, startServer, type ServerOptions, type RunningServer } from '.
|
||||
export { AccountStore, type RegistrationResult } from './accounts/account-store.ts';
|
||||
export { REGISTRATION_PATH } from './accounts/registration-service.ts';
|
||||
export { HEALTH_PATH } from './transport/http-listener.ts';
|
||||
export { editSaveCurrencies, listSaveSummaries, readSaveDetails } from './saves/save-editor.ts';
|
||||
export { multiVehiclePackIds } from './store/store-pack-ids.ts';
|
||||
@@ -0,0 +1,105 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { decodeBlk, findBlock, findParam, type BlkBlock } from '@wto/blk';
|
||||
import { AccountStore, type Account } from '../accounts/account-store.ts';
|
||||
import { ClanStore } from '../clans/clan-store.ts';
|
||||
import { DatasetCache } from '../game-data/datasets/dataset-cache.ts';
|
||||
import { GameFiles } from '../game-data/game-files.ts';
|
||||
import { intField, stringField } from '../profile/blk-fields.ts';
|
||||
import { balanceOf, setGjnCents, setGoldenEagles, setSilverLions } from '../profile/profile-currency.ts';
|
||||
import { remainingPremiumDays } from '../profile/profile-entitlements.ts';
|
||||
import { ownedVehicles } from '../profile/profile-hangar.ts';
|
||||
import { freeResearchPoints, setFreeResearchPoints } from '../profile/profile-research-points.ts';
|
||||
import { EULT_SESSION_RESULT } from '../profile/profile-trophies-log.ts';
|
||||
import { researchPointsOf } from '../profile/profile-vehicle-research.ts';
|
||||
import { serverTimeNow } from '../profile/server-time.ts';
|
||||
import { setUpProfiles } from '../profiles-setup.ts';
|
||||
import { loadTuning } from '../tuning/tuning.ts';
|
||||
|
||||
export interface SaveLocation {
|
||||
readonly gameFolder: string;
|
||||
readonly dataFolder: string;
|
||||
}
|
||||
|
||||
export interface CurrencyEdits {
|
||||
readonly silverLions?: number | undefined;
|
||||
readonly goldenEagles?: number | undefined;
|
||||
readonly gjnCents?: number | undefined;
|
||||
readonly freeRp?: number | undefined;
|
||||
}
|
||||
|
||||
function summary(account: Account, profile: BlkBlock | undefined) {
|
||||
const battles = profile ? battleEntries(profile) : [];
|
||||
const { userId, login, nick, created } = account;
|
||||
return {
|
||||
userId, login, nick, created,
|
||||
currencies: profile ? { ...balanceOf(profile), freeRp: freeResearchPoints(profile) } : null,
|
||||
vehicleCount: profile ? ownedVehicles(profile).size : 0,
|
||||
battleCount: battles.length,
|
||||
lastBattle: battles[0]?.time ?? null,
|
||||
premiumDays: profile ? remainingPremiumDays(profile, serverTimeNow()) : 0,
|
||||
};
|
||||
}
|
||||
|
||||
function snapshot(dataFolder: string, userId: string): BlkBlock | undefined {
|
||||
if (!/^\d+$/.test(userId)) throw new Error('Invalid local account id.');
|
||||
try {
|
||||
return decodeBlk(readFileSync(join(dataFolder, 'saves', userId, 'profile.blk')));
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function battleEntries(profile: BlkBlock) {
|
||||
return (findBlock(profile, 'userlogs')?.blocks ?? [])
|
||||
.filter((entry) => intField(entry, 'type') === EULT_SESSION_RESULT)
|
||||
.map((entry) => {
|
||||
const body = findBlock(entry, 'body');
|
||||
const win = body && findParam(body, 'win')?.value;
|
||||
return {
|
||||
id: entry.name,
|
||||
time: intField(entry, 'time'),
|
||||
won: win?.type === 'bool' ? win.value : null,
|
||||
mission: stringField(body, 'locName') || stringField(body, 'mission'),
|
||||
mode: stringField(body, 'eventId'),
|
||||
vehicles: (body && findBlock(body, 'aircrafts')?.params.map(({ name }) => name)) ?? [],
|
||||
silverLions: intField(body, 'wpEarned'),
|
||||
researchPoints: intField(body, 'xpEarned'),
|
||||
};
|
||||
})
|
||||
.sort((a, b) => b.time - a.time || b.id.localeCompare(a.id));
|
||||
}
|
||||
|
||||
export function listSaveSummaries(dataFolder: string) {
|
||||
return new AccountStore(dataFolder).all().map((account) => summary(account, snapshot(dataFolder, account.userId)));
|
||||
}
|
||||
|
||||
export function readSaveDetails(dataFolder: string, userId: string) {
|
||||
const account = new AccountStore(dataFolder).byUserId(userId);
|
||||
if (!account) return null;
|
||||
const profile = snapshot(dataFolder, userId);
|
||||
const owned = profile ? [...ownedVehicles(profile)].sort() : [];
|
||||
const research = profile
|
||||
? (findBlock(profile, 'unitResearch')?.blocks ?? []).map(({ name }) => ({ name, points: researchPointsOf(profile, name) }))
|
||||
.filter(({ points }) => points > 0)
|
||||
: [];
|
||||
return { save: summary(account, profile), battles: profile ? battleEntries(profile) : [], owned, research };
|
||||
}
|
||||
|
||||
export function editSaveCurrencies(location: SaveLocation, userId: string, values: CurrencyEdits): boolean {
|
||||
const accounts = new AccountStore(location.dataFolder);
|
||||
if (!accounts.byUserId(userId)) return false;
|
||||
const tuning = loadTuning(location.dataFolder, () => {});
|
||||
const gameFiles = new GameFiles(location.gameFolder);
|
||||
const datasets = new DatasetCache(location.dataFolder, gameFiles, () => {});
|
||||
const clans = new ClanStore(location.dataFolder, tuning.clans);
|
||||
const { profiles } = setUpProfiles(location.dataFolder, gameFiles, datasets, tuning, { accounts, clans });
|
||||
profiles.update(userId, (profile) => {
|
||||
if (values.silverLions !== undefined) setSilverLions(profile, values.silverLions);
|
||||
if (values.goldenEagles !== undefined) setGoldenEagles(profile, values.goldenEagles);
|
||||
if (values.gjnCents !== undefined) setGjnCents(profile, values.gjnCents);
|
||||
if (values.freeRp !== undefined) setFreeResearchPoints(profile, values.freeRp);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user