ExchangeCodeViewPresenter.ts 3.19 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";
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 = {
            titletxt: "",
            contenttxt: "",
            onActionClick: this.onActionClickCallback
        };
    }

    onOpen(param: ExchangeCodeParam) {
        super.onOpen(param);
        this._viewProps.titletxt = param.titletxt;
        this._viewProps.contenttxt = param.contenttxt;
        this._view.setProps(this._viewProps);
    }

    onActionClickCallback = async (ret: boolean, cdkey: string) => {
        if (ret) {
            let ret = await HttpRequests.exchangeCDKEY(cdkey) as any;
            console.log("current ret is" + JSON.stringify(ret));
            if (ret.code === "0") {

                let player = GameModelManager.getPlayerData();
                let outstr = "";
                for (let i = 0; i < ret.obj.props.length; i++) {
                    let data = ret.obj.props[i];
                    let id = Number.parseInt(data.prop_id);
                    if (id === GameConstData.GAME_CONST_GOLD_ID_VALUE) {
                        player.addGoldCoin(data.count);
                    } else if (id === GameConstData.GAME_CONST_CLOTH_ID_VALUE) {
                        player.addClothCoin(data.count);
                    } else {
                        player.addProps(id, data.count);
                    }
                    let item = GameModelManager.getItemConfig(id);
                    outstr += item.name + " x" + data.count;

                    if (i != ret.obj.props.length - 1) {
                        outstr += ",";
                    }
                }
                UIManager.showToast(outstr);
            } 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);
            }
        }
    }
}