feat(saves): edit research branches in local saves
This commit is contained in:
1 parent
f5c5377d44
commit
61255f5473
14 files changed
+291
-15
No files matched your search
@@ -6,6 +6,7 @@ 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 { editSaveResearch, readSaveResearch } from './saves/save-research.ts';
|
||||
export { editSaveCurrencies, listSaveSummaries, readSaveDetails } from './saves/save-editor.ts';
|
||||
export { importOfficialBackup, previewOfficialBackup } from './saves/import/official-backup.ts';
|
||||
export { prepareTransferredProfile } from './saves/transferred-profile.ts';
|
||||
|
||||
@@ -41,7 +41,7 @@ function summary(account: Account, profile: BlkBlock | undefined) {
|
||||
};
|
||||
}
|
||||
|
||||
function snapshot(dataFolder: string, userId: string): BlkBlock | undefined {
|
||||
export function readSaveSnapshot(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')));
|
||||
@@ -72,13 +72,13 @@ function battleEntries(profile: BlkBlock) {
|
||||
}
|
||||
|
||||
export function listSaveSummaries(dataFolder: string) {
|
||||
return new AccountStore(dataFolder).all().map((account) => summary(account, snapshot(dataFolder, account.userId)));
|
||||
return new AccountStore(dataFolder).all().map((account) => summary(account, readSaveSnapshot(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 profile = readSaveSnapshot(dataFolder, userId);
|
||||
const owned = profile ? [...ownedVehicles(profile)].sort() : [];
|
||||
const research = profile
|
||||
? (findBlock(profile, 'unitResearch')?.blocks ?? []).map(({ name }) => ({ name, points: researchPointsOf(profile, name) }))
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { AccountStore } from '../accounts/account-store.ts';
|
||||
import { ClanStore } from '../clans/clan-store.ts';
|
||||
import { DatasetCache } from '../game-data/datasets/dataset-cache.ts';
|
||||
import { vehicleCatalogue } from '../game-data/datasets/vehicle-catalogue.ts';
|
||||
import { GameFiles } from '../game-data/game-files.ts';
|
||||
import { addOwnedVehicle, ownedVehicles } from '../profile/profile-hangar.ts';
|
||||
import { ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts';
|
||||
import { researchPointsOf, setResearchPoints } from '../profile/profile-vehicle-research.ts';
|
||||
import { setUpProfiles } from '../profiles-setup.ts';
|
||||
import { TechTree } from '../research/tech-tree.ts';
|
||||
import { loadTuning } from '../tuning/tuning.ts';
|
||||
import { readSaveSnapshot, type SaveLocation } from './save-editor.ts';
|
||||
|
||||
export interface ResearchBranch {
|
||||
readonly country: string;
|
||||
readonly type: string;
|
||||
}
|
||||
export type ResearchOperation = 'research' | 'purchase' | 'equipment';
|
||||
|
||||
function tree(location: SaveLocation) {
|
||||
const gameFiles = new GameFiles(location.gameFolder);
|
||||
const datasets = new DatasetCache(location.dataFolder, gameFiles, () => {});
|
||||
const vehicles = datasets.load(vehicleCatalogue);
|
||||
return { gameFiles, datasets, vehicles, catalogue: new TechTree(vehicles) };
|
||||
}
|
||||
|
||||
export function readSaveResearch(location: SaveLocation, userId: string, selection?: ResearchBranch) {
|
||||
if (!new AccountStore(location.dataFolder).byUserId(userId)) return null;
|
||||
const { catalogue } = tree(location);
|
||||
const branches = catalogue.branches().map(({ country, type }) => ({ country, type }));
|
||||
const selected = branches.find((branch) => branch.country === selection?.country && branch.type === selection?.type) ?? branches[0] ?? null;
|
||||
const save = readSaveSnapshot(location.dataFolder, userId);
|
||||
const owned = save ? ownedVehicles(save) : new Set<string>();
|
||||
const vehicles = [...catalogue.all()]
|
||||
.filter((unit) => unit.inTechTree && unit.country === selected?.country && unit.type === selected?.type)
|
||||
.sort((a, b) => a.rank - b.rank || a.treeOrder - b.treeOrder || a.name.localeCompare(b.name))
|
||||
.map((unit) => ({
|
||||
name: unit.name, rank: unit.rank, owned: owned.has(unit.name),
|
||||
points: save ? researchPointsOf(save, unit.name) : 0, needed: unit.researchPoints,
|
||||
}));
|
||||
return { branches, selected, vehicles };
|
||||
}
|
||||
|
||||
export function editSaveResearch(location: SaveLocation, userId: string, branch: ResearchBranch, operation: ResearchOperation) {
|
||||
const accounts = new AccountStore(location.dataFolder);
|
||||
if (!accounts.byUserId(userId)) return null;
|
||||
const { gameFiles, datasets, vehicles, catalogue } = tree(location);
|
||||
const units = [...catalogue.all()].filter((unit) => unit.inTechTree && unit.country === branch.country && unit.type === branch.type);
|
||||
if (!units.length) return null;
|
||||
const tuning = loadTuning(location.dataFolder, () => {});
|
||||
const clans = new ClanStore(location.dataFolder, tuning.clans);
|
||||
const { profiles, equipment } = setUpProfiles(location.dataFolder, gameFiles, datasets, tuning, { accounts, clans }, vehicles);
|
||||
profiles.update(userId, (profile) => {
|
||||
const owned = ownedVehicles(profile);
|
||||
for (const unit of units) {
|
||||
if (operation === 'research' && unit.kind === 'research' && !owned.has(unit.name)) {
|
||||
setResearchPoints(profile, unit.name, Math.max(researchPointsOf(profile, unit.name), unit.researchPoints));
|
||||
}
|
||||
if (operation === 'purchase' && (unit.kind === 'research' || unit.kind === 'purchase')) {
|
||||
addOwnedVehicle(profile, unit.name);
|
||||
for (const part of unit.parts) addOwnedVehicle(profile, part);
|
||||
}
|
||||
if (operation === 'equipment' && owned.has(unit.name)) {
|
||||
for (const mod of equipment.vehicle(unit.name)?.modifications ?? []) {
|
||||
if (ownsModification(profile, unit.name, mod.name)) continue;
|
||||
writeModificationRecord(profile, unit.name, mod.name, {
|
||||
silverLionsSpent: 0, goldenEaglesSpent: 0, enabled: true, count: Math.max(1, mod.maxCount),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return readSaveResearch(location, userId, branch);
|
||||
}
|
||||
Reference in new issue
Block a user