ItemRecycling.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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(ITEM_RECYCLING_TYPE, {})
  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. this.setItemRecycling(ownActor,msgData)
  210. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, msgData)
  211. end
  212. function this.setItemRecycling(ownActor,msgData)
  213. callonserial(ownActor, "set_item_recycling", msgData)
  214. end
  215. function set_item_recycling(ownActor,msgData)
  216. local itemRecoveryType = getsysvar(ownActor,ITEM_RECYCLING_TYPE)
  217. if table.isNullOrEmpty(itemRecoveryType) then
  218. itemRecoveryType = {}
  219. end
  220. itemRecoveryType[ownActor:toString()] = msgData
  221. setsysvar(ownActor,ITEM_RECYCLING_TYPE,itemRecoveryType)
  222. end
  223. -- 获取当前玩家回收设置信息
  224. function ItemRecycling.getItemRecyclingType(ownActor)
  225. local itemRecoveryType = getsysvar(ownActor,ITEM_RECYCLING_TYPE)
  226. if table.isNullOrEmpty(itemRecoveryType) then
  227. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, nil)
  228. return
  229. end
  230. for actor, recyclingType in pairs(itemRecoveryType) do
  231. if actor == ownActor:toString() then
  232. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, recyclingType)
  233. return
  234. end
  235. end
  236. sendluamsg(ownActor, LuaMessageIdToClient.RES_GET_RECYCLING_TYPE, nil)
  237. end
  238. -- 自动回收道具
  239. function ItemRecycling.autoRecycling(actor)
  240. jprint("开启自动回收")
  241. local allItemRecoveryType = getsysvar(actor,ITEM_RECYCLING_TYPE)
  242. local bagIndex = {}
  243. if table.isNullOrEmpty(allItemRecoveryType) then
  244. table.insert(bagIndex,0)
  245. return bagIndex
  246. end
  247. local itemRecoveryType = allItemRecoveryType[actor:toString()]
  248. if table.isNullOrEmpty(itemRecoveryType) then
  249. table.insert(bagIndex,0)
  250. return bagIndex
  251. end
  252. local allItemInfo = getallbagiteminfo(actor)
  253. if table.isEmpty(allItemInfo) then
  254. table.insert(bagIndex,0)
  255. return bagIndex
  256. end
  257. local alldata = EquipAndAppear.getLuaItemExtData(actor)
  258. for _, itemInfo in pairs(allItemInfo) do
  259. if this.getItemInfo(alldata,itemInfo.id) then
  260. local itemCfgId = itemInfo.cfgid
  261. if this.medicineCanRecycling(actor,itemCfgId) then
  262. local recoveryGroup = ConfigDataManager.getTableValue("cfg_item","recoveryGroup","id",itemCfgId)
  263. if not string.isNullOrEmpty(recoveryGroup) then
  264. local secondType = itemRecoveryType[recoveryGroup]
  265. if secondType ~= nil then
  266. if not secondType["1"] then
  267. table.insert(bagIndex,itemInfo.bagindex)
  268. else
  269. for _, type in pairs(secondType) do
  270. local recoveryScreen = ConfigDataManager.getTable("cfg_recovery_screen","id",type)
  271. if not table.isNullOrEmpty(recoveryScreen) then
  272. local recoveryScreenInfo = recoveryScreen[1]
  273. local recoveryScreenType = tonumber(recoveryScreenInfo.type)
  274. if recoveryScreenType == ItemRecyclingType.PIN_JIE then
  275. local rank = recoveryScreenInfo.rank
  276. local cfgRank = ConfigDataManager.getTableValue("cfg_item","rank","id",itemCfgId)
  277. if not string.isNullOrEmpty(rank) and not string.isNullOrEmpty(cfgRank) and tonumber(rank) == tonumber(cfgRank)then
  278. table.insert(bagIndex,itemInfo.bagindex)
  279. break
  280. end
  281. elseif recoveryScreenType == ItemRecyclingType.ZHUO_YUE then
  282. local cfgCount = recoveryScreenInfo.count
  283. local entries = itemInfo.entries
  284. if not string.isNullOrEmpty(cfgCount) and not table.isNullOrEmpty(entries) and tonumber(cfgCount) == table.count(entries) then
  285. table.insert(bagIndex,itemInfo.bagindex)
  286. break
  287. end
  288. elseif recoveryScreenType == ItemRecyclingType.ZHI_YE then
  289. local cfgCareer = recoveryScreenInfo.career
  290. local job = ConfigDataManager.getTableValue("cfg_item","job","id",itemCfgId)
  291. if not string.isNullOrEmpty(cfgCareer) and not string.isNullOrEmpty(job) then
  292. local recoveryCareer = string.split(cfgCareer,"|")
  293. local itemCareer = string.split(job,"|")
  294. for _, career in pairs(itemCareer) do
  295. if table.contains(recoveryCareer,career) then
  296. table.insert(bagIndex,itemInfo.bagindex)
  297. break
  298. end
  299. end
  300. end
  301. end
  302. end
  303. end
  304. end
  305. end
  306. end
  307. end
  308. end
  309. end
  310. table.insert(bagIndex,0)
  311. return bagIndex
  312. end
  313. --- 判断道具是否合乎药水回收要求
  314. function this.medicineCanRecycling(actor,itemCfgId)
  315. local type = ConfigDataManager.getTableValue("cfg_item","type","id",itemCfgId)
  316. local subType = ConfigDataManager.getTableValue("cfg_item","subType","id",itemCfgId)
  317. if tonumber(type) == 3 and tonumber(subType) == 1 then
  318. local conditions = ConfigDataManager.getTableValue("cfg_npcShop","conditions","itemId",itemCfgId)
  319. local result = checkcondition(actor,conditions)
  320. return result == "0"
  321. end
  322. return true
  323. end
  324. -- 判断当前道具有没有锻造
  325. function this.getItemInfo(alldata,itemId)
  326. local equipdata = alldata[tonumber(itemId)]
  327. if equipdata == nil then
  328. return true
  329. end
  330. if equipdata.strengthlv == nil and equipdata.appendlv == nil then
  331. return true
  332. end
  333. if equipdata.strengthlv ~= 0 or equipdata.appendlv ~= 0 then
  334. return false
  335. end
  336. return true
  337. end
  338. ---------------------------- 日志打印 -----------------------------
  339. this.log_open = false
  340. function this.debug(...)
  341. if not this.log_open then
  342. return
  343. end
  344. gameDebug.print(...)
  345. end
  346. function this.jprint(param)
  347. if not this.log_open then
  348. return
  349. end
  350. if param == nil then
  351. param = "error! 输出内容为空. nil"
  352. end
  353. jprint(param)
  354. end
  355. function this.loginfo(actor, param)
  356. if not this.log_open then
  357. return
  358. end
  359. if param == nil then
  360. param = "error! 日志输出内容为空. nil"
  361. end
  362. jprint(param)
  363. info(actor, param)
  364. end