ItemIntroductionViewPresenter.ts 7.05 KB
import { ResUtils } from "simba-cc-resutils";
import { ConfigManager } from "simba-config-manager";
import { SDK } from "simba-sdk";
import { DeepReadonlyObject } from "simba-utils";
import { createPresenter, Presenter } from "../../../common/classbase/PresenterBase";
import { GameConstData } from "../../../common/gameplay/gamedata/GameConstData";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { IItemTbl, itemTbl } from "../../../config/ItemTbl";
import GameCenter from "../../../cooperation/script/GameCenter";
import { channel, GameConfig } from "../../../GameConfig";
import GameDotMgr from "../../GameDotMgr";
import { GameModelManager } from "../../model/GameModelManager";
import { UnlockItemModelManager } from "../../model/UnlockItemModelManager";
import UnlockSpecialPlotEventManager from "../../model/UnlockSpecialPlotEventManager";
import { UnlockSpecialPlotModelManager } from "../../model/UnlockSpecialPlotModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { ItemIntroductionView, ItemIntroductionViewProps, ItemIntroductionViewType } from "../view/type/ItemIntroductionView";
import { SpecialPlotViewProps } from "../view/type/SpecialPlotView";
import SpecialPlotViewPresenter from "./SpecialPlotViewPresenter";

@RegPresenter(ItemIntroductionViewType)
export default class ItemIntroductionViewPresenter extends Presenter<ItemIntroductionViewProps, ItemIntroductionView>
{
    static uuid = "ItemIntroductionViewPresenter";

    private _viewProps: ItemIntroductionViewProps;
    private specialPresenter: SpecialPlotViewPresenter | undefined = undefined;

    constructor() {
        super();
    }

    onOpen(props: ItemIntroductionViewProps) {
        super.onOpen(props);
        this._viewProps = props;
        this._viewProps.showItemSpr = this.showItemSpr;
        this._viewProps.onSpecialPlotBtnClick = this.onSpecialPlotClickCallBack;
        this._viewProps.onShopBtnClick = this.onShopBtnClickCallBack;
        this.view.setProps(this._viewProps);
        this.onShow();
        createPresenter(SpecialPlotViewPresenter).then((presenter) => {
            this.specialPresenter = presenter;
        });
    }

    onShow() {
        super.onShow();
        this.showItemSpr(this._viewProps.itemId);
    }

    onEnterBackground() {
        super.onEnterBackground();
    }

    onEnterForeground() {
        super.onEnterForeground();
    }

    onClose() {
        super.onClose();
    }

    showItemSpr = async (itemId: number) => {
        try {
            let cfg: DeepReadonlyObject<IItemTbl> | undefined = undefined;
            if (itemId) {
                cfg = ConfigManager.getConfig(itemTbl, itemId);
                if (cfg) {
                    //如果没有模糊图,那么直接展示简介图
                    if (cfg.foggy === 0) {
                        UnlockItemModelManager.saveUnlockedIntroductionItemIdToGameRecord(itemId);
                        let path: string = GameConstData.GAME_CONST_ITEM_BIG_SPR_DIR + cfg.type;
                        let itemSpriteFrame = await ResUtils.loadRes(path, cc.SpriteFrame);
                        this.view.getItemSpriteNode().spriteFrame = itemSpriteFrame;
                    }
                    //如果有模糊图,那么就判断是否已经解锁了清晰的简介图
                    else {
                        let unlockedIntroduction = false;
                        let unlockedIntroductionItems = UnlockItemModelManager.unlockedIntroductionItems;
                        for (let i = 0; i < unlockedIntroductionItems.length; i++) {
                            if (itemId == unlockedIntroductionItems[i]) {
                                unlockedIntroduction = true;
                            }
                        }
                        if (!unlockedIntroduction) {
                            let path: string = GameConstData.GAME_CONST_ITEM_BIG_SPR_DIR + cfg.foggyPicture;
                            let itemSpriteFrame = await ResUtils.loadRes(path, cc.SpriteFrame);
                            this.view.getItemSpriteNode().spriteFrame = itemSpriteFrame;
                        } else {
                            UnlockItemModelManager.saveUnlockedIntroductionItemIdToGameRecord(itemId);
                            let path: string = GameConstData.GAME_CONST_ITEM_BIG_SPR_DIR + cfg.type;
                            let itemSpriteFrame = await ResUtils.loadRes(path, cc.SpriteFrame);
                            this.view.getItemSpriteNode().spriteFrame = itemSpriteFrame;
                        }
                    }
                }
            }
            this.updateSpecialPlotBtnRedDotStatus();
        } catch (error) {
            console.error(error);
        }
    }

    onSpecialPlotClickCallBack = () => {
        let itemCfg: DeepReadonlyObject<IItemTbl> | undefined = undefined;
        itemCfg = ConfigManager.getConfig(itemTbl, this._viewProps.itemId);
        let unlockedSpecialPlots = UnlockSpecialPlotModelManager.unlockedSpecialPlotIdByItemId(this._viewProps.itemId);
        if (itemCfg) {
            let unlockedSpecialPlot = false;
            if (unlockedSpecialPlots.length > 0) {
                unlockedSpecialPlot = true;
            }
            if (unlockedSpecialPlot) {
                this.openSpecialPlotView(this._viewProps.itemId, unlockedSpecialPlots);
            } else {
                UIManager.showToast(itemCfg.toastMsg);
                return;
            }
        }
        this.view.close();
    }

    openSpecialPlotView(itemId: number, specialPlotIds: number[]) {
        if (this.specialPresenter) {
            let temp: SpecialPlotViewProps = {
                itemId: itemId,
                specialPlotIds: specialPlotIds,
                onLeftBtnClick: () => { },
                onRightBtnClick: () => { },
                createGameClubBtn: () => { },
                onShopBtnClick: () => { },
                onCloseBtnClick: () => { }
            };
            this.specialPresenter.open(this.view.getContainer(), temp);
        }
    }

    onShopBtnClickCallBack = () => {
        GameDotMgr.getInstance().dotClickUI("shop_btn_introduction");
        let type = GameModelManager.getTypeByBedroomItemId(this._viewProps.itemId);
        if (channel == "wechat") {
            // GameCenter.getInstance().openSubProgram(GameConfig.youZanShopAppId);
            if (type) {
                let link = GameModelManager.getYouZanShopUrlByType(type);
                if (link) {
                    SDK.openProgramV2(GameConfig.youZanShopAppId, link);
                }
            }
        } else if (channel == "android") {
            if (type) {
                let link = GameModelManager.getTaoBaoShopUrlByType(type);
                if (link) {
                    GameModelManager.jumpToTaobaoShop(link);
                }
            }
        } else {
            UIManager.showToast("该功能在当前平台不可使用");
        }
    }

    updateSpecialPlotBtnRedDotStatus() {
        this.view.showSpecialPlotBtnRedDot(UnlockSpecialPlotEventManager.getInstance().isShowBedRoomRedDotStatus(this._viewProps.itemId));
    }
}