EquipBossBox.lua 13 KB

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