CapsuleAction.lua 7.39 KB
local ipairs = ipairs
local table = table
local math = math
local next = next
local string = string
local redisproxy = redisproxy
local MsgPack = MsgPack
local getRandomName = getRandomName
local mcast_util = mcast_util
local string_format = string.format
local tonumber = tonumber
local require = require

local _M = {}

function _M.listRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local typ = msg.typ
    local id = msg.id

    local capsules = {}
    if typ == 1 then
        local ret = skynet.call(agent.capsule_serv, "lua", "list", id)
        if next(ret) then
            for k, v in pairs(ret) do
                capsules[k] = v
            end
        end
    elseif typ == 0 then
        local ret = role:getCapsuleList(id)
        if next(ret) then
            for k, v in pairs(ret) do
                capsules[k] = v
            end
        end
    end

    SendPacket(actionCodes.Capsule_listRpc, MsgPack.pack(capsules))
    return true
end

function _M.joinRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local roleId = role:getProperty("id")
    local capsuleId = msg.capsule_id --如果刷新则需要传递当前扭蛋机id
    local typ = msg.typ or 1

    local ret = {}
    if typ == 1 then
        ret = skynet.call(agent.capsule_serv, "lua", "join", roleId, capsuleId)
    elseif typ == 0 then
        ret = role:joinCapsule(roleId, capsuleId)
    end
    SendPacket(actionCodes.Capsule_joinRpc, MsgPack.pack(ret))
    return true
end

function _M.exitRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local roleId = role:getProperty("id")
    local capsuleId = msg.capsule_id --如果刷新则需要传递当前扭蛋机id
    local typ = msg.typ or 1

    local ret = {}
    if typ == 1 then
        ret = skynet.call(agent.capsule_serv, "lua", "exit", roleId, capsuleId)
    elseif typ == 0 then
        ret = role:exitCapsule()
    end

    SendPacket(actionCodes.Capsule_exitRpc, MsgPack.pack(ret))
    return true
end

function _M.registerRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local roleId = role:getProperty("id")
    local capsuleId = msg.capsule_id

    local ret = skynet.call(agent.capsule_serv, "lua", "register", roleId, capsuleId)
    if not ret then return 1 end
    SendPacket(actionCodes.Capsule_registerRpc, MsgPack.pack(ret))
    return true
end

function _M.drawRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local roleId = role:getProperty("id")
    local capsuleId = msg.capsule_id
    local typ = msg.typ --0=独享,1= 公开
    local full = msg.full or 0-- 0=单次,1=十连抽 2=全收
    local cares = msg.cares

    local ret, token,  change, drawReward, capsule

    --检查库存
    if typ == 1 then
        ret, token = skynet.call(agent.capsule_serv, "lua", "goods_stock", capsuleId)
    else
        ret, token = role:goodStock(capsuleId)
    end
    if ret == 0 then skynet.error("零库存 " .. capsuleId) return 0 end

    --检查余额
    if not next(token) then skynet.error("代币未配置 " .. capsuleId) return 1 end
    local cost, drawsNum = {}, 0
    if full == 0 then
        drawsNum = 1
    elseif full == 1 then
        if ret < 10 then
            drawsNum = ret
        else
            drawsNum = 10
        end
    elseif full == 2 then
        drawsNum = ret
    end
    cost[token[1]] = drawsNum * token[2]

    if not role:checkItemEnough(cost) then return 2 end
    role:costItems(cost, {log = {desc = "CapsuleCoinCost", int1 = token[1], int2 = cost[token[1]]}})

    --开始抽奖
    if typ == 1 then
        ret, drawReward, capsule = skynet.call(agent.capsule_serv, "lua", "draw_capsule", roleId, capsuleId, full, cares)
    else
        ret, drawReward, capsule = role:drawCapsule(capsuleId, full, cares)
    end
    if ret < 5 then
        return ret
    end

    if ret == 5 then
        SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack({change = drawReward, capsule = capsule}))
        return true
    end

    if drawReward["reward"] and next(drawReward["reward"]) then
        _, change = role:award(drawReward["reward"], {log = {desc = "CapsuleReward", int1 = tonumber(capsuleId), int2 = roleId}})
        drawReward["capsule"] = capsule
        SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack(drawReward))
    else
        return ret
    end
    return true
end

function _M.getDataRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local capsuleId = msg.capsule_id
    local typ = msg.typ --0=独享,1= 公开
    local roleId = role:getProperty("id")

    local capsule
    if typ == 1 then
        capsule = skynet.call(agent.capsule_serv, "lua", "capsule_data", roleId,  capsuleId)
    else
        capsule = role:getCapsuleData(roleId, capsuleId)
    end
    if not capsule then return 1 end

    SendPacket(actionCodes.Capsule_getDataRpc, MsgPack.pack({capsule = capsule}))
    return true

end

function _M.convertCapsuleRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local coin = msg.coin
    local count = msg.count or 1

    local cost, convert = {}
    for k, v in pairs(globalCsv.ichibankuji_buy_cost)do
        if k == coin then
            convert = v:toArray(true, "=")
            break
        end
    end
    cost[convert[1]] = convert[2] * count
    if not role:checkItemEnough(cost) then return 1 end
    role:costItems(cost, {log = {desc = "CapsuleConvert", int1 = convert[1], int2 = cost[convert[1]]}})


    local reward, change = role:award({[coin]= count}, {log = {desc = "CapsuleConvert"}})
    SendPacket(actionCodes.Capsule_convertCapsuleRpc, MsgPack.pack(role:packReward(reward, change)))
    return true
end

function _M.pageRecordRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local idx = msg.idx
    local up = msg.up or 0
    local capsuleId = msg.capsule_id
    local typ = msg.typ --0=独享,1= 公开

    local record
    if typ == 1 then
        record = skynet.call(agent.capsule_serv, "lua", "page_record", capsuleId, up, idx)
    else
        record = role:pageRecord(capsuleId, up, idx)
    end
    if not record then return 1 end

    SendPacket(actionCodes.Capsule_pageRecordRpc, MsgPack.pack({record = record}))
    return true
end

function _M.specialRewardRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local capsuleId = msg.capsule_id
    local typ = msg.typ or 1 --0=独享,1= 公开
    local roleId = role:getProperty("id")

    local ret
    if typ == 1 then
        ret = skynet.call(agent.capsule_serv, "lua", "get_special_nty", roleId, capsuleId)
    else
        ret = role:getSpecialNotify(roleId, capsuleId)
    end
    if not ret or not next(ret) then return 1 end

    SendPacket(actionCodes.Capsule_specialRewardRpc, MsgPack.pack({special= ret}))
    return true
end

function _M.clearSpecialNtyRpc(agent, data)
    local role = agent.role
    local msg = MsgPack.unpack(data)
    local capsuleId = msg.capsule_id
    local typ = msg.typ --0=独享,1= 公开
    local good_ids = msg.good_ids
    local roleId = role:getProperty("id")

    local ret
    if typ == 1 then
        ret = skynet.call(agent.capsule_serv, "lua", "clear_special_nty", roleId, capsuleId, good_ids)
    end
    if not ret or not next(ret) then return 1 end

    SendPacket(actionCodes.Capsule_clearSpecialNtyRpc, MsgPack.pack({}))
    return true
end

return _M