UnlockItemModelManager.ts 6.74 KB
import { ConfigManager } from "simba-config-manager";
import { GameRecord } from "../../avg/AVG";
import { itemTbl } from "../../config/ItemTbl";

class UnlockItemModelManager1 {
    /**存档Key */
    private UnlockItems = 'UnlockItems';
    /**存档Key */
    private ClickedItems = 'ClickedItems';
    /**存档Key */
    private UnlockedIntroductionItems = 'UnlockedIntroductionItems';
    /**解锁的物品ids */
    private items: Array<number> = [];
    /**点击过的物品ids */
    private clickedItem: Array<number> = [];
    /**已经解锁简介的物品ids */
    private unlockedIntroductionItem: Array<number> = [];
    /**配置掉落物品的剧情id和午评id的映射表 */
    private _unlockPlotIds: Array<{ itemId: number, plotId: number }> = [];

    /**获取配置掉落物品的剧情id和午评id的映射表 */
    public get unlockPlotIds(): Array<{ itemId: number, plotId: number }> {
        if (this._unlockPlotIds.length == 0) this.analysisItemCfg();
        return this._unlockPlotIds
    }

    public get unlockItems(): Array<number> {
        let items: Array<number> = [];
        let itemsStr = GameRecord.globalVariables[this.UnlockItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    items.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        return items;
    }

    public get clickedItems(): Array<number> {
        this.clickedItem = [];
        let itemsStr = GameRecord.globalVariables[this.ClickedItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.clickedItem.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        return this.clickedItem;
    }

    public get unlockedIntroductionItems(): Array<number> {
        this.unlockedIntroductionItem = [];
        let itemsStr = GameRecord.globalVariables[this.UnlockedIntroductionItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.unlockedIntroductionItem.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        return this.unlockedIntroductionItem;
    }

    /**
     * 保存解锁了简介的物品id到存档
     * @param id 点击过的物品id
     */
    saveUnlockedIntroductionItemIdToGameRecord(id: number): boolean {
        this.unlockedIntroductionItem = [];
        let itemsStr = GameRecord.globalVariables[this.UnlockedIntroductionItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.unlockedIntroductionItem.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        if (this.unlockedIntroductionItem.indexOf(id) == -1) {
            this.unlockedIntroductionItem.push(id);
            GameRecord.globalVariables[this.UnlockedIntroductionItems] = this.unlockedIntroductionItem.toString();
            GameRecord.saveRecord();
            return true;
        }
        return false;
    }

    /**
     * 保存点击过的物品id到存档
     * @param id 点击过的物品id
     */
    saveClickedItemIdToGameRecord(id: number): boolean {
        this.clickedItem = [];
        let itemsStr = GameRecord.globalVariables[this.ClickedItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.clickedItem.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        if (this.clickedItem.indexOf(id) == -1) {
            this.clickedItem.push(id);
            GameRecord.globalVariables[this.ClickedItems] = this.clickedItem.toString();
            GameRecord.saveRecord();
            return true;
        }
        return false;
    }


    /**
     * 保存解锁的物品id到存档
     * @param id 解锁的物品id
     */
    saveItemIdToGameRecord(id: number): boolean {
        this.items = [];
        let itemsStr = GameRecord.globalVariables[this.UnlockItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.items.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        if (this.items.indexOf(id) == -1) {
            this.items.push(id);
            GameRecord.globalVariables[this.UnlockItems] = this.items.toString();
            // GameRecord.autoSave();
            return true;
        }
        return false;
    }

    /**
     * 登录初始化已经解锁的items
     */
    loginInitUnlockItems() {
        //获取已经存在的items
        this.items = [];
        let itemsStr = GameRecord.globalVariables[this.UnlockItems];
        if (itemsStr) {
            let itemsIdStr: Array<string> = itemsStr.split(',');
            itemsIdStr.forEach(id => {
                try {
                    this.items.push(Number(id));
                } catch (error) {
                    console.error(error);
                }
            });
        }
        //当前剧情应该解锁的所有物品
        let itemcfg: { itemId: number, plotId: number }[] = this.unlockPlotIds;
        //获取当前的剧情id
        let record = GameRecord.getPlotsInfo();
        for (let i = 0; i < itemcfg.length; i++) {
            let ele = itemcfg[i];
            if (ele) {
                if (record[`${ele.plotId}`] && record[`${ele.plotId}`].cnt >= 1) {
                    if (this.items.indexOf(ele.itemId) == -1) {
                        this.items.push(ele.itemId);
                    }
                }
            }
        }
        GameRecord.globalVariables[this.UnlockItems] = this.items.toString();
        // GameRecord.autoSave();
    }

    /**
     * 解析item表结果
     */
    analysisItemCfg() {
        let ret: { itemId: number, plotId: number }[] = [];
        let cfg = ConfigManager.getAllConfig(itemTbl);
        for (let id in cfg) {
            ret.push({ itemId: Number(id), plotId: cfg[id].plot });
        }
        this._unlockPlotIds = ret;
    }
}

export let UnlockItemModelManager = new UnlockItemModelManager1();