ValleyMiddle.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. --------------------------------------------------------------
  2. -- 荣耀峡谷(龙族战场)跨服逻辑
  3. --------------------------------------------------------------
  4. local LuaMongo = _G.lua_mongo
  5. local DB = require("common.DB")
  6. local Lang = require("common.Lang")
  7. local Util = require("common.Util")
  8. local InnerMsg = require("core.InnerMsg")
  9. local CombatDefine = require("combat.CombatDefine")
  10. local MiddleLogic = require("middle.MiddleLogic")
  11. local MiddleManager = require("middle.MiddleManager")
  12. local RoleLogic = require("role.RoleLogic")
  13. local ValleyLogic = require("valley.ValleyLogic")
  14. local QueryByUuid = {_id = nil}
  15. local FieldsRoleBase = {rolebase = 1}
  16. local FilldsCombatLog = {attacker = 1, defender = 1, isWin = 1, time = 1, tili1 = 1, tili2 = 1}
  17. local DBUpdateField = {}
  18. local MAX_BOARD_LEN = 50
  19. -- 获取活动服基础信息
  20. function getMainInfo(svrIndex)
  21. if not svrIndex then return end
  22. local commonDB = ValleyLogic.getCommonDB()
  23. if not commonDB.mainInfos then
  24. return
  25. end
  26. return commonDB.mainInfos[svrIndex]
  27. end
  28. -- 保存活动服基础信息
  29. function saveMainInfo(svrIndex, data)
  30. local commonDB = ValleyLogic.getCommonDB()
  31. commonDB.mainInfos = commonDB.mainInfos or {}
  32. commonDB.mainInfos[svrIndex] = Util.copyTable(data)
  33. ValleyLogic.saveCommonDB(commonDB)
  34. end
  35. -- 获取参赛者信息
  36. function getPlayerData(uuid, fields)
  37. if not uuid then return end
  38. QueryByUuid._id = uuid
  39. LuaMongo.find(DB.db_valley, QueryByUuid, fields)
  40. local playerData = {}
  41. return LuaMongo.next(playerData) and playerData
  42. end
  43. -- 保存/更新参赛者信息
  44. function updatePlayerData(playerData)
  45. local oldData = getPlayerData(playerData._id)
  46. if not oldData then
  47. -- 首次收到,插入数据
  48. LuaMongo.insert(DB.db_jjcLadder, playerData)
  49. return true
  50. else
  51. -- 非首次,更新数据
  52. QueryByUuid._id = playerData._id
  53. LuaMongo.update(DB.db_jjcLadder, QueryByUuid, playerData)
  54. end
  55. end
  56. -- 刷新部分数据
  57. function upsetPlayerData(playerData)
  58. if not playerData._id then return end
  59. QueryByUuid._id = playerData._id
  60. DBUpdateField["$set"] = playerData
  61. LuaMongo.update(DB.db_jjcLadder, QueryByUuid, DBUpdateField)
  62. end
  63. -- 获取鼓舞选择的选项
  64. function getInspireIndex(mainInfo, uuid)
  65. if not mainInfo then return end
  66. if not mainInfo.inspireList then return end
  67. return mainInfo.inspireList[uuid]
  68. end
  69. -- 设置鼓舞选择的选项
  70. function setInspireIndex(mainInfo, uuid, index)
  71. if not mainInfo then return end
  72. mainInfo.inspireList = mainInfo.inspireList or {}
  73. mainInfo.inspireList[uuid] = index
  74. mainInfo.inspireCnt = (mainInfo.inspireCnt or 0) + 1
  75. ValleyLogic.saveCommonDB(ValleyLogic.getCommonDB())
  76. end
  77. -- 是否有日志红点
  78. function hasLogRed(mainInfo, uuid)
  79. if not mainInfo then return end
  80. if not mainInfo.logs or #mainInfo.logs < 1 then
  81. return
  82. end
  83. return (not mainInfo.logReads[uuid])
  84. end
  85. -- 记录已读
  86. function setLogRead(mainInfo, uuid)
  87. if not mainInfo then return end
  88. if mainInfo.logReads[uuid] then
  89. return
  90. end
  91. mainInfo.logReads[uuid] = true
  92. return true
  93. end
  94. -- 获取战斗记录
  95. function getCombatInfo(videoUuid, field)
  96. if not videoUuid then return end
  97. QueryByUuid._id = videoUuid
  98. LuaMongo.find(DB.db_valley_record, QueryByUuid, field)
  99. local combatInfo = {}
  100. return LuaMongo.next(combatInfo) and combatInfo
  101. end
  102. -- 添加记录
  103. function addLog(combatInfo, mainInfo1, mainInfo2)
  104. LuaMongo.insert(DB.db_valley_record, combatInfo)
  105. end
  106. -- 处理日志
  107. function solveLog(camp, mainInfo, combatInfo, playerData)
  108. mainInfo.logReads = {}
  109. table.insert(mainInfo.logs, 1, combatInfo._id)
  110. local isWin = combatInfo.isWin
  111. if camp == CombatDefine.ATTACK_SIDE then
  112. isWin = not combatInfo.isWin
  113. end
  114. if isWin then
  115. mainInfo.fightWinCnt = mainInfo.fightWinCnt + 1
  116. end
  117. if isWin ~= true or playerData.rolebase.tili < 1 then
  118. mainInfo.fightIndexChange = mainInfo.fightIndexChange or {}
  119. mainInfo.fightIndexChange[combatInfo.roadIndex] = true
  120. end
  121. mainInfo.fightResult = mainInfo.fightResult or {}
  122. mainInfo.fightResult[combatInfo.roadIndex] = isWin
  123. mainInfo.fightVideo = mainInfo.fightVideo or {}
  124. mainInfo.fightVideo[combatInfo.roadIndex] = combatInfo._id
  125. end
  126. -- 获取已行军时间
  127. function getMoveTime()
  128. local _, roundState, roundStateEndTime = ValleyLogic.getRound()
  129. if roundState ~= ValleyLogic.STATE_FIGHT_MOVE then
  130. return 0
  131. end
  132. return ValleyLogic.roundMoveTime - (roundStateEndTime - os.time())
  133. end
  134. -- 获取x路第y个移动中角色的uuid
  135. function getMovePlayerUuid(mainInfo, roadIndex, index)
  136. if not mainInfo then return end
  137. local roadList = mainInfo.roadList[roadIndex]
  138. if not roadList then return end
  139. local fightIndexStart = mainInfo.fightIndexList[roadIndex] or 1
  140. local fightIndex = fightIndexStart + index - 1
  141. local uuid = roadList[fightIndex]
  142. return uuid
  143. end
  144. -- 目标点
  145. function getMoveToPos(index)
  146. return ValleyLogic.ROAD_POINT_CNT - index + 1
  147. end
  148. -- 获取移动中的对象结构体
  149. function getMovePlayer(mainInfo, camp, roadIndex, index)
  150. local uuid = getMovePlayerUuid(mainInfo, roadIndex, index)
  151. local playerData = getPlayerData(uuid, FieldsRoleBase)
  152. if not playerData then return end
  153. local movePlayer = {}
  154. movePlayer.camp = camp
  155. movePlayer.roadIndex = roadIndex
  156. movePlayer.startPos = mainInfo.moveList[uuid] or 0
  157. movePlayer.moveToPos = getMoveToPos(index)
  158. movePlayer.moveTime = getMoveTime()
  159. movePlayer.moveTimeMax = ValleyLogic.roundMoveTime
  160. movePlayer.name = playerData.rolebase.name
  161. movePlayer.body = playerData.rolebase.body
  162. return movePlayer
  163. end
  164. -- 是否所有战斗都结束了
  165. function isAllFightEnd(mainInfo)
  166. if not mainInfo then return true end
  167. for roadIndex = 1, ValleyLogic.ROAD_MAX_CNT do
  168. if getMovePlayerUuid(mainInfo, roadIndex, 1) then
  169. return
  170. end
  171. end
  172. return true
  173. end
  174. -- 复制发送到logic的mainInfo
  175. function copyWLMainInfo(mainInfo)
  176. if not mainInfo then return end
  177. local tMainInfo = {}
  178. tMainInfo.svrIndex = mainInfo.svrIndex
  179. tMainInfo.svrName = mainInfo.svrName
  180. tMainInfo.inspireCnt = mainInfo.inspireCnt
  181. tMainInfo.fightIndexList = mainInfo.fightIndexList
  182. tMainInfo.roadList = mainInfo.roadList
  183. return tMainInfo
  184. end
  185. -------------------------------------- middle -------------------------------------------
  186. -- 接收活动服基础信息
  187. function LW_VALLEY_MAIN_INFO(fd, data)
  188. local svrIndex = MiddleManager.FD_2_SVRINDEX[fd]
  189. if not svrIndex then return end
  190. saveMainInfo(svrIndex, data)
  191. end
  192. -- 接收玩家信息
  193. function LW_VALLEY_ROAD_PLAYER(fd, playerData)
  194. local svrIndex = MiddleManager.FD_2_SVRINDEX[fd]
  195. if not svrIndex then return end
  196. local data = getMainInfo(svrIndex)
  197. if not data then return end
  198. local roadIndex = playerData.roadIndex
  199. local isFirst = updatePlayerData(playerData)
  200. if isFirst then
  201. data.roadList = data.roadList or {}
  202. data.roadList[roadIndex] = data.roadList[roadIndex] or {}
  203. local len = #data.roadList[roadIndex]
  204. data.roadList[roadIndex][len + 1] = playerData._id
  205. saveMainInfo(svrIndex, data)
  206. end
  207. end
  208. -- 查询主界面信息
  209. function LW_VALLEY_QUERY(fd, uuid)
  210. local svrIndex1 = MiddleManager.FD_2_SVRINDEX[fd]
  211. if not svrIndex1 then return end
  212. local state, leftTime = ValleyLogic.getState()
  213. local msgInner = InnerMsg.wl.WL_VALLEY_QUERY
  214. msgInner.uuid = uuid
  215. local data = {}
  216. if state == ValleyLogic.STATE_ACT_FIGHT then
  217. local mainInfo1 = getMainInfo(svrIndex1)
  218. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  219. local mainInfo2 = getMainInfo(svrIndex2)
  220. local round, roundState, roundStateEndTime = ValleyLogic.getRound()
  221. if isAllFightEnd(mainInfo1) or isAllFightEnd(mainInfo2) then -- 战斗提前结束
  222. roundState = STATE_FIGHT_END
  223. end
  224. data.mainInfo1 = copyWLMainInfo(mainInfo1)
  225. data.mainInfo2 = copyWLMainInfo(mainInfo2)
  226. data.round = round
  227. data.roundState = roundState
  228. data.roundStateEndTime = roundStateEndTime
  229. data.logRed = hasLogRed(mainInfo1, uuid)
  230. -- 移动中的对象
  231. data.players = {}
  232. for roadIndex = 1, ValleyLogic.ROAD_MAX_CNT do
  233. for i = 1, ValleyLogic.ROAD_POINT_CNT do
  234. local movePlayer1 = getMovePlayer(mainInfo1, CombatDefine.ATTACK_SIDE, roadIndex, index)
  235. data.players[#data.players + 1] = movePlayer1
  236. local movePlayer2 = getMovePlayer(mainInfo2, CombatDefine.DEFEND_SIDE, roadIndex, index)
  237. data.players[#data.players + 1] = movePlayer2
  238. end
  239. end
  240. else
  241. data.state = state
  242. data.leftTime = leftTime
  243. end
  244. msgInner.data = data
  245. InnerMsg.sendMsg(fd, msgInner)
  246. end
  247. function fontRoadPlayer(uuid)
  248. local playerData = getPlayerData(uuid)
  249. if not playerData then return end
  250. local net = {}
  251. net.uuid = uuid
  252. net.name = playerData.rolebase.name
  253. net.tili = playerData.rolebase.tili
  254. net.zhandouli = playerData.rolebase.zhandouli
  255. net.heroList = {[0] = 0}
  256. for index, obj in pairs(playerData.objList) do
  257. net.heroList[0] = net.heroList[0] + 1
  258. local heroNet = net.heroList[net.heroList[0]]
  259. HeroGrid.makeHeroSimple(heroNet, obj, index)
  260. end
  261. return net
  262. end
  263. -- 营地查看
  264. function LW_VALLEY_ROAD_QUERY(fd, uuid, camp, roadIndex)
  265. local svrIndex1 = MiddleManager.FD_2_SVRINDEX[fd]
  266. if not svrIndex1 then return end
  267. local mainInfo1 = getMainInfo(svrIndex1)
  268. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  269. local mainInfo2 = getMainInfo(svrIndex2)
  270. local roadList = nil
  271. if camp == CombatDefine.ATTACK_SIDE then
  272. roadList = mainInfo1 and mainInfo1.roadList[roadIndex]
  273. else
  274. roadList = mainInfo2 and mainInfo2.roadList[roadIndex]
  275. end
  276. local roadListLen = roadList and #roadList or 0
  277. local msgInner = InnerMsg.wl.WL_VALLEY_ROAD_QUERY
  278. msgInner.uuid = uuid
  279. msgInner.camp = camp
  280. msgInner.roadIndex = roadIndex
  281. msgInner.playerList = {}
  282. for i = 1, roadListLen do
  283. local tuuid = roadList[i]
  284. msgInner.playerList[#msgInner.playerList + 1] = fontRoadPlayer(tuuid)
  285. end
  286. msgInner.myData = fontRoadPlayer(uuid) or {}
  287. InnerMsg.sendMsg(fd, msgInner)
  288. end
  289. -- 封装鼓舞信息
  290. function fontInspireData(dataNet, round, mainInfo1, mainInfo2, uuid)
  291. dataNet.round = round
  292. dataNet.mainInfo1 = copyWLMainInfo(mainInfo1)
  293. dataNet.mainInfo2 = copyWLMainInfo(mainInfo2)
  294. dataNet.selectIndex = getInspireIndex(mainInfo1, uuid)
  295. end
  296. -- 鼓舞查询
  297. function LW_VALLEY_INSPIRE_QUERY(fd, uuid)
  298. local state = ValleyLogic.getState()
  299. if state ~= ValleyLogic.STATE_ACT_FIGHT then
  300. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_INSPIRE_ERR_TIME)
  301. end
  302. local round, roundState = ValleyLogic.getRound()
  303. if roundState ~= ValleyLogic.STATE_FIGHT_READY then
  304. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_INSPIRE_ERR_TIME)
  305. end
  306. local mainInfo1 = getMainInfo(svrIndex1)
  307. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  308. local mainInfo2 = getMainInfo(svrIndex2)
  309. -- 战斗提前结束
  310. if isAllFightEnd(mainInfo1) or isAllFightEnd(mainInfo2) then
  311. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_ERR_FINISH)
  312. end
  313. local msgInner = InnerMsg.wl.WL_VALLEY_INSPIRE_QUERY
  314. msgInner.uuid = uuid
  315. msgInner.data = msgInner.data or {}
  316. Util.cleanTable(msgInner.data)
  317. fontInspireData(msgInner.data, round, mainInfo1, mainInfo2, uuid)
  318. InnerMsg.sendMsg(fd, msgInner)
  319. end
  320. -- 鼓舞
  321. function LW_VALLEY_INSPIRE(fd, uuid, selectIndex)
  322. local state = ValleyLogic.getState()
  323. if state ~= ValleyLogic.STATE_ACT_FIGHT then
  324. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_INSPIRE_ERR_TIME)
  325. end
  326. local round, roundState = ValleyLogic.getRound()
  327. if roundState ~= ValleyLogic.STATE_FIGHT_READY then
  328. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_INSPIRE_ERR_TIME)
  329. end
  330. local mainInfo1 = getMainInfo(svrIndex1)
  331. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  332. local mainInfo2 = getMainInfo(svrIndex2)
  333. -- 战斗提前结束
  334. if isAllFightEnd(mainInfo1) or isAllFightEnd(mainInfo2) then
  335. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_ERR_FINISH)
  336. end
  337. -- 是否进行过鼓舞
  338. if getInspireIndex(mainInfo1, uuid) then
  339. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_INSPIRE_ERR_HAD)
  340. end
  341. setInspireIndex(mainInfo1, uuid, selectIndex)
  342. LW_VALLEY_INSPIRE_QUERY(fd, uuid)
  343. end
  344. -- 封装观战界面-玩家信息
  345. function fontFightPlayerNet(playerData1, playerData2, isWin)
  346. if not playerData1 or not playerData2 then return end
  347. local fightPlayer = {}
  348. fightPlayer.roleBase = {}
  349. RoleLogic.makeRoleBase(playerData1.rolebase, fightPlayer.roleBase)
  350. local inspireCnt1 = playerData1.rolebase.inspireCnt
  351. local inspireCnt2 = playerData2.rolebase.inspireCnt
  352. fightPlayer.attrsUp = getAttrsUp(inspireCnt1, inspireCnt2, CombatDefine.ATTACK_SIDE)
  353. fightPlayer.attrsUp = fightPlayer.attrsUp or {}
  354. fightPlayer.attrsUp[0] = #fightPlayer.attrsUp
  355. fightPlayer.tili = playerData1.rolebase.tili
  356. fightPlayer.isWin = isWin and 1 or 0
  357. fightPlayer.heroList = {[0] = 0}
  358. for index, obj in pairs(playerData1.objList) do
  359. fightPlayer.heroList[0] = fightPlayer.heroList[0] + 1
  360. local heroNet = fightPlayer.heroList[fightPlayer.heroList[0]]
  361. HeroGrid.makeHeroSimple(heroNet, obj, index)
  362. end
  363. return fightPlayer
  364. end
  365. -- 封装双方基础信息
  366. function fontFightBaseNet(mainInfo, roadIndex)
  367. if not mainInfo then return end
  368. local fightBase = {}
  369. local fightIndex = mainInfo.fightIndexList[roadIndex] or 1
  370. fightBase.svrName = mainInfo.svrName
  371. fightBase.maxCnt = #mainInfo.roadList
  372. fightBase.leftCnt = fightBase.maxCnt - fightIndex + 1
  373. return fightBase
  374. end
  375. -- 观战界面
  376. function LW_VALLEY_FIGHT_QUERY(fd, uuid, roadIndex)
  377. local state = ValleyLogic.getState()
  378. if state ~= ValleyLogic.STATE_ACT_FIGHT then
  379. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_FIGHT_ERR_TIME)
  380. end
  381. local round, roundState = ValleyLogic.getRound()
  382. if roundState ~= ValleyLogic.STATE_FIGHT_FIGHT then
  383. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_FIGHT_ERR_TIME)
  384. end
  385. local mainInfo1 = getMainInfo(svrIndex1)
  386. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  387. local mainInfo2 = getMainInfo(svrIndex2)
  388. -- 战斗提前结束
  389. if isAllFightEnd(mainInfo1) or isAllFightEnd(mainInfo2) then
  390. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.VALLEY_ERR_FINISH)
  391. end
  392. local msgInner = InnerMsg.wl.WL_VALLEY_FIGHT_QUERY
  393. msgInner.uuid = uuid
  394. msgInner.data = msgInner.data or {}
  395. Util.cleanTable(msgInner.data)
  396. fontInspireData(msgInner.data, round, mainInfo1, mainInfo2, uuid)
  397. local uuid1 = getMovePlayerUuid(mainInfo1, roadIndex, 1)
  398. local uuid2 = getMovePlayerUuid(mainInfo2, roadIndex, 1)
  399. local isWin1 = mainInfo1.fightResult and mainInfo1.fightResult[roadIndex]
  400. local isWin2 = mainInfo2.fightResult and mainInfo2.fightResult[roadIndex]
  401. local playerData1 = getPlayerData(uuid1)
  402. local playerData2 = getPlayerData(uuid2)
  403. local fightPlayer1 = fontFightPlayerNet(playerData1, playerData2, isWin1)
  404. local fightPlayer2 = fontFightPlayerNet(playerData2, playerData1, isWin2)
  405. if fightPlayer1 and fightPlayer2 then
  406. msgInner.data.fightPlayers = {[0] = 2}
  407. msgInner.data.fightPlayers[1] = fightPlayer1
  408. msgInner.data.fightPlayers[2] = fightPlayer2
  409. end
  410. local videoUuid = mainInfo1.fightVideo and mainInfo1.fightVideo[roadIndex]
  411. msgInner.data.fightState = videoUuid and 1 or 0 --战斗结果出来了
  412. msgInner.data.videoUuid = videoUuid
  413. msgInner.data.fightBase[0] = 2
  414. msgInner.data.fightBase[1] = fontFightBaseNet(mainInfo1, roadIndex)
  415. msgInner.data.fightBase[2] = fontFightBaseNet(mainInfo2, roadIndex)
  416. InnerMsg.sendMsg(fd, msgInner)
  417. end
  418. -- 观战
  419. function LW_VALLEY_PLAY_FIGHT(fd, uuid, videoUuid)
  420. local combatInfo = getCombatInfo(videoUuid)
  421. if not combatInfo then
  422. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.COMBAT_ERR_NOT_VIDEO)
  423. end
  424. local msgInner = InnerMsg.wl.WL_VALLEY_PLAY_FIGHT
  425. msgInner.uuid = uuid
  426. msgInner.combatInfo = combatInfo
  427. InnerMsg.sendMsg(fd, msgInner)
  428. end
  429. -- 比较胜利次数
  430. local function cmpWinCnt(a, b)
  431. if a.winCnt ~= b.winCnt then
  432. return a.winCnt > b.winCnt
  433. end
  434. return a.time < b.time
  435. end
  436. -- 刷新排行榜
  437. function sortBoard(mainInfo)
  438. if not mainInfo then return end
  439. local list = {}
  440. for _, list in pairs(mainInfo.roadList) do
  441. for _, uuid in ipairs(list) do
  442. local playerData = getPlayerData(uuid, FieldsRoleBase)
  443. if playerData then
  444. list[#list + 1] = playerData.rolebase
  445. end
  446. end
  447. end
  448. if #list > 1 then
  449. table.sort(list, cmpWinCnt)
  450. end
  451. mainInfo.board = {}
  452. for i = 1, #list do
  453. mainInfo.board[i] = list[i].uuid
  454. end
  455. end
  456. -- 战斗结果
  457. function LW_VALLEY_COMBATINFO(fd, svrIndex1, combatInfo)
  458. local mainInfo1 = getMainInfo(svrIndex1)
  459. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  460. local mainInfo2 = getMainInfo(svrIndex2)
  461. if not mainInfo1 or not mainInfo2 then
  462. return -- 不可能哦,不过加一下预防
  463. end
  464. local uuid1 = combatInfo.attacker.uuid
  465. local uuid2 = combatInfo.defender.uuid
  466. local playerData1 = getPlayerData(uuid1, FieldsRoleBase)
  467. local playerData2 = getPlayerData(uuid2, FieldsRoleBase)
  468. if not playerData1 or not not playerData2 then
  469. return
  470. end
  471. -- 保存战斗结果
  472. combatInfo.svrIndex1 = svrIndex1
  473. combatInfo.svrIndex2 = svrIndex2
  474. combatInfo.tili1 = playerData1.rolebase.tili
  475. combatInfo.tili2 = playerData2.rolebase.tili
  476. -- 修改胜利场次 体力 剩余血量等信息
  477. if combatInfo.isWin then
  478. playerData1.rolebase.winCnt = playerData1.rolebase.winCnt + 1
  479. else
  480. playerData2.rolebase.winCnt = playerData2.rolebase.winCnt + 1
  481. end
  482. playerData1.hpRateList = combatInfo.hpRateListAtk
  483. playerData2.hpRateList = combatInfo.hpRateListDef
  484. playerData1.rolebase.tili = playerData1.rolebase.tili - 1
  485. playerData1.rolebase.time = os.time()
  486. playerData2.rolebase.tili = playerData2.rolebase.tili - 1
  487. playerData2.rolebase.time = os.time()
  488. upsetPlayerData(playerData1)
  489. upsetPlayerData(playerData2)
  490. -- 记录到日志中
  491. addLog(combatInfo, mainInfo1, mainInfo2)
  492. solveLog(CombatDefine.ATTACK_SIDE, mainInfo1, combatInfo, playerData1)
  493. solveLog(CombatDefine.DEFEND_SIDE, mainInfo2, combatInfo, playerData2)
  494. -- 刷新排行榜
  495. sortBoard(mainInfo1)
  496. sortBoard(mainInfo2)
  497. ValleyLogic.saveCommonDB(ValleyLogic.getCommonDB())
  498. end
  499. -- 日志
  500. function LW_VALLEY_LOG_QUERY(fd, uuid, roadIndex)
  501. local svrIndex = MiddleManager.FD_2_SVRINDEX[fd]
  502. if not svrIndex then return end
  503. local mainInfo = getMainInfo(svrIndex)
  504. local logsLen = mainInfo and #mainInfo.logs or 0
  505. local msgInner = InnerMsg.wl.WL_VALLEY_LOG_QUERY
  506. msgInner.uuid = uuid
  507. msgInner.roadIndex = roadIndex
  508. msgInner.logs = msgInner.logs or {}
  509. Util.cleanTable(msgInner.logs)
  510. for i = 1, logsLen do
  511. local videoUuid = mainInfo.logs[i]
  512. local combatInfo = getCombatInfo(videoUuid, FilldsCombatLog)
  513. if combatInfo then
  514. if (roadIndex == 0 and (-- 自己的
  515. combatInfo.attacker.uuid == uuid or
  516. combatInfo.defender.uuid == uuid)) or
  517. (roadIndex > 0 and -- 某路的
  518. combatInfo.roadIndex == roadIndex) then
  519. local log = {}
  520. log.time = combatInfo.time
  521. log.roleBase1 = combatInfo.attacker
  522. log.roleBase2 = combatInfo.defender
  523. log.tili1 = combatInfo.tili1
  524. log.tili2 = combatInfo.tili2
  525. log.isWin = combatInfo.isWin and 1 or 0
  526. msgInner.logs[#msgInner.logs + 1] = log
  527. end
  528. end
  529. end
  530. InnerMsg.sendMsg(fd, msgInner)
  531. end
  532. -- 获取排行榜名次
  533. function getRank(mainInfo, uuid)
  534. if not mainInfo then return end
  535. for rank, tuuid in ipairs(mainInfo.board) do
  536. if tuuid == uuid then
  537. return rank
  538. end
  539. end
  540. end
  541. -- 封装排行榜结构体
  542. function fontBoardNet(uuid, rank)
  543. local playerData = getPlayerData(uuid, FieldsRoleBase)
  544. local net = {}
  545. net.rank = rank or 0
  546. if playerData then
  547. net.killCnt = playerData.rolebase.winCnt or 0
  548. net.roleBase = {}
  549. RoleLogic.makeRoleBase(playerData.rolebase, net.roleBase)
  550. end
  551. return net
  552. end
  553. -- 排行榜
  554. function LW_VALLEY_BOARD_QUERY(fd, uuid, camp)
  555. local svrIndex1 = MiddleManager.FD_2_SVRINDEX[fd]
  556. if not svrIndex1 then return end
  557. local mainInfo1 = getMainInfo(svrIndex1)
  558. local svrIndex2 = mainInfo1 and mainInfo1.targetSvrIndex
  559. local mainInfo2 = getMainInfo(svrIndex2)
  560. local mainInfo = mainInfo1
  561. if camp ~= CombatDefine.ATTACK_SIDE then
  562. mainInfo = mainInfo2
  563. end
  564. local boardLen = mainInfo and #mainInfo.board or 0
  565. local msgInner = InnerMsg.wl.WL_VALLEY_BOARD_QUERY
  566. msgInner.uuid = uuid
  567. msgInner.camp = camp
  568. msgInner.list = msgInner.list or {}
  569. Util.cleanTable(msgInner.list)
  570. for i = 1, boardLen do
  571. if #msgInner.list >= MAX_BOARD_LEN then
  572. break
  573. end
  574. local tuuid = mainInfo.board[i]
  575. msgInner.list[#msgInner.list + 1] = fontBoardNet(tuuid, i)
  576. end
  577. local myRank = getRank(mainInfo1, uuid)
  578. msgInner.myData = fontBoardNet(uuid, myRank)
  579. InnerMsg.sendMsg(fd, msgInner)
  580. end
  581. ------------------------------------- 活动状态变化 ------------------------------------------
  582. -- 按照战力排序
  583. local function cmpZDL(a, b)
  584. if a.zhandouli ~= b.zhandouli then
  585. return a.zhandouli > b.zhandouli
  586. end
  587. return a.svrIndex < b.svrIndex
  588. end
  589. -- 初始匹配组信息
  590. local function createMatchData(svrIndex1, svrIndex2)
  591. if (not svrIndex1) and (not svrIndex2) then return end
  592. local matchData = {}
  593. matchData.svrIndex1 = svrIndex1 or 0
  594. matchData.svrIndex2 = svrIndex2 or 0
  595. return matchData
  596. end
  597. -- 匹配对手 按战力排序(从高到低下) 两两匹配
  598. function matchMainInfo()
  599. local list = {}
  600. local commonDB = ValleyLogic.getCommonDB()
  601. if commonDB.mainInfos then
  602. for _, data in pairs(commonDB.mainInfos) do
  603. list[#list + 1] = data
  604. end
  605. end
  606. if #list > 1 then
  607. table.sort(list, cmpZDL)
  608. end
  609. commonDB.matchList = {}
  610. for i = 1, #list, 2 do
  611. local data1 = list[i]
  612. local data2 = list[i + 1]
  613. if data1 then
  614. data1.targetSvrIndex = data2 and data2.svrIndex or 0
  615. end
  616. if data2 then
  617. data2.targetSvrIndex = data1 and data1.svrIndex or 0
  618. end
  619. local matchData = createMatchData(data1 and data1.svrIndex, data2 and data2.svrIndex)
  620. commonDB.matchList[#commonDB.matchList + 1] = matchData
  621. end
  622. ValleyLogic.saveCommonDB(commonDB)
  623. end
  624. -- 侦查开始/匹配结束
  625. function onActExplore()
  626. matchMainInfo()
  627. end
  628. -- 发送破营+杀敌奖励
  629. function sendWLFinalResult(mainInfo, winCnt, failCnt)
  630. local fd = MiddleManager.getFDBySvrIndex(mainInfo.svrIndex)
  631. if not fd then return end
  632. local msgInner = InnerMsg.wl.WL_VALLEY_FINAL_RESULT
  633. msgInner.winCnt = winCnt
  634. msgInner.failCnt = failCnt
  635. msgInner.tieCnt = ValleyLogic.ROAD_MAX_CNT - winCnt - failCnt
  636. msgInner.killList = {}
  637. for _, list in pairs(mainInfo.roadList) do
  638. for _, uuid in ipairs(list) do
  639. local playerData = getPlayerData(uuid, FieldsRoleBase)
  640. if playerData then
  641. msgInner.killList[uuid] = playerData.rolebase.winCnt
  642. end
  643. end
  644. end
  645. InnerMsg.sendMsg(fd, msgInner)
  646. end
  647. -- 活动结束
  648. function onActEnd()
  649. -- 发送破营+杀敌奖励
  650. local commonDB = ValleyLogic.getCommonDB()
  651. if commonDB.matchList then
  652. for _, matchData in ipairs(commonDB.matchList) do
  653. local svrIndex1 = matchData.svrIndex1
  654. local svrIndex2 = matchData.svrIndex2
  655. local mainInfo1 = getMainInfo(svrIndex1)
  656. local mainInfo2 = getMainInfo(svrIndex2)
  657. local star1 = ValleyLogic.getMainStar(mainInfo1, mainInfo2)
  658. local star2 = ValleyLogic.getMainStar(mainInfo2, mainInfo1)
  659. sendWLFinalResult(mainInfo1, star1, star2)
  660. sendWLFinalResult(mainInfo2, star2, star1)
  661. end
  662. end
  663. ValleyLogic.cleanDB()
  664. end
  665. -- 活动开始
  666. function onActStart()
  667. ValleyLogic.cleanDB()
  668. end
  669. -- 发送鼓舞结果
  670. function sendWLInspireResult(mainInfo)
  671. local fd = MiddleManager.getFDBySvrIndex(mainInfo.svrIndex)
  672. if not fd then return end
  673. if not mainInfo.inspireList then return end
  674. local msgInner = InnerMsg.wl.WL_VALLEY_INSPIRE_RESULT
  675. msgInner.winCnt = mainInfo.fightWinCnt
  676. msgInner.failCnt = mainInfo.fightFailCnt
  677. msgInner.inspireList = mainInfo.inspireList
  678. InnerMsg.sendMsg(fd, msgInner)
  679. end
  680. -- 行军开始/前一轮结束
  681. function onRoundEnd()
  682. local commonDB = ValleyLogic.getCommonDB()
  683. if not commonDB.mainInfos then return end
  684. for _, mainInfo in pairs(commonDB.mainInfos) do
  685. -- 退场 失败或者体力为0
  686. local fightIndexChange = mainInfo.fightIndexChange
  687. mainInfo.fightIndexChange = nil
  688. if fightIndexChange then
  689. for roadIndex in pairs(fightIndexChange) do
  690. local oldFightIndex = mainInfo.fightIndexList[roadIndex] or 1
  691. mainInfo.fightIndexList[roadIndex] = oldFightIndex + 1
  692. end
  693. end
  694. -- 发送鼓舞奖励
  695. sendWLInspireResult(mainInfo)
  696. -- 重置鼓舞信息+本轮胜场
  697. mainInfo.fightWinCnt = 0
  698. mainInfo.fightFailCnt = 0
  699. mainInfo.inspireCnt = nil
  700. mainInfo.inspireList = nil
  701. mainInfo.fightResult = nil
  702. mainInfo.fightVideo = nil
  703. end
  704. ValleyLogic.saveCommonDB(ValleyLogic.getCommonDB())
  705. end
  706. -- 行军结束
  707. function onMoveEnd()
  708. local commonDB = ValleyLogic.getCommonDB()
  709. if not commonDB.mainInfos then return end
  710. -- 移动到目标点
  711. for _, mainInfo in pairs(commonDB.mainInfos) do
  712. for roadIndex = 1, ValleyLogic.ROAD_MAX_CNT do
  713. for index = 1, ValleyLogic.ROAD_POINT_CNT do
  714. local uuid = getMovePlayerUuid(mainInfo, roadIndex, index)
  715. if uuid then
  716. mainInfo.moveList[uuid] = getMoveToPos(index)
  717. end
  718. end
  719. end
  720. end
  721. ValleyLogic.saveCommonDB(ValleyLogic.getCommonDB())
  722. end
  723. -- 发送战斗开打
  724. function sendWLCombatBegin(round, roadIndex, mainInfo1, mainInfo2, playerData1, playerData2)
  725. if not playerData1 or not playerData2 then
  726. return
  727. end
  728. local fd = MiddleManager.getFDBySvrIndex(mainInfo1.svrIndex)
  729. fd = fd or MiddleManager.getFDBySvrIndex(mainInfo2.svrIndex)
  730. if not fd then return end
  731. local msgInner = InnerMsg.wl.WL_VALLEY_COMBAT_BEGIN
  732. msgInner.svrIndex = mainInfo1.svrIndex
  733. msgInner.roadIndex = roadIndex
  734. msgInner.round = round
  735. msgInner.inspireCnt1 = mainInfo1.inspireCnt or 0
  736. msgInner.inspireCnt2 = mainInfo2.inspireCnt or 0
  737. msgInner.playerData1 = playerData1
  738. msgInner.playerData2 = playerData2
  739. InnerMsg.sendMsg(fd, msgInner)
  740. end
  741. -- 开始战斗
  742. function onFightBegin(round)
  743. local commonDB = ValleyLogic.getCommonDB()
  744. if not commonDB.matchList then return end
  745. for _, matchData in ipairs(commonDB.matchList) do
  746. local svrIndex1 = matchData.svrIndex1
  747. local svrIndex2 = matchData.svrIndex2
  748. local mainInfo1 = getMainInfo(svrIndex1)
  749. local mainInfo2 = getMainInfo(svrIndex2)
  750. for roadIndex = 1, ValleyLogic.ROAD_MAX_CNT do
  751. local uuid1 = getMovePlayerUuid(mainInfo1, roadIndex, 1)
  752. local playerData1 = getPlayerData(uuid1)
  753. local uuid2 = getMovePlayerUuid(mainInfo2, roadIndex, 1)
  754. local playerData2 = getPlayerData(uuid2)
  755. sendWLCombatBegin(round, roadIndex, mainInfo1, mainInfo2, playerData1, playerData2)
  756. end
  757. end
  758. end
  759. -- 切换战斗轮次状态
  760. function onActRound(round, roundState)
  761. if roundState == ValleyLogic.STATE_FIGHT_MOVE then
  762. -- 行军开始/前一轮结束
  763. onRoundEnd()
  764. elseif roundState == ValleyLogic.STATE_FIGHT_READY then
  765. -- 鼓舞开始/行军结束
  766. onMoveEnd()
  767. elseif roundState == ValleyLogic.STATE_FIGHT_FIGHT then
  768. -- 开打!!
  769. onFightBegin(round)
  770. end
  771. end