BingshuLogic.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 return end
  115. if index < 1 or index > BINGSHU_MAXCNT then
  116. return
  117. end
  118. local heroConfig = HeroExcel.hero[heroGrid.id]
  119. if heroGrid.star < BINGSHU_OPEN_LIST[index] then
  120. return
  121. end
  122. if getBingshuGrid(heroGrid, index) then
  123. return Broadcast.sendErr(human, Lang.BINGSHU_LEARN_ERR_HAD)
  124. end
  125. local config = FuwenExcel.skill[skillID]
  126. if not config then return end
  127. if config.isBingshuSkill ~= 1 then return end
  128. if config.lv ~= 1 then return end
  129. for _, item in ipairs(config.bingshuUpNeed) do
  130. local itemID = item[1]
  131. local itemCnt = item[2]
  132. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  133. return
  134. end
  135. end
  136. for _, item in ipairs(config.bingshuUpNeed) do
  137. local itemID = item[1]
  138. local itemCnt = item[2]
  139. BagLogic.delItem(human, itemID, itemCnt, "bingshu")
  140. end
  141. local grid = BingshuGrid.create(skillID)
  142. heroGrid.bingshu = heroGrid.bingshu or {}
  143. heroGrid.bingshu[index] = grid
  144. local msgRet = Msg.gc.GC_BINGSHU_LEARN
  145. msgRet.heroID = heroID
  146. msgRet.heroIndex = heroIndex
  147. BingshuGrid.makeBingshuNet(msgRet.data, grid, index)
  148. -- Msg.trace(msgRet)
  149. Msg.send(msgRet, human.fd)
  150. ObjHuman.doCalcHero(human, heroIndex)
  151. local newcf = FuwenExcel.skill[grid.skillID]
  152. LiLianLogic.onCallback(human,LiLianLogic.LILIAN_OUTID28,1,newcf.lv)
  153. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  154. sendQuery(human, heroID, heroIndex)
  155. end
  156. function getForgetCost(lv)
  157. local costs = FuwenExcel.define[1].bingshuForgetCosts
  158. return costs[lv] or costs[#costs]
  159. end
  160. -- 升级查询
  161. function sendLevelUpQuery(human, heroID, heroIndex, index)
  162. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  163. if not heroGrid then return end
  164. local grid = getBingshuGrid(heroGrid, index)
  165. if not grid then return end
  166. local config = FuwenExcel.skill[grid.skillID]
  167. local nextSkillID = getNextSkillID(grid.skillID)
  168. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  169. local msgRet = Msg.gc.GC_BINGSHU_LEVELUP_QUERY
  170. msgRet.heroID = heroID
  171. msgRet.heroIndex = heroIndex
  172. BingshuGrid.makeBingshuNet(msgRet.bingshu, grid, index)
  173. msgRet.upData[0] = 0
  174. if nextConfig then
  175. msgRet.upData[0] = 1
  176. BingshuGrid.makeBingshuLearnNet(msgRet.upData[1], nextSkillID)
  177. end
  178. msgRet.forgetCost = getForgetCost(config.lv)
  179. msgRet.returnItems[0] = config and #config.bingshuForgetReturn or 0
  180. for i = 1, msgRet.returnItems[0] do
  181. local itemID = config.bingshuForgetReturn[i][1]
  182. local itemCnt = config.bingshuForgetReturn[i][2]
  183. Grid.makeItem(msgRet.returnItems[i], itemID, itemCnt)
  184. end
  185. -- Msg.trace(msgRet)
  186. Msg.send(msgRet, human.fd)
  187. end
  188. -- 升级
  189. function levelUp(human, heroID, heroIndex, index)
  190. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  191. if not heroGrid then return end
  192. local grid = getBingshuGrid(heroGrid, index)
  193. if not grid then return end
  194. local nextSkillID = getNextSkillID(grid.skillID)
  195. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  196. if not nextConfig then
  197. return Broadcast.sendErr(human, Lang.BINGSHU_LEVELUP_ERR_FULL)
  198. end
  199. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  200. local itemID = item[1]
  201. local itemCnt = item[2]
  202. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  203. return
  204. end
  205. end
  206. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  207. local itemID = item[1]
  208. local itemCnt = item[2]
  209. BagLogic.delItem(human, itemID, itemCnt, "bingshu")
  210. end
  211. grid.skillID = nextSkillID
  212. local msgRet = Msg.gc.GC_BINGSHU_LEVELUP
  213. msgRet.heroID = heroID
  214. msgRet.heroIndex = heroIndex
  215. BingshuGrid.makeBingshuNet(msgRet.bingshu, grid, index)
  216. -- Msg.trace(msgRet)
  217. Msg.send(msgRet, human.fd)
  218. ObjHuman.doCalcHero(human, heroIndex)
  219. sendLevelUpQuery(human, heroID, heroIndex, index)
  220. sendQuery(human, heroID, heroIndex)
  221. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  222. end
  223. -- 遗忘
  224. function forget(human, heroID, heroIndex, index)
  225. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  226. if not heroGrid then return end
  227. local grid = getBingshuGrid(heroGrid, index)
  228. if not grid then return end
  229. local config = FuwenExcel.skill[grid.skillID]
  230. local needZuanshi = getForgetCost(config.lv)
  231. if not ObjHuman.checkRMB(human, needZuanshi) then
  232. return
  233. end
  234. ObjHuman.decZuanshi(human, -needZuanshi, "bingshu")
  235. heroGrid.bingshu[index] = nil
  236. BagLogic.addItemList(human, config.bingshuForgetReturn, "bingshu")
  237. local msgRet = Msg.gc.GC_BINGSHU_FORGET
  238. msgRet.heroID = heroID
  239. msgRet.heroIndex = heroIndex
  240. msgRet.index = index
  241. Msg.send(msgRet, human.fd)
  242. ObjHuman.doCalcHero(human, heroIndex)
  243. HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
  244. end
  245. BINGSHU_OPEN_TIME = nil
  246. function isBingShuDot(human, heroGrid)
  247. if not BINGSHU_OPEN_TIME then
  248. -- local d = os.date("*t",time)
  249. local tmpDate = {}
  250. tmpDate.year = 2022
  251. tmpDate.month = 1
  252. tmpDate.day = 31
  253. tmpDate.hour = 0
  254. tmpDate.min = 0
  255. tmpDate.sec = 0
  256. BINGSHU_OPEN_TIME = os.time(tmpDate)
  257. end
  258. if os.time() <= BINGSHU_OPEN_TIME then return -1 end
  259. if not heroGrid then return end
  260. if heroGrid.star < BINGSHU_OPEN_LIST[1] then return end
  261. for i = 1, BINGSHU_MAXCNT do
  262. if heroGrid.star < BINGSHU_OPEN_LIST[i] then return end
  263. local grid = getBingshuGrid(heroGrid, i)
  264. if grid then
  265. -- 检测是否能升级
  266. local nextSkillID = getNextSkillID(grid.skillID)
  267. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  268. if nextConfig then
  269. local canUp = true
  270. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  271. local itemID = item[1]
  272. local itemCnt = item[2]
  273. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  274. canUp = false
  275. end
  276. end
  277. if canUp then
  278. return true
  279. end
  280. end
  281. else
  282. local learnList = getCanLearnList()
  283. local canUp = true
  284. for i, skillID in ipairs(learnList) do
  285. canUp = true
  286. local config = FuwenExcel.skill[skillID]
  287. for k, v in ipairs(config.bingshuUpNeed) do
  288. local itemID = v[1]
  289. local itemCnt = v[2]
  290. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  291. canUp = false
  292. end
  293. end
  294. if canUp then
  295. return true
  296. end
  297. end
  298. end
  299. end
  300. end
  301. function isBingShuDotByIndex(human, heroGrid, index)
  302. if not BINGSHU_OPEN_TIME then
  303. -- local d = os.date("*t",time)
  304. local tmpDate = {}
  305. tmpDate.year = 2022
  306. tmpDate.month = 1
  307. tmpDate.day = 31
  308. tmpDate.hour = 0
  309. tmpDate.min = 0
  310. tmpDate.sec = 0
  311. BINGSHU_OPEN_TIME = os.time(tmpDate)
  312. end
  313. if os.time() <= BINGSHU_OPEN_TIME then return end
  314. if not heroGrid then return end
  315. if heroGrid.star < BINGSHU_OPEN_LIST[index] then return end
  316. local grid = getBingshuGrid(heroGrid, index)
  317. if grid then
  318. -- 检测是否能升级
  319. local nextSkillID = getNextSkillID(grid.skillID)
  320. local nextConfig = nextSkillID and FuwenExcel.skill[nextSkillID]
  321. if nextConfig then
  322. local canUp = true
  323. for _, item in ipairs(nextConfig.bingshuUpNeed) do
  324. local itemID = item[1]
  325. local itemCnt = item[2]
  326. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  327. canUp = false
  328. end
  329. end
  330. if canUp then
  331. return true
  332. end
  333. end
  334. else
  335. local learnList = getCanLearnList()
  336. local canUp = true
  337. for i, skillID in ipairs(learnList) do
  338. canUp = true
  339. local config = FuwenExcel.skill[skillID]
  340. for k, v in ipairs(config.bingshuUpNeed) do
  341. local itemID = v[1]
  342. local itemCnt = v[2]
  343. if BagLogic.getItemCnt(human, itemID) < itemCnt then
  344. canUp = false
  345. end
  346. end
  347. if canUp then
  348. return true
  349. end
  350. end
  351. end
  352. end