<commit_before>import * as RNFS from "react-native-fs";
import BaseContainer from "../../../lib/container/base/BaseContainer";
import * as MstContainers from "../../../model/impl/MstContainer";
import {
    MstSvtLimitContainer,
    MstSvtTreasureDeviceContainer,
    MstTreasureDeviceLvContainer
} from "../../../model/impl/MstContainer";
import MstUtil from "../utility/MstUtil";
import {EmbeddedCodeConverted, TransSvtName} from "../../../model/master/EmbeddedCodeConverted";
import {MstSvtLimit, MstSvtTreasureDevice, MstTreasureDeviceLv} from "../../../model/master/Master";
import {MstGoal, defaultMstGoal} from "./MstGoal";
import Const from "../const/Const";

export default class MstLoader {

    private static _instance: MstLoader = undefined;

    public static get instance(): MstLoader {
        if (MstLoader._instance === undefined) {
            MstLoader._instance = new MstLoader();
        }
        return MstLoader._instance;
    }

    private constructor() {
        this._cache = new Map<string, BaseContainer<any>>();
    }

    private _cache: Map<string, BaseContainer<any>>;
    private _embeddedCode: EmbeddedCodeConverted;

    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* MAIN
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    public async loadModel(name: string): Promise<BaseContainer<any>> {
        if (this._cache.has(name)) {
            return Promise.resolve(this._cache.get(name));
        }

        let rawData = await MstUtil.instance.loadJson(
            `${await MstUtil.instance.getDbPath()}/master/${name}.json`
        );

        let containerName = `${name}Container`;
        let instance = new MstContainers[containerName]();
        instance.parse(rawData);
        this._cache.set(name, instance);

        return Promise.resolve(instance);
    }

    public async loadEmbeddedCode(): Promise<EmbeddedCodeConverted> {
        if (this._embeddedCode) {
            return Promise.resolve(this._embeddedCode);
        }

        let data = await MstUtil.instance.loadJson(
            `${await MstUtil.instance.getDbPath()}/embedded_code.json`
        ) as EmbeddedCodeConverted;
        this._embeddedCode = data;

        return Promise.resolve(data);
    }

    public async loadGoal(): Promise<MstGoal> {
        let goal = defaultMstGoal;
        let path = `${RNFS.DocumentDirectoryPath}/${Const.DB_FILE_PATH}`;

        let exists = await RNFS.exists(path);
        if (exists) {
            goal = await MstUtil.instance.loadJson(path) as MstGoal;
        }

        return Promise.resolve(goal);
    }

    public async writeGoal(goal: MstGoal): Promise<any> {
        return MstUtil.instance.writeJson(`${RNFS.DocumentDirectoryPath}/${Const.DB_FILE_PATH}`,
            JSON.stringify(goal, null, 4));
    }

    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* MASTER
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    public async loadSvtDefaultLimitInfo(svtId: number): Promise<MstSvtLimit> {
        let container = await MstLoader.instance.loadModel("MstSvtLimit") as MstSvtLimitContainer;
        let mstSvtLimit = container.get(svtId, 0); // 首位

        return Promise.resolve(mstSvtLimit);
    }

