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