ExtraPlotDatingEventSubViewPresenter.ts 13.8 KB
import { ConfigManager } from "simba-config-manager";
import { GameRecord, getPlot } from "../../../avg/AVG";
import { DateType } from "../../../avg/EditorEnums";
import { Presenter, Subpresenter } from "../../../common/classbase/PresenterBase";
import { GameTextData } from "../../../common/gameplay/gamedata/GameTextData";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { StringUtils } from "../../../common/utils/StringUtils";
import { dateSceneConfig } from "../../../config/DateSceneConfig";
import { CommonLocation, DatePrefabDirectType, DirectionType, EventLocation, EventOperationType, GuideLocation, GuideOperationType, GuideState, StepType } from "../../Enums";
import GameDotMgr from "../../GameDotMgr";
import CDKeyEventManager from "../../model/CDKeyEventManager";
import { DatingEventSceneModel, DatingEventStatus } from "../../model/DatingEventSceneModel";
import { ExtraStoryModelManager } from "../../model/ExtraStoryModelManager";
import { GameModelManager } from "../../model/GameModelManager";
import { RegPresenter, RegSubpresenter } from "../PresenterCCViewFactory";
import { AlertDialogViewProps } from "../view/type/AlertDialogView";
import { ExtraPlotDatingEventItemProps, ExtraPlotDatingEventSubView, ExtraPlotDatingEventSubViewType } from "../view/type/ExtraPlotDatingEventSubView";
import AlertDialogViewPresenter from "./AlertDialogViewPresenter";
import { DatingScenePresenter } from "./DatingScenePresenter";
import GuideViewPresenter, { GuideViewParamModel } from "./GuideViewPresenter";

@RegSubpresenter(ExtraPlotDatingEventSubViewType)
export class ExtraPlotDatingEventSubViewPresenter extends Subpresenter<ExtraPlotDatingEventSubView> {
    private _modelMap: { [key: string]: DatingEventSceneModel } = {};
    private _dailySceneNum: number = -1;
    private _curChapterIndex: number = 1;
    private _maxChapterIndex: number = 1;
    private _maxChapterIsChange: boolean = false;
    isReview: boolean;

    private sceneModelToViewModel(item: DatingEventSceneModel, index: number): ExtraPlotDatingEventItemProps {
        this._modelMap[item.id.toString()] = item;
        let direct = DatePrefabDirectType.Left;
        if (this._curChapterIndex % 2 === 1) {
            direct = (index + 1) % 2 === 1 ? DatePrefabDirectType.Left : DatePrefabDirectType.Right;
        } else {
            direct = (index + 1) % 2 === 0 ? DatePrefabDirectType.Left : DatePrefabDirectType.Right;
        }
        return {
            key: item.id + "",
            title: GameModelManager.getConfigLanguageTxt(item.config.name),
            status: item.status,
            coverImage: "textures/dating_event/cover/" + item.config.background,
            itype: direct
        }
    }

    // async refreshItems() {
    //     let items: ExtraDateItemViewProps[] = [];
    //     let cfgIdArr: number[] = this.getCfgIdArr();
    //     let role = GameModelManager.getRoleData(this._viewProps.roleId);
    //     for (let i = 0; i < cfgIdArr.length; i++) {
    //         let cfg = ConfigManager.getConfig(extraSceneConfig, cfgIdArr[i]);
    //         if (role && role.getRelationValue() >= cfg.relationCondition) {
    //             await ExtraStoryModelManager.startBranches([cfg.start_plot_id]);
    //         }
    //         let sceneModel = new DatingEventSceneModel(cfg.id, false);
    //         await sceneModel.initFromRecords();
    //         if (sceneModel.status === DatingEventStatus.Locked && role!.getRelationValue() >= cfg.relationCondition) {
    //             sceneModel.status = DatingEventStatus.New;
    //         }
    //         let p = this.sceneModelToViewModel(sceneModel);
    //         p.showLine = i !== cfgIdArr.length - 1;
    //         items.push(p);
    //     }
    //     this._viewProps.items = items;
    // }

