FashionBox.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. --- 宝箱读表随机生成道具
  2. FashionBox = {}
  3. local this = {}
  4. local fashionReward = "T$fashionReward"
  5. function FashionBox.canUse(actor, cfgid)
  6. local itemRow = ConfigDataManager.getTable("cfg_item", "id", cfgid)
  7. if table.isNullOrEmpty(itemRow) then
  8. gameDebug.print("RandomChest.generateChestItem itemConfigId error")
  9. return true
  10. end
  11. if itemRow[1].boxsubtype ~= "1" then
  12. return true
  13. end
  14. local count = 1
  15. local allItem = {}
  16. if tonumber(itemRow[1].type) == ItemType.BOX and tonumber(itemRow[1].subtype) == ItemSubType.EQUIP_BOX then
  17. local rewardRecord = getplaydef(actor, fashionReward)
  18. for i = 1, count do
  19. local res = this.getItemAndCount(actor, itemRow[1].useparam)
  20. allItem = res
  21. if table.isNullOrEmpty(res) then
  22. gameDebug.print("RandomChest.generateChestItem getItemAndCount res is nil")
  23. else
  24. for k, v in pairs(res) do
  25. local itemId = res[k].cfgid
  26. if table.isNullOrEmpty(rewardRecord) then
  27. local temp = {}
  28. temp[itemId] = 1
  29. setplaydef(actor, fashionReward, temp)
  30. else
  31. if rewardRecord[itemId] then
  32. rewardRecord[itemId] = rewardRecord[itemId] + 1
  33. else
  34. rewardRecord[itemId] = 1
  35. end
  36. setplaydef(actor, fashionReward, rewardRecord)
  37. end
  38. end
  39. end
  40. end
  41. end
  42. local ptCheckBag = {}
  43. for _, v in pairs(allItem) do
  44. local paramMap = {}
  45. paramMap["cfgid"] = v.cfgid
  46. paramMap["itemcount"] = v.num
  47. table.insert(ptCheckBag, paramMap)
  48. end
  49. local canPutBag = checkitemscanputbag(actor, ptCheckBag)
  50. if tonumber(canPutBag) == 0 and table.notNullOrEmpty(ptCheckBag) then
  51. messagebox(actor, "背包已满,使用失败")
  52. return false
  53. end
  54. return true
  55. end
  56. --- 使用宝箱生成道具
  57. ---@param actor any 玩家对象
  58. ---@param itemConfigId number 道具配置ID
  59. ---@param count number 数量
  60. function FashionBox.generateChestItem(actor, itemConfigId, count)
  61. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  62. if table.isNullOrEmpty(itemRow) then
  63. gameDebug.print("RandomChest.generateChestItem itemConfigId error")
  64. return
  65. end
  66. if itemRow[1].boxsubtype ~= "1" then
  67. return
  68. end
  69. local allItem = {}
  70. if tonumber(itemRow[1].type) == ItemType.BOX and tonumber(itemRow[1].subtype) == ItemSubType.EQUIP_BOX then
  71. local rewardRecord = getplaydef(actor, fashionReward)
  72. for i = 1, count do
  73. local res = this.getItemAndCount(actor, itemRow[1].useparam)
  74. allItem = res
  75. if table.isNullOrEmpty(res) then
  76. gameDebug.print("RandomChest.generateChestItem getItemAndCount res is nil")
  77. else
  78. for k, v in pairs(res) do
  79. local itemId = res[k].cfgid
  80. if table.isNullOrEmpty(rewardRecord) then
  81. local temp = {}
  82. temp[itemId] = 1
  83. setplaydef(actor, fashionReward, temp)
  84. else
  85. if rewardRecord[itemId] then
  86. rewardRecord[itemId] = rewardRecord[itemId] + 1
  87. else
  88. rewardRecord[itemId] = 1
  89. end
  90. setplaydef(actor, fashionReward, rewardRecord)
  91. end
  92. end
  93. end
  94. end
  95. end
  96. if table.count(allItem) > 0 then
  97. -- additemmaptobag(actor, allItem)
  98. this.sendRewards(actor, allItem, itemConfigId)
  99. this.excHorseLamp(actor, allItem)
  100. end
  101. end
  102. ---发送奖励导背包,并且通知面板消息
  103. ---@param actor any 玩家
  104. ---@param rewardsMap table {道具id=数量,道具id=数量}
  105. function this.sendRewards(actor, rewardsMap, itemConfigId, actionDesc)
  106. local tipItems = {}
  107. -- 变身卡牌需要走批量
  108. local cardRewardMap = {}
  109. local otherRewardMap = {}
  110. for k, v in pairs(rewardsMap) do
  111. local data = rewardsMap[k]
  112. local cfgId = data.cfgid
  113. local count = data.num
  114. local itemCfg = ConfigDataManager.getById("cfg_item", cfgId)
  115. if table.notNullOrEmpty(itemCfg) then
  116. local itemType = string.tonumber(itemCfg.type)
  117. if itemType == ItemType.TRANSFER_CARD then
  118. cardRewardMap[cfgId] = count
  119. else
  120. table.insert(otherRewardMap, v)
  121. end
  122. end
  123. end
  124. if table.notNullOrEmpty(cardRewardMap) then
  125. if string.isNullOrEmpty(actionDesc) then
  126. Bag.addItemMapToBag(actor, cardRewardMap)
  127. else
  128. Bag.addItemMapToBag(actor, cardRewardMap, 0, 9999, actionDesc)
  129. end
  130. end
  131. -- 添加奖励到背包
  132. for k, v in pairs(otherRewardMap) do
  133. local data = otherRewardMap[k]
  134. local cfgId = data.cfgid
  135. local count = data.num
  136. local id = data.id
  137. local tableValue = ConfigDataManager.getTable("cfg_item", "id", cfgId)
  138. if table.isNullOrEmpty(tableValue) then
  139. error("Bag.sendRewards cfgId error", cfgId)
  140. goto continue
  141. end
  142. local notEnterPack = tableValue[1]["notenterpack"]
  143. local overlying = tableValue[1]["overlying"]
  144. if
  145. (string.isNullOrEmpty(notEnterPack) or tonumber(notEnterPack) == 0) and
  146. (string.isNullOrEmpty(overlying) or tonumber(overlying) <= 1)
  147. then
  148. for i = 1, count do
  149. local itemId
  150. if string.isNullOrEmpty(actionDesc) then
  151. itemId = Bag.addItemToBag(actor, cfgId, 1)
  152. else
  153. itemId = Bag.addItemToBag(actor, cfgId, 1, 0, 9999, actionDesc)
  154. end
  155. if (itemId == false or itemId == "false") then
  156. error("添加道具失败:", actor, cfgId, count, actionDesc)
  157. else
  158. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  159. if itemRow ~= nil and table.count(itemRow) > 0 and itemRow[1].useparam then
  160. local cfgFastionBox = ConfigDataManager.getById(itemRow[1].useparam, id)
  161. if cfgFastionBox ~= nil then
  162. this.resetEquipEntry(actor, cfgFastionBox, itemId)
  163. end
  164. end
  165. end
  166. table.insert(
  167. tipItems,
  168. {
  169. ["id"] = itemId,
  170. ["cfgId"] = cfgId,
  171. ["count"] = 1
  172. }
  173. )
  174. end
  175. else
  176. local itemId
  177. if string.isNullOrEmpty(actionDesc) then
  178. itemId = Bag.addItemToBag(actor, cfgId, count)
  179. else
  180. itemId = Bag.addItemToBag(actor, cfgId, count, 0, 9999, actionDesc)
  181. end
  182. if (itemId == false or itemId == "false") then
  183. error("添加道具失败:", actor, cfgId, count, actionDesc)
  184. else
  185. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  186. if itemRow ~= nil and table.count(itemRow) > 0 and itemRow[1].useparam then
  187. local cfgFastionBox = ConfigDataManager.getById(itemRow[1].useparam, id)
  188. if cfgFastionBox ~= nil then
  189. this.resetEquipEntry(actor, cfgFastionBox, itemId)
  190. end
  191. end
  192. end
  193. table.insert(
  194. tipItems,
  195. {
  196. ["id"] = itemId,
  197. ["cfgId"] = cfgId,
  198. ["count"] = count
  199. }
  200. )
  201. end
  202. ::continue::
  203. end
  204. if table.count(tipItems) > 0 then
  205. -- 奖励面板通知
  206. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tipItems)
  207. end
  208. end
  209. function this.resetEquipEntry(actor, cfgFastionBox, itemId)
  210. if cfgFastionBox == nil then
  211. return
  212. end
  213. -- 获取装备信息
  214. local item = getequipinfo(actor, itemId, 1)
  215. if not item or item == nil then
  216. return
  217. end
  218. if not string.isNullOrEmpty(cfgFastionBox.bind) then
  219. Platform.setItemBind(actor, itemId, 1)
  220. API.SetItemData(actor, itemId, "appendlv", 0)
  221. API.SetItemData(actor, itemId, "isbind", 1)
  222. resetequipentry(actor, itemId, {})
  223. end
  224. local entrys = {}
  225. if not string.isNullOrEmpty(cfgFastionBox.entryrandom) then
  226. local strentrys = string.split(cfgFastionBox.entryrandom, "|")
  227. local randnum = math.random(1, table.count(strentrys))
  228. if table.count(strentrys) > 0 and randnum >= 1 and randnum <= table.count(strentrys) then
  229. local strAttr = string.split(strentrys[randnum], "#")
  230. if table.count(strAttr) > 0 then
  231. table.insert(
  232. entrys,
  233. {
  234. entryid = table.count(entrys) + 1,
  235. attrid = strAttr[1],
  236. value = tonumber(strAttr[2])
  237. }
  238. )
  239. end
  240. end
  241. end
  242. if not string.isNullOrEmpty(cfgFastionBox.entry) then
  243. local strentrys = string.split(cfgFastionBox.entry, "|")
  244. for i, v in ipairs(strentrys) do
  245. local strAttr = string.split(v, "#")
  246. if table.count(strAttr) > 0 then
  247. table.insert(
  248. entrys,
  249. {
  250. entryid = table.count(entrys) + 1,
  251. attrid = strAttr[1],
  252. value = tonumber(strAttr[2])
  253. }
  254. )
  255. end
  256. end
  257. end
  258. if not string.isNullOrEmpty(cfgFastionBox.fastionlevel) then
  259. local lv = tonumber(cfgFastionBox.fastionlevel)
  260. -- 刷新装备
  261. API.SetItemData(actor, itemId, "strengthlv", lv)
  262. -- 刷新流光
  263. API.refreshLiuGuang(actor, itemId)
  264. Equip_QiangHua.RefreshLevel(actor, itemId, tonumber(cfgFastionBox.item))
  265. if lv == 13 then
  266. local cfg_item = ConfigDataManager.getById("cfg_item", tonumber(cfgFastionBox.item))
  267. local itemName = cfg_item ~= nil and cfg_item.name or ""
  268. -- cfgFastionBox.item
  269. local playerName = getbaseinfo(actor, "rolename")
  270. noticeTip.noticeinfo(actor, StringIdConst.text35008, playerName, itemName)
  271. end
  272. end
  273. -- 应用新的词条到装备
  274. local result = resetequipentry(actor, itemId, entrys)
  275. if not result then
  276. return
  277. end
  278. end
  279. function this.excHorseLamp(actor, itemMap)
  280. if table.isNullOrEmpty(itemMap) then
  281. return
  282. end
  283. local name = getbaseinfo(actor, "rolename")
  284. for k, v in pairs(itemMap) do
  285. local data = itemMap[k]
  286. local cfgid = data.cfgid
  287. local item = ConfigDataManager.getById("cfg_item", cfgid)
  288. if not table.isNullOrEmpty(item) then
  289. local itemName = item.name
  290. local noticeId = item.runninghorselamp
  291. if #noticeId > 0 then
  292. noticeTip.noticeinfo(actor, noticeId, name, itemName)
  293. end
  294. end
  295. end
  296. end
  297. --- 获取随机生成的道具数量
  298. ---@param actor any 玩家对象
  299. ---@param tableName string 配置表名
  300. function this.getItemAndCount(actor, tableName)
  301. local res = {}
  302. local boxConfig = ConfigDataManager.getList(tableName)
  303. if table.isNullOrEmpty(boxConfig) then
  304. gameDebug.print("RandomChest.generateChestItem useParam error")
  305. return res
  306. end
  307. local idMap = {}
  308. -- 职业限制判断
  309. local playerCareer = tonumber(getbaseinfo(actor, "getbasecareer"))
  310. -- 等级限制判断
  311. local playerLevel = getbaseinfo(actor, "level")
  312. local rewardRecord = getplaydef(actor, fashionReward)
  313. for _, config in pairs(boxConfig) do
  314. if config.career ~= "" and tonumber(playerCareer) ~= tonumber(string.split(config.career, "#")[1]) then
  315. goto tag
  316. end
  317. local lv_split = string.split(config.level, "#")
  318. if
  319. config.level ~= "" and
  320. (tonumber(playerLevel) < tonumber(lv_split[1]) or tonumber(playerLevel) > tonumber(lv_split[2]))
  321. then
  322. goto tag
  323. end
  324. local id = config.id
  325. local item = config.item
  326. local probability = string.split(config.probability, "|")
  327. local pro1 = string.split(probability[1], "#")
  328. if tonumber(config.type) == 1 then
  329. if not rewardRecord or not rewardRecord[item] then
  330. idMap[id] = pro1[2]
  331. else
  332. for _, v in pairs(probability) do
  333. local strs = string.split(v, "#")
  334. idMap[id] = tonumber(strs[1]) <= tonumber(rewardRecord[item]) and strs[2] or pro1[2]
  335. end
  336. end
  337. else
  338. local weight = pro1[2]
  339. if rewardRecord and rewardRecord[item] then
  340. for _, v in pairs(probability) do
  341. local v_split = string.split(v, "#")
  342. if tonumber(v_split[1]) ~= 0 then
  343. weight =
  344. tonumber(v_split[1]) >= rewardRecord[item] and tonumber(v_split[2]) or tonumber(pro1[2])
  345. end
  346. end
  347. end
  348. if tonumber(randomex(actor, weight, 10000)) == 1 then
  349. local num_split = string.split(config.num, "#")
  350. table.insert(
  351. res,
  352. {
  353. id = id,
  354. cfgid = item,
  355. num = math.random(num_split[1], num_split[2])
  356. }
  357. )
  358. end
  359. end
  360. ::tag::
  361. end
  362. if table.isNullOrEmpty(idMap) then
  363. gameDebug.print("RandomChest.generateChestItem itemMap is nil")
  364. return res
  365. end
  366. if tonumber(boxConfig[1].type) == 1 then
  367. local id = randombyweight(actor, idMap, 1)[1]
  368. local num = ConfigDataManager.getTableValue(tableName, "num", "id", id)
  369. local itemId = ConfigDataManager.getTableValue(tableName, "item", "id", id)
  370. local num_split = string.split(num, "#")
  371. table.insert(
  372. res,
  373. {
  374. id = id,
  375. cfgid = itemId,
  376. num = math.random(num_split[1], num_split[2])
  377. }
  378. )
  379. return res
  380. else
  381. return res
  382. end
  383. end