PhoneCallViewPresenter.ts 3.77 KB
import { ConfigManager } from "simba-config-manager";
import { DeepReadonly } from "simba-utils";
import { PlotManager, ReadonlyPlots, SentenceSelectContent } from "../../../avg/AVG";
import { Presenter } from "../../../common/classbase/PresenterBase";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { role } from "../../../config/Role";
import { PlotSceneType } from "../../Enums";
import { GameModelManager } from "../../model/GameModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { PhoneCallItemProps, PhoneCallView, PhoneCallViewProps, PhoneCallViewType } from "../view/type/PhoneCallView";
import { SentenceSelectorViewProps } from "../view/type/SentenceSelectorView";
import SentenceSelectorViewPresenter from "./SentenceSelectorViewPresenter";

@RegPresenter(PhoneCallViewType)
export class PhoneCallViewPresenter extends Presenter<undefined, PhoneCallView> {
    static uuid = "PhoneCallViewPresenter";
    private _items: PhoneCallItemProps[] = [];
    private _viewProps: PhoneCallViewProps;

    onSetContent = (content: DeepReadonly<SentenceSelectContent> | undefined) => {
        let props: SentenceSelectorViewProps =
        {
            sentence: content,
            backgroundpath: "",
            clickcausehide: false,
            onSelectIndexCallback: this.onSelectIndexCallback

        };
        UIManager.pushPresenter(SentenceSelectorViewPresenter, props);
    }


    onSelectIndexCallback = (index: number) => {
        this.view.select(index);
    }

    isSentenceSelectVisible = (): boolean => {
        let preset = UIManager.getTopPresenter();
        if (preset instanceof SentenceSelectorViewPresenter) {
            let selectorPresenter = preset as SentenceSelectorViewPresenter;
            return !selectorPresenter.view.isHidden;
        }
        return false;
    }

    onGameChangedClickCallback = (ret: boolean) => {
        this.view.applicationChange(ret);
    }

    async onOpen() {
        super.onOpen(undefined);
        let plots = await GameModelManager.rollbackPhoneCallIfNeeded();
        this.analysisPlots(plots);
    }

    analysisPlots(plots: ReadonlyPlots) {
        this._items = [];
        let plot = plots[0];
        if (plot.plotSceneType !== PlotSceneType.PhoneCall) {
            console.error("Phone call plot data error.");
        } else {
            let roleData = ConfigManager.getConfig(role, plot.plotSceneTypeId);
            this._items = [{ key: plot.id + "", isSelf: plot.sentences[0].roleId === 2, sentence: plot.sentences[0] }];
            this._viewProps =
            {
                name: roleData.name, // TODO i18n
                icon: GameModelManager.getRoleData(plot.plotSceneTypeId)!.getHeadIcon(),
                items: this._items,
                onCompletePlot: (index?: number) => PlotManager.completePlot(plot, index),
                onSetContent: this.onSetContent,
                isSentenceSelectVisible: this.isSentenceSelectVisible
            };
            this._view.setProps(this._viewProps);

            this._disposable.clear();
            this._disposable.add(PlotManager.PlotStartEvent.on((plots) => {
                plot = plots[0];
                if (plot.plotSceneType !== PlotSceneType.PhoneCall) {
                    GameModelManager.dotMainLinePlotEnd();
                    this._view.close();
                } else {
                    let item = { key: plot.id + "", isSelf: plot.sentences[0].roleId === 2, sentence: plot.sentences[0] };
                    let index = this._items.findIndex((v) => v.key === item.key);
                    if (index === -1) {
                        this._items = [...this._items, item];
                    }
                    this._view.updateProps({ items: this._items });
                }
            }));
        }
    }

}