ItemRecycling.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. ItemRecycling = {}
  2. local this = {}
  3. local ItemRecyclingType = {
  4. PIN_JIE = 1,
  5. ZHUO_YUE = 2,
  6. ZHI_YE = 3
  7. }
  8. ITEM_RECYCLING_TYPE = "R$itemRecyclingTYPE"
  9. -- 合服时取消所有选择的回收类型
  10. function ItemRecycling.combineglobalvar(varName)
  11. local allWorldGoods = getsysvar(ITEM_RECYCLING_TYPE)
  12. if table.isEmpty(allWorldGoods) then
  13. return
  14. end
  15. setsysvar(SYS_TRADE_WORLD_GOODS, {})
  16. end
  17. -- 分解
  18. function ItemRecycling.decompositionlua(actor,msgID,msgData)
  19. local decompositionItemList = msgData[1]
  20. local allItemInfo = getallbagiteminfo(actor)
  21. if table.isEmpty(allItemInfo) then
  22. return
  23. end
  24. if table.isEmpty(decompositionItemList) then
  25. return
  26. end
  27. local allequip = getplaydef(actor, "T$luaitemextdata")
  28. local allNeedMaterial = {}
  29. local allObtainList = {}
  30. for bagIndex, count in pairs(decompositionItemList) do
  31. for _, itemInfo in pairs(allItemInfo) do
  32. if itemInfo.bagindex == tonumber(bagIndex) then
  33. local itemCfgId = itemInfo.cfgid
  34. local decomposeGroup = ConfigDataManager.getTableValue("cfg_item","decomposeGroup","id",itemCfgId)
  35. if string.isNullOrEmpty(decomposeGroup) then
  36. tipinfo(actor,"选择的道具中拥有不能分解的道具,请重新选择")
  37. return
  38. end
  39. local decomposeTable = ConfigDataManager.getTable("cfg_recovery_out","id",decomposeGroup)
  40. if table.isEmpty(decomposeTable) then
  41. -- error(actor,"分解类型配置错误")
  42. return
  43. end
  44. if not table.isNullOrEmpty(allequip) then
  45. local equipext = allequip[tonumber(itemInfo.id)]
  46. if not table.isNullOrEmpty(equipext) then
  47. allequip[tonumber(itemInfo.id)] = nil
  48. end
  49. end
  50. local decomposeInfo = decomposeTable[1]
  51. -- 计算分解需要的东西
  52. local needMaterial = decomposeInfo.needmaterial
  53. if not string.isNullOrEmpty(needMaterial) then
  54. local needMaterialInfo = string.split(needMaterial,"#")
  55. local needMaterialCount = allNeedMaterial[needMaterialInfo[1]]
  56. if not needMaterialCount then
  57. needMaterialCount = 0
  58. end
  59. allNeedMaterial[needMaterialInfo[1]] = needMaterialCount + (tonumber(needMaterialInfo[2]) * count)
  60. end
  61. -- 计算分解获得的东西
  62. local material = decomposeInfo.material
  63. local group = decomposeInfo.group
  64. if tonumber(group) == 2 then
  65. if not string.isNullOrEmpty(material) then
  66. local allObtainTable = string.split(material,"|")
  67. if not table.isEmpty(allObtainTable) then
  68. if table.count(allObtainTable) == 1 then
  69. -- 分解只会获得一种物品
  70. local obtainInfo = string.split(allObtainTable[1],"#")
  71. local obtainCount = allObtainList[obtainInfo[1]]
  72. if obtainCount == nil then
  73. obtainCount = 0
  74. end
  75. local total = obtainCount + (tonumber(obtainInfo[2]) * count)
  76. allObtainList[obtainInfo[1]] = total
  77. else
  78. -- 分解获得物品配置权重
  79. local allWeight = 0
  80. local newAllObtainTable = {}
  81. for _, obtainInfo in pairs(allObtainTable) do
  82. local obtainWeight = string.split(obtainInfo,"#")
  83. allWeight = allWeight + tonumber(obtainWeight[3])
  84. obtainWeight[3] = allWeight
  85. table.insert(newAllObtainTable,obtainWeight)
  86. end
  87. for i = 1, count do
  88. local successIndex = math.random(1, allWeight)
  89. local lastWeight = 0
  90. for _, newObtainInfo in pairs(newAllObtainTable) do
  91. if lastWeight < successIndex and newObtainInfo[3] >= successIndex then
  92. local obtainCount = allObtainList[newObtainInfo[1]]
  93. if obtainCount == nil then
  94. obtainCount = 0
  95. end
  96. allObtainList[newObtainInfo[1]] = obtainCount + (tonumber(newObtainInfo[2]))
  97. end
  98. lastWeight = newObtainInfo[3]
  99. end
  100. end
  101. end
  102. end
  103. end
  104. else
  105. if not string.isNullOrEmpty(material) then
  106. local allObtainTable = string.split(material,"|")
  107. for _, obtainInfo in pairs(allObtainTable) do
  108. local obtainItemInfo = string.split(obtainInfo,"#")
  109. local obtainCount = allObtainList[obtainItemInfo[1]]
  110. if obtainCount == nil then
  111. obtainCount = 0
  112. end
  113. local total = obtainCount + (tonumber(obtainItemInfo[2]) * count)
  114. allObtainList[obtainItemInfo[1]] = total
  115. end
  116. end
  117. end
  118. end
  119. end
  120. end
  121. itemdecomposition(actor,decompositionItemList,allObtainList,allNeedMaterial)
  122. sendluamsg(actor,LuaMessageIdToClient.RES_ITEM_DECOMPOSITION,allObtainList)
  123. setplaydef(actor, "T$luaitemextdata",allequip)
  124. end
  125. function ItemRecycling.recyclingRateReward(actor, reward)
  126. this.recyclingRateReward(actor, reward)
  127. end
  128. function this.recyclingRateReward(actor, reward)
  129. local rate = 0
  130. -- 月卡特权
  131. local is_open, count = PrivilegeMonth.hasPrivilege(actor, PrivilegeMonth.PrivilegeType.RECYCLING_INCOME_UP)
  132. if is_open then
  133. rate = rate + count;
  134. end
  135. -- VIP特权
  136. local vip_open, vip_rate = VipGiftPack.hasPrivilege(actor, VipPrivilege.Type.recycle)
  137. if vip_open and string.tonumber(vip_rate) > 0 then
  138. rate = rate + tonumber(vip_rate)
  139. end
  140. if rate < 1 then
  141. return
  142. end
  143. for key, value in pairs(reward) do
  144. local final_value = value * rate / 100
  145. if final_value >= 1 then
  146. reward[key] = final_value
  147. end
  148. end
  149. additemmaptobag(actor, reward, 0, 9999, '回收')
  150. GameTips.sendGetRewardMsg(actor, reward)
  151. this.debug("加成过的回收奖励", reward)
  152. end
  153. -- 回收
  154. function ItemRecycling.recovery(actor,msgData)
  155. local allItemInfo = getallbagiteminfo(actor)
  156. if table.isEmpty(allItemInfo) then
  157. return
  158. end
  159. if table.isEmpty(msgData) then
  160. return
  161. end
  162. local allObtainList = {}
  163. local allequip = getplaydef(actor, "T$luaitemextdata")
  164. for bagIndex, count in pairs(msgData) do
  165. for _, itemInfo in pairs(allItemInfo) do
  166. if itemInfo.bagindex == tonumber(bagIndex) then
  167. local itemCfgId = itemInfo.cfgid
  168. local recoveryGroup = ConfigDataManager.getTableValue("cfg_item","recoveryGroup","id",itemCfgId)
  169. local recoveryUp = ConfigDataManager.getTableValue("cfg_item","recoveryUp","id",itemCfgId)
  170. if string.isNullOrEmpty(recoveryGroup) then
  171. tipinfo(actor,"选择的道具中拥有不能分解的道具,请重新选择")
  172. return
  173. end
  174. local recoveryTable = ConfigDataManager.getTable("cfg_recovery","id",recoveryGroup)
  175. if table.isEmpty(recoveryTable) then
  176. -- error(actor,"回收类型配置错误")
  177. return
  178. end
  179. if not table.isNullOrEmpty(allequip) then
  180. local equipext = allequip[tonumber(itemInfo.id)]
  181. if not table.isNullOrEmpty(equipext) then
  182. allequip[tonumber(itemInfo.id)] = nil
  183. end
  184. end
  185. local recoveryInfo = recoveryTable[1]
  186. local materialString = recoveryInfo.material
  187. local material = string.split(materialString,"#")
  188. local addCount = math.random(tonumber(material[2]), tonumber(material[3]))
  189. addCount = addCount * count
  190. if not string.isNullOrEmpty(recoveryUp) then
  191. addCount = addCount * ( 1 + math.floor(tonumber(recoveryUp) / 100))
  192. end
  193. local obtainCount = allObtainList[material[1]]
  194. if obtainCount == nil then
  195. obtainCount = 0
  196. end
  197. allObtainList[material[1]] = obtainCount + addCount
  198. end
  199. end
  200. end
  201. itemrecovery(actor,msgData,allObtainList)
  202. sendluamsg(actor, LuaMessageIdToClient.RES_RECOVERY_REWARD, allObtainList)
  203. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, allObtainList)
  204. setplaydef(actor, "T$luaitemextdata",allequip)
  205. -- sendluamsg(actor,LuaMessageIdToClient.RES_ITEM_RECOVERY,allObtainList)
  206. end
  207. -- 设置玩家回收类型
  208. function ItemRecycling.setItemRecyclingType(ownActor,msgData)
  209. local itemRecoveryType = getsysvar(ownActor,ITEM_RECYCLING_TYPE)
  210. if table.isNullOrEmpty(itemRecoveryType) then
  211. itemRecoveryType = {}
  212. end
  213. itemRecoveryType[ownActor:toString()] = msgData
  214. setsysvar(ownActor,ITEM_RECYCLING_TYPE,itemRecoveryType)
  215. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, msgData)
  216. end
  217. -- 获取当前玩家回收设置信息
  218. function ItemRecycling.getItemRecyclingType(ownActor)
  219. local itemRecoveryType = getsysvar(ownActor,ITEM_RECYCLING_TYPE)
  220. if table.isNullOrEmpty(itemRecoveryType) then
  221. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, nil)
  222. return
  223. end
  224. for actor, recyclingType in pairs(itemRecoveryType) do
  225. if actor == ownActor:toString() then
  226. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, recyclingType)
  227. return
  228. end
  229. end
  230. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, nil)
  231. end
  232. -- 自动回收道具
  233. function ItemRecycling.autoRecycling(actor)
  234. jprint("开启自动回收")
  235. local allItemRecoveryType = getsysvar(actor,ITEM_RECYCLING_TYPE)
  236. local bagIndex = {}
  237. if table.isNullOrEmpty(allItemRecoveryType) then
  238. table.insert(bagIndex,0)
  239. return bagIndex
  240. end
  241. local itemRecoveryType = allItemRecoveryType[actor:toString()]
  242. if table.isNullOrEmpty(itemRecoveryType) then
  243. table.insert(bagIndex,0)
  244. return bagIndex
  245. end
  246. local allItemInfo = getallbagiteminfo(actor)
  247. if table.isEmpty(allItemInfo) then
  248. table.insert(bagIndex,0)
  249. return bagIndex
  250. end
  251. local alldata = EquipAndAppear.getLuaItemExtData(actor)
  252. for _, itemInfo in pairs(allItemInfo) do
  253. if this.getItemInfo(alldata,itemInfo.id) then
  254. local itemCfgId = itemInfo.cfgid
  255. local recoveryGroup = ConfigDataManager.getTableValue("cfg_item","recoveryGroup","id",itemCfgId)
  256. if not string.isNullOrEmpty(recoveryGroup) then
  257. local secondType = itemRecoveryType[recoveryGroup]
  258. if secondType ~= nil then
  259. if not secondType["1"] then
  260. table.insert(bagIndex,itemInfo.bagindex)
  261. else
  262. for _, type in pairs(secondType) do
  263. local recoveryScreen = ConfigDataManager.getTable("cfg_recovery_screen","id",type)
  264. if not table.isNullOrEmpty(recoveryScreen) then
  265. local recoveryScreenInfo = recoveryScreen[1]
  266. local recoveryScreenType = tonumber(recoveryScreenInfo.type)
  267. if recoveryScreenType == ItemRecyclingType.PIN_JIE then
  268. local rank = recoveryScreenInfo.rank
  269. local cfgRank = ConfigDataManager.getTableValue("cfg_item","rank","id",itemCfgId)
  270. if not string.isNullOrEmpty(rank) and not string.isNullOrEmpty(cfgRank) and tonumber(rank) == tonumber(cfgRank)then
  271. table.insert(bagIndex,itemInfo.bagindex)
  272. break
  273. end
  274. elseif recoveryScreenType == ItemRecyclingType.ZHUO_YUE then
  275. local cfgCount = recoveryScreenInfo.count
  276. local entries = itemInfo.entries
  277. if not string.isNullOrEmpty(cfgCount) and not table.isNullOrEmpty(entries) and tonumber(cfgCount) == table.count(entries) then
  278. table.insert(bagIndex,itemInfo.bagindex)
  279. break
  280. end
  281. elseif recoveryScreenType == ItemRecyclingType.ZHI_YE then
  282. local cfgCareer = recoveryScreenInfo.career
  283. local job = ConfigDataManager.getTableValue("cfg_item","job","id",itemCfgId)
  284. if not string.isNullOrEmpty(cfgCareer) and not string.isNullOrEmpty(job) then
  285. local recoveryCareer = string.split(cfgCareer,"|")
  286. local itemCareer = string.split(job,"|")
  287. for _, career in pairs(itemCareer) do
  288. if table.contains(recoveryCareer,career) then
  289. table.insert(bagIndex,itemInfo.bagindex)
  290. break
  291. end
  292. end
  293. end
  294. end
  295. end
  296. end
  297. end
  298. end
  299. end
  300. end
  301. end
  302. table.insert(bagIndex,0)
  303. return bagIndex
  304. end
  305. -- 判断当前道具有没有锻造
  306. function this.getItemInfo(alldata,itemId)
  307. local equipdata = alldata[tonumber(itemId)]
  308. if equipdata == nil then
  309. return true
  310. end
  311. if equipdata.strengthlv == nil and equipdata.appendlv == nil then
  312. return true
  313. end
  314. if equipdata.strengthlv ~= 0 or equipdata.appendlv ~= 0 then
  315. return false
  316. end
  317. return true
  318. end
  319. ---------------------------- 日志打印 -----------------------------
  320. this.log_open = false
  321. function this.debug(...)
  322. if not this.log_open then
  323. return
  324. end
  325. gameDebug.print(...)
  326. end
  327. function this.jprint(param)
  328. if not this.log_open then
  329. return
  330. end
  331. if param == nil then
  332. param = "error! 输出内容为空. nil"
  333. end
  334. jprint(param)
  335. end
  336. function this.loginfo(actor, param)
  337. if not this.log_open then
  338. return
  339. end
  340. if param == nil then
  341. param = "error! 日志输出内容为空. nil"
  342. end
  343. jprint(param)
  344. info(actor, param)
  345. end