WarOrder.lua 9.6 KB

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