FashionBox.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. --- 宝箱读表随机生成道具
  2. FashionBox = {}
  3. local this = {}
  4. local fashionReward = "T$fashionReward"
  5. --- 使用宝箱生成道具
  6. ---@param actor any 玩家对象
  7. ---@param itemConfigId number 道具配置ID
  8. ---@param count number 数量
  9. function FashionBox.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].fashionbox ~= "1" 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, fashionReward)
  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, fashionReward, 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, fashionReward, 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)
  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. additemmaptobag(actor, cardRewardMap)
  76. else
  77. 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)
  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)
  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)
  159. if cfgFastionBox == nil then
  160. return
  161. end
  162. -- 获取装备信息
  163. local item = getequipinfo(actor, itemId, 1)
  164. if not item or item == nil then
  165. return
  166. end
  167. local entrys = {}
  168. if not string.isNullOrEmpty(cfgFastionBox.entryrandom) then
  169. local strentrys = string.split(cfgFastionBox.entryrandom, "|")
  170. local randnum = math.random(1, table.count(strentrys))
  171. if table.count(strentrys) > 0 and randnum >= 1 and randnum <= table.count(strentrys) then
  172. local strAttr = string.split(strentrys[randnum], "#")
  173. if table.count(strAttr) > 0 then
  174. table.insert(
  175. entrys,
  176. {
  177. entryid = table.count(entrys) + 1,
  178. attrid = strAttr[1],
  179. value = tonumber(strAttr[2])
  180. }
  181. )
  182. end
  183. end
  184. end
  185. if not string.isNullOrEmpty(cfgFastionBox.entry) then
  186. local strentrys = string.split(cfgFastionBox.entry, "|")
  187. for i, v in ipairs(strentrys) do
  188. local strAttr = string.split(v, "#")
  189. if table.count(strAttr) > 0 then
  190. table.insert(
  191. entrys,
  192. {
  193. entryid = table.count(entrys) + 1,
  194. attrid = strAttr[1],
  195. value = tonumber(strAttr[2])
  196. }
  197. )
  198. end
  199. end
  200. end
  201. if not string.isNullOrEmpty(cfgFastionBox.fastionlevel) then
  202. local lv = tonumber(cfgFastionBox.fastionlevel)
  203. -- 刷新装备
  204. API.SetItemData(actor, itemId, "strengthlv", lv)
  205. -- 刷新流光
  206. API.refreshLiuGuang(actor, itemId)
  207. Equip_QiangHua.RefreshLevel(actor, itemId, tonumber(cfgFastionBox.item))
  208. if lv == 13 then
  209. local cfg_item = ConfigDataManager.getById("cfg_item", tonumber(cfgFastionBox.item))
  210. local itemName = cfg_item ~= nil and cfg_item.name or ""
  211. -- cfgFastionBox.item
  212. local playerName = getbaseinfo(actor, "rolename")
  213. noticeTip.noticeinfo(actor, StringIdConst.text35008, playerName, itemName)
  214. end
  215. end
  216. -- 应用新的词条到装备
  217. local result = resetequipentry(actor, itemId, entrys)
  218. if not result then
  219. return
  220. end
  221. end
  222. function this.excHorseLamp(actor, itemMap)
  223. if table.isNullOrEmpty(itemMap) then
  224. return
  225. end
  226. local name = getbaseinfo(actor, "rolename")
  227. for k, v in pairs(itemMap) do
  228. local data = itemMap[k]
  229. local cfgid = data.cfgid
  230. local item = ConfigDataManager.getById("cfg_item", cfgid)
  231. if not table.isNullOrEmpty(item) then
  232. local itemName = item.name
  233. local noticeId = item.runninghorselamp
  234. if #noticeId > 0 then
  235. noticeTip.noticeinfo(actor, noticeId, name, itemName)
  236. end
  237. end
  238. end
  239. end
  240. --- 获取随机生成的道具数量
  241. ---@param actor any 玩家对象
  242. ---@param tableName string 配置表名
  243. function this.getItemAndCount(actor, tableName)
  244. local res = {}
  245. local boxConfig = ConfigDataManager.getList(tableName)
  246. if table.isNullOrEmpty(boxConfig) then
  247. gameDebug.print("RandomChest.generateChestItem useParam error")
  248. return res
  249. end
  250. local idMap = {}
  251. -- 职业限制判断
  252. local playerCareer = tonumber(getbaseinfo(actor, "getbasecareer"))
  253. -- 等级限制判断
  254. local playerLevel = getbaseinfo(actor, "level")
  255. local rewardRecord = getplaydef(actor, fashionReward)
  256. for _, config in pairs(boxConfig) do
  257. if config.career ~= "" and tonumber(playerCareer) ~= tonumber(string.split(config.career, "#")[1]) then
  258. goto tag
  259. end
  260. local lv_split = string.split(config.level, "#")
  261. if
  262. config.level ~= "" and
  263. (tonumber(playerLevel) < tonumber(lv_split[1]) or tonumber(playerLevel) > tonumber(lv_split[2]))
  264. then
  265. goto tag
  266. end
  267. local id = config.id
  268. local item = config.item
  269. local probability = string.split(config.probability, "|")
  270. local pro1 = string.split(probability[1], "#")
  271. if tonumber(config.type) == 1 then
  272. if not rewardRecord or not rewardRecord[item] then
  273. idMap[id] = pro1[2]
  274. else
  275. for _, v in pairs(probability) do
  276. local strs = string.split(v, "#")
  277. idMap[id] = tonumber(strs[1]) <= tonumber(rewardRecord[item]) and strs[2] or pro1[2]
  278. end
  279. end
  280. else
  281. local weight = pro1[2]
  282. if rewardRecord and rewardRecord[item] then
  283. for _, v in pairs(probability) do
  284. local v_split = string.split(v, "#")
  285. if tonumber(v_split[1]) ~= 0 then
  286. weight =
  287. tonumber(v_split[1]) >= rewardRecord[item] and tonumber(v_split[2]) or tonumber(pro1[2])
  288. end
  289. end
  290. end
  291. if tonumber(randomex(actor, weight, 10000)) == 1 then
  292. local num_split = string.split(config.num, "#")
  293. table.insert(
  294. res,
  295. {
  296. id = id,
  297. cfgid = item,
  298. num = math.random(num_split[1], num_split[2])
  299. }
  300. )
  301. end
  302. end
  303. ::tag::
  304. end
  305. if table.isNullOrEmpty(idMap) then
  306. gameDebug.print("RandomChest.generateChestItem itemMap is nil")
  307. return res
  308. end
  309. if tonumber(boxConfig[1].type) == 1 then
  310. local id = randombyweight(actor, idMap, 1)[1]
  311. local num = ConfigDataManager.getTableValue(tableName, "num", "id", id)
  312. local itemId = ConfigDataManager.getTableValue(tableName, "item", "id", id)
  313. local num_split = string.split(num, "#")
  314. table.insert(
  315. res,
  316. {
  317. id = id,
  318. cfgid = itemId,
  319. num = math.random(num_split[1], num_split[2])
  320. }
  321. )
  322. return res
  323. else
  324. return res
  325. end
  326. end