GameStartView.lua 10.3 KB
local GameStartView;
if CHANNEL == "shdx" then
    GameStartView = dialog.uinode("ui/feiyanzoubi/GameStartLayer_shdx.csb",import("..view.BaseViewNew"));
else
    GameStartView = dialog.uinode("ui/feiyanzoubi/GameStartLayer.csb",import("..view.BaseViewNew"));
end

local game_data = require("app/views/feiyanzoubi/game_data");

local ox,oy = 1050,128;
function GameStartView:ctor()
    GameStartView.super.ctor(self);
end

function GameStartView:show(onStartFn)
    local scene = cc.Director:getInstance():getRunningScene()
    local inst = GameStartView:create();
    scene:addChild(inst, dialog.ZORDER_POP);

    inst.onStartFn = onStartFn;

    inst:init();

    return inst;
end

function GameStartView:init()
    self:setContentSize(cc.size(display.width,display.height));
    ccui.Helper:doLayout(self);

    local btn_start = self:getChildByName("btn_start");
    btn_start.fn = handler(self,self.onStart);
    btn_start:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, btn_start.fn)end);

    local btn_introduce = self:getChildByName("btn_introduce");
    btn_introduce.fn = handler(self,self.onIntroduce);
    btn_introduce:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, btn_introduce.fn)end);

    self.bg_mask = self:getChildByName("bg_mask");
    self.bg_mask:hide();
    self.game_introduce = self:getChildByName("game_introduce");
    self.game_introduce:setLocalZOrder(100);
    self.game_introduce:hide();

    self.nodes[1] = {btn_start,btn_introduce};
    self:updateSelectedState(btn_start);

    if IS_XHX then
        btn_introduce:setPosition(cc.p(80,display.height-80));
        self:initRankView();
    end
end

function GameStartView:initRankView()
    self.rankView = cc.CSLoader:createNode("ui/xyx/XyxRankLayer.csb");
    local rect = self.rankView:getContentSize();
    self.rankView:setPosition(cc.p(display.width-rect.width-20,20));
    self:addChild(self.rankView);

    for i=1,8 do
        self.rankView:getChildByName("item_"..i):setVisible(false);
    end

    self.onRankResFun = handler(self,self.onRankRes);
    cmsg.on("gateway_msg.single_game_rank_msg_res",self.onRankResFun);
    SocketClient:getInstance():send("gateway_msg.single_game_rank_msg",{id = EnumGame.FEIYANZOUBI});
end