    public async loadSvtDefaultTreasureDeviceWithLv(svtId: number, level: number): Promise<MstTreasureDeviceLv> {
        // ensure level secure
        if (level < 0) {
            level = 1;
        } else if (level > 5) {
            level = 5;
        }

        let svtTreasureDeviceCon = await this.loadModel("MstSvtTreasureDevice") as MstSvtTreasureDeviceContainer;
        let svtTreasureDeviceLvCon = await this.loadModel("MstTreasureDeviceLv") as MstTreasureDeviceLvContainer;

        let devices = svtTreasureDeviceCon.getGroup(svtId);
        let device = devices.values().next().value;
<commit_msg>Fix goal db saving function.<commit_after>import * as RNFS from "react-native-fs";
import BaseContainer from "../../../lib/container/base/BaseContainer";
import * as MstContainers from "../../../model/impl/MstContainer";
import {
    MstSvtLimitContainer,
    MstSvtTreasureDeviceContainer,
    MstTreasureDeviceLvContainer
} from "../../../model/impl/MstContainer";
import MstUtil from "../utility/MstUtil";
import {EmbeddedCodeConverted, TransSvtName} from "../../../model/master/EmbeddedCodeConverted";
import {MstSvtLimit, MstSvtTreasureDevice, MstTreasureDeviceLv} from "../../../model/master/Master";
import {MstGoal, defaultMstGoal} from "./MstGoal";
import Const from "../const/Const";

export default class MstLoader {

    private static _instance: MstLoader = undefined;

    public static get instance(): MstLoader {
        if (MstLoader._instance === undefined) {
            MstLoader._instance = new MstLoader();
        }
        return MstLoader._instance;
    }

    private constructor() {
        this._cache = new Map<string, BaseContainer<any>>();
    }

    private _cache: Map<string, BaseContainer<any>>;
    private _embeddedCode: EmbeddedCodeConverted;

    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* MAIN
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    public async loadModel(name: string): Promise<BaseContainer<any>> {
        if (this._cache.has(name)) {
            return Promise.resolve(this._cache.get(name));
        }

        let rawData = await MstUtil.instance.loadJson(
            `${await MstUtil.instance.getDbPath()}/master/${name}.json`
        );

        let containerName = `${name}Container`;
        let instance = new MstContainers[containerName]();
        instance.parse(rawData);
        this._cache.set(name, instance);

        return Promise.resolve(instance);
    }

    public async loadEmbeddedCode(): Promise<EmbeddedCodeConverted> {
        if (this._embeddedCode) {
            return Promise.resolve(this._embeddedCode);
        }

        let data = await MstUtil.instance.loadJson(
            `${await MstUtil.instance.getDbPath()}/embedded_code.json`
        ) as EmbeddedCodeConverted;
        this._embeddedCode = data;

        return Promise.resolve(data);
    }

    public async loadGoal(): Promise<MstGoal> {
        let goal = defaultMstGoal;
        let path = `${RNFS.DocumentDirectoryPath}/${Const.DB_FILE_PATH}`;

        let exists = await RNFS.exists(path);
        if (exists) {
            goal = await MstUtil.instance.loadJson(path) as MstGoal;
        }

        return Promise.resolve(goal);
    }

    public async writeGoal(goal: Object): Promise<any> {
        return MstUtil.instance.writeJson(`${RNFS.DocumentDirectoryPath}/${Const.DB_FILE_PATH}`, goal);
    }

    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* MASTER
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    public async loadSvtDefaultLimitInfo(svtId: number): Promise<MstSvtLimit> {
        let container = await MstLoader.instance.loadModel("MstSvtLimit") as MstSvtLimitContainer;
        let mstSvtLimit = container.get(svtId, 0); // 首位

        return Promise.resolve(mstSvtLimit);
    }

    public async loadSvtDefaultTreasureDeviceWithLv(svtId: number, level: number): Promise<MstTreasureDeviceLv> {
        // ensure level secure
        if (level < 0) {
            level = 1;
        } else if (level > 5) {
            level = 5;
        }

        let svtTreasureDeviceCon = await this.loadModel("MstSvtTreasureDevice") as MstSvtTreasureDeviceContainer;
        let svtTreasureDeviceLvCon = await this.loadModel("MstTreasureDeviceLv") as MstTreasureDeviceLvContainer;

        let devices = svtTreasureDeviceCon.getGroup(svtId);
        let device = devices.values().next().value;

{'fraction_non_alphanumeric': 0.19096117122851686, 'fraction_numerical': 0.001400381922342457, 'mean_word_length': 2.5467268623024832, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107145', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2467, 'n_words': 438}