PlotBranchManager.ts 2.7 KB
import { GameRecord, SpecialPlotId } from "../../avg/AVG";
import { UIManager } from "../../common/gameplay/managers/UIManager";
import AlertDialogViewPresenter from "../ui/presenter/AlertDialogViewPresenter";
import { AlertDialogViewProps } from "../ui/view/type/AlertDialogView";

export namespace PlotBranchManager {
    /**存档Key */
    const KEY_PLOT_RECORD = 'r_save_p';
    const ONLYONE = true;

    export function savePlotIdToGlobal(recordModel: IPlotRecordModel) {
        //如果保存的结构体不存在或者存档剧情中的id是剧情结束或者未完待续这两种特殊剧情,弹出提示并直接返回不做存档处理
        if (!recordModel || recordModel.plotId === SpecialPlotId.End || recordModel.plotId === SpecialPlotId.ToBeContinued) {
            UIManager.showSceneToast("存档数据错误!");
            return;
        }
        let temp: AlertDialogViewProps =
        {
            dataptr: {},
            titlecontent: "提示",
            content: "确定将当前剧情覆盖到存档位吗?",
            ishasad: true,
            istwobtn: true,
            adconfig: "inject_fruit",
            hasBanner: false,
            callback: (type, ret) => {
                if (ret) {
                    let saveAlredy: boolean = checkRecordAlredy(recordModel.plotId);
                    if (saveAlredy) {
                        UIManager.showSceneToast("你已经在此处存储过存档");
                    } else {
                        savePlotRecord(recordModel);
                        UIManager.showSceneToast("保存成功");
                    }
                }
            }
        };
        UIManager.pushPresenter(AlertDialogViewPresenter, temp);
    }

    /**检测是否已经存档过当前剧情 */
    function checkRecordAlredy(pid: number): boolean {
        let ret: boolean = false;
        let s = GameRecord.globalVariables[KEY_PLOT_RECORD] as string;
        if (s && "" !== s.trim()) {
            let mArr: IPlotRecordModel[] = JSON.parse(s) as IPlotRecordModel[];
            ret = mArr.findIndex((v) => v.plotId === pid) !== (-1);
        }
        return ret;
    }

    /**实际存储 */
    function savePlotRecord(recordModel: IPlotRecordModel) {
        let mArr: IPlotRecordModel[] = [];
        if (!ONLYONE) {
            let s = GameRecord.globalVariables[KEY_PLOT_RECORD] as string;
            if (s && "" !== s.trim()) {
                mArr = JSON.parse(s) as IPlotRecordModel[];
            }
        }
        mArr.push(recordModel);
        GameRecord.globalVariables[KEY_PLOT_RECORD] = JSON.stringify(mArr);
        GameRecord.saveRecord();
    }
}

/**存档结构体 */
export interface IPlotRecordModel {
    plotId: number;
    desc: string;
}