Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
1.9 kB · 70 lines
TypeScript
at develop
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import type { Snapshot, ID } from "./types";
/** A save place to store your snapshots. */export class Vault { private _vault: Snapshot[] = []; private _vaultSize = 120;
/** Get a Snapshot by its ID. */ getById(id: ID): Snapshot { return this._vault.filter((snapshot) => snapshot.id === id)?.[0]; }
/** Clear this Vault */ clear(): void { this._vault = []; }
/** Get the latest snapshot */ get(): Snapshot | undefined; /** Get the two snapshots around a specific time */ get(time: number): { older: Snapshot; newer: Snapshot } | undefined; /** Get the closest snapshot to e specific time */ get(time: number, closest: boolean): Snapshot | undefined;
get(time?: number, closest?: boolean) { // zero index is the newest snapshot const sorted = this._vault.sort((a, b) => b.time - a.time); if (typeof time === "undefined") return sorted[0];
for (let i = 0; i < sorted.length; i++) { const snap = sorted[i]; if (snap.time <= time) { const snaps = { older: sorted[i], newer: sorted[i - 1] }; if (closest) { const older = Math.abs(time - snaps.older.time); const newer = Math.abs(time - snaps.newer?.time); if (Number.isNaN(newer)) return snaps.older; if (newer <= older) return snaps.older; return snaps.newer; } return snaps; } } return; }
/** Add a snapshot to the vault. */ add(snapshot: Snapshot) { if (this._vault.length > this._vaultSize - 1) { // remove the oldest snapshot this._vault.sort((a, b) => a.time - b.time).shift(); } this._vault.push(snapshot); }
/** Get the current capacity (size) of the vault. */ public get size() { return this._vault.length; }
/** Set the max capacity (size) of the vault. */ setMaxSize(size: number) { this._vaultSize = size; }
/** Get the max capacity (size) of the vault. */ getMaxSize() { return this._vaultSize; }}