JjcLadderMiddle.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. -------------------------------------------------------
  2. -- 天梯争霸 跨服逻辑
  3. -------------------------------------------------------
  4. local LuaMongo = _G.lua_mongo
  5. local DB = require("common.DB")
  6. local Util = require("common.Util")
  7. local Lang = require("common.Lang")
  8. local Log = require("common.Log")
  9. local InnerMsg = require("core.InnerMsg")
  10. local BillboardDB = require("billboard.BillboardDB")
  11. local BillboardDefine = require("billboard.BillboardDefine")
  12. local MiddleLogic = require("middle.MiddleLogic")
  13. local MiddleManager = require("middle.MiddleManager")
  14. local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
  15. local RoleLogic = require("role.RoleLogic")
  16. local QueryByUuid = {_id = nil}
  17. local FieldSort = {rolebase = 1}
  18. local FieldLogDel = {cnt = 1}
  19. local FieldCombatLog = {time=1,attacker=1,defender=1,isWin=1,jjcLadder=1}
  20. local DBUpdateField = {} -- 更新域
  21. local LASTTOP_CNT = 100
  22. local BOARD_MAX_CNT = 50
  23. local LOG_MAX_CNT = 30
  24. local RANDOM_TARGET_CNT = 5
  25. function saveTop(uuid2beMobaiCnt)
  26. local commonDB = JjcLadderLogic.getCommonDB()
  27. local rank2uuid = commonDB.rank2uuid
  28. local lastTopList = commonDB.lastTopList
  29. -- 保存上一次前x信息
  30. commonDB.lastTopList = {}
  31. commonDB.lastUuid2Rank = {}
  32. for i = 1, LASTTOP_CNT do
  33. local uuid = rank2uuid and rank2uuid[i]
  34. local playerData = uuid and getPlayerData(uuid)
  35. if not playerData then break end
  36. commonDB.lastUuid2Rank[uuid] = i
  37. commonDB.lastTopList[i] = playerData.rolebase
  38. if uuid2beMobaiCnt and uuid2beMobaiCnt[uuid] then
  39. playerData.rolebase.beMobaiCnt = uuid2beMobaiCnt[uuid]
  40. end
  41. end
  42. JjcLadderLogic.saveCommonDB(commonDB)
  43. end
  44. -- 重置活动数据
  45. function resetDB()
  46. local commonDB = JjcLadderLogic.getCommonDB()
  47. commonDB.uuid2rank = nil
  48. commonDB.rank2uuid = nil
  49. commonDB.uuid2logs = nil
  50. commonDB.logReds = nil
  51. commonDB.cleanTime = JjcLadderLogic.getCleanUpTime()
  52. -- 保存上一次前x信息
  53. --[[
  54. commonDB.lastTopList = {}
  55. commonDB.lastUuid2Rank = {}
  56. for i = 1, LASTTOP_CNT do
  57. local uuid = rank2uuid and rank2uuid[i]
  58. local playerData = uuid and getPlayerData(uuid)
  59. if not playerData then break end
  60. commonDB.lastUuid2Rank[uuid] = i
  61. commonDB.lastTopList[i] = playerData.rolebase
  62. if uuid2beMobaiCnt and uuid2beMobaiCnt[uuid] then
  63. playerData.rolebase.beMobaiCnt = uuid2beMobaiCnt[uuid]
  64. end
  65. end
  66. ]]
  67. JjcLadderLogic.saveCommonDB(commonDB)
  68. LuaMongo.remove(DB.db_jjcLadder)
  69. LuaMongo.remove(DB.db_jjcLadder_record)
  70. end
  71. -- 获取名次 0表示未上榜
  72. function getRank(uuid)
  73. local commonDB = JjcLadderLogic.getCommonDB()
  74. if not commonDB.uuid2rank then
  75. return 0
  76. end
  77. return commonDB.uuid2rank[uuid] or 0
  78. end
  79. function getLastRank(uuid)
  80. local commonDB = JjcLadderLogic.getCommonDB()
  81. if not commonDB.lastUuid2Rank then
  82. return 0
  83. end
  84. return commonDB.lastUuid2Rank[uuid] or 0
  85. end
  86. -- 获取参赛者信息
  87. function getPlayerData(uuid, field)
  88. if not uuid then return end
  89. QueryByUuid._id = uuid
  90. LuaMongo.find(DB.db_jjcLadder, QueryByUuid, field)
  91. local playerData = {}
  92. return LuaMongo.next(playerData) and playerData
  93. end
  94. -- 保存/更新参赛者信息
  95. function updatePlayerData(playerData)
  96. local oldData = getPlayerData(playerData._id)
  97. if not oldData then
  98. -- 首次收到,插入数据
  99. local commonDB = JjcLadderLogic.getCommonDB()
  100. commonDB.rank2uuid = commonDB.rank2uuid or {}
  101. commonDB.uuid2rank = commonDB.uuid2rank or {}
  102. local rank = #commonDB.rank2uuid + 1
  103. commonDB.rank2uuid[rank] = playerData._id
  104. commonDB.uuid2rank[playerData._id] = rank
  105. JjcLadderLogic.saveCommonDB(commonDB)
  106. LuaMongo.insert(DB.db_jjcLadder, playerData)
  107. else
  108. -- 非首次,更新数据
  109. QueryByUuid._id = playerData._id
  110. LuaMongo.update(DB.db_jjcLadder, QueryByUuid, playerData)
  111. end
  112. end
  113. -- 排序比较函数
  114. local function cmpRank(a, b)
  115. if a.zhandouli ~= b.zhandouli then
  116. return a.zhandouli > b.zhandouli
  117. end
  118. return a.lv > b.lv
  119. end
  120. -- 按照战力重新排序
  121. function sortRank()
  122. local commonDB = JjcLadderLogic.getCommonDB()
  123. commonDB.rank2uuid = {}
  124. commonDB.uuid2rank = {}
  125. local list = {}
  126. LuaMongo.find(DB.db_jjcLadder, nil, FieldSort)
  127. while true do
  128. local playerData = {}
  129. if not LuaMongo.next(playerData) then
  130. break
  131. end
  132. list[#list + 1] = playerData.rolebase
  133. end
  134. if #list > 1 then
  135. table.sort(list, cmpRank)
  136. end
  137. for rank = 1, #list do
  138. local uuid = list[rank].uuid
  139. commonDB.rank2uuid[rank] = uuid
  140. commonDB.uuid2rank[uuid] = rank
  141. end
  142. JjcLadderLogic.saveCommonDB(commonDB)
  143. end
  144. -- 获取可膜拜的人数
  145. function getMobaiCnt()
  146. local commonDB = JjcLadderLogic.getCommonDB()
  147. if not commonDB.lastTopList then
  148. return 0
  149. end
  150. return math.min(#commonDB.lastTopList, JjcLadderLogic.MOBAI_PLAYER_CNT)
  151. end
  152. -- 是否有新纪录
  153. function hasLogRed(uuid)
  154. local commonDB = JjcLadderLogic.getCommonDB()
  155. if not commonDB.logReds then
  156. return
  157. end
  158. return commonDB.logReds[uuid]
  159. end
  160. -- 刷新纪录红点
  161. function setLogRed(commonDB, uuid, flag)
  162. if not commonDB.logReds then
  163. commonDB.logReds = {}
  164. end
  165. if commonDB.logReds[uuid] == flag then
  166. return
  167. end
  168. if flag == false then
  169. if commonDB.logReds[uuid] then
  170. commonDB.logReds[uuid] = nil
  171. end
  172. else
  173. commonDB.logReds[uuid] = flag
  174. end
  175. return true
  176. end
  177. function getCombatInfo(videoUuid, field)
  178. if not videoUuid then return end
  179. QueryByUuid._id = videoUuid
  180. LuaMongo.find(DB.db_jjcLadder_record, QueryByUuid, field)
  181. local combatInfo = {}
  182. return LuaMongo.next(combatInfo) and combatInfo
  183. end
  184. --
  185. function delLog(videoUuid)
  186. local combatInfo = getCombatInfo(videoUuid, FieldLogDel)
  187. if not combatInfo then return end
  188. combatInfo.cnt = combatInfo.cnt - 1
  189. if combatInfo.cnt < 1 then
  190. QueryByUuid._id = videoUuid
  191. LuaMongo.remove(DB.db_jjcLadder_record, QueryByUuid)
  192. else
  193. QueryByUuid._id = videoUuid
  194. DBUpdateField["$set"] = combatInfo
  195. LuaMongo.update(DB.db_jjcLadder_record, QueryByUuid, combatInfo)
  196. end
  197. end
  198. -- 添加记录
  199. function addLog(commonDB, combatInfo, uuid, targetUuid)
  200. combatInfo.cnt = 2
  201. LuaMongo.insert(DB.db_jjcLadder_record, combatInfo)
  202. commonDB.uuid2logs = commonDB.uuid2logs or {}
  203. commonDB.uuid2logs[uuid] = commonDB.uuid2logs[uuid] or {}
  204. commonDB.uuid2logs[targetUuid] = commonDB.uuid2logs[targetUuid] or {}
  205. local logs = commonDB.uuid2logs[uuid]
  206. local targetLogs = commonDB.uuid2logs[targetUuid]
  207. table.insert(logs, 1, combatInfo._id)
  208. table.insert(targetLogs, 1, combatInfo._id)
  209. for i = LOG_MAX_CNT + 1, #logs do
  210. delLog(logs[i])
  211. logs[i] = nil
  212. end
  213. for i = LOG_MAX_CNT + 1, #targetLogs do
  214. delLog(targetLogs[i])
  215. targetLogs[i] = nil
  216. end
  217. end
  218. -- 随机对手
  219. local TEMP_RANDOM_LIST = {}
  220. local function randomRank(minRank, maxRank, noRanks)
  221. local commonDB = JjcLadderLogic.getCommonDB()
  222. local cnt = 0
  223. for rank = minRank, maxRank do
  224. if (not noRanks[rank]) and
  225. commonDB.rank2uuid[rank] then
  226. cnt = cnt + 1
  227. TEMP_RANDOM_LIST[cnt] = rank
  228. end
  229. end
  230. if cnt < 1 then return end
  231. local r = math.random(1, cnt)
  232. local rank = TEMP_RANDOM_LIST[r]
  233. local uuid = commonDB.rank2uuid[rank]
  234. return rank, uuid
  235. end
  236. -- 根据名次随机x个对手
  237. local function randomTargetList(myRank)
  238. local targetList = {}
  239. if myRank < 1 then -- 没有参加资格,固定显示前5名
  240. local commonDB = JjcLadderLogic.getCommonDB()
  241. if not commonDB or not commonDB.rank2uuid then
  242. return targetList
  243. end
  244. for i = 1, RANDOM_TARGET_CNT do
  245. local uuid = commonDB.rank2uuid[i]
  246. local playerData = getPlayerData(uuid)
  247. local rolebase = playerData and playerData.rolebase
  248. if rolebase then
  249. rolebase.rank = i
  250. local list = {}
  251. list.roleBase = Util.copyTable(rolebase)
  252. list.objList = Util.copyTable(playerData.objList)
  253. targetList[#targetList + 1] = list
  254. end
  255. end
  256. return targetList
  257. end
  258. -- a.自身排名1000≤A,则每次从排名[A-100,A-50]的库中随机抽取5个,排名顺序展示在界面上
  259. -- b.自身排名200≤A<1000,则每次从排名[A-80,A-30]的库中随机抽取5个,排名顺序展示在界面上
  260. -- c.自身排名100<A<200,则每次从排名[A-30,A-10]的库中随机抽取5个,排名顺序展示在界面上
  261. -- d.自身排名20<A≤99,则每次从排名[A-15,A-5]的库中随机抽取5个,排名顺序展示在界面上
  262. -- e.自身排名6≤A≤20,则从3~20名中随机取
  263. -- f.自身排名A≤6,则从1~6名中随机取
  264. local minRank, maxRank
  265. if myRank >= 1000 then
  266. minRank = myRank - 100
  267. maxRank = myRank - 50
  268. elseif myRank >= 200 then
  269. minRank = myRank - 80
  270. maxRank = myRank - 30
  271. elseif myRank > 100 then
  272. minRank = myRank - 30
  273. maxRank = myRank - 10
  274. elseif myRank >= 20 then
  275. minRank = myRank - 15
  276. maxRank = myRank - 5
  277. elseif myRank > 6 then
  278. minRank = 3
  279. maxRank = 20
  280. else
  281. minRank = 1
  282. maxRank = 6
  283. end
  284. local noRanks = {}
  285. noRanks[myRank] = true
  286. for i = 1, RANDOM_TARGET_CNT do
  287. local rank, uuid = randomRank(minRank, maxRank, noRanks)
  288. if rank and uuid then
  289. local playerData = getPlayerData(uuid)
  290. noRanks[rank] = true
  291. local rolebase = playerData and playerData.rolebase
  292. if rolebase then
  293. rolebase.rank = rank
  294. local list = {}
  295. list.roleBase = Util.copyTable(rolebase)
  296. list.objList = Util.copyTable(playerData.objList)
  297. targetList[#targetList + 1] = list
  298. end
  299. end
  300. end
  301. return targetList
  302. end
  303. -------------------------------------- middle -------------------------------------------
  304. -- 主界面查询
  305. function LW_JJC_LADDER_QUERY(fd, uuid)
  306. local playerData = getPlayerData(uuid)
  307. local myRank = getRank(uuid)
  308. local msgInner = InnerMsg.wl.WL_JJC_LADDER_QUERY
  309. msgInner.uuid = uuid
  310. -- 随机对手
  311. msgInner.targetList = randomTargetList(myRank)
  312. msgInner.mobaiCnt = getMobaiCnt()
  313. msgInner.logRed = hasLogRed(uuid) and 1 or 0
  314. msgInner.myRank = myRank
  315. InnerMsg.sendMsg(fd, msgInner)
  316. end
  317. -- 数据同步
  318. function LW_JJC_LADDER_SENDDATA(fd, player)
  319. updatePlayerData(player)
  320. end
  321. -- 膜拜信息
  322. function LW_JJC_LADDER_MOBAI_QUERY(fd, uuid)
  323. local commonDB = JjcLadderLogic.getCommonDB()
  324. local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI_QUERY
  325. msgInner.uuid = uuid
  326. msgInner.list = msgInner.list or {}
  327. Util.cleanTable(msgInner.list)
  328. for i = 1, JjcLadderLogic.MOBAI_PLAYER_CNT do
  329. local rolebase = commonDB.lastTopList and commonDB.lastTopList[i]
  330. if not rolebase then break end
  331. msgInner.list[i] = rolebase
  332. end
  333. InnerMsg.sendMsg(fd, msgInner)
  334. end
  335. -- 膜拜点赞
  336. function LW_JJC_LADDER_MOBAI(fd, uuid, rank)
  337. local commonDB = JjcLadderLogic.getCommonDB()
  338. local rolebase = commonDB.lastTopList and commonDB.lastTopList[rank]
  339. if not rolebase then
  340. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_MOBAI_ERR_NONE)
  341. end
  342. rolebase.beMobaiCnt = (rolebase.beMobaiCnt or 0) + 1
  343. JjcLadderLogic.saveCommonDB(commonDB)
  344. local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI
  345. msgInner.uuid = uuid
  346. msgInner.rank = rank
  347. InnerMsg.sendMsg(fd, msgInner)
  348. LW_JJC_LADDER_MOBAI_QUERY(fd, uuid)
  349. -- LW_JJC_LADDER_QUERY(fd, uuid)
  350. -- 被膜拜的人同步次数
  351. local toFd = MiddleManager.getFDBySvrIndex(rolebase.svrIndex)
  352. if toFd then
  353. local msgInner2 = InnerMsg.wl.WL_JJC_LADDER_BEMOBAICNT
  354. msgInner2.uuid = rolebase.uuid
  355. msgInner2.beMobaiCnt = rolebase.beMobaiCnt
  356. InnerMsg.sendMsg(toFd, msgInner2)
  357. end
  358. end
  359. -- 查看名次
  360. function LW_JJC_LADDER_RANK(fd, uuid)
  361. local msgInner = InnerMsg.wl.WL_JJC_LADDER_RANK
  362. msgInner.uuid = uuid
  363. if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
  364. local myRank = getRank(uuid)
  365. msgInner.isLast = 0
  366. msgInner.rank = myRank
  367. else
  368. msgInner.isLast = 1
  369. msgInner.rank = getLastRank(uuid)
  370. end
  371. InnerMsg.sendMsg(fd, msgInner)
  372. end
  373. local function makeBoardRankData(rankData, rolebase)
  374. rankData.roleBase = Util.copyTable(rolebase)
  375. rankData.uuid = rolebase.uuid
  376. rankData.value1 = rolebase.zhandouli or 0
  377. if rolebase.name and rolebase.svrName then
  378. rankData.roleBase.name = "[" .. rolebase.svrName .. "]" .. rolebase.name
  379. end
  380. end
  381. --
  382. function getBoard()
  383. local billboard = BillboardDB.db[BillboardDefine.TYPE_JJCLADDER_M]
  384. if not billboard then return end
  385. local nowTime = os.time()
  386. if billboard.lastSortTime == nowTime then
  387. return billboard
  388. end
  389. billboard.lastSortTime = nowTime
  390. local commonDB = JjcLadderLogic.getCommonDB()
  391. local rank2uuid = commonDB.rank2uuid
  392. Util.cleanTable(billboard.uuid2rank)
  393. Util.cleanTable(billboard.rank2data)
  394. if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
  395. if rank2uuid then
  396. for rank, uuid in ipairs(rank2uuid) do
  397. billboard.uuid2rank[uuid] = rank
  398. local playerData = getPlayerData(uuid)
  399. if not playerData then break end
  400. local rankData = BillboardDB.initRankData()
  401. billboard.rank2data[rank] = rankData
  402. makeBoardRankData(rankData, playerData.rolebase)
  403. end
  404. end
  405. else
  406. local lastTopListLen = commonDB.lastTopList and #commonDB.lastTopList or 0
  407. for rank = 1, lastTopListLen do
  408. local rolebase = commonDB.lastTopList[rank]
  409. if not rolebase then break end
  410. billboard.uuid2rank[rolebase.uuid] = rank
  411. local rankData = BillboardDB.initRankData()
  412. billboard.rank2data[rank] = rankData
  413. makeBoardRankData(rankData, rolebase)
  414. end
  415. end
  416. return billboard
  417. end
  418. -- 排行榜
  419. function LW_JJC_LADDER_BOARD(fd, uuid)
  420. local commonDB = JjcLadderLogic.getCommonDB()
  421. local rank2uuid = commonDB.rank2uuid
  422. local msgInner = InnerMsg.wl.WL_JJC_LADDER_BOARD
  423. msgInner.uuid = uuid
  424. msgInner.list = msgInner.list or {}
  425. Util.cleanTable(msgInner.list)
  426. if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
  427. -- 活动开始,活动排行
  428. for i = 1, BOARD_MAX_CNT do
  429. local tuuid = rank2uuid and rank2uuid[i]
  430. local playerData = tuuid and getPlayerData(tuuid)
  431. if not playerData then break end
  432. msgInner.list[i] = playerData.rolebase
  433. end
  434. local myPlayerData = getPlayerData(uuid)
  435. msgInner.myData = myPlayerData and myPlayerData.rolebase or {}
  436. msgInner.myData.rank = getRank(uuid)
  437. msgInner.isLast = 0
  438. else
  439. -- 其它时候,上一期排行
  440. if commonDB.lastTopList then
  441. for i = 1, BOARD_MAX_CNT do
  442. local rolebase = commonDB.lastTopList[i]
  443. if not rolebase then break end
  444. msgInner.list[i] = rolebase
  445. if rolebase.uuid == uuid then
  446. msgInner.myData = rolebase
  447. msgInner.myData.rank = i
  448. end
  449. end
  450. else
  451. msgInner.myData = {}
  452. msgInner.myData.rank = 0
  453. end
  454. msgInner.isLast = 1
  455. end
  456. InnerMsg.sendMsg(fd, msgInner)
  457. end
  458. -- 战斗请求
  459. function LW_JJC_LADDER_COMBAT_ASK(fd, uuid, targetUuid, targetRank)
  460. -- 活动结束就拜拜
  461. if JjcLadderLogic.getState() ~= JjcLadderLogic.STATE_ACT_START then
  462. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_TIME)
  463. end
  464. if not getPlayerData(uuid) then
  465. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_JOIN)
  466. end
  467. local targetPlayerData = getPlayerData(targetUuid)
  468. if not targetPlayerData then
  469. return LW_JJC_LADDER_QUERY(fd, uuid)
  470. end
  471. if getRank(targetUuid) ~= targetRank then
  472. return LW_JJC_LADDER_QUERY(fd, uuid)
  473. end
  474. local msgInner = InnerMsg.wl.WL_JJC_LADDER_COMBAT_ASK
  475. msgInner.uuid = uuid
  476. msgInner.rank = getRank(uuid)
  477. msgInner.targetUuid = targetUuid
  478. msgInner.targetRank = targetRank
  479. msgInner.playerData = targetPlayerData
  480. InnerMsg.sendMsg(fd, msgInner)
  481. end
  482. -- 战斗结果同步
  483. function LW_JJC_LADDER_COMBAT_RESULT(fd, uuid, rank, targetUuid, targetRank, combatInfo)
  484. local selfData = getPlayerData(uuid)
  485. if not selfData then
  486. return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_JOIN)
  487. end
  488. local targetPlayerData = getPlayerData(targetUuid)
  489. if not targetPlayerData then
  490. return LW_JJC_LADDER_QUERY(fd, uuid)
  491. end
  492. if getRank(uuid) ~= rank or getRank(targetUuid) ~= targetRank then
  493. return LW_JJC_LADDER_QUERY(fd, uuid)
  494. end
  495. -- 结果处理
  496. local commonDB = JjcLadderLogic.getCommonDB()
  497. local newRank, newTargetRank = rank, targetRank
  498. if combatInfo.isActEnd then
  499. else
  500. if combatInfo.isWin and (rank > targetRank) then
  501. -- 胜利可以提升名次
  502. commonDB.uuid2rank[uuid] = targetRank
  503. commonDB.uuid2rank[targetUuid] = rank
  504. commonDB.rank2uuid[rank] = targetUuid
  505. commonDB.rank2uuid[targetRank] = uuid
  506. newRank, newTargetRank = targetRank, rank
  507. end
  508. end
  509. if combatInfo.isWin then
  510. setLogRed(commonDB, targetUuid, true)
  511. end
  512. combatInfo.jjcLadder = {}
  513. combatInfo.jjcLadder.rank = rank
  514. combatInfo.jjcLadder.targetRank = targetRank
  515. combatInfo.jjcLadder.newRank = newRank
  516. combatInfo.jjcLadder.newTargetRank = newTargetRank
  517. addLog(commonDB, combatInfo, uuid, targetUuid)
  518. JjcLadderLogic.saveCommonDB(commonDB)
  519. local msgInner = InnerMsg.wl.WL_JJC_LADDER_COMBAT_RESULT
  520. msgInner.uuid = uuid
  521. msgInner.rank = rank
  522. msgInner.targetUuid = targetUuid
  523. msgInner.targetRank = targetRank
  524. InnerMsg.sendMsg(fd, msgInner)
  525. end
  526. local function getLogContent(combatInfo, seeuuid)
  527. local jjcLadder = combatInfo.jjcLadder
  528. if combatInfo.defender.uuid == seeuuid then
  529. -- 防守方观看
  530. if combatInfo.isWin then
  531. if jjcLadder.targetRank ~= jjcLadder.newTargetRank then
  532. return Util.format(Lang.JJC_LADDER_LOG4, combatInfo.attacker.name, jjcLadder.targetRank, jjcLadder.newTargetRank)
  533. else
  534. return Util.format(Lang.JJC_LADDER_LOG5, combatInfo.attacker.name)
  535. end
  536. else
  537. return Util.format(Lang.JJC_LADDER_LOG6, combatInfo.attacker.name)
  538. end
  539. else
  540. -- 攻击方观看/其它情况
  541. if combatInfo.isWin then
  542. if jjcLadder.rank ~= jjcLadder.newRank then
  543. return Util.format(Lang.JJC_LADDER_LOG1, combatInfo.defender.name, jjcLadder.rank, jjcLadder.newRank)
  544. else
  545. return Util.format(Lang.JJC_LADDER_LOG2, combatInfo.defender.name)
  546. end
  547. else
  548. return Util.format(Lang.JJC_LADDER_LOG3, combatInfo.defender.name)
  549. end
  550. end
  551. end
  552. local function fontLadderLog(net, combatInfo, seeuuid)
  553. net.time = combatInfo.time
  554. net.isWin = combatInfo.isWin
  555. local jjcLadder = combatInfo.jjcLadder
  556. net.attacker = Util.copyTable(combatInfo.attacker)
  557. net.defender = Util.copyTable(combatInfo.defender)
  558. net.atkRank = combatInfo.jjcLadder.rank
  559. net.defRank = combatInfo.jjcLadder.targetRank
  560. -- net.jjcLadder = Util.copyTable(combatInfo.jjcLadder)
  561. end
  562. local function sortGodLog(a, b)
  563. return a[2] > b[2]
  564. end
  565. function getGodFightLog(msgInner, commonDB)
  566. local logList ={}
  567. local haveList = {}
  568. local cnt = 0
  569. for rank, uuid in ipairs(commonDB.rank2uuid) do
  570. if rank > 20 then break end
  571. local logs = commonDB.uuid2logs and commonDB.uuid2logs[uuid]
  572. local logsLen = logs and #logs or 0
  573. logsLen = logsLen < 10 and logsLen or 10
  574. for i = 1, logsLen do
  575. local videoUuid = logs[i]
  576. if not haveList[videoUuid] then
  577. local combatInfo = getCombatInfo(videoUuid, FieldCombatLog)
  578. if combatInfo.jjcLadder and (combatInfo.jjcLadder.rank <= 20 or combatInfo.jjcLadder.targetRank <= 20 ) then
  579. cnt = cnt + 1
  580. logList[cnt] = {}
  581. logList[cnt][1] =videoUuid
  582. logList[cnt][2] = combatInfo.time
  583. haveList[videoUuid] = combatInfo.time
  584. end
  585. end
  586. end
  587. end
  588. if #logList > 0 then
  589. table.sort( logList, sortGodLog )
  590. for i = 1, 10 do
  591. if logList[i] and logList[i][1] then
  592. local combatInfo = getCombatInfo(logList[i][1], FieldCombatLog)
  593. local log = {}
  594. fontLadderLog(log, combatInfo)
  595. log.videoUuid = logList[i][1]
  596. msgInner.godLogs[#msgInner.godLogs + 1] = log
  597. end
  598. end
  599. end
  600. end
  601. -- 对战日志
  602. function LW_JJC_LADDER_LOG(fd, uuid)
  603. local commonDB = JjcLadderLogic.getCommonDB()
  604. local logs = commonDB.uuid2logs and commonDB.uuid2logs[uuid]
  605. local logsLen = logs and #logs or 0
  606. local msgInner = InnerMsg.wl.WL_JJC_LADDER_LOG
  607. msgInner.uuid = uuid
  608. msgInner.logs = msgInner.logs or {}
  609. msgInner.godLogs = msgInner.godLogs or {}
  610. Util.cleanTable(msgInner.logs)
  611. Util.cleanTable(msgInner.godLogs)
  612. for i = 1, logsLen do
  613. local videoUuid = logs[i]
  614. local combatInfo = getCombatInfo(videoUuid, FieldCombatLog)
  615. if combatInfo then
  616. local log = {}
  617. fontLadderLog(log, combatInfo, uuid)
  618. log.videoUuid = videoUuid
  619. msgInner.logs[#msgInner.logs + 1] = log
  620. end
  621. end
  622. getGodFightLog(msgInner, commonDB)
  623. InnerMsg.sendMsg(fd, msgInner)
  624. setLogRed(commonDB, uuid, false)
  625. end
  626. -- 战斗回放
  627. function LW_JJC_LADDER_REPEAT_VIDEO(fd, uuid, videoUuid)
  628. local combatInfo = getCombatInfo(videoUuid)
  629. local msgInner = InnerMsg.wl.WL_JJC_LADDER_REPEAT_VIDEO
  630. msgInner.uuid = uuid
  631. msgInner.combatInfo = combatInfo or {}
  632. InnerMsg.sendMsg(fd, msgInner)
  633. end
  634. -- 同步可膜拜人数
  635. function sendWLMobaiCnt(fd)
  636. local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI_CNT
  637. msgInner.mobaiCnt = getMobaiCnt()
  638. InnerMsg.sendMsg(fd, msgInner)
  639. end
  640. ------------------------------------- 活动状态变化 ------------------------------------------
  641. function onActReady()
  642. -- 活动准备开始 把上期前三 称号去除
  643. local commonDB = JjcLadderLogic.getCommonDB()
  644. print(" onActReady middle !!! go top chenghao del ")
  645. if commonDB.lastTopList then
  646. local svrList = {}
  647. local msgInner = InnerMsg.wl.WL_JJC_LADDER_CHENGHAO_DEL
  648. msgInner.list = msgInner.list or {}
  649. Util.cleanTable(msgInner.list)
  650. for i = 1, 3 do
  651. local rolebase = commonDB.lastTopList[i]
  652. if not rolebase then break end
  653. msgInner.list[rolebase.uuid] = {}
  654. msgInner.list[rolebase.uuid].svrIndex = rolebase.svrIndex
  655. msgInner.list[rolebase.uuid].rank = i
  656. svrList[rolebase.svrIndex] = 1
  657. end
  658. for svrIndex in pairs(svrList) do
  659. local toFd = MiddleManager.getFDBySvrIndex(svrIndex)
  660. InnerMsg.sendMsg(toFd, msgInner)
  661. end
  662. end
  663. end
  664. function onActStart()
  665. -- 活动开始,按照战力排序
  666. sortRank()
  667. end
  668. function onActEnd()
  669. -- 告知各个服结果
  670. local commonDB = JjcLadderLogic.getCommonDB()
  671. local svrIndex2ranks = {}
  672. if commonDB.rank2uuid then
  673. for rank, uuid in ipairs(commonDB.rank2uuid) do
  674. local playerData = getPlayerData(uuid, FieldSort)
  675. local svrIndex = playerData and playerData.rolebase and playerData.rolebase.svrIndex
  676. -- 日志
  677. --Log.write(Log.LOGID_DEBUG, "jjcLadder", rank, uuid, svrIndex)
  678. -- 按照服对结果进行分组,返还本服
  679. local lastSvrIndex = svrIndex and MiddleManager.getLastSvrIndex(svrIndex)
  680. if lastSvrIndex then
  681. svrIndex2ranks[lastSvrIndex] = svrIndex2ranks[lastSvrIndex] or {}
  682. svrIndex2ranks[lastSvrIndex][rank] = uuid
  683. end
  684. end
  685. end
  686. local msgResult = InnerMsg.wl.WL_JJC_LADDER_FINAL_RESULT
  687. local uuid2beMobaiCnt = {}
  688. local lastTopCnt = commonDB.lastTopList and #commonDB.lastTopList or 0
  689. for i = 1, lastTopCnt do
  690. local rolebase = commonDB.lastTopList[i]
  691. uuid2beMobaiCnt[rolebase.uuid] = rolebase.beMobaiCnt
  692. end
  693. -- 保存前3
  694. saveTop(uuid2beMobaiCnt)
  695. -- 同步结果
  696. for svrIndex, fd in pairs(MiddleManager.SVRINDEX_2_FD) do
  697. -- 排名结果
  698. msgResult.list = svrIndex2ranks[svrIndex] or {}
  699. InnerMsg.sendMsg(fd, msgResult)
  700. sendWLMobaiCnt(fd)
  701. end
  702. end
  703. function resetDbActEnd()
  704. -- 重置数据
  705. resetDB()
  706. end