123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- NpcChest = {
- }
- local this = {}
- local shopOpenId = 25002 -- 商店开启等级全局表id
- local PriceInfo = {}
- PriceInfo.__index = PriceInfo
- function PriceInfo:new(cfgId, count, bind)
- local instance = {
- cfgId = cfgId,
- count = count,
- bind = bind
- }
- setmetatable(instance, self)
- return instance
- end
- -- 初始化全局数据
- function NpcChest.initGlobalGoods()
- local goodsList = ConfigDataManager.getList("cfg_npcchest")
- if table.isNullOrEmpty(goodsList) then
- error("袖珍罐子商店不存在商品")
- return
- end
- local globalGoods = {}
- for i, item in ipairs(goodsList) do
- local shopId = tonumber(item.shopid)
- local goosByShopId = globalGoods[shopId] or {}
- local goods = {}
- goods.id = tonumber(item.id)
- goods.cfgId = tonumber(string.splitByAll(item.itemid, "#")[1])
- goods.count = tonumber(string.splitByAll(item.itemid, "#")[2])
- local price = item.price
- local weightMap = this.convert(price)
- local randomItem = this.doRandom(weightMap)
- if randomItem then
- goods.costType = randomItem.cfgId
- goods.price = math.floor(randomItem.count)
- goods.bind = randomItem.bind
- goods.conditions = item.conditions
- goosByShopId[goods.id] = goods
- globalGoods[shopId] = goosByShopId
- end
- end
- this.saveGlobalGoods(globalGoods)
- end
- function this.saveBuyRecord(actor, data)
- setplaydef(actor, "T$npcChestBuyRecord", data)
- end
- function this.getBuyRecord(actor)
- return getplaydef(actor, "T$npcChestBuyRecord") or {}
- end
- function this.saveGlobalGoods(data)
- setsysvar("G$npcChestGoods", data)
- end
- function this.getGlobalGoods()
- return getsysvar("G$npcChestGoods") or {}
- end
- -- 获取商品列表
- function NpcChest.sendShopGoodsInfoList(actor, msgData)
- local sendList = {}
- local shopId = msgData.shopId
- if shopId <= 0 then
- this.toClient(actor, sendList)
- return
- end
- local openLv = ConfigDataManager.getTableValue("cfg_global", "value", "id", shopOpenId)
- local lv = getbaseinfo(actor, "level")
- if lv < tonumber(openLv) then
- tipinfo(actor, "等级不足")
- this.toClient(actor, sendList)
- return
- end
- local globalGoods = this.getGlobalGoods()
- local shopGoodsList = globalGoods[shopId] or {}
- local newGoods = this.filterGoods(actor, shopGoodsList)
- -- lg("检验条件后的商品列表", newGoods)
- if table.isNullOrEmpty(newGoods) then
- this.toClient(actor, sendList)
- return
- end
- local record = this.getBuyRecord(actor)
- local recordByShop = record[shopId] or {}
- for id, goods in pairs(newGoods) do
- local alreadyBuyNum = this.getAlreadyBuyNum(id, recordByShop)
- -- lg("goodsId", id, "buyNum", alreadyBuyNum)
- local sendGoods = this.buildSendGoods(goods, alreadyBuyNum)
- table.insert(sendList, sendGoods)
- end
- this.toClient(actor, sendList)
- end
- function this.toClient(actor, msg)
- -- lg("发送商品列表", msg)
- sendluamsg(actor, LuaMessageIdToClient.RES_NPC_CHEST_GOODS_INFO, msg)
- end
- -- 购买商品
- function NpcChest.buyGoods(actor, msgData)
- local shopId = msgData.shopId
- local goodsId = msgData.goodsId
- local count = msgData.count
- if count <= 0 then
- return
- end
- local globalGoods = this.getGlobalGoods()
- local shopGoodsList = globalGoods[shopId] or {}
- local goods = shopGoodsList[goodsId] or {}
- -- lg("购买商品", goodsId, goods)
- if table.isNullOrEmpty(goods) or table.isNullOrEmpty(shopGoodsList) then
- return
- end
- if tonumber(goods.count) ~= count then
- tipinfo(actor, "购买数量不合法")
- return
- end
- local totalPrice = 0
- local unitPrice = goods.price
- totalPrice = unitPrice * count
- -- lg("购买商品价格", totalPrice)
- local costType = goods.costType
- local enough = false
- local bind = goods.bind
- if bind == 1 then
- enough = Bag.checkItemWithBind(actor, costType, totalPrice)
- else
- enough = Bag.checkItem(actor, costType, totalPrice)
- end
- if not enough then
- tipinfo(actor, "货币不足")
- return
- end
- Bag.costBind(actor, costType, totalPrice, "宝箱商人")
- Bag.sendRewards(actor, { [goods.cfgId] = goods.count }, "宝箱商人")
- -- this.excHorseLamp(actor, { [goods.cfgId] = goods.count })
- local record = this.getBuyRecord(actor)
- local recordByShop = record[shopId] or {}
- local alreadyBuyNum = this.getAlreadyBuyNum(goods.id, recordByShop)
- this.saveBuyRecords(actor, shopId, count, goods, alreadyBuyNum)
- end
- -- 存储购买记录
- function this.saveBuyRecords(actor, shopId, count, goods, alreadyBuyNum)
- local buyRecords = this.getBuyRecord(actor)
- local buyRecordMap = buyRecords[shopId] or {}
- local id = goods.id
- local buyRecord = buyRecordMap[id] or { id = id, buyNum = 0 }
- buyRecord.buyNum = alreadyBuyNum + count
- buyRecordMap[id] = buyRecord
- buyRecords[shopId] = buyRecordMap
- -- lg("购买记录", buyRecords)
- this.saveBuyRecord(actor, buyRecords)
- end
- function this.convert(source)
- local priceInfoMap = {}
- if not source or string.trim(source) == "" then
- return priceInfoMap
- end
- local outerArray = string.splitByAll(source, "|")
- for _, o in ipairs(outerArray) do
- local innerArray = string.splitByAll(o, "#")
- local cfgId = tonumber(innerArray[1])
- local count = tonumber(innerArray[2])
- local bind = tonumber(innerArray[3])
- local weight = tonumber(innerArray[4])
- local priceInfo = PriceInfo:new(cfgId, count, bind)
- priceInfoMap[priceInfo] = weight
- end
- return priceInfoMap
- end
- function this.doRandom(weightMap)
- local allWeight = 0
- if not weightMap or next(weightMap) == nil then
- return nil
- end
- for _, weight in pairs(weightMap) do
- allWeight = allWeight + weight
- end
- if allWeight == 0 then
- return nil
- end
- local randomWeight = math.random(1, allWeight)
- for key, weight in pairs(weightMap) do
- randomWeight = randomWeight - weight
- if randomWeight <= 0 then
- return key
- end
- end
- return nil
- end
- function this.filterGoods(actor, goodsMap)
- local newGoodsMap = {}
- for id, goods in pairs(goodsMap) do
- local conditions = goods.conditions
- if conditions and #conditions > 0 then
- local result = checkcondition(actor, conditions)
- if tonumber(result) ~= 1 then
- goto continue
- end
- end
- newGoodsMap[id] = goods
- ::continue::
- end
- return newGoodsMap
- end
- function this.getAlreadyBuyNum(goodsId, recordByShop)
- local buyNum = 0
- if table.isNullOrEmpty(recordByShop) then
- return buyNum
- end
- local record = recordByShop[goodsId]
- if record then
- buyNum = record.buyNum
- end
- return buyNum
- end
- function this.buildSendGoods(goods, alreadyBuyNum)
- local item = {}
- item.cfgId = goods.cfgId
- item.unitPrice = goods.price
- item.bind = goods.bind
- item.coinType = goods.costType
- item.count = goods.count
- item.id = goods.id
- return item
- end
- function this.excHorseLamp(actor, itemMap)
- if table.isNullOrEmpty(itemMap) then
- return
- end
- local name = getbaseinfo(actor, "rolename")
- for cfgId, count in pairs(itemMap) do
- local item = ConfigDataManager.getById("cfg_item", cfgId)
- if not table.isNullOrEmpty(item) then
- local itemName = item.name
- local noticeId = item.runninghorselamp
- if #noticeId > 0 then
- noticeTip.noticeinfo(actor, noticeId, name, itemName)
- end
- end
- end
- end
- function initchest(actor)
- NpcChest.initGlobalGoods()
- end
|