    async refreshItems() {
        let items: ExtraPlotDatingEventItemProps[] = [];
        let srcModels: DatingEventSceneModel[] = [];
        let cfg = ConfigManager.getAllConfig(dateSceneConfig);
        for (const id in cfg) {
            if (cfg[id].DateType == DateType.Date_Sp && cfg[id].chapter_index == this._curChapterIndex) {
                await ExtraStoryModelManager.startBranches([cfg[id].start_plot_id]);
                let plot = await getPlot(cfg[id].start_plot_id);
                let model = new DatingEventSceneModel(Number(id));
                // model.status = ExtraStoryModelManager.getBranchStatus(model.id, model.config.start_plot_id);
                await model.initFromRecords();
                if (model.status === DatingEventStatus.Locked) {
                    model.status = DatingEventStatus.New;
                }
                // model.firstPlot = plot;
                model.isExtra = true;
                srcModels.push(model);
            }
        }
        for (let i = srcModels.length - 1; i >= 0; i--) {
            items.push(this.sceneModelToViewModel(srcModels[i], i));
        }
        if (this._dailySceneNum == -1) {
            let maxItemsNum: number = 0;
            let cfg = ConfigManager.getAllConfig(dateSceneConfig);
            for (let id in cfg) {
                let c = cfg[id];
                if (c) {
                    maxItemsNum++;
                }
            }
            this._dailySceneNum = maxItemsNum;
        }
        let showContinue = false;
        if (srcModels.length !== 0) {
            let c = 0;
            let cfgs = ConfigManager.getAllConfig(dateSceneConfig);
            for (let id in cfgs) {
                let cfg = cfgs[id];
                if (cfg) {
                    c++;
                }
            }
            let last = srcModels[srcModels.length - 1];
            showContinue = last.id === c;
        }
        this.view.setProps({
            items,
            onItemClick: this.onItemAdModeClick,
            showContinue: showContinue,
            chapterName: this.getChapterName(this._curChapterIndex),
            showLeft: this._curChapterIndex !== 1,
            showRight: this._curChapterIndex !== this._maxChapterIndex,
            showChapterClear: this.checkChapterClear(items, this._curChapterIndex),
            onOptionChapter: this.onOptionChapter,
            chapterIndex: this._curChapterIndex
        });
    }

    checkChapterClear(items: ExtraPlotDatingEventItemProps[], chapterIndex: number): boolean {
        let showClear: boolean = true;
        let cfg = ConfigManager.getAllConfig(dateSceneConfig);
        let cnt = 0;
        for (let id in cfg) {
            let c = cfg[id]
            if (c && c.chapter_index === chapterIndex) {
                cnt++;
            }
        }
        if (cnt !== items.length) {
            return false;
        }
        return showClear;
    }