function GameStartView:onRankRes(params)
    cmsg.off("gateway_msg.single_game_rank_msg_res",self.onRankResFun);
    self.rank_list = params.data.game_rank.rank_user;
    for i=1,8 do
        local item = self.rankView:getChildByName("item_"..i);
        if self.rank_list[i] then
            item:setVisible(true);
            if StringUtil.GetUTFLen(self.rank_list[i].user_show.nick) >= 16 then
                item:getChildByName("txt"):setString(StringUtil.getMaxLenString(self.rank_list[i].user_show.nick,12).."...");
            else
                item:getChildByName("txt"):setString(self.rank_list[i].user_show.nick);
            end
            if i > 3 then
                item:getChildByName("txt"):setColor(cc.c3b(255,165,0));
                item:getChildByName("txt_score"):setColor(cc.c3b(255,165,0));
            end
            item:getChildByName("txt_score"):setString(self.rank_list[i].val);

            local btn = item:getChildByName("btn");
            btn.user_show = self.rank_list[i].user_show;
            btn.fn = function ()
                self:onOpenUserInfo(btn);
            end;
            btn:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, handler(self,self.onOpenUserInfo))end);
            self.nodes[1][#self.nodes[1]+1] = btn;
        else
            item:setVisible(false);
        end
    end
end

function GameStartView:onOpenUserInfo(ref)
    local user_show = ref.user_show;
    local receiver = require("app.views.view.UserInfoOtherView"):show(user_show);
    cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(receiver);
end

function GameStartView:onStart()
    if game_data.get_is_guide() == 0 then
        self:onIntroduce();
        game_data.set_is_guide(1);
        return;
    end

    local function paySuccessFun()
        if self.onStartFn then
            self.onStartFn();
        end
        self:close();
    end

    if IS_XHX then
        local game_info = SingleGameInfo.get_game_info(EnumGame.FEIYANZOUBI);
        print("打开小游戏:",game_info.id);
        if game_info.needVip == 1 and UserModel.getLeftVipDay() == 0 and not TableUtil.IsInTable(UserModel.free_game_id,game_info.id) then
            print("小游戏购买界面");
            local my_diamond_num = UserModel.getDiamond();
            if my_diamond_num >= game_info.diamond then
                Alert.showTwo("玩这游戏需要扣除"..game_info.diamond.."个钻石,是否继续?",paySuccessFun);
            else
                if CHANNEL=="shdx" then
                    local reciever = require("app.views.view.XyxPayView_shdx"):show(game_info.id,paySuccessFun);
                    cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(reciever);
                elseif CHANNEL=="cucc" then
                    local reciever = require("app.views.view.XyxPayView_tianjin_cucc"):show(game_info.id,paySuccessFun);
                    cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(reciever);
                else
                    local reciever = require("app.views.view.XyxPayView"):show(game_info.id,paySuccessFun);
                    cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(reciever);
                end
            end
            return;
        else
            paySuccessFun();
        end
    elseif CHANNEL == "shdx" and game_data.get_game_play_cnt() > 0 then
        local reciever = require("app.views.feiyanzoubi.GamePayView"):show("",paySuccessFun);
        cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(reciever);
    elseif CHANNEL == "ws" then
        local unlock_price = 9.9;
        local function onPaySuccessCallBack(result)
            if result == "success" then
                UmengHelper.pay(unlock_price.."&1");
                game_data.set_is_vip(1);
                self:onStart();
            end
        end
        local function okPayFun()
            UmengHelper.onEvent("unlock_click");
            ShopModel.pay("tjlhxkgxyx_unlock","永久解锁",unlock_price,onPaySuccessCallBack,timeoutCallBack);
        end
        if game_data.get_is_vip() == 0 and game_data.get_game_play_cnt() >= 3 then
            local receiver = require("app.views.view.AlertView"):show({msg="您的免费试玩已结束,是否花"..unlock_price.."元付费解锁?"},okPayFun,nil,1);
            cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(receiver);
            return;
        else
            paySuccessFun();
        end
    else
        paySuccessFun();
    end
end

function GameStartView:onIntroduce()
    if not self.game_introduce:isVisible() then
        local game_data = require("app.views.feiyanzoubi.game_data");
        game_data.set_is_guide(1);

        self.game_introduce:show();
        self.bg_mask:show();
    else
        self.game_introduce:hide();
        self.bg_mask:hide();
    end
end

function GameStartView:close()
    if IS_XHX then
        cmsg.off("gateway_msg.single_game_rank_msg_res",self.onRankResFun);
    end
    GameStartView.super.close(self);
end

--返回键处理函数
function GameStartView:onKeypadBack()
    if not IS_XHX then
        local function okFun()
            cc.Director:getInstance():endToLua();
        end

        local receiver = require("app.views.view.AlertView"):show({msg="确定要退出游戏?"},okFun,cancelFun,2);
        cc.Director:getInstance():getRunningScene().keypadManager:addKeypadReceiver(receiver);
    else
        cmsg.off("gateway_msg.single_game_rank_msg_res",self.onRankResFun);
        app:enterScene("LoadingScene",{nextSceneName="MainScene",class_name="app.views.view.XyxView"});
    end
end

--隐藏按键
function GameStartView:onKeypadSecrect()
    if not self.game_introduce:isVisible() then
        return;
    end

    clearLogUI();
    if DEBUG == 0 then
        DEBUG = 1;
        
        logUI("进入调试模式:");
        if CHANNEL == "shdx" then
            self.auto_pay_index = 0;
            self.auto_pay_cnt = 0;
            self:autoPay();
        end
    else
        DEBUG = 0;
        if self.auto_pay_action then
            self:stopAction(self.auto_pay_action);
            self.auto_pay_action = nil;
        end
    end
end

function GameStartView:autoPay()
    if self.auto_pay_cnt >= 417 then
        self:onKeypadSecrect();
        return;
    end

    if self.auto_pay_index >= 10 then
        self.auto_pay_index = 0;
        clearLogUI();
    end
    local tradeNo = tostring(os.time())..math.random(10000,99999);	--订单号
	local productId = PAY_ID_SHDX;	--商品ID
	local subject = PAY_PRICE_SHDX.."元体验游戏";	--商品名称
	local amount = PAY_PRICE_SHDX;	--商品价格
	local notificationURL = "http://www.baidu.com";--回调URL
    local peng_bo_shi_token = "123";--鹏博士密钥

    local function callbackLua(params)
        if params == "success" then
            if cc.Application:getInstance():getTargetPlatform() == cc.PLATFORM_OS_ANDROID then
                HttpRequest:getInstance():post("http://139.196.55.173:22999/st",{id=102,gameid=GAME_ID,channel=CHANNEL,account=DeviceUtil.getDeviceId(),price=PAY_PRICE_SHDX});
            end
            logUI("...*");
        else
            logUI("...!");
        end
    end

    local function payCallbackFromJava(params)
        delayCall(0.5,function()
            callbackLua(params);
        end);
    end

    logUI("...");
    local pay_params = tradeNo..","..productId..","..subject..","..amount..","..notificationURL..","..peng_bo_shi_token;
    DeviceUtil.sendBilling(pay_params,payCallbackFromJava);
    if cc.Application:getInstance():getTargetPlatform() == cc.PLATFORM_OS_ANDROID then
        HttpRequest:getInstance():post("http://139.196.55.173:22999/st",{id=101,gameid=GAME_ID,channel=CHANNEL,account=DeviceUtil.getDeviceId(),price=PAY_PRICE_SHDX});
    end

    self.auto_pay_index = self.auto_pay_index + 1;

    math.randomseed(tostring(os.time()+self.auto_pay_index):reverse():sub(1,6));
    self.auto_pay_action = self:runAction(cc.Sequence:create(cc.DelayTime:create(math.random(532,1196)*0.01),cc.CallFunc:create(function()
        self:autoPay();
    end)));
end

return GameStartView