FashionBox.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 (string.isNullOrEmpty(notEnterPack) or tonumber(notEnterPack) == 0) and
  94. (string.isNullOrEmpty(overlying) or tonumber(overlying) <= 1) then
  95. for i = 1, count do
  96. local itemId
  97. if string.isNullOrEmpty(actionDesc) then
  98. itemId = additemtobag(actor, cfgId, 1)
  99. else
  100. itemId = additemtobag(actor, cfgId, 1, 0, 9999, actionDesc)
  101. end
  102. if (itemId == false or itemId == "false") then
  103. error("添加道具失败:", actor, cfgId, count, actionDesc)
  104. else
  105. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  106. if itemRow ~= nil and table.count(itemRow) > 0 and itemRow[1].useparam then
  107. local cfgFastionBox = ConfigDataManager.getById(itemRow[1].useparam, id)
  108. if cfgFastionBox ~= nil then
  109. this.resetEquipEntry(actor, cfgFastionBox, itemId)
  110. end
  111. end
  112. end
  113. table.insert(tipItems, {
  114. ["id"] = itemId,
  115. ["cfgId"] = cfgId,
  116. ["count"] = 1
  117. })
  118. end
  119. else
  120. local itemId
  121. if string.isNullOrEmpty(actionDesc) then
  122. itemId = additemtobag(actor, cfgId, count)
  123. else
  124. itemId = additemtobag(actor, cfgId, count, 0, 9999, actionDesc)
  125. end
  126. if (itemId == false or itemId == "false") then
  127. error("添加道具失败:", actor, cfgId, count, actionDesc)
  128. else
  129. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  130. if itemRow ~= nil and table.count(itemRow) > 0 and itemRow[1].useparam then
  131. local cfgFastionBox = ConfigDataManager.getById(itemRow[1].useparam, id)
  132. if cfgFastionBox ~= nil then
  133. this.resetEquipEntry(actor, cfgFastionBox, itemId)
  134. end
  135. end
  136. end
  137. table.insert(tipItems, {
  138. ["id"] = itemId,
  139. ["cfgId"] = cfgId,
  140. ["count"] = count
  141. })
  142. end
  143. ::continue::
  144. end
  145. if table.count(tipItems) > 0 then
  146. -- 奖励面板通知
  147. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tipItems)
  148. end
  149. end
  150. function this.resetEquipEntry(actor, cfgFastionBox, itemId)
  151. if cfgFastionBox == nil then
  152. return
  153. end
  154. -- 获取装备信息
  155. local item = getequipinfo(actor, itemId, 1)
  156. if not item or item == nil then
  157. return
  158. end
  159. local entrys = {}
  160. if not string.isNullOrEmpty(cfgFastionBox.entryrandom) then
  161. local strentrys = string.split(cfgFastionBox.entryrandom, "|")
  162. local randnum = math.random(1, table.count(strentrys))
  163. if table.count(strentrys) > 0 and randnum >= 1 and randnum <= table.count(strentrys) then
  164. local strAttr = string.split(strentrys[randnum], "#")
  165. if table.count(strAttr) > 0 then
  166. table.insert(entrys, {
  167. entryid = table.count(entrys) + 1,
  168. attrid = strAttr[1],
  169. value = tonumber(strAttr[2])
  170. })
  171. end
  172. end
  173. end
  174. if not string.isNullOrEmpty(cfgFastionBox.entry) then
  175. local strentrys = string.split(cfgFastionBox.entry, "|")
  176. for i, v in ipairs(strentrys) do
  177. local strAttr = string.split(v, "#")
  178. if table.count(strAttr) > 0 then
  179. table.insert(entrys, {
  180. entryid = table.count(entrys) + 1,
  181. attrid = strAttr[1],
  182. value = tonumber(strAttr[2])
  183. })
  184. end
  185. end
  186. end
  187. if not string.isNullOrEmpty(cfgFastionBox.fastionlevel) then
  188. local lv = tonumber(cfgFastionBox.fastionlevel)
  189. -- 刷新装备
  190. API.SetItemData(actor, itemId, "strengthlv", lv)
  191. -- 刷新流光
  192. API.refreshLiuGuang(actor, itemId)
  193. Equip_QiangHua.RefreshLevel(actor, itemId, tonumber(cfgFastionBox.item))
  194. if lv == 13 then
  195. local cfg_item = ConfigDataManager.getById("cfg_item", tonumber(cfgFastionBox.item))
  196. local itemName = cfg_item ~= nil and cfg_item.name or ""
  197. -- cfgFastionBox.item
  198. local playerName = getbaseinfo(actor, "rolename")
  199. noticeTip.noticeinfo(actor, StringIdConst.text35008, playerName, itemName)
  200. end
  201. end
  202. -- 应用新的词条到装备
  203. local result = resetequipentry(actor, itemId, entrys)
  204. if not result then
  205. return
  206. end
  207. end
  208. function this.excHorseLamp(actor, itemMap)
  209. if table.isNullOrEmpty(itemMap) then
  210. return
  211. end
  212. local name = getbaseinfo(actor, "rolename")
  213. for k, v in pairs(itemMap) do
  214. local data = itemMap[k]
  215. local cfgid = data.cfgid
  216. local item = ConfigDataManager.getById("cfg_item", cfgid)
  217. if not table.isNullOrEmpty(item) then
  218. local itemName = item.name
  219. local noticeId = item.runninghorselamp
  220. if #noticeId > 0 then
  221. noticeTip.noticeinfo(actor, noticeId, name, itemName)
  222. end
  223. end
  224. end
  225. end
  226. --- 获取随机生成的道具数量
  227. ---@param actor any 玩家对象
  228. ---@param tableName string 配置表名
  229. function this.getItemAndCount(actor, tableName)
  230. local res = {}
  231. local boxConfig = ConfigDataManager.getList(tableName)
  232. if table.isNullOrEmpty(boxConfig) then
  233. gameDebug.print("RandomChest.generateChestItem useParam error")
  234. return res
  235. end
  236. local idMap = {}
  237. -- 职业限制判断
  238. local playerCareer = tonumber(getbaseinfo(actor, "getbasecareer"))
  239. -- 等级限制判断
  240. local playerLevel = getbaseinfo(actor, "level")
  241. local rewardRecord = getplaydef(actor, fashionReward)
  242. for _, config in pairs(boxConfig) do
  243. if config.career ~= "" and tonumber(playerCareer) ~= tonumber(string.split(config.career, "#")[1]) then
  244. goto tag
  245. end
  246. local lv_split = string.split(config.level, "#")
  247. if config.level ~= "" and
  248. (tonumber(playerLevel) < tonumber(lv_split[1]) or tonumber(playerLevel) > tonumber(lv_split[2])) then
  249. goto tag
  250. end
  251. local id = config.id
  252. local item = config.item
  253. local probability = string.split(config.probability, "|")
  254. local pro1 = string.split(probability[1], "#")
  255. if tonumber(config.type) == 1 then
  256. if not rewardRecord or not rewardRecord[item] then
  257. idMap[id] = pro1[2]
  258. else
  259. for _, v in pairs(probability) do
  260. local strs = string.split(v, "#")
  261. idMap[id] = tonumber(strs[1]) <= tonumber(rewardRecord[item]) and strs[2] or pro1[2]
  262. end
  263. end
  264. else
  265. local weight = pro1[2]
  266. if rewardRecord and rewardRecord[item] then
  267. for _, v in pairs(probability) do
  268. local v_split = string.split(v, "#")
  269. if tonumber(v_split[1]) ~= 0 then
  270. weight = tonumber(v_split[1]) >= rewardRecord[item] and tonumber(v_split[2]) or
  271. tonumber(pro1[2])
  272. end
  273. end
  274. end
  275. if tonumber(randomex(actor, weight, 10000)) == 1 then
  276. local num_split = string.split(config.num, "#")
  277. table.insert(res, {
  278. id = id,
  279. cfgid = item,
  280. num = math.random(num_split[1], num_split[2])
  281. })
  282. end
  283. end
  284. ::tag::
  285. end
  286. if table.isNullOrEmpty(idMap) then
  287. gameDebug.print("RandomChest.generateChestItem itemMap is nil")
  288. return res
  289. end
  290. if tonumber(boxConfig[1].type) == 1 then
  291. local id = randombyweight(actor, idMap, 1)[1]
  292. local num = ConfigDataManager.getTableValue(tableName, "num", "id", id)
  293. local itemId = ConfigDataManager.getTableValue(tableName, "item", "id", id)
  294. local num_split = string.split(num, "#")
  295. table.insert(res, {
  296. id = id,
  297. cfgid = itemId,
  298. num = math.random(num_split[1], num_split[2])
  299. })
  300. return res
  301. else
  302. return res
  303. end
  304. end