54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { Action } from '@idle-economy/engine';
|
|
import { defaultGameRulesOptions, type GameRulesOptions } from '@idle-economy/engine/src/rulesets/rules';
|
|
import { Simulation, type SimulationStats } from '@idle-economy/simulator';
|
|
import { makeAutoObservable, runInAction } from 'mobx';
|
|
|
|
export class Root {
|
|
|
|
public readonly simulation: Simulation;
|
|
|
|
public simulationTime: number = 0;
|
|
public stats: SimulationStats | undefined;
|
|
public lastResetStats: SimulationStats | undefined;
|
|
public gameOptions: GameRulesOptions = defaultGameRulesOptions;
|
|
|
|
constructor() {
|
|
|
|
this.simulation = new Simulation(
|
|
{
|
|
onAction: this.handleSimulatorAction.bind(this),
|
|
onStats: this.handleSimulatorStats.bind(this),
|
|
},
|
|
);
|
|
|
|
this.reset();
|
|
|
|
makeAutoObservable(this);
|
|
}
|
|
|
|
public reset() {
|
|
this.lastResetStats = this.stats;
|
|
this.simulation.initialize(this.gameOptions);
|
|
}
|
|
|
|
public setGameOptions(value: GameRulesOptions) {
|
|
this.gameOptions = value;
|
|
this.reset();
|
|
}
|
|
|
|
private handleSimulatorAction(action: Action): void {
|
|
// console.dir(action);
|
|
}
|
|
|
|
private handleSimulatorStats(stats: SimulationStats): void {
|
|
runInAction(() => {
|
|
if (stats.actions.length > 0) {
|
|
this.stats = { ...stats, actions: [...stats.actions] };
|
|
}
|
|
this.simulationTime = this.simulation.time;
|
|
});
|
|
}
|
|
}
|
|
|
|
export const root = new Root();
|