EquipLogic.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. local Msg = require("core.Msg")
  2. local Util = require("common.Util")
  3. local Grid = require("bag.Grid")
  4. local ItemDefine = require("bag.ItemDefine")
  5. local TuJianExcel = require("excel.equip").tujian
  6. local EquipExcel = require("excel.equip").equip
  7. local ItemExcel = require("excel.item").item
  8. local EquipLogicGrid = require("equip.EquipLogicGrid")
  9. local Log = require("common.Log")
  10. local LogDefine = require("common.LogDefine")
  11. local Lang = require("common.Lang")
  12. local Broadcast = require("broadcast.Broadcast")
  13. local MailManager = require("mail.MailManager")
  14. local MailExcel = require("excel.mail")
  15. local HeroLogic = require("hero.HeroLogic")
  16. EQUIP_BAG_MAX_CNT = 300
  17. EQUIP_BAG_OP_ADD = 1 -- 增
  18. EQUIP_BAG_OP_DEL = 2 -- 删
  19. EQUIP_BAG_OP_CHANGE = 3 -- 改
  20. EQUIP_QUALITY_MAX = 5
  21. EQUIP_QUALITY = { 5000, 3000, 1500, 500 }
  22. EQUIP_QUALITY_WEIGHT = 10000
  23. EQUIP_QUALITY_BASE_RATE = { 7000, 8000, 9000, 10000, 12000 }
  24. -- 随机属性
  25. local attrCheck = {}
  26. function checkAttr(equipGrid)
  27. local quality = equipGrid.quality
  28. local len = 0
  29. for key, value in pairs(equipGrid.attr) do
  30. len = len + 1
  31. attrCheck[len] = {key, value}
  32. end
  33. -- 砍掉后面的
  34. if quality < len then
  35. equipGrid.attr = {}
  36. for z = 1, quality do
  37. equipGrid.attr[attrCheck[z][1]] = attrCheck[z][2]
  38. end
  39. end
  40. end
  41. -- 随机属性
  42. function randomAttr(itemID, isRandom, otherData)
  43. local equipConfig = EquipExcel[itemID]
  44. if not equipConfig then return end
  45. -- 固定属性
  46. local quality = 1
  47. local attr = { }
  48. if equipConfig.random == 1 and not isRandom then
  49. quality = #equipConfig.randomAttr
  50. if quality > EQUIP_QUALITY_MAX then
  51. quality = EQUIP_QUALITY_MAX
  52. end
  53. for i = 1, quality do
  54. local key = equipConfig.randomAttr[i][1]
  55. local value = equipConfig.randomAttr[i][2]
  56. attr[key] = attr[key] or 0
  57. attr[key] = attr[key] + value
  58. end
  59. return attr, quality
  60. end
  61. if not otherData then
  62. -- 随机品质
  63. local random = math.random(1, EQUIP_QUALITY_WEIGHT)
  64. for i = 1, #EQUIP_QUALITY do
  65. local weight = EQUIP_QUALITY[i]
  66. if random <= weight then
  67. quality = i > #equipConfig.randomAttr and #equipConfig.randomAttr or i
  68. break
  69. else
  70. random = random - weight
  71. end
  72. end
  73. else
  74. if type(otherData) == "number" then
  75. otherData = otherData < 0 and 1 or otherData
  76. quality = otherData > #equipConfig.randomAttr and #equipConfig.randomAttr or otherData
  77. end
  78. end
  79. if quality > EQUIP_QUALITY_MAX then
  80. quality = EQUIP_QUALITY_MAX
  81. end
  82. -- 计算总权重
  83. local totalWeight = 0
  84. for k, v in pairs(equipConfig.randomAttr) do
  85. totalWeight = totalWeight + v[3]
  86. end
  87. -- 随机条目属性
  88. local MathRandom = math.random
  89. local randomAttr = Util.copyTable(equipConfig.randomAttr)
  90. for z = 1, quality do
  91. local randmWeight = MathRandom(1, totalWeight)
  92. for k, v in pairs(randomAttr) do
  93. local key = v[1]
  94. local value = v[2]
  95. local weight = v[3]
  96. if randmWeight <= weight then
  97. attr[key] = attr[key] or 0
  98. attr[key] = attr[key] + value
  99. randomAttr[k] = nil
  100. -- 排除这个权重
  101. totalWeight = totalWeight - weight
  102. break
  103. else
  104. randmWeight = randmWeight - weight
  105. end
  106. end
  107. end
  108. return attr, quality
  109. end
  110. -- 检查背包空间
  111. function checkEmptyCnt(human, itemCnt)
  112. if itemCnt > getEmptyCnt(human) then
  113. Broadcast.sendErr(human, Lang.COMMON_BAG_FULL)
  114. return
  115. end
  116. return true
  117. end
  118. -- 获取装备对基础属性的影响
  119. function getEquipBaseRate(quality)
  120. local baseRate = EQUIP_QUALITY_BASE_RATE[quality]
  121. if not baseRate then return 1 end
  122. return baseRate / 10000
  123. end
  124. -- 获取装备品质
  125. function getEquipMaxQuality(equipConfig)
  126. if not equipConfig.randomAttr then return 0 end
  127. local maxQuality = 0
  128. if equipConfig.random == 1 then
  129. maxQuality = #equipConfig.randomAttr
  130. end
  131. return maxQuality
  132. end
  133. -- 获取装备附加属性
  134. function getEquipTzAttr(equipConfig)
  135. if not equipConfig.randomAttr then return end
  136. if equipConfig.random == 1 then
  137. return equipConfig.randomAttr
  138. end
  139. return nil
  140. end
  141. -- 返回装备背包空余格子数
  142. function getEmptyCnt(human)
  143. local emptyCnt = 0
  144. for i = 1, EQUIP_BAG_MAX_CNT do
  145. if human.db.equipBag[i] == nil then
  146. emptyCnt = emptyCnt + 1
  147. end
  148. end
  149. return emptyCnt
  150. end
  151. -- 获得装备背包第一个空格子下标
  152. function getEmptyIndex(human)
  153. for index = 1, EQUIP_BAG_MAX_CNT do
  154. local grid = human.db.equipBag[index]
  155. if grid == nil then
  156. return index
  157. end
  158. end
  159. end
  160. -- 返回装备根据uuid
  161. function getEquipByUuid(human, uuid)
  162. local emptyCnt = 0
  163. for i = 1, EQUIP_BAG_MAX_CNT do
  164. local equipGrid = human.db.equipBag[i]
  165. if equipGrid and equipGrid.uuid == uuid then
  166. return equipGrid
  167. end
  168. end
  169. end
  170. -- 返回装备根据pos
  171. function getEquipByPos(human, star, pos)
  172. local emptyCnt = 0
  173. for i = 1, EQUIP_BAG_MAX_CNT do
  174. local equipGrid = human.db.equipBag[i]
  175. if equipGrid then
  176. local conf = EquipExcel[equipGrid.id]
  177. if conf and conf.subType == pos and star >= conf.star then
  178. return equipGrid
  179. end
  180. end
  181. end
  182. end
  183. -- 修复老数据
  184. function modifyEquip(human, equipGrid)
  185. if equipGrid.putUuid == nil then return end
  186. local heroGrid = HeroLogic.getHeroGridByUuid(human, equipGrid.putUuid)
  187. if heroGrid == nil then
  188. equipGrid.putUuid = nil
  189. end
  190. end
  191. -- 推送装备背包信息
  192. function sendEquipBagList(human)
  193. local msgRet = Msg.gc.GC_EQUIP_BAG_LIST
  194. local len = 0
  195. for index = 1, EQUIP_BAG_MAX_CNT do
  196. local equipGrid = human.db.equipBag[index]
  197. if equipGrid then
  198. len = len + 1
  199. Grid.makeItem(msgRet.list[len], equipGrid.id, 1, nil, equipGrid, index, Grid.getOpflagAtBag(equipGrid.id))
  200. if len >= 30 then
  201. msgRet.list[0] = len
  202. len = 0
  203. Msg.send(msgRet, human.fd)
  204. end
  205. end
  206. end
  207. if len > 0 then
  208. msgRet.list[0] = len
  209. Msg.send(msgRet, human.fd)
  210. end
  211. end
  212. -- 制造一个获得列表
  213. function makeEquipItem(human, nets, len)
  214. if not human.getEquip then return len end
  215. for i = 1, #human.getEquip do
  216. len = len + 1
  217. if not nets[len] then
  218. len = len - 1
  219. break
  220. end
  221. local equipGrid = human.getEquip[i]
  222. Grid.makeItem(nets[len], equipGrid.id, 1, nil, equipGrid)
  223. end
  224. human.getEquip = nil
  225. return len
  226. end
  227. -- 制造一个装备获得
  228. function makeEquipItemOne(human, net)
  229. if not human.getEquip then return end
  230. local equipGrid = human.getEquip[1]
  231. Grid.makeItem(net, equipGrid.id, 1, nil, equipGrid)
  232. human.getEquip = nil
  233. end
  234. -- 制造一个装备
  235. function makeEquip(itemID, otherData)
  236. local equipGrid = EquipLogicGrid.createGrid(itemID)
  237. if not equipGrid then return end
  238. -- 随机属性
  239. local attr, quality = randomAttr(itemID, nil, otherData)
  240. equipGrid.attr = attr
  241. equipGrid.quality = quality
  242. checkAttr(equipGrid)
  243. return equipGrid
  244. end
  245. -- 添加装备
  246. function addEquip(human, itemID, itemCnt, logType, otherData)
  247. if not EquipExcel[itemID] then return end
  248. for i = 1, itemCnt do
  249. local equipGrid = makeEquip(itemID, otherData)
  250. if equipGrid then
  251. addByEquipGrid(human, equipGrid, logType)
  252. end
  253. end
  254. end
  255. -- 通过gird添加装备
  256. function addByEquipGrid(human, equipGrid, logType, noSend)
  257. local index = getEmptyIndex(human)
  258. if not index then
  259. --邮件处理
  260. local items = {}
  261. items[1] = { equipGrid.id, equipGrid }
  262. local title = MailExcel.mail[1000].title
  263. local content = MailExcel.mail[1000].content
  264. local senderName = MailExcel.mail[1000].senderName
  265. MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
  266. return
  267. end
  268. human.db.equipBag[index] = equipGrid
  269. sendEquipChange(human, index, equipGrid, EQUIP_BAG_OP_ADD)
  270. Log.write(Log.LOGID_OSS_EQUIP, human.db._id, human.db.account,
  271. human.db.name, human.db.lv, LogDefine.DEFINE[logType] + LogDefine.TYPE["equip"], equipGrid.id, 1, equipGrid.uuid)
  272. if not noSend then
  273. human.getEquip = human.getEquip or {}
  274. human.getEquip[#human.getEquip + 1] = equipGrid
  275. end
  276. return index
  277. end
  278. -- 从背包删除装备
  279. function delEquip(human, index, logType, sendNotify)
  280. local equipGrid = human.db.equipBag[index]
  281. if not equipGrid then return end
  282. Log.write(Log.LOGID_OSS_EQUIP, human.db._id, human.db.account,
  283. human.db.name, human.db.lv, LogDefine.DEFINE[logType] + LogDefine.TYPE["equip"], equipGrid.id, -1, equipGrid.uuid)
  284. human.db.equipBag[index] = nil
  285. if not sendNotify then
  286. sendEquipChange(human, index, nil, EQUIP_BAG_OP_DEL)
  287. end
  288. end
  289. -- 发送装备改变
  290. function sendEquipChange(human, bagIndex, equipGrid, op)
  291. local msgRet = Msg.gc.GC_EQUIP_BAG_CHANGE
  292. if op == EQUIP_BAG_OP_ADD or op == EQUIP_BAG_OP_CHANGE then
  293. msgRet.itemID = equipGrid.id
  294. msgRet.itemIndex = bagIndex
  295. msgRet.itemData[0] = 1
  296. Grid.makeItem(msgRet.itemData[1], equipGrid.id, 1, nil, equipGrid, bagIndex, Grid.getOpflagAtBag(equipGrid.id))
  297. elseif op == EQUIP_BAG_OP_DEL then
  298. msgRet.itemID = 0
  299. msgRet.itemIndex = bagIndex
  300. msgRet.itemData[0] = 0
  301. else
  302. assert(nil)
  303. end
  304. Msg.send(msgRet, human.fd)
  305. end
  306. function getNewAttrByItemID(itemID, attr)
  307. local config = EquipExcel[itemID]
  308. if not config then return end
  309. for _, v in ipairs(config.randomAttr) do
  310. if attr[v[1]] then
  311. attr[v[1]] = v[2]
  312. end
  313. end
  314. end