RecommendLineup.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. --------------------------------
  2. -- 文件名 : RecommendLineup.lua
  3. -- 文件说明 : 推荐阵容
  4. -- 创建时间 : 2025/1/15
  5. -- 创建人 : FC
  6. --------------------------------
  7. local HeroLogic = require("hero.HeroLogic")
  8. local BagLogic = require("bag.BagLogic")
  9. local HeroGrid = require("hero.HeroGrid")
  10. local CommonDB = require("common.CommonDB")
  11. local Msg = require("core.Msg")
  12. local CombatPosLogic = require("combat.CombatPosLogic")
  13. local CombatDefine = require("combat.CombatDefine")
  14. local HeroExcel = require("excel.hero")
  15. local nRecommendLineupLen = 30 -- 阵容记录推荐数量
  16. local tRecommendLineupData = nil
  17. local nSendLen = 15 -- 一次性发送数据长度
  18. ---------------------------------------- 内部函数 ------------------------------------
  19. local function RecommendLineup_GetData()
  20. if not tRecommendLineupData then
  21. tRecommendLineupData = CommonDB.getRecommendLineUp()
  22. table.sort(tRecommendLineupData, function (l, r)
  23. return l.nPower > r.nPower
  24. end
  25. )
  26. end
  27. return tRecommendLineupData
  28. end
  29. local function RecommendLineup_SetData(tData)
  30. table.sort(tData, function (l, r)
  31. return l.nPower > r.nPower
  32. end
  33. )
  34. tRecommendLineupData = tData
  35. CommonDB.SetRecommendLineUp(tRecommendLineupData)
  36. end
  37. -- 获取小于该战力的下表
  38. local function RecommendLineup_GetIndex(nNewPower, human)
  39. local nIndex = -1
  40. local tDBData = RecommendLineup_GetData()
  41. local nKey = human.db._id
  42. if nil == _G.next(tDBData) then
  43. nIndex = 1
  44. return nIndex
  45. end
  46. local nNowLen = #tDBData
  47. if nNowLen < nRecommendLineupLen then
  48. for i, value in ipairs(tDBData) do
  49. -- 更新阵容
  50. if value.key and value.key == nKey then
  51. -- 大于等于才更新
  52. if nNewPower >= value.nPower then
  53. print("[RecommendLineup_GetIndex] 已存在 nIndex = "..i)
  54. nIndex = i
  55. end
  56. -- 直接返回
  57. return nIndex
  58. end
  59. end
  60. if -1 == nIndex then
  61. nIndex = nNowLen + 1
  62. return nIndex
  63. end
  64. end
  65. for i, value in ipairs(tDBData) do
  66. local nPower = value.nPower
  67. if nNewPower > nPower then
  68. nIndex = i
  69. break
  70. end
  71. end
  72. return nIndex
  73. end
  74. -- 插入最新的表
  75. local function RecommendLineup_UpDateData(human, nIndex, tHeroData)
  76. local tOldData = RecommendLineup_GetData()
  77. local nOldLen = #tOldData
  78. local tNewData = {}
  79. if nil == _G.next(tOldData) then
  80. table.insert(tNewData, tHeroData)
  81. RecommendLineup_SetData(tNewData)
  82. return
  83. end
  84. -- 已经存在
  85. if nIndex <= nOldLen then
  86. tNewData = tOldData
  87. tNewData[nIndex] = tHeroData
  88. RecommendLineup_SetData(tNewData)
  89. return
  90. end
  91. -- 长度还不够
  92. if nOldLen < nRecommendLineupLen then
  93. tNewData = tOldData
  94. table.insert(tNewData, tHeroData)
  95. RecommendLineup_SetData(tNewData)
  96. return
  97. end
  98. -- 只有30个直接遍历
  99. local nLen = 0
  100. local tLastData = nil
  101. for i, value in ipairs(tOldData) do
  102. if nLen >= nRecommendLineupLen then
  103. break
  104. end
  105. if i == nIndex then
  106. table.insert(tNewData, tHeroData)
  107. tLastData = value
  108. elseif i < nIndex then
  109. table.insert(tNewData, value)
  110. elseif i > nIndex then
  111. table.insert(tNewData, tLastData)
  112. tLastData = value
  113. end
  114. nLen = nLen + 1
  115. end
  116. RecommendLineup_SetData(tNewData)
  117. end
  118. -- 封装信息
  119. local function RecommendLineup_MakeSimpleData(net, tSaveData)
  120. local cf = HeroExcel.hero[tSaveData.id]
  121. if not cf then
  122. print("[RecommendLineup_MakeSimpleData] 不存在对应英雄配置 nHeroID = "..tSaveData.id)
  123. return
  124. end
  125. net.id = tSaveData.id
  126. net.index = tSaveData.index or 0
  127. net.uuid = tSaveData.uuid or ""
  128. net.gl = tSaveData.gl or 0
  129. net.camp = tSaveData.camp or 0
  130. net.job = tSaveData.job or 0
  131. net.star = tSaveData.star or 0 --cf and cf.star or 0 星级调整 dxzeng
  132. net.icon = tSaveData.head or (cf.head or 0)
  133. net.body = tSaveData.body or 0
  134. net.up = tSaveData.up or 0 --是否能够升星
  135. net.lv = tSaveData.lv or 1
  136. net.xLv = tSaveData.xLv or 0
  137. net.zhandouli = tSaveData.zhandouli or 0
  138. net.quality = tSaveData.quality or 0
  139. net.isLock = tSaveData.isLock and 1 or 0
  140. net.hp = tSaveData.hp or 0
  141. net.hpMax = tSaveData.hpMax or 0
  142. net.cnt = tSaveData.cnt or 1
  143. net.isget = tSaveData.isget or 0
  144. net.weightLv = tSaveData.grade or 0
  145. net.name = tSaveData.name or ""
  146. net.grade = tSaveData.grade or 0
  147. net.jobDesc = tSaveData.desc or ""
  148. net.label = tSaveData.label or ""
  149. net.order = tSaveData.order or 0
  150. net.isGongMing = tSaveData.isGongMing or 0
  151. --是否发送图鉴信息
  152. net.general = tSaveData.general
  153. --宝石
  154. net.gemData = tSaveData.gemData
  155. end
  156. ---------------------------------------- 客户端请求 ------------------------------------
  157. function GetRecommendLineUp(human)
  158. local tData = RecommendLineup_GetData()
  159. local tMsgData = Msg.gc.GC_DRAWCARD_GET_RECOMMEND_LINEUP
  160. tMsgData.list[0] = 0
  161. tMsgData.bIsEnd = 0
  162. for i, value in ipairs(tData) do
  163. tMsgData.list[0] = tMsgData.list[0] + 1
  164. local tHeroData = tMsgData.list[tMsgData.list[0]]
  165. tHeroData.data[0] = 0
  166. tHeroData.nPower = value.nPower
  167. print("[GetRecommendLineUp] nPower = "..value.nPower.." i = "..i)
  168. for _, v in pairs(value.data) do
  169. tHeroData.data[0] = tHeroData.data[0] + 1
  170. RecommendLineup_MakeSimpleData(tHeroData.data[tHeroData.data[0]], v)
  171. end
  172. if i >= nSendLen then
  173. Msg.send(tMsgData, human.fd)
  174. tMsgData.list[0] = 0
  175. end
  176. end
  177. tMsgData.bIsEnd = 1
  178. Msg.send(tMsgData, human.fd)
  179. print("[GetRecommendLineUp] 发送成功 ")
  180. end
  181. function RecommendLineup_UpDate(human)
  182. if 5 >= human.db.lv then
  183. return
  184. end
  185. local combatHero,helpList = CombatPosLogic.getCombatHeros(human, CombatDefine.COMBAT_TYPE1)
  186. if not combatHero then
  187. return
  188. end
  189. local nNewPower = CombatPosLogic.getCombatHeroZDL(human, CombatDefine.COMBAT_TYPE1, true)
  190. local nIndex = RecommendLineup_GetIndex(nNewPower, human)
  191. if -1 >= nIndex then
  192. return
  193. end
  194. print("[RecommendLineup_UpDate] 获取到的下表 nIndex = "..nIndex)
  195. local tHeroData = {
  196. key = human.db._id,
  197. nPower = nNewPower,
  198. data = {},
  199. }
  200. local nLen = 1
  201. for _, v in pairs(combatHero) do
  202. local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
  203. if heroGrid then
  204. tHeroData.data[nLen] = {
  205. general = {},
  206. gemData = {},
  207. relic = {},
  208. }
  209. HeroGrid.makeHeroSimple(tHeroData.data[nLen], heroGrid, heroGrid.bagIndex, human)
  210. nLen = nLen + 1
  211. end
  212. end
  213. for _,v in pairs(helpList) do
  214. local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
  215. if heroGrid then
  216. tHeroData.data[nLen] = {
  217. general = {},
  218. gemData = {},
  219. }
  220. HeroGrid.makeHeroSimple(tHeroData.data[nLen], heroGrid, heroGrid.bagIndex, human)
  221. nLen = nLen + 1
  222. end
  223. end
  224. RecommendLineup_UpDateData(human, nIndex, tHeroData)
  225. end
  226. function RecommendLineup_GMClear()
  227. local tData = {}
  228. tRecommendLineupData = tData
  229. CommonDB.SetRecommendLineUp(tRecommendLineupData)
  230. print("[RecommendLineup_GMClear] 重置成功")
  231. end