ReviewPlotViewPresenter.ts 4.4 KB
import { PlotManager } from "simba-avg-core";
import { GameRecord } from "../../../avg/AVG";
import { Presenter } from "../../../common/classbase/PresenterBase";
import { GameModelManager } from "../../model/GameModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { ReviewPlotViewType, ReviewPlotViewProps, ReviewPlotView } from "../view/type/ReviewPlotView";

export interface ReviewPlotViewParam {
    isReview: boolean,
    plotString: string,
    startPlotId: number,
    currentPlotId: number,
}

@RegPresenter(ReviewPlotViewType)
export default class ReviewPlotViewPresenter extends Presenter<ReviewPlotViewParam, ReviewPlotView>{
    static uuid = "ReviewPlotViewPresenter";
    private _viewProps: ReviewPlotViewProps;
    private _param: ReviewPlotViewParam;

    constructor() {
        super();
        this._viewProps = {
            onCloseButtonClickCallback: this.onCloseButtonClickCallback,
        }
    }

    onOpen(param: ReviewPlotViewParam) {
        super.onOpen(param);
        this._param = param;
        this.view.setProps(this._viewProps);
        this.onShow();
    }

    onShow() {
        super.onShow();
        this.initViewDate();
    }

    onEnterBackground() {
        super.onEnterBackground();
    }

    onEnterForeground() {
        super.onEnterForeground();
    }

    onClose() {
        super.onClose();
    }

    initViewDate() {
        this.view.getPlotRichText().string = "";
        this.view.getLoadingNode().active = true;
        this.view.getPlotRichText().scheduleOnce(async () => {
            if (this._param.isReview) {
                this.view.getPlotRichText().string = this._param.plotString;
            } else {
                this.view.getPlotRichText().string = await this.getReviewPlotContent();
            }
            if (this.view.getPlotRichText().node.height >= this.view.getPlotScrollView().node.height) {
                this.view.getPlotScrollView().scrollToBottom();
            }
            this.view.getLoadingNode().active = false;
        });
    }

    async getReviewPlotContent(): Promise<string> {
        let showWhileNotSelect: boolean = false;//未选择时是否展示选项列表
        let showCurrentPlot: boolean = true;//是否展示当前剧情内容
        let plotStr = "";
        let startPlotId = this._param.startPlotId;
        let currentPlotId = this._param.currentPlotId;
        let currentRecordIndex = PlotManager.getCurrentRecordIndex();
        let currentRecord = GameRecord.getRecords()[currentRecordIndex].d;
        let startPlotIdIndex = 0;
        let currentPlotIdIndex = 0;
        for (let i = 0; i < currentRecord.length; i++) {
            if (startPlotId == currentRecord[i].p[0]) {
                startPlotIdIndex = i;
            }
            if (currentPlotId == currentRecord[i].p[0]) {
                currentPlotIdIndex = i;
            }
        }
        if (showCurrentPlot) {
            currentPlotIdIndex += 1;
        }
        let newCurrentRecord = currentRecord.slice(startPlotIdIndex, currentPlotIdIndex);
        for (let i = 0; i < newCurrentRecord.length; i++) {
            let content = await GameModelManager.getPlotContent(newCurrentRecord[i].p[0]);
            if (content) {
                let roleData = GameModelManager.getRoleData(content.roleId)!;
                let plotName = "";
                let plotContent = "";
                if (content.content.length === 0) {
                    continue;
                }
                if (content.content.length > 1) {
                    if (newCurrentRecord[i].s && newCurrentRecord[i].s?.length !== 0) {
                        plotName = "<color=#D36777>" + "选项" + ":</c>\n"
                        plotContent = content.content[newCurrentRecord[i].s![0]] + "\n";
                    } else {
                        if (showWhileNotSelect) {
                            for (let j = 0; j < content.content.length; j++) {
                                plotContent = content.content[j] + "\n";
                            }
                        }
                    }
                } else {
                    plotName = "<color=#EFDA91>" + roleData.getConfig().name + ":</c>\n"
                    plotContent = content.content[0] + "\n";
                }
                plotStr += plotName + plotContent;
            }
        }
        return plotStr;
    }

    onCloseButtonClickCallback = async () => {
        this.view.close();
    }
}