cc2ae273-cdb2-44c9-bdb2-c7cc3d991f47.js 16.5 KB
"use strict";
cc._RF.push(module, 'cc2aeJzzbJEyb2yx8w9mR9H', 'ChatListViewPresenter');
// script/game/ui/presenter/ChatListViewPresenter.ts

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatListViewPresenter = void 0;
const simba_eventkit_1 = require("simba-eventkit");
const simba_utils_1 = require("simba-utils");
const ActionManager_1 = require("../../../avg/ActionManager");
const EditorEnums_1 = require("../../../avg/EditorEnums");
const GameRecord_1 = require("../../../avg/game-data/GameRecord");
const PlotsData_1 = require("../../../avg/game-data/PlotsData");
const PlotManager_1 = require("../../../avg/PlotManager");
const PresenterBase_1 = require("../../../common/classbase/PresenterBase");
const UIManager_1 = require("../../../common/gameplay/managers/UIManager");
const Enums_1 = require("../../Enums");
const GameModelManager_1 = require("../../model/GameModelManager");
const PresenterCCViewFactory_1 = require("../PresenterCCViewFactory");
const ChatListView_1 = require("../view/type/ChatListView");
const SentenceSelectorViewPresenter_1 = require("./SentenceSelectorViewPresenter");
const MAX_HISTORY = 50;
let ChatListViewPresenter = /** @class */ (() => {
    let ChatListViewPresenter = class ChatListViewPresenter extends PresenterBase_1.Presenter {
        constructor() {
            super();
            this._historyMessages = {};
            this._currPlotShowed = false;
            this._itemKey = 0;
            this._plotExecuting = false;
            this._excutedPlots = {};
            this.onSelectIndexCallback = (index) => {
                this.selectSentence(index);
            };
            this.onSetContent = (content) => {
                if (content != undefined) {
                    let props = {
                        sentence: content,
                        backgroundpath: "textures/message/selector_bg",
                        clickcausehide: false,
                        onSelectIndexCallback: this.onSelectIndexCallback
                    };
                    UIManager_1.UIManager.pushPresenter(SentenceSelectorViewPresenter_1.default, props);
                }
            };
            this.isSentenceSelectVisible = () => {
                let preset = UIManager_1.UIManager.getTopPresenter();
                if (preset instanceof SentenceSelectorViewPresenter_1.default) {
                    let selectorPresenter = preset;
                    return !selectorPresenter.view.isHidden;
                }
                return false;
            };
            this.closeAction = () => {
                if (this._plotExecuting)
                    return;
                UIManager_1.UIManager.popToPresenter(this);
                this._view.closeAnimation().then(() => {
                    this._view.close();
                });
            };
            this.selectChapter = () => {
            };
            this.selectSentence = (index) => {
                if (!this._currPlot)
                    throw new Error("ChatList: ePlot error! No currnt plot!");
                this.completePlot(index, ChatListView_1.ChatListAutoNextPlot);
            };
            this.showCurrPlot = async () => {
                if (this._currPlot && !this._excutedPlots[this._currPlot.id] && !this._plotExecuting && !this._currPlotShowed) {
                    let item = this.convertPlotToViewModel(this._currPlot);
                    this._excutedPlots[this._currPlot.id] = true;
                    this._plotExecuting = true;
                    let disposable = new simba_eventkit_1.CompositeDisposable;
                    this._plotChangeLike = undefined;
                    GameRecord_1.GameRecord.onRecordVariableChange("like" + this._currPlot.sentences[0].roleId, (scope, varName, preValue, value) => {
                        this._plotChangeLike = value - preValue;
                    });
                    await ActionManager_1.ActionManager.executeActions(this._currPlot.sentences[0].actions); // TODO 处理加好感
                    disposable.dispose();
                    this._plotExecuting = false;
                    if (item) {
                        if (this._chatList.length >= MAX_HISTORY) {
                            this._chatList.shift();
                        }
                        this._chatList.push(item);
                        if (item.type === "plot" && this._plotChangeLike) {
                            item.addLike = this._plotChangeLike;
                        }
                        this._view.updateProps({ items: this._chatList });
                    }
                    this._currPlotShowed = true;
                }
                else if (this._currPlot && this._excutedPlots[this._currPlot.id]) {
                    this._currPlotShowed = true;
                }
            };
            this.completePlot = async (index, proceedNext = true) => {
                if (!this._currPlot)
                    return;
                if (!this._currPlotShowed) {
                    this.showCurrPlot();
                    return;
                }
                if (index !== undefined) { // 刷新界面剧情选择
                    let lastItem = this._chatList.pop();
                    if (lastItem.type === "plot") {
                        this._chatList.push(Object.assign(Object.assign({}, lastItem), { select: index }));
                    }
                    else {
                        throw new Error("ChatList: Plot error!");
                    }
                    this._view.updateProps({ items: this._chatList });
                }
                // await this.execCurrPlot(); // 改为开始的时候执行
                if (proceedNext) {
                    this.sceneModel.lastPlot = this._currPlot;
                    if (this._selectIndex !== undefined) {
                        index = this._selectIndex;
                        this._selectIndex = undefined;
                    }
                    if (ChatListView_1.ChatListAutoNextPlot)
                        await simba_utils_1.delay(0.5);
                    await this.nextPlot(index);
                }
                else {
                    this._selectIndex = index;
                }
            };
            this.nextPlot = async (index) => {
                if (this.isClosed())
                    return;
                if (this._currPlot) {
                    await PlotManager_1.PlotManager.completePlot(this._currPlot, index, this._plotChangeLike ? { addlike: this._plotChangeLike } : undefined);
                }
                else {
                    console.error("ChatList: Plot error! No current plot!");
                }
            };
            this.convertPlotToViewModel = (plot, select, customData) => {
                let role = GameModelManager_1.GameModelManager.getRoleData(plot.sentences[0].roleId);
                let content = plot.sentences[0].content;
                if (plot.id >= 0) {
                    if (!content)
                        return undefined;
                    return { key: (this._itemKey++).toString(), type: "plot", name: role.getConfig().RoleType === EditorEnums_1.RoleType.Role_Blank ? "" : role.getConfig().name, icon: role.getHeadIcon(true), isSelf: role.getConfig().RoleType === EditorEnums_1.RoleType.Role_Player, content, select, addLike: customData ? customData["addlike"] : undefined };
                }
                else {
                    // // custom plot
                    // let giftId = plot.customData?.giftId;
                    // if (giftId) {// 送礼
                    //     let giftItem = ConfigManager.getConfig(itemConfig, giftId);
                    //     return { key: (this._itemKey++).toString(), type: "customplot", name: role.getConfig().name, icon: role.getHeadIcon(true), isSelf: true, gift: { icon: GameResData.GAME_ICON_BASE_PATH + giftItem.icon, text: "送你一个" + giftItem.name } };
                    // } else {
                    //     if (content && content.type !== SentenceType.SELECT) {
                    //         let addLike = plot.customData?.addLike;
                    //         return { key: (this._itemKey++).toString(), type: "customplot", name: role.getConfig().name, icon: role.getHeadIcon(true), isSelf: false, content, addLike }
                    //     } else {
                    //         throw new Error("This cannot happen!");
                    //     }
                    // }
                }
            };
            this.handlePlotsStart = async (plots) => {
                let plot = plots.find(v => v.plotSceneType === Enums_1.PlotSceneType.Message && v.plotSceneTypeId === this._sceneModel.id);
                if (plot) {
                    if (this._currPlot && plot === this._currPlot)
                        return; // 有分支剧情的情况
                    this._currPlot = plot;
                    this._currPlotShowed = false;
                    await this.showCurrPlot();
                }
                else if (this._currPlot && this._currPlot.id >= 0) {
                    this._currPlot = undefined;
                    this._chatList.push({ key: (this._itemKey++).toString(), type: "end" });
                    if (this._chatList.length >= MAX_HISTORY) {
                        this._chatList.shift();
                    }
                    this._view.updateProps({ items: this._chatList, backRedDot: true });
                }
            };
            PlotManager_1.PlotManager.PlotsRollbackEvent.on(() => {
                this._excutedPlots = {};
            });
        }
        get sceneModel() { return this._sceneModel; }
        getRoleId() {
            if (this.sceneModel.config.roles.length == 1)
                return this.sceneModel.config.roles[0];
            return 1;
        }
        async onOpen(scene) {
            this._sceneModel = scene;
            super.onOpen(scene);
            this._disposable.add(PlotManager_1.PlotManager.PlotStartEvent.on(this.handlePlotsStart));
            this._currPlot = PlotManager_1.PlotManager.getCurrentPlots().find(v => v.plotSceneType === Enums_1.PlotSceneType.Message && v.plotSceneTypeId === scene.id);
            //@ts-ignore
            delete this._chatList;
            if (this._historyMessages[scene.id]) {
                this._chatList = this._historyMessages[scene.id];
            }
            if (this._chatList && this._chatList.length) {
                if (this._chatList[this._chatList.length - 1].type === "end") { // 上一次的结束改为历史
                    let item = this._chatList.pop();
                    this._chatList.push({ key: item.key, type: "history" });
                    for (let i = this._chatList.length - 2; i >= 0; i--) { // 删除之前的历史分割,只保留最后一个
                        let item = this._chatList[i];
                        if (item.type === "history") {
                            this._chatList.splice(i, 1);
                            break;
                        }
                    }
                }
                this._viewProps = {
                    msgSceneId: scene.id, title: GameModelManager_1.GameModelManager.getConfigLanguageTxt(scene.config.title),
                    items: this._chatList, historyMsgCount: this._chatList.length,
                    onCloseBtnClick: this.closeAction,
                    onSelectChapter: this.selectChapter,
                    onCompletePlot: this.completePlot,
                    onSelectSentence: this.selectSentence,
                    onSetContent: this.onSetContent,
                    isSentenceSelectVisible: this.isSentenceSelectVisible,
                    backRedDot: !this._currPlot
                };
                this._view.setProps(this._viewProps);
            }
            else {
                this._viewProps = {
                    msgSceneId: scene.id, title: GameModelManager_1.GameModelManager.getConfigLanguageTxt(scene.config.title),
                    items: [], historyMsgCount: 0,
                    onCloseBtnClick: this.closeAction,
                    onSelectChapter: this.selectChapter,
                    onCompletePlot: this.completePlot,
                    onSelectSentence: this.selectSentence,
                    onSetContent: this.onSetContent,
                    isSentenceSelectVisible: this.isSentenceSelectVisible,
                    backRedDot: !this._currPlot
                };
                this._view.setProps(this._viewProps);
                await this.loadChatList();
            }
            if (this._currPlot) {
                this._currPlotShowed = false;
                await this.showCurrPlot();
                if (this._plotChangeLike) {
                    let item = Object.assign({}, this._chatList.pop());
                    if (item.type === "plot") { // 理论上肯定成立
                        item.addLike = this._plotChangeLike;
                    }
                    this._chatList.push(item);
                    this.view.updateProps({ items: this._chatList });
                }
            }
        }
        async loadChatList() {
            // if (chapter === -1) {
            //     chapter = GameModelManager.getCurrentChapterId();
            // }
            if (this._historyMessages[this._sceneModel.id] && this._historyMessages[this._sceneModel.id]) {
                this._view.updateProps({ items: this._chatList });
                return;
            }
            this._historyMessages[this._sceneModel.id] = [];
            let historyPlots = [];
            let records = GameRecord_1.GameRecord.getCurrentRecordItems();
            let pushHistory = false;
            let hasHistory = false;
            let lastPlotId = Number.MAX_SAFE_INTEGER;
            for (let i = records.length - 1; i >= 0; i--) {
                let item = records[i];
                for (let j = 0; j < item.p.length; j++) {
                    let plotId = item.p[j];
                    if (plotId === lastPlotId)
                        break;
                    let plot = (await PlotsData_1.getPlot(plotId));
                    if (!plot)
                        continue;
                    if (plot.plotSceneType === Enums_1.PlotSceneType.Message && plot.plotSceneTypeId === this._sceneModel.id) {
                        lastPlotId = plotId;
                        if (i === records.length - 1)
                            continue;
                        if (pushHistory && !hasHistory) {
                            historyPlots.unshift({ key: (this._itemKey++).toString(), type: "history" });
                            hasHistory = true;
                        }
                        let select = undefined;
                        if (item.s)
                            select = item.s[j];
                        let customData = undefined;
                        if (item.c)
                            customData = item.c[j];
                        let vm = this.convertPlotToViewModel(plot, select, customData);
                        if (vm)
                            historyPlots.unshift(vm);
                    }
                    else { // TODO process branch plots if needed.
                        if (!pushHistory && !hasHistory) {
                            pushHistory = true;
                        }
                    }
                    if (plot.id === this._sceneModel.firstPlot.id) {
                        break;
                    }
                }
                if (historyPlots.length >= MAX_HISTORY) {
                    break;
                }
            }
            this._chatList = historyPlots;
            this._view.updateProps({ items: historyPlots, historyMsgCount: this._chatList.length });
            this._historyMessages[this._sceneModel.id] = this._chatList;
        }
        isClosed() {
            return !this._disposable;
        }
    };
    ChatListViewPresenter.uuid = "ChatListViewPresenter";
    ChatListViewPresenter = __decorate([
        PresenterCCViewFactory_1.RegPresenter(ChatListView_1.ChatListViewType)
    ], ChatListViewPresenter);
    return ChatListViewPresenter;
})();
exports.ChatListViewPresenter = ChatListViewPresenter;

cc._RF.pop();