BingshuLogic.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ------------------------------------------------------
  2. -- 兵书逻辑
  3. ------------------------------------------------------
  4. local FuwenExcel = require("excel.fuwen")
  5. local HeroExcel = require("excel.hero")
  6. local Lang = require("common.Lang")
  7. local Util = require("common.Util")
  8. local Msg = require("core.Msg")
  9. local ObjHuman = require("core.ObjHuman")
  10. local Broadcast = require("broadcast.Broadcast")
  11. local Grid = require("bag.Grid")
  12. local BagLogic = require("bag.BagLogic")
  13. local BingshuGrid = require("fuwen.BingshuGrid")
  14. local HeroLogic = require("hero.HeroLogic")
  15. local LiLianLogic = require("dailyTask.LiLianLogic")
  16. BINGSHU_MAXCNT = 3 -- 最多x个兵书
  17. BINGSHU_OPEN_LIST = {6, 11, 13}
  18. ------------------------------------- config ------------------------------------------
  19. -- 获取可以激活的技能列表
  20. local CAN_LEARN_LIST = nil
  21. function getCanLearnList()
  22. if not CAN_LEARN_LIST then
  23. CAN_LEARN_LIST = {}
  24. for id, config in Util.pairsByKeys(FuwenExcel.skill) do
  25. if config.isBingshuSkill == 1 and config.lv == 1 then
  26. CAN_LEARN_LIST[#CAN_LEARN_LIST + 1] = id
  27. end
  28. end
  29. end
  30. return CAN_LEARN_LIST
  31. end
  32. -- 获取下一级技能
  33. function getNextSkillID(skillID)
  34. local tconfig = FuwenExcel.skill[skillID]
  35. if not tconfig then return end
  36. for id, config in pairs(FuwenExcel.skill) do
  37. if (config.isBingshuSkill == 1) and
  38. (config.groupID == tconfig.groupID) and
  39. (config.lv == tconfig.lv + 1) and
  40. (#config.bingshuUpNeed > 0) then
  41. return id
  42. end
  43. end
  44. end
  45. ----------------------------------------- db -------------------------------------------------
  46. -- 获取兵书grid
  47. function getBingshuGrid(heroGrid, index)
  48. if not heroGrid then return end
  49. if not heroGrid.bingshu then return end
  50. return index and heroGrid.bingshu[index]
  51. end
  52. -- 是否已经学过某个技能
  53. function isLearnSkill(heroGrid, skillID)
  54. if not heroGrid then return end
  55. if not heroGrid.bingshu then return end
  56. local skillConfig = FuwenExcel.skill[skillID]
  57. for _, grid in pairs(heroGrid.bingshu) do
  58. local tconfig = FuwenExcel.skill[grid.skillID]
  59. if tconfig.groupID == skillConfig.groupID then
  60. return true
  61. end
  62. end
  63. end
  64. ----------------------------------------- msg -------------------------------------------------
  65. -- 查询
  66. function sendQuery(human, heroID, heroIndex)
  67. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  68. if not heroGrid then return end
  69. local msgRet = Msg.gc.GC_BINGSHU_QUERY
  70. msgRet.heroID = heroID
  71. msgRet.heroIndex = heroIndex
  72. msgRet.skills[0] = 0
  73. for i = 1, BINGSHU_MAXCNT do
  74. local grid = getBingshuGrid(heroGrid, i)
  75. if grid then
  76. msgRet.skills[0] = msgRet.skills[0] + 1
  77. BingshuGrid.makeBingshuNet(msgRet.skills[msgRet.skills[0]], grid, i)
  78. end
  79. end
  80. msgRet.openList[0] = BINGSHU_MAXCNT
  81. for i = 1, msgRet.openList[0] do
  82. msgRet.openList[i] = BINGSHU_OPEN_LIST[i] or 0
  83. end
  84. for i = 1, BINGSHU_MAXCNT do
  85. msgRet.red[i] = isBingShuDotByIndex(human, heroGrid, i) and 1 or 0
  86. end
  87. msgRet.red[0] = BINGSHU_MAXCNT
  88. -- Msg.trace(msgRet)
  89. Msg.send(msgRet, human.fd)
  90. end
  91. -- 可激活列表
  92. function sendLearnList(human, heroID, heroIndex, index)
  93. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  94. if not heroGrid then return end
  95. local learnList = getCanLearnList()
  96. local msgRet = Msg.gc.GC_BINGSHU_LEARN_LIST
  97. msgRet.list[0] = 0
  98. for _, skillID in ipairs(learnList) do
  99. if msgRet.list[0] >= #msgRet.list then
  100. break
  101. end
  102. if not isLearnSkill(heroGrid, skillID) then
  103. msgRet.list[0] = msgRet.list[0] + 1
  104. BingshuGrid.makeBingshuLearnNet(msgRet.list[msgRet.list[0]], skillID)
  105. end
  106. end
  107. msgRet.index = index
  108. -- Msg.trace(msgRet)
  109. Msg.send(msgRet, human.fd)
  110. end
  111. -- 学习技能
  112. function learnSkill(human, heroID, heroIndex, index, skillID)
  113. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  114. if not heroGrid then
  115. return Broadcast.sendErr(human, Lang.DRILL_CHOOSE_FRIEND_ERR_INFO)
  116. end
  117. if index < 1 or index > BINGSHU_MAXCNT then
  118. return Broadcast.sendErr(human, Lang.DRILL_CHOOSE_FRIEND_ERR_INFO)
  119. end
  120. local heroConfig = HeroExcel.hero[heroGrid.id]
  121. if heroGrid.star < BINGSHU_OPEN_LIST[index] then
  122. return
  123. end
  124. if getBingshuGrid(heroGrid, index) then
  125. return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
  126. end
  127. local config = FuwenExcel.skill[skillID]
  128. if not config then return end
  129. if config.isBingshuSkill ~= 1 then return end
  130. if config.lv ~= 1 then return end
  131. for _, item in ipairs(config.bingshuUpNeed) do
  132. local itemID = item[1]
  133. local itemCnt = item[2]
  134. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  135. return Broadcast.sendErr(human, Lang.ABS_ITEM_ERR)
  136. end
  137. end
  138. for _, item in ipairs(config.bingshuUpNeed) do
  139. local itemID = item[1]
  140. local itemCnt = item[2]
  141. BagLogic.delItem(human, itemID, itemCnt, "bingshu")
  142. end
  143. local grid = BingshuGrid.create(skillID)
  144. heroGrid.bingshu = heroGrid.bingshu or {}
  145. heroGrid.bingshu[index] = grid
  146. local msgRet = Msg.gc.GC_BINGSHU_LEARN
  147. msgRet.heroID = heroID
  148. msgRet.heroIndex = heroIndex
  149. BingshuGrid.makeBingshuNet(msgRet.data, grid, index)
  150. -- Msg.trace(msgRet)
  151. Msg.send(msgRet, human.fd)
  152. ObjHuman.doCalcHero(human, heroIndex)
  153. local newcf = FuwenExcel.skill[grid.skillID]
  154. LiLianLogic.onCallback(human,LiLianLogic.LILIAN_OUTID28,1,newcf.lv)
  155. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  156. sendQuery(human, heroID, heroIndex)
  157. end
  158. function getForgetCost(lv)
  159. local costs = FuwenExcel.define[1].bingshuForgetCosts
  160. return costs[lv] or costs[#costs]
  161. end
  162. -- 升级查询
  163. function sendLevelUpQuery(human, heroID, heroIndex, index)
  164. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  165. if not heroGrid then return end
  166. local grid = getBingshuGrid(heroGrid, index)
  167. if not grid then return end
  168. local config = FuwenExcel.skill[grid.skillID]
  169. local nextSkillID = getNextSkillID(grid.skillID)
  170. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  171. local msgRet = Msg.gc.GC_BINGSHU_LEVELUP_QUERY
  172. msgRet.heroID = heroID
  173. msgRet.heroIndex = heroIndex
  174. BingshuGrid.makeBingshuNet(msgRet.bingshu, grid, index)
  175. msgRet.upData[0] = 0
  176. if nextConfig then
  177. msgRet.upData[0] = 1
  178. BingshuGrid.makeBingshuLearnNet(msgRet.upData[1], nextSkillID)
  179. end
  180. msgRet.forgetCost = getForgetCost(config.lv)
  181. msgRet.returnItems[0] = config and #config.bingshuForgetReturn or 0
  182. for i = 1, msgRet.returnItems[0] do
  183. local itemID = config.bingshuForgetReturn[i][1]
  184. local itemCnt = config.bingshuForgetReturn[i][2]
  185. Grid.makeItem(msgRet.returnItems[i], itemID, itemCnt)
  186. end
  187. -- Msg.trace(msgRet)
  188. Msg.send(msgRet, human.fd)
  189. end
  190. -- 升级
  191. function levelUp(human, heroID, heroIndex, index)
  192. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  193. if not heroGrid then return end
  194. local grid = getBingshuGrid(heroGrid, index)
  195. if not grid then return end
  196. local nextSkillID = getNextSkillID(grid.skillID)
  197. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  198. if not nextConfig then
  199. return Broadcast.sendErr(human, Lang.BINGSHU_LEVELUP_ERR_FULL)
  200. end
  201. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  202. local itemID = item[1]
  203. local itemCnt = item[2]
  204. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  205. return
  206. end
  207. end
  208. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  209. local itemID = item[1]
  210. local itemCnt = item[2]
  211. BagLogic.delItem(human, itemID, itemCnt, "bingshu")
  212. end
  213. grid.skillID = nextSkillID
  214. local msgRet = Msg.gc.GC_BINGSHU_LEVELUP
  215. msgRet.heroID = heroID
  216. msgRet.heroIndex = heroIndex
  217. BingshuGrid.makeBingshuNet(msgRet.bingshu, grid, index)
  218. -- Msg.trace(msgRet)
  219. Msg.send(msgRet, human.fd)
  220. ObjHuman.doCalcHero(human, heroIndex)
  221. sendLevelUpQuery(human, heroID, heroIndex, index)
  222. sendQuery(human, heroID, heroIndex)
  223. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  224. end
  225. -- 遗忘
  226. function forget(human, heroID, heroIndex, index)
  227. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  228. if not heroGrid then return end
  229. local grid = getBingshuGrid(heroGrid, index)
  230. if not grid then return end
  231. local config = FuwenExcel.skill[grid.skillID]
  232. local needZuanshi = getForgetCost(config.lv)
  233. if not ObjHuman.checkRMB(human, needZuanshi) then
  234. return
  235. end
  236. ObjHuman.decZuanshi(human, -needZuanshi, "bingshu")
  237. heroGrid.bingshu[index] = nil
  238. BagLogic.addItemList(human, config.bingshuForgetReturn, "bingshu")
  239. local msgRet = Msg.gc.GC_BINGSHU_FORGET
  240. msgRet.heroID = heroID
  241. msgRet.heroIndex = heroIndex
  242. msgRet.index = index
  243. Msg.send(msgRet, human.fd)
  244. ObjHuman.doCalcHero(human, heroIndex)
  245. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  246. end
  247. BINGSHU_OPEN_TIME = nil
  248. function isBingShuDot(human, heroGrid)
  249. if not BINGSHU_OPEN_TIME then
  250. -- local d = os.date("*t",time)
  251. local tmpDate = {}
  252. tmpDate.year = 2022
  253. tmpDate.month = 1
  254. tmpDate.day = 31
  255. tmpDate.hour = 0
  256. tmpDate.min = 0
  257. tmpDate.sec = 0
  258. BINGSHU_OPEN_TIME = os.time(tmpDate)
  259. end
  260. if os.time() <= BINGSHU_OPEN_TIME then return -1 end
  261. if not heroGrid then return end
  262. if heroGrid.star < BINGSHU_OPEN_LIST[1] then return end
  263. for i = 1, BINGSHU_MAXCNT do
  264. if heroGrid.star < BINGSHU_OPEN_LIST[i] then return end
  265. local grid = getBingshuGrid(heroGrid, i)
  266. if grid then
  267. -- 检测是否能升级
  268. local nextSkillID = getNextSkillID(grid.skillID)
  269. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  270. if nextConfig then
  271. local canUp = true
  272. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  273. local itemID = item[1]
  274. local itemCnt = item[2]
  275. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  276. canUp = false
  277. end
  278. end
  279. if canUp then
  280. return true
  281. end
  282. end
  283. else
  284. local learnList = getCanLearnList()
  285. local canUp = true
  286. for i, skillID in ipairs(learnList) do
  287. canUp = true
  288. local config = FuwenExcel.skill[skillID]
  289. for k, v in ipairs(config.bingshuUpNeed) do
  290. local itemID = v[1]
  291. local itemCnt = v[2]
  292. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  293. canUp = false
  294. end
  295. end
  296. if canUp then
  297. return true
  298. end
  299. end
  300. end
  301. end
  302. end
  303. function isBingShuDotByIndex(human, heroGrid, index)
  304. if not BINGSHU_OPEN_TIME then
  305. -- local d = os.date("*t",time)
  306. local tmpDate = {}
  307. tmpDate.year = 2022
  308. tmpDate.month = 1
  309. tmpDate.day = 31
  310. tmpDate.hour = 0
  311. tmpDate.min = 0
  312. tmpDate.sec = 0
  313. BINGSHU_OPEN_TIME = os.time(tmpDate)
  314. end
  315. if os.time() <= BINGSHU_OPEN_TIME then return end
  316. if not heroGrid then return end
  317. if heroGrid.star < BINGSHU_OPEN_LIST[index] then return end
  318. local grid = getBingshuGrid(heroGrid, index)
  319. if grid then
  320. -- 检测是否能升级
  321. local nextSkillID = getNextSkillID(grid.skillID)
  322. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  323. if nextConfig then
  324. local canUp = true
  325. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  326. local itemID = item[1]
  327. local itemCnt = item[2]
  328. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  329. canUp = false
  330. end
  331. end
  332. if canUp then
  333. return true
  334. end
  335. end
  336. else
  337. local learnList = getCanLearnList()
  338. local canUp = true
  339. for i, skillID in ipairs(learnList) do
  340. canUp = true
  341. local config = FuwenExcel.skill[skillID]
  342. for k, v in ipairs(config.bingshuUpNeed) do
  343. local itemID = v[1]
  344. local itemCnt = v[2]
  345. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  346. canUp = false
  347. end
  348. end
  349. if canUp then
  350. return true
  351. end
  352. end
  353. end
  354. end