13297d9a-7696-4c46-96e0-0bbebf0919d2.js 17.1 KB
"use strict";
cc._RF.push(module, '132972adpZMRpbgC76/CRnS', 'GameModelManager');
// script/game/model/GameModelManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameModelManager = void 0;
const simba_eventkit_1 = require("simba-eventkit");
const simba_utils_1 = require("simba-utils");
const GameRecord_1 = require("../../avg/game-data/GameRecord");
const PlotModel_1 = require("../../avg/model/PlotModel");
const PlotManager_1 = require("../../avg/PlotManager");
const GameConstData_1 = require("../../common/gameplay/gamedata/GameConstData");
const ConfigManager_1 = require("../../common/gameplay/managers/ConfigManager");
const ResUtils_1 = require("../../common/utils/ResUtils");
const BannerRewardsConfig_1 = require("../../config/BannerRewardsConfig");
const ConstConfig_1 = require("../../config/ConstConfig");
const ItemConfig_1 = require("../../config/ItemConfig");
const Role_1 = require("../../config/Role");
const Enums_1 = require("../Enums");
const GameRoleDataModel_1 = require("./GameRoleDataModel");
const MessageSceneModel_1 = require("./MessageSceneModel");
const EditorEvents_1 = require("../../avg/EditorEvents");
const AchievementConfig_1 = require("../../config/AchievementConfig");
const DailyQuestConfig_1 = require("../../config/DailyQuestConfig");
const DatingEventSceneModel_1 = require("./DatingEventSceneModel");
const DateSceneConfig_1 = require("../../config/DateSceneConfig");
const EditorEnums_1 = require("../../avg/EditorEnums");
const GameEnumData_1 = require("../../common/gameplay/gamedata/GameEnumData");
const I18nConfig_1 = require("../../config/I18nConfig");
var GameModelManager;
(function (GameModelManager) {
    let emitter = new simba_eventkit_1.Emitter;
    GameModelManager.MessageSceneChanged = emitter.createEvent();
    GameModelManager.RedPotReduction = emitter.createEvent(); //红点还原
    GameModelManager.MainDatingEventChanged = emitter.createEvent();
    /**报幕数据准备就绪事件 */
    GameModelManager.ForceClickDataReady = emitter.createEvent();
    let messageScenes;
    let mainDatingScenes;
    let currPlots;
    let phoneCallIndex = -1;
    let unlockGift = false;
    function isGiftUnlock() { return unlockGift; }
    GameModelManager.isGiftUnlock = isGiftUnlock;
    async function rollbackPhoneCallIfNeeded() {
        // if (phoneCallIndex < 0) {
        //     console.error("Error: no phone call.");
        // }
        if (phoneCallIndex >= 0 && phoneCallIndex < GameRecord_1.GameRecord.getCurrentRecordItems().length - 1) {
            let ret = await PlotManager_1.PlotManager.rollbackToIndex(phoneCallIndex);
            savePhoneCall();
            return ret;
        }
        else {
            return PlotManager_1.PlotManager.getCurrentPlots();
        }
    }
    GameModelManager.rollbackPhoneCallIfNeeded = rollbackPhoneCallIfNeeded;
    //#region  //数据模块 
    let roleDatasMap = new Map();
    let languagesMap = new Map(); //语言
    //#endregion
    function getCurrentChapterId() {
        return currPlots[0].chapterId;
    }
    GameModelManager.getCurrentChapterId = getCurrentChapterId;
    function getMessageScenesModel() {
        return messageScenes;
    }
    GameModelManager.getMessageScenesModel = getMessageScenesModel;
    function getMessageSceneModel(id) {
        return messageScenes.find(v => v.id === id);
    }
    GameModelManager.getMessageSceneModel = getMessageSceneModel;
    function getMainDatingEventScenesModel() {
        return mainDatingScenes;
    }
    GameModelManager.getMainDatingEventScenesModel = getMainDatingEventScenesModel;
    function getMainDatingEventSceneModel(id) {
        return mainDatingScenes.find(v => v.id === id);
    }
    GameModelManager.getMainDatingEventSceneModel = getMainDatingEventSceneModel;
    async function initConfigData() {
        await ConfigManager_1.ConfigManager.initConfigData();
        initLanguageConfig();
        initRoleConfig();
    }
    function initRoleConfig() {
        let cfg = ConfigManager_1.ConfigManager.getAllConfig(Role_1.role);
        for (let id in cfg) {
            let element = cfg[id];
            if (element) {
                let data = new GameRoleDataModel_1.default();
                data.setConfig(element);
                roleDatasMap.set(element.id, data);
            }
        }
    }
    function getLanguagePath() {
        return "userdata/language/chinese";
    }
    async function initLanguageConfig() {
        let cfgFile = await ResUtils_1.ResUtils.loadRes(getLanguagePath(), cc.JsonAsset);
        if (cfgFile) {
            let lans = cfgFile;
            for (let i = 0; i < lans.length; ++i) {
                let element = lans[i];
                if (element) {
                    languagesMap.set(element.id, element.value);
                }
            }
        }
    }
    function getLanguageTxt(id) {
        if (languagesMap.has(id)) {
            return languagesMap.get(id);
        }
        else {
            console.log("current language id is miss " + id);
            return "";
        }
    }
    GameModelManager.getLanguageTxt = getLanguageTxt;
    function getConfigLanguageTxt(id) {
        return ConfigManager_1.ConfigManager.getConfig(I18nConfig_1.i18nConfig, id).cn;
    }
    GameModelManager.getConfigLanguageTxt = getConfigLanguageTxt;
    function getConstConfig(id) {
        return ConfigManager_1.ConfigManager.getConfig(ConstConfig_1.constConfig, id);
    }
    GameModelManager.getConstConfig = getConstConfig;
    function getItemConfig(id) {
        return ConfigManager_1.ConfigManager.getConfig(ItemConfig_1.itemConfig, id);
    }
    GameModelManager.getItemConfig = getItemConfig;
    function getItemConfigs() {
        return ConfigManager_1.ConfigManager.getAllConfig(ItemConfig_1.itemConfig);
    }
    GameModelManager.getItemConfigs = getItemConfigs;
    function getBannerRewardsConfigs() {
        return ConfigManager_1.ConfigManager.getAllConfig(BannerRewardsConfig_1.bannerRewardsConfig);
    }
    GameModelManager.getBannerRewardsConfigs = getBannerRewardsConfigs;
    function getBannerRewardsConfig(id) {
        return ConfigManager_1.ConfigManager.getConfig(BannerRewardsConfig_1.bannerRewardsConfig, id);
    }
    GameModelManager.getBannerRewardsConfig = getBannerRewardsConfig;
    function getDailyQuestConfigs() {
        return ConfigManager_1.ConfigManager.getAllConfig(DailyQuestConfig_1.dailyQuestConfig);
    }
    GameModelManager.getDailyQuestConfigs = getDailyQuestConfigs;
    function getDailyQuestConfig(id) {
        return ConfigManager_1.ConfigManager.getConfig(DailyQuestConfig_1.dailyQuestConfig, id);
    }
    GameModelManager.getDailyQuestConfig = getDailyQuestConfig;
    function getAchievementConfigs() {
        return ConfigManager_1.ConfigManager.getAllConfig(AchievementConfig_1.achievementConfig);
    }
    GameModelManager.getAchievementConfigs = getAchievementConfigs;
    function getAchievementConfig(id) {
        return ConfigManager_1.ConfigManager.getConfig(AchievementConfig_1.achievementConfig, id);
    }
    GameModelManager.getAchievementConfig = getAchievementConfig;
    function getRoleData(id) {
        return roleDatasMap.get(id);
    }
    GameModelManager.getRoleData = getRoleData;
    function getPlayerData() {
        return GameModelManager.getRoleData(GameConstData_1.GameConstData.GAME_CONST_PLAYER_ROLE_VALUE);
    }
    GameModelManager.getPlayerData = getPlayerData;
    let disposable;
    async function init() {
        disposable = new simba_eventkit_1.CompositeDisposable;
        await initConfigData();
        phoneCallIndex = GameRecord_1.GameRecord.recordVariables["gm.pci"];
        unlockGift = !!GameRecord_1.GameRecord.recordVariables["gm.unlockGift"];
        if (phoneCallIndex === undefined)
            phoneCallIndex = -1;
        let initRet = [];
        { // init message models
            let ids = GameRecord_1.GameRecord.recordVariables["gm.ms"];
            if (ids !== undefined) {
                messageScenes = ids.map((id) => new MessageSceneModel_1.MessageSceneModel(id));
                initRet.push(...messageScenes.map((s) => s.initFromRecords()));
            }
        }
        { // init main dating models
            let ids = GameRecord_1.GameRecord.recordVariables["gm.md"];
            if (ids !== undefined) {
                mainDatingScenes = ids.map((id) => new DatingEventSceneModel_1.DatingEventSceneModel(id));
                initRet.push(...mainDatingScenes.map((s) => s.initFromRecords()));
            }
        }
        if (initRet.length) {
            await Promise.all(initRet);
        }
        disposable.add(PlotManager_1.PlotManager.PlotWillStart.on(handlePlotWillStart));
        disposable.add(PlotManager_1.PlotManager.PlotsRollbackEvent.on(handlePlotsRollback));
        disposable.add(EditorEvents_1.EditorEvents.UNLOCK_GIFT.on(() => {
            unlockGift = true;
            GameRecord_1.GameRecord.recordVariables["gm.unlockGift"] = 1;
        }));
        initPlots();
        for (let scene of mainDatingScenes) { // 检查主线剧情状态
            if (currPlots[0].plotSceneType !== Enums_1.PlotSceneType.DatingEvent ||
                (scene.id !== currPlots[0].plotSceneTypeId && scene.status !== DatingEventSceneModel_1.DatingEventStatus.Completed)) {
                scene.status = DatingEventSceneModel_1.DatingEventStatus.Completed;
            }
        }
    }
    GameModelManager.init = init;
    function save() {
        let oldValue = GameRecord_1.GameRecord.recordVariables["gm.ms"];
        let newValue = messageScenes.map(v => v.id);
        if (!simba_utils_1.shallowEqual(oldValue, newValue)) {
            GameRecord_1.GameRecord.recordVariables["gm.ms"] = newValue;
        }
        oldValue = GameRecord_1.GameRecord.recordVariables["gm.md"];
        newValue = mainDatingScenes.map(v => v.id);
        if (!simba_utils_1.shallowEqual(oldValue, newValue)) {
            GameRecord_1.GameRecord.recordVariables["gm.md"] = newValue;
        }
    }
    function savePhoneCall() {
        GameRecord_1.GameRecord.recordVariables["gm.pci"] = phoneCallIndex;
        GameRecord_1.GameRecord.saveRecord();
    }
    async function initPlots() {
        let plots = PlotManager_1.PlotManager.getCurrentPlots();
        if (!currPlots) {
            currPlots = [...plots];
            if (!messageScenes) { // 第一次进游戏,根据剧情初始化数据
                messageScenes = [];
            }
            if (!mainDatingScenes) {
                mainDatingScenes = [];
            }
            for (let i = 0; i < plots.length; i++) {
                let plot = plots[i];
                if (plot.plotSceneType === Enums_1.PlotSceneType.Message) {
                    let index = messageScenes.findIndex(v => v.id === plot.plotSceneTypeId);
                    if (index < 0) {
                        index = 0;
                        let model = new MessageSceneModel_1.MessageSceneModel(plot.plotSceneTypeId);
                        model.firstPlot = plot;
                        messageScenes.unshift(model);
                    }
                    // if (plot.sentences[0].content && plot.sentences[0].content.type !== SentenceType.SELECT) {
                    // messageScenes[index].lastPlot = plot;
                    // }
                }
                else if (plot.plotSceneType === Enums_1.PlotSceneType.DatingEvent) {
                    let cfg = ConfigManager_1.ConfigManager.getConfig(DateSceneConfig_1.dateSceneConfig, plot.plotSceneTypeId);
                    if (cfg.DateType === EditorEnums_1.DateType.Date_Normal) { // 主线约会剧情
                        if (plots[0] !== plot) {
                            console.error("主线约会剧情应在主线开启");
                            continue;
                        }
                        if (mainDatingScenes.findIndex(v => v.id === plot.plotSceneTypeId) < 0) {
                            let model = new DatingEventSceneModel_1.DatingEventSceneModel(plot.plotSceneTypeId);
                            model.firstPlot = plot;
                            model.status = DatingEventSceneModel_1.DatingEventStatus.New;
                            mainDatingScenes.push(model);
                        }
                    }
                }
            }
            save();
        }
    }
    async function handlePlotWillStart(plot, branch) {
        handleNewPlot(currPlots[branch], plot, branch);
        currPlots[branch] = plot;
        // for (let i = 0; i < plots.length; i++) {
        //     if (!currPlots![i] || (currPlots![i].id !== plots[i].id)) {
        //         handleNewPlot(currPlots![i], plots[i], i);
        //         currPlots![i] = plots[i];
        //     }
        // }
    }
    async function handlePlotsRollback(plots) {
        messageScenes = undefined;
        currPlots = undefined;
        disposable.dispose();
        await init();
        GameRecord_1.GameRecord.startTransaction();
        GameRecord_1.GameRecord.endTransaction(true);
    }
    async function handleNewPlot(prevPlot, plot, branch) {
        if (plot.plotSceneType === Enums_1.PlotSceneType.Message) {
            let idx = messageScenes.findIndex(v => v.id === plot.plotSceneTypeId);
            if (idx >= 0) { // 原会话中有新消息
                let model = messageScenes[idx];
                if (idx > 0) {
                    for (let i = idx; i > 0; i--) {
                        messageScenes[i] = messageScenes[i - 1];
                    }
                    messageScenes[0] = model;
                }
                if (plot.sentences[0].content && plot.sentences[0].content.type !== PlotModel_1.SentenceType.SELECT) {
                    // model.lastPlot = plot;
                }
            }
            else { // 新会话
                let model = new MessageSceneModel_1.MessageSceneModel(plot.plotSceneTypeId);
                model.firstPlot = plot;
                if (plot.sentences[0].content && plot.sentences[0].content.type !== PlotModel_1.SentenceType.SELECT) {
                    // model.lastPlot = plot;
                }
                messageScenes.unshift(model);
            }
            GameModelManager.MessageSceneChanged.emit(messageScenes);
            save();
        }
        else if (plot.plotSceneType === Enums_1.PlotSceneType.PhoneCall) {
            if (!prevPlot || prevPlot.plotSceneType !== Enums_1.PlotSceneType.PhoneCall) {
                phoneCallIndex = GameRecord_1.GameRecord.getCurrentRecordItems().length; // 当前存档还没生成,不需要减1
                savePhoneCall();
            }
        }
        else if (plot.plotSceneType === Enums_1.PlotSceneType.DatingEvent) {
            let cfg = ConfigManager_1.ConfigManager.getConfig(DateSceneConfig_1.dateSceneConfig, plot.plotSceneTypeId);
            if (cfg.DateType === EditorEnums_1.DateType.Date_Normal) { // 主线约会剧情
                if (branch !== 0) {
                    console.error("主线约会剧情应在主线开启");
                    return;
                }
                let idx = mainDatingScenes.findIndex(v => v.id === plot.plotSceneTypeId);
                if (idx < 0) { // 新开启
                    let model = new DatingEventSceneModel_1.DatingEventSceneModel(plot.plotSceneTypeId);
                    model.firstPlot = plot;
                    model.status = DatingEventSceneModel_1.DatingEventStatus.New;
                    mainDatingScenes.push(model);
                    GameModelManager.MainDatingEventChanged.emit(mainDatingScenes);
                }
                save();
            }
        }
        if (prevPlot && prevPlot.plotSceneType === Enums_1.PlotSceneType.PhoneCall && plot.plotSceneType !== Enums_1.PlotSceneType.PhoneCall) {
            phoneCallIndex = -1;
            savePhoneCall();
        }
        if (prevPlot && (prevPlot.plotSceneType !== plot.plotSceneType || prevPlot.plotSceneTypeId !== plot.plotSceneTypeId)) { // 切换场景
            if (prevPlot.plotSceneType === Enums_1.PlotSceneType.Message) { // 最后一个是选项的情况下,此时已做选择,记录最后一条剧情
                // let model = messageScenes!.find(v => v.id === prevPlot.plotSceneTypeId);
                // model!.lastPlot = prevPlot;
            }
            if (prevPlot.plotSceneType === Enums_1.PlotSceneType.DatingEvent) {
                let model = mainDatingScenes.find(v => v.id === prevPlot.plotSceneTypeId);
                if (model)
                    model.status = DatingEventSceneModel_1.DatingEventStatus.Completed;
            }
        }
    }
    function checkFuncUnloced(key) {
        return GameRecord_1.GameRecord.recordVariables[key] === GameEnumData_1.FuncStateEnum.locked ? false : true;
    }
    GameModelManager.checkFuncUnloced = checkFuncUnloced;
    function checkRedPot(key) {
        return GameRecord_1.GameRecord.globalVariables["red_pot_" + key] === GameEnumData_1.RedPotStateEnum.Show;
    }
    GameModelManager.checkRedPot = checkRedPot;
    function setRedPotState(key, state) {
        GameRecord_1.GameRecord.globalVariables["red_pot_" + key] = state;
        GameRecord_1.GameRecord.autoSave();
    }
    GameModelManager.setRedPotState = setRedPotState;
})(GameModelManager = exports.GameModelManager || (exports.GameModelManager = {}));

cc._RF.pop();