local ipairs = ipairs local table = table local math = math local redisproxy = redisproxy local MsgPack = MsgPack local _M = {} function _M.addSellRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) for _,sellData in pairs(msg) do local slot = sellData.slot local sells = json.decode(role.dinerData:getProperty("sells")) if sells[slot] and sells[slot].count and sells[slot].count ~= 0 then return 0 end if math.illegalNum(slot, 1, role.dinerData:getMaxSlots()) then return 1 end slot = tostring(slot) local dish = sellData.dish local dishSet = csvdb["diner_dishCsv"][dish] if not dishSet then return 2 end local dishLevel = role.dinerData:getProperty("dishTree"):getv(dish, 0) if dishLevel == 0 then return 3 end local dishData = dishSet[dishLevel] if not dishData then return 4 end local calSell = role.dinerData:updateSell(slot, true) or { deltaCount = 0, deltaTime = 0, lastCount = 0, } local count = sellData.count local maxDishCount = role.dinerData:getMaxDishs() if math.illegalNum(count + calSell.lastCount, 1, maxDishCount) then return 5 end local cost = dishData.material:toNumMap() for k, n in pairs(cost) do cost[k] = n * count end if not role:checkItemEnough(cost) then return 6 end role:costItems(cost) role.dinerData:updateSell(slot) local dirty = false for id, value in pairs(cost) do if role.dinerData:checkDinerTask(DinerTask.UseMaterial, value, id, nil, true) then dirty = true end end if role.dinerData:checkDinerTask(DinerTask.AddDish, count, dish, nil, true) then dirty = true end if dirty then role.dinerData:notifyUpdateProperty("order", role.dinerData:getProperty("order")) end sells = json.decode(role.dinerData:getProperty("sells")) if not sells[slot] then sells[slot] = { reward = "", } end sells[slot].dish = dish sells[slot].level = dishLevel sells[slot].count = count sells[slot].time = skynet.timex() - calSell.deltaTime role.dinerData:updateProperty({field = "sells", value = json.encode(sells)}) end SendPacket(actionCodes.Diner_addSellRpc, "") return true end function _M.removeSellRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local slot = msg.slot if math.illegalNum(slot, 1, role.dinerData:getMaxSlots()) then return 1 end slot = tostring(slot) local sells = json.decode(role.dinerData:getProperty("sells")) local sell = sells[slot] if not sell then return 2 end local dish = sell.dish local dishSet = csvdb["diner_dishCsv"][dish] if not dishSet then return 3 end local dishLevel = role.dinerData:getProperty("dishTree"):getv(dish, 0) if dishLevel == 0 then return 4 end local dishData = dishSet[dishLevel] if not dishData then return 5 end role.dinerData:updateSell(slot) local reward = {} local cost = dishData.material:toNumMap() for k, n in pairs(cost) do local sum = n*sell.count role:addItem({itemId = k,count = sum}) reward[k] = sum end sells[slot].count = 0 role.dinerData:updateProperty({field = "sells", value = json.encode(sells)}) SendPacket(actionCodes.Diner_removeSellRpc, MsgPack.pack({reward = reward})) return true end function _M.getSellRewardRpc( agent, data ) local role = agent.role local dirty = false local reward = "" local sells = json.decode(role.dinerData:getProperty("sells")) for slot, _ in pairs(sells) do role.dinerData:updateSell(slot) end sells = json.decode(role.dinerData:getProperty("sells")) for slot, sell in pairs(sells) do local rewards = sell.reward:toNumMap() for k,v in pairs(rewards) do reward = reward:incrv(k, v) end if rewards[ItemId.Gold] and rewards[ItemId.Gold] > 0 then if role.dinerData:checkDinerTask(DinerTask.DishWithGold, rewards[ItemId.Gold], sell.dish, nil, true) then dirty = true end end sells[slot].reward = "" end role.dinerData:updateProperty({field = "sells", value = json.encode(sells)}) for k, v in pairs(reward:toNumMap()) do role:addItem({itemId = k,count = v}) end if dirty then role.dinerData:notifyUpdateProperty("order", role.dinerData:getProperty("order")) end SendPacket(actionCodes.Diner_getSellRewardRpc, MsgPack.pack({reward = reward})) return true end function _M.expediteSellRpc( agent, data ) local role = agent.role local count = role.dinerData:getProperty("expedite") local max = #globalCsv.diner_sell_quick_cost if count > max then return 1 end local diamond = globalCsv.diner_sell_quick_cost[count] if diamond > 0 then local cost = {[ItemId.Diamond] = diamond} if not role:checkItemEnough(cost) then return 2 end role:costItems(cost) end local dirty = false local reward,popular = "",0 local sells = json.decode(role.dinerData:getProperty("sells")) for slot, _ in pairs(sells) do role.dinerData:updateSell(slot) end sells = json.decode(role.dinerData:getProperty("sells")) for slot, sell in pairs(sells) do local result = role.dinerData:expediteSell(slot) if result then local rewards = result.reward:toNumMap() for k,v in pairs(result.reward:toNumMap()) do reward = reward:incrv(k,v) end popular = popular + result.popular if rewards[ItemId.Gold] and rewards[ItemId.Gold] > 0 then if role.dinerData:checkDinerTask(DinerTask.DishWithGold, rewards[ItemId.Gold], sell.dish, nil, true) then dirty = true end end end end role.dinerData:notifyUpdateProperty("sells", role.dinerData:getProperty("sells")) role.dinerData:setProperty("expedite",count+1) role.dinerData:notifyUpdateProperty("expedite", count+1) for k, v in pairs(reward:toNumMap()) do role:addItem({itemId = k,count = v}) end if dirty then role.dinerData:notifyUpdateProperty("order", role.dinerData:getProperty("order")) end SendPacket(actionCodes.Diner_expediteSellRpc, MsgPack.pack({reward = reward,popular = popular})) return true end function _M.levelUpRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local index = msg.index local buildingSet = csvdb["diner_buildingCsv"][index] if not buildingSet then return end local buildL = role.dinerData:getProperty("buildL") local curLevel = buildL:getv(index, 1) if curLevel >= #buildingSet then return end local buildingData = buildingSet[curLevel] if not buildingData then return end if buildingData.upLimit ~= "" then local id, level = buildingData.upLimit:match("(%d+)=(%d+)") if buildL:getv(tonumber(id), 1) < tonumber(level) then return end end local cost = buildingData.starCost:toNumMap() if not role:checkItemEnough(cost) then return end role:costItems(cost) role.dinerData:updateProperty({field = "buildL", value = buildL:setv(index, curLevel + 1)}) SendPacket(actionCodes.Diner_levelUpRpc, '') return true end function _M.talentUpRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local dish = msg.dish local dishTree = role.dinerData:getProperty("dishTree") local dishLevel = dishTree:getv(dish, 0) local talentSet = csvdb["diner_talentCsv"][dish] if not talentSet then return 1 end local talentData = talentSet[dishLevel] if not talentData then return 2 end if not talentSet[dishLevel+1] then return 21 end local typ = math.floor(dish/100 + 1) local treeSet = csvdb["diner_treeCsv"][typ] if not treeSet then return 3 end local treeData = treeSet[dish] if not treeData then return 4 end local limit = talentData.pointFront:toNumMap() for k,v in pairs(limit) do local lv = dishTree:getv(k, 0) if lv < v then return 5 end end local cost = talentData.cost:toNumMap() if not role:checkItemEnough(cost) then return 6 end -- 正在贩卖不能升级料理天赋 local sells = json.decode(role.dinerData:getProperty("sells")) for slot, _ in pairs(sells) do role.dinerData:updateSell(slot) end sells = json.decode(role.dinerData:getProperty("sells")) for _, sell in pairs(sells) do if sell.dish == msg.dish and sell.count > 0 then return 7 end end -- 正在冒险不能升级营养剂天赋 -- if talentData.effect:toArray(true,"=")[1] == 2 then -- if next(role:getProperty("advTeam")) then return 8 end -- end role:costItems(cost) role.dinerData:updateProperty({field = "dishTree", value = dishTree:setv(dish, dishLevel + 1)}) local treePoint = talentData.tree_point:toNumMap() if next(treePoint) then role:award(treePoint) end SendPacket(actionCodes.Diner_talentUpRpc, '') return true end function _M.skillUpRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local skill = msg.skill local skillSet = csvdb["diner_skillCsv"][skill] if not skillSet then return end local skillTree = role.dinerData:getProperty("skillTree") local skillLevel = skillTree:getv(skill, 1) local skillData = skillSet[skillLevel] if not skillData then return end if skillData.unlock ~= "" then local id, level = skillData.unlock:math("(%d+)=(%d+)") if skillTree:getv(tonumber(id), 1) < tonumber(level) then return end end local cost = skillData.cost:toNumMap() if not role:checkItemEnough(cost) then return end role:costItems(cost) role.dinerData:updateProperty({field = "skillTree", value = skillTree:setv(skill, skillLevel + 1)}) SendPacket(actionCodes.Diner_skillUpRpc, '') return true end function _M.lockTaskRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local index = msg.index local orders = json.decode(role.dinerData:getProperty("order")) if math.illegalNum(index, 1, #orders) then return 1 end local order = orders[index] if not order then return 2 end if order.lock == 0 then order.lock = 1 else order.lock = 0 end role.dinerData:updateProperty({field = "order", value = json.encode(orders)}) SendPacket(actionCodes.Diner_lockTaskRpc, MsgPack.pack({lock = order.lock})) return true end function _M.updateTaskRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local index = msg.index -- 0 接受任务,1 放弃已接受任务,2 完成已接受任务 local cmd = msg.cmd local orders = json.decode(role.dinerData:getProperty("order")) if math.illegalNum(index, 1, #orders) then return 1 end local order = orders[index] if not order then return 2 end local taskSet = csvdb["diner_questCsv"][order.lv] if not taskSet then return 4 end local taskData = taskSet[order.id] if not taskData then return 5 end if cmd == 0 then if order.status ~= 0 then return 30 end orders[index].status = 1 orders[index].lock = 1 elseif cmd == 1 then if order.status ~= 1 then return 31 end orders[index].status = 0 orders[index].lock = 0 elseif cmd == 2 then if order.status ~= 1 then return 32 end if order.n < taskData.value then return 6 end for typ, count in pairs(taskData.reward:toNumMap()) do role:addItem({itemId = typ, count = count}) end table.remove(orders,index) else return 33 end role.dinerData:updateProperty({field = "order", value = json.encode(orders)}) SendPacket(actionCodes.Diner_updateTaskRpc, '') return true end function _M.refreshTaskRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local cost = {[ItemId.Diamond] = 40} if not role:checkItemEnough(cost) then return 1 end local orders = json.decode(role.dinerData:getProperty("order")) local hadTask = {} local needCount = 0 for idx, temp in pairs(orders) do if temp.lock ~= 0 or temp.status ~= 0 then hadTask[temp.id] = 1 else needCount = needCount + 1 end end if needCount <= 0 then return 2 end local taskLevel = role.dinerData:getProperty("buildL"):getv(5, 1) local taskData = csvdb["diner_questCsv"][taskLevel] if not taskData then return 3 end local pool = {} for id, temp in pairs(taskData) do if not hadTask[id] then table.insert(pool, temp) end end local needCount = math.min(#pool, needCount) -- 需要的任务个数 if needCount <= 0 then return end local cost = globalCsv.diner_task_refresh_cost:toNumMap() for itemId, count in pairs(cost) do cost[itemId] = count * needCount end if not role:checkItemEnough(cost) then return end role:costItems(cost) for idx, order in ipairs(orders) do if (order.lock == 0 and order.status == 0) then if needCount > 0 then local index = math.randWeight(pool, "chance") local data = pool[index] orders[idx] = {lv = taskLevel, id = data.id, n = 0, lock = 0, status = 0} needCount = needCount - 1 table.remove(pool, index) end end end role.dinerData:updateProperty({field = "order", value = json.encode(orders)}) SendPacket(actionCodes.Diner_refreshTaskRpc, '') return true end function _M.getGreenhouseRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local buildType = 6 local level = role.dinerData:getProperty("buildL"):getv(buildType, 1) local buildingData = csvdb["diner_buildingCsv"][buildType][level] if not buildingData then return 1 end if buildingData.speed <= 0 then return 2 end local reward = "" local now = skynet.timex() local timePass = now - role.dinerData:getProperty("gTime") role.dinerData:setProperty("gTime",now) role.dinerData:notifyUpdateProperty("gTime",now) local count = math.floor(timePass/buildingData.speed) count = math.min(count,buildingData.storage) for _=1,count do reward = reward:incrv(string.randWeight(buildingData.gift_normal), 1) end for k, v in pairs(reward:toNumMap()) do role:addItem({itemId = k,count = v}) end SendPacket(actionCodes.Diner_getGreenhouseRpc, MsgPack.pack({reward = reward})) return true end return _M