MessageSceneModel.ts 1.4 KB
import { ReadonlyPlot, GameRecord, getPlot } from "../../avg/AVG";
import { ConfigManager } from "simba-config-manager";
import { messageSceneConfig } from "../../config/MessageSceneConfig";

export class MessageSceneModel {
    private _id: number;
    get id() { return this._id }

    get config() { return ConfigManager.getConfig(messageSceneConfig, this._id); }

    private _firstPlot: ReadonlyPlot;
    get firstPlot() { return this._firstPlot; }
    set firstPlot(plot: ReadonlyPlot) {
        this._firstPlot = plot;
        GameRecord.recordVariables[this._recordKey + ".f"] = plot.id;
    }

    private _lastPlot?: ReadonlyPlot;
    get lastPlot() { return this._lastPlot; }
    set lastPlot(plot: ReadonlyPlot | undefined) {
        if (this._lastPlot === plot) return;
        this._lastPlot = plot;
        if (plot) {
            GameRecord.recordVariables[this._recordKey + ".l"] = plot.id;
        }
    }

    private _recordKey: string;

    constructor(id: number) {
        this._id = id;
        this._recordKey = "m" + this._id;
    }

    async initFromRecords() {
        let firstPlotId = GameRecord.recordVariables[this._recordKey + ".f"] as number;
        let lastPlotId = GameRecord.recordVariables[this._recordKey + ".l"] as number;
        this._firstPlot = (await getPlot(firstPlotId))!;
        if (lastPlotId !== undefined) {
            this._lastPlot = await getPlot(lastPlotId);
        }
    }
}