WarOrder.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. local OrderExcel = require("excel.warOrder")
  2. local Grid = require("bag.Grid")
  3. local ItemDefine = require("bag.ItemDefine")
  4. local BagLogic = require("bag.BagLogic")
  5. ------------------------------------------------------
  6. -- 战令对应数据
  7. ------------------------------------------------------
  8. local battleOrder = 1
  9. local devilOrder = 2
  10. local clanOrder = 3
  11. local arenaOrder = 4
  12. local passBattleExp = 5
  13. local passDevilExp = 5
  14. local passClanBattleExp = 10
  15. local victoryArenaBattleExp = 2
  16. local orderExpMap = {
  17. [battleOrder] = passBattleExp,
  18. [devilOrder] = passDevilExp,
  19. [clanOrder] = passClanBattleExp,
  20. [arenaOrder] = victoryArenaBattleExp,
  21. }
  22. local orderNameMap = {
  23. [battleOrder] = "battleOrder",
  24. [devilOrder] = "devilOrder",
  25. [clanOrder] = "clanOrder",
  26. [arenaOrder] = "arenaOrder",
  27. }
  28. local GC_WARORDER_CHANGE = 13
  29. local isRed = {}
  30. ------------------------------------------------------
  31. -- local 函数
  32. ------------------------------------------------------
  33. --[[
  34. warOrder = {
  35. [battleOrder] = {
  36. unlock = number
  37. exp = number
  38. finish = {
  39. [lv] = number
  40. }
  41. upgradeFinish = {}
  42. }
  43. }
  44. ]]
  45. local function checkRewardByIdx(human,orderType,idx)
  46. local orderName = orderNameMap[orderType]
  47. local orderData = human.db.warOrder[orderName]
  48. local isUpgradeOk
  49. if orderData.unlock ~= 0 and not orderData.upgradeFinish[idx] then
  50. isUpgradeOk = true
  51. end
  52. return not orderData.finish[idx] , isUpgradeOk
  53. end
  54. local function checkRed(human,orderType)
  55. if isRed[orderType] then
  56. return 1
  57. end
  58. local orderName = orderNameMap[orderType]
  59. local cfg = OrderExcel[orderName]
  60. local orderData = human.db.warOrder[orderName]
  61. local exp = orderData.exp
  62. local maxFinishIdx = 0
  63. for lv in ipairs(orderData.finish) do
  64. if maxFinishIdx <= lv then
  65. maxFinishIdx = lv
  66. end
  67. end
  68. local maxUpgradeFinishIdx = 0
  69. for lv in ipairs(orderData.upgradeFinish) do
  70. if maxUpgradeFinishIdx <= lv then
  71. maxUpgradeFinishIdx = lv
  72. end
  73. end
  74. local maxFinishIdx = (maxFinishIdx > maxUpgradeFinishIdx) and maxUpgradeFinishIdx or maxFinishIdx
  75. local maxExpIdx = 0
  76. for _,cfg in pairs(cfg) do
  77. if cfg.process <= exp and maxExpIdx <= cfg.idx then
  78. maxExpIdx = cfg.idx
  79. end
  80. end
  81. isRed[orderType] = maxFinishIdx < maxExpIdx
  82. return isRed[orderType] and 1 or 0
  83. end
  84. local function push2Client(human,orderType)
  85. local orderName = orderNameMap[orderType]
  86. local orderData = human.db.warOrder[orderName]
  87. local finish = {}
  88. for lv in pairs(orderData.finish) do
  89. finish[#finish+1] = lv
  90. end
  91. local upgradeFinish = {}
  92. for lv in pairs(orderData.upgradeFinish) do
  93. finish[#finish+1] = lv
  94. end
  95. local data = {
  96. orderType = orderType,
  97. exp = orderData.exp,
  98. unlock = orderData.unlock,
  99. finish = finish,
  100. upgradeFinish = upgradeFinish,
  101. isRed = checkRed(human,orderType),
  102. }
  103. -- 临时require 确保两个文件不会相互引用
  104. local newLogic = require "role.NewLogic"
  105. newLogic.PushClient(human,GC_WARORDER_CHANGE,data)
  106. end
  107. ------------------------------------------------------
  108. -- 对外暴露接口
  109. ------------------------------------------------------
  110. function warOrderInfo(human,orderType)
  111. local orderName = orderNameMap[orderType]
  112. local cfg = OrderExcel[orderName]
  113. local langCfg = OrderExcel.desc
  114. local orderData = human.db.warOrder[orderName]
  115. if not cfg or not orderData then
  116. return
  117. end
  118. -- 获取配置表相关数据, 发送给客户端
  119. local normal = {}
  120. for _,v in pairs(cfg) do
  121. local cfgData = {
  122. idx = v.idx,
  123. process = v.process,
  124. }
  125. local content = {}
  126. local upgradeContent = {}
  127. if #v.content ~= 0 then
  128. for _,item in ipairs(v.content) do
  129. local data = {
  130. getway = {},
  131. suipian = {},
  132. equip = {},
  133. fuwen = {},
  134. }
  135. if not ItemDefine.isEquip(item[1]) then
  136. Grid.makeItem(data, item[1], item[2])
  137. end
  138. content[#content + 1] = data
  139. end
  140. end
  141. if #v.upgradeContent ~= 0 then
  142. for _,item in ipairs(v.upgradeContent) do
  143. local data = {
  144. getway = {},
  145. suipian = {},
  146. equip = {},
  147. fuwen = {},
  148. }
  149. if not ItemDefine.isEquip(item[1]) then
  150. Grid.makeItem(data, item[1], item[2])
  151. end
  152. upgradeContent[#upgradeContent + 1] = data
  153. end
  154. end
  155. cfgData.content = content
  156. cfgData.upgradeContent = upgradeContent
  157. normal[#normal+1] = cfgData
  158. end
  159. local language = ""
  160. for _,v in pairs(langCfg) do
  161. if v.type == orderType then
  162. language = v.desc
  163. break
  164. end
  165. end
  166. -------------------
  167. local finish = {}
  168. for lv in pairs(orderData.finish) do
  169. finish[#finish+1] = lv
  170. end
  171. local upgradeFinish = {}
  172. for lv in pairs(orderData.upgradeFinish) do
  173. finish[#finish+1] = lv
  174. end
  175. return {
  176. orderType = orderType,
  177. normal = normal,
  178. language = language,
  179. -- 自身的进度问题
  180. exp = orderData.exp,
  181. unlock = orderData.unlock,
  182. finish = finish,
  183. upgradeFinish = upgradeFinish,
  184. }
  185. end
  186. function warOrderReward(human,orderType)
  187. local orderName = orderNameMap[orderType]
  188. local cfg = OrderExcel[orderName]
  189. local orderData = human.db.warOrder[orderName]
  190. if not cfg or not orderData then
  191. return
  192. end
  193. local process = orderData.exp
  194. local itemMap = {}
  195. --local finish_copy = table.copy(orderData.finish)
  196. --local upgrade_finish_copy = table.copy(orderData.upgradeFinish)
  197. for _,v in pairs(cfg) do
  198. if process >= v.process then
  199. local isOk,isUpgradeOk = checkRewardByIdx(human,orderType,v.idx)
  200. if isOk then
  201. for _,item in ipairs(v.content) do
  202. itemMap[item[1]] = itemMap[item[1]] or 0
  203. itemMap[item[1]] = itemMap[item[1]] + item[2]
  204. end
  205. orderData.finish[v.idx] = 0
  206. end
  207. if isUpgradeOk then
  208. for _,item in ipairs(v.upgradeContent) do
  209. itemMap[item[1]] = itemMap[item[1]] or 0
  210. itemMap[item[1]] = itemMap[item[1]] + item[2]
  211. end
  212. orderData.upgradeFinish[v.idx] = 0
  213. end
  214. end
  215. end
  216. -- 检查是否有可领取的item
  217. if not next(itemMap) then
  218. return
  219. end
  220. local itemList = {}
  221. for id,count in pairs(itemMap) do
  222. itemList[#itemList+1] = {
  223. id,count
  224. }
  225. end
  226. BagLogic.addItemList(human,itemList,"war_order_get")
  227. -- 同步数据
  228. --orderData.finish = finish_copy
  229. --orderData.upgradeFinish = upgrade_finish_copy
  230. ------------------------------------------------
  231. local finish = {}
  232. for lv in pairs(orderData.finish) do
  233. finish[#finish+1] = lv
  234. end
  235. local upgradeFinish = {}
  236. for lv in pairs(orderData.upgradeFinish) do
  237. finish[#finish+1] = lv
  238. end
  239. isRed[orderType] = nil
  240. return {
  241. orderType = orderType,
  242. exp = orderData.exp,
  243. unlock = orderData.unlock,
  244. finish = finish,
  245. upgradeFinish = upgradeFinish,
  246. isRed = 0,
  247. }
  248. end
  249. -- 解锁战令
  250. function warOrderUnlock(human,orderType)
  251. local orderName = orderNameMap[orderType]
  252. local orderData = human.db.warOrder[orderName]
  253. if not orderData then
  254. print(" invalid orderType,not found data ")
  255. return
  256. end
  257. -- 已经解锁
  258. if orderData.unlock ~= 0 then
  259. print(" invalid op, warOrder unlocked ")
  260. return
  261. end
  262. orderData.unlock = 1
  263. push2Client(human,orderType)
  264. return true
  265. end
  266. -- 触发战令增加经验
  267. function trigger(human,orderType)
  268. local orderName = orderNameMap[orderType]
  269. local orderData = human.db.warOrder[orderName]
  270. local exp = orderExpMap[orderType]
  271. orderData.exp = orderData.exp + exp
  272. -- 是否需要推送?
  273. push2Client(human,orderType)
  274. end
  275. function getWarOrderInfo(human,orderType,net)
  276. local orderName = orderNameMap[orderType]
  277. local orderData = human.db.warOrder[orderName]
  278. net.type = orderType
  279. net.exp = orderData.exp
  280. net.unlock = orderData.unlock
  281. net.isRed = checkRed(human,orderType)
  282. local finishIdx = 0
  283. for lv in pairs(orderData.finish) do
  284. finishIdx = finishIdx + 1
  285. net.finish[finishIdx] = lv
  286. end
  287. net.finish[0] = finishIdx
  288. local upgradeFinishIdx = 0
  289. for lv in pairs(orderData.upgradeFinish) do
  290. upgradeFinishIdx = upgradeFinishIdx + 1
  291. net.upgradeFinish[upgradeFinishIdx] = lv
  292. end
  293. net.upgradeFinish[0] = upgradeFinishIdx
  294. end