RobotMj.lua 4.41 KB
local RobotMj = class("RobotMj")

RobotMj.enabled = false;

--单机打牌机器人
function RobotMj:ctor(uid)
    self.uid = uid;
    self.pokers = {};

    self.whoShotlistener = EventListener.addEventListener(nil,EnumEvent.GAME_WHO_SHOT,handler(self,self.onWhoShot));
    self.pghListener = EventListener.addEventListener(nil,EnumEvent.MJ_HGPG,handler(self,self.onMjHGPG));
end

function RobotMj:setPokers(pokers)
    self.pokers = pokers;
end

function RobotMj:addCards(pokers)
end

function RobotMj:removeCards(pokers)
    for i,v in ipairs(pokers) do
        for j=#self.pokers,1,-1 do
            if v.key == self.pokers[j].key then
                table.remove(self.pokers,j);
                break;
            end
        end
    end
end

--该谁出牌
function RobotMj:onWhoShot(params)
    if self.enabled then
        local uid = params.data.uid;
        if uid == self.uid and not self.isPgh then
            local key = params.data.key;
            if uid == UserModel.user_info.uid then
                self.shotDelayID = delayCall(2,function ()
                    self:startUserShot(uid,key);
                end);
            else
                self.shotDelayID = delayCall(2,function ()
                    self:startAIShot(uid,key);
                end);
            end
        end
    end
end

--帮用户出牌
function RobotMj:startUserShot(uid,key)
    if key > 0 then
        local has_ding = MahjongTool.hasDing(self.pokers);
        local user_ding = MahjongRoomModel.getUserDing(uid);
        local faInfo = MahjongTool.getInfoByKey(key);
        if (not has_ding or faInfo.suit == user_ding) then
            MahjongRoomModel.da(MahjongTool.getInfoByKey(key));
        else
            MahjongTool:sortMjList(self.pokers);
            MahjongRoomModel.da(self.pokers[#self.pokers]);
        end
    else
        MahjongTool:sortMjList(self.pokers);
        MahjongRoomModel.da(self.pokers[#self.pokers]);
    end
end

--AI出牌
function RobotMj:startAIShot(key)
    local faInfo = MahjongTool.getInfoByKey(key);
    local user_ding = MahjongRoomModel.getUserDing(uid);
    local has_ding = MahjongTool.hasDing(self.pokers);
    if key > 0 and (not has_ding or faInfo.suit == user_ding)then
        MahjongRoomModel.da(MahjongTool.getInfoByKey(key));
    else
        MahjongTool:sortMjList(self.pokers);
        MahjongRoomModel.da(self.pokers[#self.pokers]);
    end
end

--有碰杠胡
function RobotMj:onMjHGPG(params)
    if self.enabled then
        local hgp = params.data.hgp;
        local mjInfo = params.data.mj;
        self.isPgh = true;
        local function pghFun()
--            if hgp[1] == "gang" then
--                SocketClient:getInstance():send("room_msg.xzdd_gang_msg",{key=mjInfo.key});
--            elseif hgp[1] == "peng" then
--                SocketClient:getInstance():send("room_msg.xzdd_peng_msg",{});
--            elseif hgp[1] == "hu" then
--                SocketClient:getInstance():send("room_msg.xzdd_hu_msg",{});
--            elseif hgp[1] == "guo" then
--                SocketClient:getInstance():send("room_msg.xzdd_guo_msg",{});
--            end
            if TableUtil.IsInTable(hgp,"hu") then
                SocketClient:getInstance():send("room_msg.xzdd_hu_msg",{});
            elseif TableUtil.IsInTable(hgp,"guo") then
                SocketClient:getInstance():send("room_msg.xzdd_guo_msg",{});
            end
            self.isPgh = false;
        end
    
        if #hgp > 0 then
            self.delayID = delayCall(1,pghFun);
        end
    end
end

function RobotMj:pass()
    if UserModel.where == EnumWhere.ROOM_SINGLE then
        SingleManager.pass(self.uid);
    elseif UserModel.where == EnumWhere.ROOM_MULTI_GD or UserModel.where == EnumWhere.ROOM_PERSONAL_GD then
        MahjongRoomModel.pass();
    end
end

function RobotMj:start()
    self.enabled = true;
end

function RobotMj:stop()
    self.isPgh = false;
    self.enabled = false;
    if self.delayID then
        removeDelay(self.delayID);
        self.delayID = nil;
    end
    if self.shotDelayID then
        removeDelay(self.shotDelayID);
        self.shotDelayID = nil;
    end
end

function RobotMj:destroy()
    print("RobotMj:destroy:",self.delayID)
    if self.delayID then
        removeDelay(self.delayID);
        self.delayID = nil;
    end
    if self.shotDelayID then
        removeDelay(self.shotDelayID);
        self.shotDelayID = nil;
    end
    EventListener.removeEventListener(nil,self.whoShotlistener);
end

return RobotMj