    getChapterName(chapterIndex: number): string {
        chapterIndex -= 1;
        let nameArr: string[] =
            [
                "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十"
            ];
        let name = StringUtils.format(GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_CHAPTER_NAME), nameArr[chapterIndex]);
        return name
    }

    onItemAdModeClick = (key: string) => {
        GameModelManager.setGuideEventItemRecord(key, GuideState.Complete);
        let m = this._modelMap[key];
        let hasRecord = this.checkIsSceneHasAdRecord(m.id);
        CDKeyEventManager.getInstance().shiftCDKeyRemovePlotAD();
        let removeAdCardNum: number = CDKeyEventManager.getInstance().checkCDKeyRemovePlotAD();
        if (m.status !== DatingEventStatus.Completed) {
            GameDotMgr.getInstance().dotClickUI("click_date_card" + m.config.id);
        }
        let showAdAlert: boolean = !(removeAdCardNum > 0) && !hasRecord && m.config.ads === 1 && m.status == DatingEventStatus.New;
        if (showAdAlert) {
            let temp: AlertDialogViewProps =
            {
                dataptr: { key },
                titlecontent: GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_WAIT_VALUE),
                content: GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_SHOW_AD_UNLOCK_DATE_VALUE),
                ishasad: true,
                istwobtn: true,
                adconfig: "inject_fruit",
                location: CommonLocation.Event,
                callback: this.showAdCallBack,
                hasBanner: false,
                isthreebtn: true
            };
            console.log("Push AlertDialogViewPros data end");
            UIManager.pushPresenter(AlertDialogViewPresenter, temp);
            console.log("pushPresenter=========");
        } else {
            this.enterDatingScene(key);
        }
    }

    showAdCallBack = (type, ret: boolean, param: any) => {
        if (ret) {
            if (true) {
                let key = param["key"] as string;
                let m = this._modelMap[key];
                this.pushSceneAdToRecord(m.id);
                setTimeout(() => {
                    this.enterDatingScene(key);
                }, 300);
            }
        }
    }

    checkIsSceneHasAdRecord(id: number): boolean {
        let ret: boolean = false;
        let record = GameRecord.globalVariables["date_ad_arr"] as string;
        if (record) {
            let a: number[] = JSON.parse(record);
            let i = a.findIndex((v) => v === id);
            ret = i !== -1;
        }
        return ret;
    }

    pushSceneAdToRecord(id: number) {
        let record = GameRecord.globalVariables["date_ad_arr"] as string;
        let a: number[] = [];
        let isExsit: boolean = false;
        if (record) {
            a = JSON.parse(record);
            let i = a.findIndex((v) => v === id);
            if (i >= 0) {
                isExsit = true;
            }
        }
        if (!isExsit) {
            a.push(id);
            a.reverse();
            GameRecord.globalVariables["date_ad_arr"] = JSON.stringify(a);
            GameRecord.autoSave();
        }
    }

    enterDatingScene(key: string) {
        ExtraStoryModelManager.enterExtraStoryByStartPlotId(this._modelMap[key].config.start_plot_id);
        GameModelManager.ShowDatingEventArrowGuide.emit(false);
        UIManager.pushPresenter(DatingScenePresenter, this._modelMap[key], true);
    }

    onOwnerShow() {
        if (!this.view.isHidden) {
            this.selectCurChapter();
            this.refreshMaxChapterIndex();
            // this.refreshItems();
        }
    }

    onOwnerEnterForeground() {
        if (!this.view.isHidden) {
            this.refreshMaxChapterIndex();
            this.refreshItems();
        }
        if (this._maxChapterIsChange) {
            this.selectCurChapter();
        }
    }

    onOpen() {
        super.onOpen();
        this._disposable.add(GameModelManager.CheckDateGuide.on(this.checkGuideItem));
        this._disposable.add(GameModelManager.RefreshDateSceneProgress.on(this.onRefreshDateSceneProgress));
        this._disposable.add(GameModelManager.ForceClickDatingItem.on(this.onForceClickItem));
        this._disposable.add(GameModelManager.RefreshExtraPlotByType.on(this.onRefreshExtraPlotByType));
        this.selectCurChapter();
        if (!this.view.isHidden) {
            this.refreshMaxChapterIndex();
            this.onShow();
        }
    }

    onShow() {
        this.refreshMaxChapterIndex();
        // this.refreshItems();
        this.checkGuideItem();
    }

    onHide() {

    }

    selectCurChapter() {
        let models = GameModelManager.getMainDatingEventScenesModel();
        if (models && models.length) {
            let last = models[models.length - 1];
            this._curChapterIndex = last.config.chapter_index;
            if (this._curChapterIndex !== 1) {
                GameModelManager.setNotNeedEnterCurDateChapter();
            }
        }
    }

    checkGuideItem = () => {
        let k = GameModelManager.getEventItemGuideKey();
        if (k && "" !== k) {
            let node = this.view.getItemNode(k);
            if (node) {
                let wp = node.convertToWorldSpaceAR(cc.v2(0, 0));
                let m: GuideViewParamModel = {
                    x: wp.x,
                    y: wp.y,
                    w: node.width,
                    h: node.height,
                    d: DirectionType.DOWM,
                    call: () => {
                        this.onItemAdModeClick(k);
                        // this.onItemEnergyCostClick(k);
                        GameModelManager.setGuideEventItemRecord(k, GuideState.Complete);
                    }
                }
                UIManager.pushPresenter(GuideViewPresenter, m);
                GameDotMgr.getInstance().dotGuide(GuideOperationType.Show, GuideLocation.EventItem);
            }
        }
    }

    refreshMaxChapterIndex() {
        let maxChapterIndex = this._maxChapterIndex;
        let arr = GameModelManager.getMainDatingEventScenesModel();
        if (arr && arr.length) {
            let dId = arr[arr.length - 1].id;
            let cfg = ConfigManager.getConfig(dateSceneConfig, dId);
            this._maxChapterIndex = cfg.chapter_index;
        }
        this._maxChapterIsChange = maxChapterIndex == this._maxChapterIndex ? false : true;
    }

    onOptionChapter = (stepType: StepType) => {
        let tempIndex = stepType === StepType.Add ? this._curChapterIndex + 1 : this._curChapterIndex - 1;
        this._curChapterIndex = tempIndex >= 1 && tempIndex <= this._maxChapterIndex ? tempIndex : this._curChapterIndex;
        this.refreshItems();
    }

    onRefreshDateSceneProgress = () => {
        this.refreshItems();
    }

    onForceClickItem = (id: number) => {
        console.log("DatingEventSubpresenter onForceClickItem id = ", id);
        let k = id + "";
        if (!this._modelMap || !this._modelMap[k]) {
            return;
        }
        this.onItemAdModeClick(k);
    }

    onRefreshExtraPlotByType = (id: number) => {
        this._curChapterIndex = id;
        this.refreshItems();
    }
}