ExchangeCodeViewPresenter.ts 7.26 KB
import { RegPresenter } from "../PresenterCCViewFactory";
import { ExchangeCodeViewType, ExchangeCodeView, ExchangeCodeViewProps } from "../view/type/ExChangeCodeView";
import { Presenter } from "../../../common/classbase/PresenterBase";
import { HttpRequests } from "../../network/HttpRequests";
import { GameModelManager } from "../../model/GameModelManager";
import { GameConstData } from "../../../common/gameplay/gamedata/GameConstData";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { GameTextData } from "../../../common/gameplay/gamedata/GameTextData";
import GameDotMgr from "../../GameDotMgr";
import { SDK } from "simba-sdk";
import UnlockSpecialPlotEventManager from "../../model/UnlockSpecialPlotEventManager";
import CDKeyEventManager from "../../model/CDKeyEventManager";
import { exchangeCode } from "simba-sdk-exchangecode";
import { ExchangeErrorCode } from "simba-sdk-exchangecode/dist/ExchangeCodeClassBase";
import { GameConfig } from "../../../GameConfig";

interface ExchangeCodeParam {
    titletxt: string;
    contenttxt: string;
}
@RegPresenter(ExchangeCodeViewType)
export default class ExchangeCodeViewPresenter extends Presenter<ExchangeCodeParam, ExchangeCodeView>
{
    static uuid = "ExchangeCodeViewPresenter";

    private _viewProps: ExchangeCodeViewProps;

    constructor() {
        super();
        this._viewProps = {
            onActionClick: this.onActionClickCallback
        };
    }

    onOpen(param: ExchangeCodeParam) {
        super.onOpen(param);
        this._view.setProps(this._viewProps);
        this.view.setContentTxt(param.titletxt, param.contenttxt);
        this.view.showBanner(false);
    }

    onClose() {
        super.onClose();
        this.view.setContentTxt("", "");
    }

    onActionClickCallback = async (ret: boolean, cdkey: string) => {
        if (!cdkey || "" === cdkey.trim()) {
            return;
        }
        GameDotMgr.getInstance().dotClickUI("exchange_code");
        let sdkExchangeCode = true;
        let toastStr = "";
        if (sdkExchangeCode) {
            let ret = await exchangeCode(cdkey);
            if (GameConfig.debug) console.log("SDKExchangeCode ret: ", ret);
            switch (ret.errorCode) {
                case ExchangeErrorCode.Invalid:
                    {
                        toastStr = "兑换码无效";
                        if (GameConfig.debug) console.log("SDKExchangeCode Invalid, toastStr: ", toastStr);
                        UIManager.showToast(toastStr);
                    }
                    break;
                case ExchangeErrorCode.OK:
                    {
                        if (ret.props && 0 !== ret.props.length) {
                            let player = GameModelManager.getPlayerData();
                            for (let i = 0; i < ret.props.length; i++) {
                                let p = ret.props[i];
                                // player.addProps(parseInt(p.id + ''), parseInt(p.num + ''));
                                let id = Number.parseInt(p.id + '');
                                let count = Number.parseInt(p.num + '');
                                if (id >= GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID && id < 1000) {
                                    let itemId = Math.floor(id / GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID);
                                    let specialPlotId = id - GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID * itemId;
                                    UnlockSpecialPlotEventManager.getInstance().cdKeyUnlockSpecialPlot(specialPlotId, itemId);
                                } else if (id === GameConstData.UNLOCK_PLOT_WITHOUT_AD) {
                                    CDKeyEventManager.getInstance().cdKeyRemovePlotAD();
                                } else if (id >= GameConstData.CHANGE_ROLE_LIKE && id < 3000) {
                                    let roleId = id - GameConstData.CHANGE_ROLE_LIKE;
                                    CDKeyEventManager.getInstance().changeRoleLike(roleId, count);
                                }
                            }
                        }
                    }
                    break;
                case ExchangeErrorCode.TimeOut:
                    {
                        toastStr = "兑换码已过期!";
                        if (GameConfig.debug) console.log("SDKExchangeCode TimeOut, toastStr: ", toastStr);
                        UIManager.showToast(toastStr);
                    }
                    break;
                case ExchangeErrorCode.Used:
                    {
                        toastStr = "兑换码已经兑换过了";
                        if (GameConfig.debug) console.log("SDKExchangeCode Used, toastStr: ", toastStr);
                        UIManager.showToast(toastStr);
                    }
                    break;
                case ExchangeErrorCode.ScoreWeak:
                    {
                        toastStr = "积分不足";
                        if (GameConfig.debug) console.log("SDKExchangeCode Used, toastStr: ", toastStr);
                        UIManager.showToast(toastStr);
                    }
                    break;
            }
        } else {
            if (ret) {
                let ret = await HttpRequests.exchangeCDKEY(cdkey) as any;
                console.log("current ret is" + JSON.stringify(ret));
                if (ret.code === "0") {
                    for (let i = 0; i < ret.obj.props.length; i++) {
                        let data = ret.obj.props[i];
                        let id = Number.parseInt(data.prop_id);
                        let count = Number.parseInt(data.count);
                        if (id >= GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID && id < 1000) {
                            let itemId = Math.floor(id / GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID);
                            let specialPlotId = id - GameConstData.GAME_ITEM_AND_SPECIAL_PLOT_ID * itemId;
                            UnlockSpecialPlotEventManager.getInstance().cdKeyUnlockSpecialPlot(specialPlotId, itemId);
                        } else if (id === GameConstData.UNLOCK_PLOT_WITHOUT_AD) {
                            CDKeyEventManager.getInstance().cdKeyRemovePlotAD();
                        } else if (id >= GameConstData.CHANGE_ROLE_LIKE && id < 3000) {
                            let roleId = id - GameConstData.CHANGE_ROLE_LIKE;
                            CDKeyEventManager.getInstance().changeRoleLike(roleId, count);
                        }
                    }
                } else if (ret.code === "104001") {//错误的验证码
                    let content = GameModelManager.getLanguageTxt(GameTextData.TEXT_CDKEYINVALID_VALUE);
                    UIManager.showToast(content);
                } else if (ret.code === "104002") {//已经领过了
                    let content = GameModelManager.getLanguageTxt(GameTextData.TEXT_CDKEYHADEXCHANGED_VALUE);
                    UIManager.showToast(content);
                } else {//其他
                    let content = GameModelManager.getLanguageTxt(GameTextData.TEXT_CDKEYINVALID_VALUE);
                    UIManager.showToast(content);
                }
            }
        }
    }
}