AnotherWorldBattleCS.lua 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. -- 异界之战(跨服)
  2. local InnerMsg = require("core.InnerMsg")
  3. local Log = require("common.Log")
  4. local Timer = require("core.Timer")
  5. local Util = require("common.Util")
  6. local MiddleManager = require("middle.MiddleManager")
  7. local AnotherWorldBattleDB = require("anotherWorldBattle.AnotherWorldBattleDB")
  8. local AnotherWorldBattleDefine = require("anotherWorldBattle.AnotherWorldBattleDefine")
  9. local CombatDefine = require("combat.CombatDefine")
  10. local AnotherWorldBattleConfig = require("excel.anotherWorldBattle")
  11. -- 当天处于星期几(星期六为7, 星期天为1)
  12. local wDay
  13. local function updateWDay()
  14. wDay = Util.getWeekDay()
  15. end
  16. local function getTodayStartTime()
  17. local now = os.time()
  18. return Util.getDayStartTime(now)
  19. end
  20. local function isOpen()
  21. if not wDay then
  22. updateWDay()
  23. end
  24. if wDay > AnotherWorldBattleDefine.AB_OPEN_WDAY_AREA[2] and wDay < AnotherWorldBattleDefine.AB_OPEN_WDAY_AREA[1] then
  25. return false
  26. end
  27. local now = os.time()
  28. local toDayStartTime = getTodayStartTime()
  29. if wDay == AnotherWorldBattleDefine.AB_OPEN_WDAY_AREA[1] and now < (toDayStartTime + AnotherWorldBattleDefine.AB_START_SEC) then
  30. return false
  31. end
  32. -- 本函数中, 为了处理发奖, 周四23点过后不算结束
  33. -- if wDay == AnotherWorldBattleDefine.OPEN_WDAY_AREA[2] and now > (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  34. -- return false
  35. -- end
  36. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  37. if lastRoundStartTime ~= 0 then
  38. local diffDays = Util.diffDay(lastRoundStartTime)
  39. -- <= 6天说明处于本轮活动时间范围内, > 6 且 < 9 说明处于活动结束~新一轮活动未开启的时间段
  40. if diffDays > AnotherWorldBattleDefine.AB_OPEN_DAYS and diffDays <= AnotherWorldBattleDefine.AB_SUB_DAY then
  41. return false
  42. end
  43. end
  44. return true
  45. end
  46. local function isRunning()
  47. if not isOpen() then
  48. return false
  49. end
  50. local now = os.time()
  51. local toDayStartTime = getTodayStartTime()
  52. if wDay == AnotherWorldBattleDefine.AB_OPEN_WDAY_AREA[2] and now > (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  53. return false
  54. end
  55. return true
  56. end
  57. -- 进入新一轮的处理
  58. local function newRoundHandle()
  59. local stage = AnotherWorldBattleDB.GetStage()
  60. -- 上一轮奖励没有发放, 在新一轮开始时, 先发奖
  61. if stage == AnotherWorldBattleDefine.AB_STATE_AWARD then
  62. IssueRewardManager()
  63. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  64. end
  65. -- 重置数据
  66. AnotherWorldBattleDB.ResetData()
  67. -- 更新状态
  68. AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_JOIN)
  69. -- 更新活动开始时间
  70. local now = os.time()
  71. AnotherWorldBattleDB.UpdateLastRoundStartTime(now)
  72. -- 通知所有普通服, 新一轮活动开启了
  73. ActOpen(now)
  74. end
  75. -- 进入新一轮报名阶段检测
  76. local function joinStageCheck()
  77. local stage = AnotherWorldBattleDB.GetStage()
  78. if stage ~= AnotherWorldBattleDefine.AB_STATE_END and stage ~= AnotherWorldBattleDefine.AB_STATE_AWARD then
  79. return
  80. end
  81. -- local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  82. -- local diffDays = Util.diffDay(lastRoundStartTime)
  83. -- 上一轮活动结束时间为0 或 当前时间 - 上一轮活动开启时间 > 9天, 开启新一轮活动
  84. -- if lastRoundStartTime == 0 or diffDays > AnotherWorldBattleDefine.AB_SUB_DAY then
  85. newRoundHandle()
  86. -- end
  87. end
  88. -- 公会分组算法
  89. local function groupingAlgorithm(unionArray, len)
  90. local usedTb = {}
  91. local groupArr = {}
  92. local function getNextUnuseIdx(nowIdx)
  93. for i=nowIdx, len do
  94. if not usedTb[i] then
  95. return i
  96. end
  97. end
  98. end
  99. local function checkRandIdx(startIdx, endIdx)
  100. local tbl= {}
  101. for i= startIdx, endIdx do
  102. if not usedTb[i] then
  103. table.insert(tbl, i)
  104. end
  105. end
  106. return tbl
  107. end
  108. local currentIdx = 1
  109. local selectNum, cnt = 0, 0
  110. while currentIdx <= len do
  111. currentIdx = getNextUnuseIdx(currentIdx)
  112. if not currentIdx then
  113. break
  114. end
  115. local startIdx = currentIdx + 1
  116. local endIdx = math.min(currentIdx + 19, len)
  117. groupArr[#groupArr+1] = { unionArray[currentIdx] }
  118. selectNum = selectNum + 1
  119. cnt = math.min(AnotherWorldBattleDefine.AB_GROUP_UNION_NUM - 1, len - selectNum )
  120. for i=1, cnt do
  121. local correctTb = checkRandIdx(startIdx, endIdx)
  122. if #correctTb == 0 then break end
  123. local matchIdx = correctTb[math.random(1, #correctTb)]
  124. usedTb[currentIdx] = true
  125. usedTb[matchIdx] = true
  126. table.insert(groupArr[#groupArr], unionArray[matchIdx])
  127. selectNum = selectNum + 1
  128. end
  129. currentIdx = currentIdx + 1
  130. end
  131. return groupArr
  132. end
  133. -- 分组
  134. local function grouping()
  135. local function genUnionIdArray(sourceUnionArr, targetUnionArr)
  136. for k, v in ipairs(sourceUnionArr) do
  137. targetUnionArr[k] = v.unionId
  138. end
  139. end
  140. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  141. if not joinUnionArr then
  142. -- 没有公会参加, 本轮活动结束
  143. local now = os.time()
  144. -- AnotherWorldBattleDB.UpdateLastRoundStartTime(now)
  145. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  146. end
  147. local len = #joinUnionArr
  148. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  149. table.sort(joinUnionArr, function (a, b)
  150. return a.power > b.power
  151. end)
  152. end
  153. local unionIdArr = {}
  154. genUnionIdArray(joinUnionArr, unionIdArr)
  155. local newGroupArray
  156. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  157. newGroupArray = groupingAlgorithm(unionIdArr, len)
  158. else
  159. newGroupArray = { unionIdArr }
  160. end
  161. AnotherWorldBattleDB.UpdateGroupArray(newGroupArray)
  162. end
  163. -- 给各个分组中的公会随机分配出生点
  164. local function randomBaseCity()
  165. local baseCityIdArr = {}
  166. for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do
  167. if cityCfg.isBaseCity == 1 then
  168. baseCityIdArr[#baseCityIdArr+1] = cityId
  169. end
  170. end
  171. local now = os.time()
  172. local groupArray = AnotherWorldBattleDB.GetGroupArray()
  173. for _, unionIdArr in ipairs(groupArray) do
  174. -- 乱序
  175. table.shuffle(baseCityIdArr)
  176. for i, unionId in ipairs(unionIdArr) do
  177. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  178. if union then
  179. union.baseCityId = baseCityIdArr[i]
  180. union.baseCityStartTime = now
  181. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  182. end
  183. end
  184. end
  185. end
  186. -- 获取自己公会所在分组的Id
  187. local function getMyUnionGourpId(myUnionId)
  188. local groupId = AnotherWorldBattleDB.GetUnionGroupId(myUnionId)
  189. return groupId
  190. end
  191. -- 获取自己公会/所在区服第一公会所在分组Id
  192. local function getGroupId(myUnionId, myServerId)
  193. local groupId = getMyUnionGourpId(myUnionId)
  194. if not groupId then
  195. groupId = AnotherWorldBattleDB.GetGroupIdByServerId(myServerId)
  196. end
  197. return groupId
  198. end
  199. -- 获取排名
  200. local function getRank(rankArr, targetUuid, isUnion)
  201. local rank = 9999
  202. if not rankArr or not targetUuid then
  203. return rank
  204. end
  205. for rankIdx, rankInfo in ipairs(rankArr) do
  206. local rankerUuid = isUnion and rankInfo.guildId or rankInfo.playerId
  207. if rankerUuid == targetUuid then
  208. rank = rankIdx
  209. break
  210. end
  211. end
  212. return rank
  213. end
  214. -- 统计公会的占领情况, 用于发放奖励
  215. local function genUnionOccupyInfo(unionId, union, playerListData)
  216. local occupyTb = {
  217. occupyCityArr = {}, -- 当前还占领的城池列表
  218. point2CityIdArr = {}, -- 曾占领/当前还占领的据点的所属城池Id列表
  219. occuoyPointNum = 0,
  220. playerInfoArr = {},
  221. unionId = unionId,
  222. unionRank = 0,
  223. }
  224. local now = os.time()
  225. local t1 = occupyTb.occupyCityArr
  226. local t2 = occupyTb.point2CityIdArr
  227. t1[#t1+1] = union.baseCityId
  228. for cityId, cityIno in pairs(union.occupCityList or {}) do
  229. if cityIno.isOccupy then
  230. t1[#t1+1] = cityId
  231. end
  232. for _, pointInfo in pairs(cityIno.occupyPointList) do
  233. if pointInfo.playerUuid then
  234. if not cityIno.isOccupy then
  235. occupyTb.occuoyPointNum = occupyTb.occuoyPointNum + 1
  236. end
  237. -- 更新据点最新占领时间段的结束时间
  238. local occupyTimeArr = pointInfo.occupyTimeArr
  239. local lastTimeTb = occupyTimeArr[#occupyTimeArr]
  240. if #lastTimeTb == 1 then
  241. occupyTimeArr[#occupyTimeArr][2] = now
  242. end
  243. end
  244. t2[#t2+1] = {cityId, pointInfo.occupyTimeArr}
  245. end
  246. end
  247. -- 出生点算5个据点
  248. local baseCiyuTimeArr = { {union.baseCityStartTime, union.baseCityEndTime or now } }
  249. for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
  250. t2[#t2+1] = {union.baseCityId, baseCiyuTimeArr}
  251. end
  252. local groupId = getMyUnionGourpId(unionId)
  253. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  254. local playerRankList = AnotherWorldBattleDB.GetPlayerRankList(groupId)
  255. occupyTb.unionRank = getRank(unionRankList, unionId, true)
  256. for playerUuid, playerInfo in pairs(playerListData) do
  257. if playerInfo.unionId == unionId then
  258. local playerRank = getRank(playerRankList, playerUuid)
  259. occupyTb.playerInfoArr[#occupyTb.playerInfoArr+1] = {playerUuid, playerRank}
  260. end
  261. end
  262. -- 防止本次没有正常发放奖励, 后续补发时, 结束时间异常的情况
  263. union.baseCityEndTime = now
  264. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  265. return occupyTb
  266. end
  267. -- 发奖
  268. local function issueReward(sourceServerId, occupyInfo)
  269. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ISSUEREWARD
  270. msgData.unionOccupyInfo = occupyInfo
  271. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  272. InnerMsg.sendMsg(fd, msgData)
  273. end
  274. -- 奖励发放管理函数
  275. function IssueRewardManager()
  276. local delay_sec = 0
  277. local unionList = AnotherWorldBattleDB.GetUnionList()
  278. local playerListData = AnotherWorldBattleDB.GetPlayerList()
  279. for unionId, union in pairs(unionList) do
  280. local occupyInfo = genUnionOccupyInfo(unionId, union, playerListData)
  281. delay_sec = delay_sec + 5
  282. Timer.addLater(delay_sec, issueReward, union.serverId, occupyInfo)
  283. end
  284. end
  285. -- 检测活动各阶段状态及相关处理
  286. local function timedStageHandle()
  287. -- 处于报名阶段
  288. if table.find(AnotherWorldBattleDefine.AB_JOIN_WDAY_AREA, wDay) then
  289. joinStageCheck()
  290. end
  291. -- if wDay == AnotherWorldBattleDefine.AB_JOIN_WDAY then
  292. -- joinStageCheck()
  293. -- end
  294. local now = os.time()
  295. -- 报名阶段 -> 战斗阶段
  296. if wDay >= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[1] and wDay <= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] then
  297. local stage = AnotherWorldBattleDB.GetStage()
  298. local toDayStartTime = getTodayStartTime()
  299. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_START_SEC) then
  300. -- 分组
  301. grouping()
  302. -- 给各个分组中的公会随机分配出生点
  303. randomBaseCity()
  304. -- 改为战斗阶段
  305. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_BATTLE)
  306. end
  307. end
  308. -- 战斗阶段 -> 发奖阶段
  309. local toDayStartTime = getTodayStartTime()
  310. if wDay == AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  311. local stage = AnotherWorldBattleDB.GetStage()
  312. if stage == AnotherWorldBattleDefine.AB_STATE_BATTLE or stage == AnotherWorldBattleDefine.AB_STATE_AWARD then
  313. -- 改为发奖阶段
  314. AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_AWARD)
  315. -- 开始发奖
  316. IssueRewardManager()
  317. -- 改为结束阶段
  318. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  319. end
  320. end
  321. end
  322. function oneMin()
  323. if _G.is_middle ~= true then return end
  324. if not isOpen() then
  325. return
  326. end
  327. -- 与 onHour() 处理错开
  328. if Util.getMin() == 0 then
  329. return
  330. end
  331. timedStageHandle()
  332. end
  333. function onHour(hour)
  334. if _G.is_middle ~= true then return end
  335. if hour == 0 or not wDay then
  336. updateWDay()
  337. end
  338. if not isOpen() then
  339. return
  340. end
  341. timedStageHandle()
  342. end
  343. ------------------------------------C2N---------------------------------------------------
  344. -- 错误提示
  345. local function errTips(sourceServerId, playerUuid, errCode)
  346. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_TIPS
  347. msgData.playerUuid = playerUuid
  348. msgData.errCode = errCode
  349. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  350. InnerMsg.sendMsg(fd, msgData)
  351. end
  352. -- 通知玩家,据点被别的玩家占领了
  353. local function pointLose(sourceServerId, playerUuid, loseCityId, losePointIdx)
  354. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_LOSE
  355. msgData.playerUuid = playerUuid
  356. msgData.loseCityId = loseCityId
  357. msgData.losePointIdx = losePointIdx
  358. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  359. InnerMsg.sendMsg(fd, msgData)
  360. end
  361. -- 通知所有普通服, 活动开启
  362. function ActOpen(ti)
  363. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ACT_START
  364. msgData.startTime = ti or os.time()
  365. local fdList = MiddleManager.MiddleManager_GetAllFD()
  366. for _, fd in pairs(fdList) do
  367. InnerMsg.sendMsg(fd, msgData)
  368. end
  369. end
  370. ------------------------------------N2C---------------------------------------------------
  371. -- 统计单个服公会报名数量
  372. local function calcSvrUnionJoinNum(serverId)
  373. local num = 0
  374. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  375. if not joinUnionArr then
  376. return num
  377. end
  378. for _, unionInfo in ipairs(joinUnionArr) do
  379. if unionInfo.serverId == serverId then
  380. num = num + 1
  381. end
  382. end
  383. return num
  384. end
  385. -- 计算多个英雄的总战力
  386. local function calcHerosPower(heroArr)
  387. local power = 0
  388. for _, v in ipairs(heroArr) do
  389. power = power + v.heroPower
  390. end
  391. return power
  392. end
  393. -- 统计公会占领不同等级的城池数量
  394. local function calcOccupyCityNum(occupCityList, cityLv)
  395. if not occupCityList then
  396. return 0
  397. end
  398. local num = 0
  399. for cityId, occupyInfo in pairs(occupCityList) do
  400. if occupyInfo.isOccupy then
  401. local cityCfg = AnotherWorldBattleConfig.city[cityId]
  402. if cityCfg.cityLv == cityLv then
  403. num = num + 1
  404. end
  405. end
  406. end
  407. return num
  408. end
  409. -- 统计公会占领的据点所属城池Id数组
  410. local function calcOccupyPointArr(occupCityList)
  411. local pointInfoArr = {}
  412. if not occupCityList then
  413. return pointInfoArr
  414. end
  415. for cityId, occupyInfo in pairs(occupCityList) do
  416. for _, pointInfo in pairs(occupyInfo.occupyPointList) do
  417. if pointInfo.playerUuid then
  418. pointInfoArr[#pointInfoArr+1] = cityId
  419. end
  420. end
  421. end
  422. return pointInfoArr
  423. end
  424. -- 获取公会占领的城池列表
  425. local function getUnionOccupyArr(unionId)
  426. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  427. if not union then
  428. return
  429. end
  430. local occupyCityArr = {}
  431. occupyCityArr[#occupyCityArr+1] = union.baseCityId
  432. for cityId, occupyInfo in pairs(union.occupCityList or {}) do
  433. if occupyInfo.isOccupy then
  434. occupyCityArr[#occupyCityArr+1] = cityId
  435. end
  436. end
  437. return occupyCityArr
  438. end
  439. -- 检查某个城池与公会占领的城池是否相邻
  440. local function isadJoin(cityIdArr, targetCityId)
  441. if not cityIdArr then
  442. return false
  443. end
  444. local targetCityCfg = AnotherWorldBattleConfig.city[targetCityId]
  445. if not targetCityCfg then
  446. return false
  447. end
  448. for _, cityId in ipairs(targetCityCfg.adJoinCityArr) do
  449. if table.find(cityIdArr, cityId) then
  450. return true
  451. end
  452. end
  453. return false
  454. end
  455. -- 统计玩家占据的据点数量
  456. local function calcPlayerOccupyPointNum(playerUuid)
  457. local playerOccupyPonitNum = 0
  458. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  459. if playerData and playerData.heroList then
  460. for _, pointList in pairs(playerData.heroList) do
  461. for _, _ in pairs(pointList) do
  462. playerOccupyPonitNum = playerOccupyPonitNum + 1
  463. end
  464. end
  465. end
  466. return playerOccupyPonitNum
  467. end
  468. -- 检查某个据点是否能被玩家挑战
  469. local function isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, playerUuid)
  470. -- 活动未开启
  471. if not isRunning() then
  472. return -1
  473. end
  474. -- 公会没有参加活动
  475. local groupId = getMyUnionGourpId(myUnionId)
  476. if not groupId then
  477. return -2
  478. end
  479. local tagetCityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  480. if not tagetCityData then
  481. return -3
  482. end
  483. local targetPointData = tagetCityData.pointArr[targetPointIdx]
  484. if not targetPointData then
  485. return -5
  486. end
  487. -- 自己占领了该据点
  488. if targetPointData.unionId and targetPointData.unionId == myUnionId and targetPointData.playerUuid == playerUuid then
  489. return 2
  490. end
  491. -- 城池已被本公会占领
  492. if tagetCityData.occupyUnion and tagetCityData.occupyUnion == myUnionId then
  493. return -4
  494. end
  495. -- 据点已被本公会占领
  496. if targetPointData.unionId and targetPointData.unionId == myUnionId then
  497. return -6
  498. end
  499. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  500. if not myUnionData then
  501. return -7
  502. end
  503. -- 城池是否相邻
  504. local occupyCityArr = getUnionOccupyArr(myUnionId)
  505. if not isadJoin(occupyCityArr, targetCityId) then
  506. return -8
  507. end
  508. return 1
  509. end
  510. -- 公会是否能报名
  511. local function isCanJoin(sourceServerId, myUnionId)
  512. if not isRunning() then
  513. return 2
  514. end
  515. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  516. for _, unionData in ipairs(joinUnionArr or {}) do
  517. if unionData.unionId == myUnionId then
  518. return 1
  519. end
  520. end
  521. local num = calcSvrUnionJoinNum(sourceServerId)
  522. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  523. return 2
  524. end
  525. return 0
  526. end
  527. -- 检查城池是否被己方公会完全占领
  528. local function isCompleteOccupy(cityData, myUnionId)
  529. for _, pointInfo in ipairs(cityData.pointArr) do
  530. if not pointInfo.unionId or pointInfo.unionId ~= myUnionId then
  531. return false
  532. end
  533. end
  534. return true
  535. end
  536. -- 获取本轮活动战斗阶段的开始时间
  537. local function getBattleStartTime()
  538. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  539. local openWDay = Util.getWeekDay(lastRoundStartTime)
  540. local subDay = 0
  541. if openWDay == AnotherWorldBattleDefine.AB_JOIN_WDAY_AREA[1] then
  542. subDay = 2
  543. else
  544. subDay = 1
  545. end
  546. local openStartTime = Util.getDayStartTime(lastRoundStartTime)
  547. local battleStartTime = openStartTime + subDay * 86400 + AnotherWorldBattleDefine.AB_START_SEC
  548. return battleStartTime
  549. end
  550. -- 获取玩家挑战次数相关数据
  551. local function getPlayerChallengeTimesInfo(playerUuid)
  552. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  553. -- if not playerData then
  554. -- return AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES, 0
  555. -- end
  556. local challengeTimes, lastTime = AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES, 0
  557. if playerData and playerData.lastTime then
  558. lastTime = playerData.lastTime
  559. challengeTimes = playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES
  560. else
  561. lastTime = getBattleStartTime()
  562. end
  563. local now = os.time()
  564. local subSex = now - lastTime
  565. if subSex >= AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC then
  566. local addTimes = math.floor(subSex / AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC)
  567. challengeTimes = math.min(challengeTimes + addTimes, AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_MAX_TIMES)
  568. lastTime = lastTime + addTimes * AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC
  569. if challengeTimes == AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_MAX_TIMES then
  570. lastTime = 0
  571. end
  572. if playerData then
  573. playerData.challengeTimes = challengeTimes
  574. playerData.lastTime = lastTime
  575. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  576. end
  577. end
  578. if not isRunning() then
  579. lastTime = 0
  580. end
  581. return challengeTimes, lastTime
  582. end
  583. -- 获取城池被占领状态
  584. local function getCityState(cityData)
  585. local state = 0
  586. if cityData.occupyUnion then
  587. state = 1
  588. return state
  589. end
  590. local occupyPlayerUuid
  591. for _, pointInfo in ipairs(cityData.pointArr) do
  592. if pointInfo.playerUuid then
  593. if not occupyPlayerUuid then
  594. occupyPlayerUuid = pointInfo.playerUuid
  595. end
  596. if pointInfo.playerUuid ~= occupyPlayerUuid then
  597. state = 2
  598. break
  599. end
  600. end
  601. end
  602. return state
  603. end
  604. -- 获取公会占领据点的数据
  605. local function getOccupPointData(unionId)
  606. local t2 = {}
  607. local unionData = AnotherWorldBattleDB.GetUnionData(unionId)
  608. if not unionData then
  609. return t2
  610. end
  611. for cityId, cityIno in pairs(unionData.occupCityList or {}) do
  612. for _, pointInfo in pairs(cityIno.occupyPointList) do
  613. t2[#t2+1] = {cityId, pointInfo.occupyTimeArr}
  614. end
  615. end
  616. -- 出生点算5个据点
  617. local baseCiyuTimeArr = { {unionData.baseCityStartTime, unionData.baseCityEndTime or now } }
  618. for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
  619. t2[#t2+1] = {unionData.baseCityId, baseCiyuTimeArr}
  620. end
  621. return t2
  622. end
  623. -- 增加士气条件之时间条件检测
  624. local function moraleCondTimeCheck()
  625. local now = os.time()
  626. local battleStartTime = getBattleStartTime()
  627. local moraleStartTime = battleStartTime + 86400
  628. return now >= moraleStartTime
  629. end
  630. -- 获取本公会占领据点排名
  631. local function getUnionRank(myUnionId)
  632. local myRank = 0
  633. local groupId = getMyUnionGourpId(myUnionId)
  634. if not groupId then
  635. return myRank
  636. end
  637. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  638. for rank, randkData in ipairs(unionRankList or {}) do
  639. if randkData.guildId == myUnionId then
  640. myRank = rank
  641. break
  642. end
  643. end
  644. return myRank
  645. end
  646. -- 查询状态
  647. function N2C_GetState_Req(msg)
  648. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GET_STATE
  649. msgData.playerUuid = msg.playerUuid
  650. msgData.systemState = 0
  651. msgData.joinState = 0
  652. msgData.isTips = msg.isTips or 0
  653. local stage = AnotherWorldBattleDB.GetStage()
  654. msgData.systemState = stage
  655. if stage == 0 then
  656. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  657. if lastRoundStartTime == 0 then
  658. msgData.systemState = 0
  659. else
  660. msgData.systemState = 3
  661. end
  662. end
  663. local sourceServerId = msg.sourceServerId
  664. local num = calcSvrUnionJoinNum(sourceServerId)
  665. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  666. msgData.joinState = 2
  667. end
  668. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  669. for _, unionData in ipairs(joinUnionArr or {}) do
  670. if unionData.unionId == msg.myUnionId then
  671. msgData.joinState = 1
  672. end
  673. end
  674. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  675. InnerMsg.sendMsg(fd, msgData)
  676. end
  677. -- 报名
  678. function N2C_Join_Req(msg)
  679. local sourceServerId = msg.sourceServerId
  680. local playerUuid = msg.playerUuid
  681. local unionInfo = msg.unionInfo
  682. local state = isCanJoin(sourceServerId, unionInfo.unionId)
  683. if state == 2 then
  684. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  685. elseif state == 1 then
  686. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_8)
  687. end
  688. -- 更新参赛列表数据
  689. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  690. joinUnionArr = joinUnionArr or {}
  691. joinUnionArr[#joinUnionArr+1] = {
  692. unionId = unionInfo.unionId,
  693. power = unionInfo.power,
  694. serverId = sourceServerId
  695. }
  696. AnotherWorldBattleDB.UpdateJoinUnionArr(joinUnionArr)
  697. -- 更新公会列表数据
  698. local newUnionData = {
  699. serverId = sourceServerId,
  700. power = unionInfo.power,
  701. name = unionInfo.name,
  702. }
  703. AnotherWorldBattleDB.UpdateUnionData(unionInfo.unionId, newUnionData)
  704. -- 通知报名成功
  705. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_JOIN
  706. msgData.playerUuid = playerUuid
  707. msgData.myUnionId = unionInfo.unionId
  708. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  709. InnerMsg.sendMsg(fd, msgData)
  710. end
  711. -- 查询所有城池信息
  712. function N2C_GetAllCity_Req(msg)
  713. local myUnionId = msg.myUnionId
  714. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ALLCITY_QUERY
  715. msgData.cityArr = {}
  716. msgData.playerUuid = msg.playerUuid
  717. msgData.myUnionBaseCityId = 0
  718. msgData.myOccupyCityArr = {}
  719. msgData.hasPointNum = 0
  720. msgData.challengeTimes = 0
  721. msgData.lastTime = 0
  722. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  723. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  724. if not groupId then
  725. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  726. local stage = AnotherWorldBattleDB.GetStage()
  727. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  728. return InnerMsg.sendMsg(fd, msgData)
  729. end
  730. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  731. end
  732. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  733. if not cityList then
  734. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  735. end
  736. local cityArrMsg = msgData.cityArr
  737. local unionList = AnotherWorldBattleDB.GetUnionList()
  738. msgData.myUnionBaseCityId = unionList[myUnionId] and unionList[myUnionId].baseCityId or 0
  739. for cityId, cityInfo in ipairs(cityList) do
  740. cityArrMsg[cityId] = { occupyPointNum = 0 }
  741. if cityInfo.occupyUnion then
  742. cityArrMsg[cityId].occupyUnionName = unionList[cityInfo.occupyUnion] and unionList[cityInfo.occupyUnion].name
  743. end
  744. for _, pointInfo in ipairs(cityInfo.pointArr) do
  745. if pointInfo.unionId and pointInfo.unionId == myUnionId then
  746. cityArrMsg[cityId].occupyPointNum = cityArrMsg[cityId].occupyPointNum + 1
  747. end
  748. end
  749. cityArrMsg[cityId].cityState = getCityState(cityInfo)
  750. end
  751. msgData.myOccupyCityArr = getUnionOccupyArr(myUnionId)
  752. msgData.hasPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  753. local challengeTimes, leftTime = getPlayerChallengeTimesInfo(msg.playerUuid)
  754. msgData.challengeTimes = challengeTimes
  755. msgData.lastTime = leftTime
  756. msgData.occupyPointData = getOccupPointData(myUnionId)
  757. InnerMsg.sendMsg(fd, msgData)
  758. end
  759. -- 查询某个城池的详细信息
  760. function N2C_GetCityDetailed_Req(msg)
  761. local myUnionId = msg.myUnionId
  762. local playerUuid = msg.playerUuid
  763. local targetCityId = msg.targetCityId
  764. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  765. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_CITYDETAILED_QUERY
  766. msgData.playerUuid = playerUuid
  767. msgData.targetCityId = targetCityId
  768. msgData.myUnionOccupyArr = {}
  769. msgData.pointArr = {}
  770. msgData.gatherState = 0
  771. msgData.gatherTime = -1
  772. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  773. if not groupId then
  774. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  775. local stage = AnotherWorldBattleDB.GetStage()
  776. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  777. return InnerMsg.sendMsg(fd, msgData)
  778. end
  779. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  780. end
  781. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  782. if not cityData then
  783. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  784. end
  785. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  786. local occupCityList = union and union.occupCityList
  787. local myUnionOccupyArr = calcOccupyPointArr(occupCityList)
  788. if union then
  789. myUnionOccupyArr[#myUnionOccupyArr+1] = union.baseCityId
  790. end
  791. msgData.myUnionOccupyArr = myUnionOccupyArr
  792. local pointArrMsg = msgData.pointArr
  793. for pointIdx, occupyInfo in ipairs(cityData.pointArr) do
  794. pointArrMsg[pointIdx] = {}
  795. local state = isCanChallengePoint(targetCityId, pointIdx, myUnionId, playerUuid)
  796. pointArrMsg[pointIdx].state = state < 0 and 0 or state
  797. if occupyInfo.unionId and occupyInfo.playerUuid then
  798. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(occupyInfo.unionId)
  799. local occupyPlayerData = AnotherWorldBattleDB.GetPlayerData(occupyInfo.playerUuid)
  800. if occupyUnionData and occupyPlayerData then
  801. pointArrMsg[pointIdx].occupyUnionName = occupyUnionData.name
  802. pointArrMsg[pointIdx].occupyPlayerName = occupyPlayerData.name
  803. pointArrMsg[pointIdx].power = 0
  804. local heroList = occupyPlayerData.heroList
  805. if heroList and heroList[targetCityId] and heroList[targetCityId][pointIdx] then
  806. pointArrMsg[pointIdx].power = calcHerosPower(heroList[targetCityId][pointIdx])
  807. end
  808. end
  809. end
  810. end
  811. local isOk = isRunning()
  812. if isOk then
  813. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  814. local occupyCityArr = getUnionOccupyArr(myUnionId)
  815. isOk = isadJoin(occupyCityArr, targetCityId)
  816. if isOk then
  817. if not cityData.occupyUnion or cityData.occupyUnion ~= myUnionId then
  818. if not myUnionData.gatherInfo then
  819. msgData.gatherState = 1
  820. else
  821. if myUnionData.gatherInfo.gatherCity == targetCityId then
  822. msgData.gatherState = 2
  823. msgData.gatherTime = myUnionData.gatherInfo.gatherTime
  824. else
  825. local now = os.time()
  826. if now - myUnionData.gatherInfo.gatherTime >= AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  827. msgData.gatherState = 1
  828. end
  829. end
  830. end
  831. end
  832. end
  833. end
  834. InnerMsg.sendMsg(fd, msgData)
  835. end
  836. -- 查询某个据点信息
  837. function N2C_GetPointDetailed_Req(msg)
  838. local myUnionId = msg.myUnionId
  839. local targetCityId = msg.targetCityId
  840. local targetPointIdx = msg.targetPointIdx
  841. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  842. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINTDETAILED_QUERY
  843. msgData.playerUuid = msg.playerUuid
  844. msgData.targetCityId = targetCityId
  845. msgData.targetPointIdx = targetPointIdx
  846. msgData.pointInfo = {}
  847. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  848. if not groupId then
  849. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  850. local stage = AnotherWorldBattleDB.GetStage()
  851. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  852. return InnerMsg.sendMsg(fd, msgData)
  853. end
  854. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  855. end
  856. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  857. if not cityData then
  858. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  859. end
  860. local pointData = cityData.pointArr[targetPointIdx]
  861. if not pointData then
  862. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  863. end
  864. local pointInfoMsg = msgData.pointInfo
  865. if pointData.unionId and pointData.playerUuid then
  866. local playerData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  867. if not playerData then
  868. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  869. end
  870. pointInfoMsg.name = playerData.name
  871. pointInfoMsg.head = playerData.head
  872. pointInfoMsg.headFrame = playerData.headFrame
  873. pointInfoMsg.defLv = playerData.lv
  874. local targetHeroArr = playerData.heroList[targetCityId][targetPointIdx]
  875. if not targetHeroArr then
  876. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  877. end
  878. pointInfoMsg.power = calcHerosPower(targetHeroArr)
  879. pointInfoMsg.heroArr = {}
  880. for i, heroInfo in ipairs(targetHeroArr) do
  881. pointInfoMsg.heroArr[i] = {
  882. heroBody = heroInfo.heroBody,
  883. heroStar = heroInfo.heroStar,
  884. heroLv = heroInfo.heroLevel,
  885. heroCamp = heroInfo.heroCamp,
  886. heroIcon = heroInfo.heroIcon,
  887. heroId = heroInfo.heroId,
  888. heroQuality = heroInfo.heroQuality,
  889. }
  890. end
  891. end
  892. local state = isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, msg.playerUuid)
  893. pointInfoMsg.state = state < 0 and 0 or state
  894. InnerMsg.sendMsg(fd, msgData)
  895. end
  896. -- 查询公会出生点信息
  897. function N2C_GetBaseCity_Req(msg)
  898. local myUnionId = msg.myUnionId
  899. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  900. if not groupId then
  901. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  902. end
  903. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_BASECITY_QUERY
  904. msgData.playerUuid = msg.playerUuid
  905. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  906. if not union then
  907. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  908. end
  909. local baseCityInfo = {}
  910. msgData.baseCityInfo = baseCityInfo
  911. baseCityInfo.cityId = union.baseCityId
  912. local pointInfoArr = calcOccupyPointArr(union.occupCityList)
  913. baseCityInfo.occupyPointNum = #pointInfoArr
  914. baseCityInfo.myUnionOccupyArr = calcOccupyPointArr(union.occupCityList)
  915. baseCityInfo.myUnionOccupyArr[#baseCityInfo.myUnionOccupyArr+1] = baseCityInfo.cityId
  916. baseCityInfo.occupyCityLv2Num = calcOccupyCityNum(union.occupCityList, 2)
  917. baseCityInfo.occupyCityLv3Num = calcOccupyCityNum(union.occupCityList, 3)
  918. baseCityInfo.occupyCityLv4Num = calcOccupyCityNum(union.occupCityList, 4)
  919. baseCityInfo.occupyCityLv5Num = calcOccupyCityNum(union.occupCityList, 5)
  920. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  921. InnerMsg.sendMsg(fd, msgData)
  922. end
  923. -- 查询玩家占领的所有据点信息
  924. function N2C_GetPlayerOccupyPoint_Req(msg)
  925. local myUnionId = msg.myUnionId
  926. local playerUuid = msg.playerUuid
  927. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  928. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYEROCCUPYPOINT_QUERY
  929. msgData.playerUuid = playerUuid
  930. msgData.occupyPointArr = {}
  931. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  932. if not union then
  933. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  934. local stage = AnotherWorldBattleDB.GetStage()
  935. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  936. return InnerMsg.sendMsg(fd, msgData)
  937. end
  938. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  939. end
  940. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  941. if not playerData then
  942. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  943. end
  944. local heroList = playerData.heroList
  945. if not heroList then
  946. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  947. end
  948. local occupyPointArrMsg = msgData.occupyPointArr
  949. for cityId, pointList in pairs(heroList) do
  950. for pointIdx, heroInfoArr in pairs(pointList) do
  951. occupyPointArrMsg[#occupyPointArrMsg+1] = {
  952. cityId = cityId,
  953. pointIdx = pointIdx,
  954. power = calcHerosPower(heroInfoArr),
  955. heroArr = {},
  956. }
  957. local targetHeroArr = occupyPointArrMsg[#occupyPointArrMsg].heroArr
  958. for _, heroInfo in ipairs(heroInfoArr) do
  959. targetHeroArr[#targetHeroArr+1] = {
  960. heroBody = heroInfo.heroBody,
  961. heroStar = heroInfo.heroStar,
  962. heroLv = heroInfo.heroLevel,
  963. heroCamp = heroInfo.heroCamp,
  964. heroIcon = heroInfo.heroIcon,
  965. heroId = heroInfo.heroId,
  966. heroQuality = heroInfo.heroQuality
  967. }
  968. end
  969. end
  970. end
  971. InnerMsg.sendMsg(fd, msgData)
  972. end
  973. -- 查询公会排行榜
  974. function N2C_GetUnionRank_Req(msg)
  975. local myUnionId = msg.myUnionId
  976. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  977. if not groupId then
  978. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  979. end
  980. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  981. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UNIONRANK_QUERY
  982. msgData.playerUuid = msg.playerUuid
  983. msgData.myUnionRank = 0
  984. msgData.unionRankArr = {}
  985. for rank, randkData in ipairs(unionRankList) do
  986. msgData.unionRankArr[rank] = {
  987. name = randkData.name,
  988. power = randkData.power,
  989. cityNum = randkData.occupyCityNum,
  990. pointNum = randkData.occupyPointNum,
  991. }
  992. if randkData.guildId == myUnionId then
  993. msgData.myUnionRank = rank
  994. end
  995. end
  996. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  997. InnerMsg.sendMsg(fd, msgData)
  998. end
  999. -- 查询玩家排行榜
  1000. function N2C_GetPlayerRank_Req(msg)
  1001. local myUnionId = msg.myUnionId
  1002. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  1003. if not groupId then
  1004. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1005. end
  1006. local playerRankList = AnotherWorldBattleDB.GetPlayerRankList(groupId)
  1007. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYERRANK_QUERY
  1008. msgData.playerUuid = msg.playerUuid
  1009. msgData.myRank = 0
  1010. msgData.playerRankArr = {}
  1011. for rank, randkData in ipairs(playerRankList) do
  1012. if rank > AnotherWorldBattleDefine.AB_RANK_MAX_NUM then
  1013. break
  1014. end
  1015. msgData.playerRankArr[rank] = {
  1016. name = randkData.name,
  1017. power = randkData.power,
  1018. pointNum = randkData.pointNum,
  1019. pointWeight = randkData.pointAllWeight,
  1020. }
  1021. if randkData.playerId == msg.playerUuid then
  1022. msgData.myRank = rank
  1023. end
  1024. end
  1025. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1026. InnerMsg.sendMsg(fd, msgData)
  1027. end
  1028. -- 集结/取消集结
  1029. function N2C_Gather_Req(msg)
  1030. local sourceServerId = msg.sourceServerId
  1031. local playerUuid = msg.playerUuid
  1032. local myUnionId = msg.myUnionId
  1033. if not isRunning() then
  1034. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  1035. end
  1036. local groupId = getMyUnionGourpId(myUnionId)
  1037. if not groupId then
  1038. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1039. end
  1040. local targetCityId = msg.targetCityId
  1041. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1042. if not myUnionData then
  1043. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1044. end
  1045. local gatherInfo = myUnionData.gatherInfo
  1046. if msg.opType == 1 then --集结
  1047. -- 集结冷却时间判断
  1048. local now = os.time()
  1049. if gatherInfo then
  1050. local gatherTime = gatherInfo.gatherTime
  1051. if now - gatherTime < AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  1052. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_6)
  1053. end
  1054. end
  1055. -- 城池是否相邻判断
  1056. local occupCityyArr = getUnionOccupyArr(myUnionId)
  1057. if not isadJoin(occupCityyArr, targetCityId) then
  1058. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_7)
  1059. end
  1060. myUnionData.gatherInfo = myUnionData.gatherInfo or {}
  1061. myUnionData.gatherInfo.gatherTime = now
  1062. myUnionData.gatherInfo.gatherCity = targetCityId
  1063. else
  1064. -- 取消集结
  1065. if not gatherInfo then
  1066. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  1067. end
  1068. if targetCityId ~= gatherInfo.gatherCity then
  1069. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  1070. end
  1071. gatherInfo.gatherCity = 0
  1072. end
  1073. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1074. -- 推送城池数据给客户端,刷新界面
  1075. N2C_GetCityDetailed_Req(msg)
  1076. -- 发给本地服, 用于在公会聊天频道生成链接
  1077. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GATHER
  1078. msgData.playerUuid = msg.playerUuid
  1079. msgData.targetCityId = targetCityId
  1080. msgData.opType = msg.opType
  1081. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1082. InnerMsg.sendMsg(fd, msgData)
  1083. end
  1084. -- 查询据点是否可以被挑战
  1085. function N2C_TryChallengdePoint_Req(msg)
  1086. local playerUuid = msg.playerUuid
  1087. local myUnionId = msg.myUnionId
  1088. local targetCityId = msg.targetCityId
  1089. local targetPointIdx = msg.targetPointIdx
  1090. local pChallengeTimes = getPlayerChallengeTimesInfo(playerUuid)
  1091. if pChallengeTimes <= 0 then
  1092. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_12)
  1093. end
  1094. local state = isCanChallengePoint(targetCityId, msg.targetPointIdx, msg.myUnionId, playerUuid)
  1095. if state ~= 1 then
  1096. return
  1097. end
  1098. local occupyPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  1099. if occupyPointNum >= AnotherWorldBattleDefine.AB_PLAYER_OCCUPY_POINT_MAX_NUM then
  1100. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_10)
  1101. end
  1102. local now = os.time()
  1103. local groupId = getMyUnionGourpId(myUnionId)
  1104. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1105. local targetPointData = cityData.pointArr[targetPointIdx]
  1106. -- 检查据点是否有人挑战
  1107. local pointBattleTime = targetPointData.battleTime
  1108. if pointBattleTime and now - pointBattleTime < AnotherWorldBattleDefine.AB_POINT_BATTLE_TIME then
  1109. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_11)
  1110. end
  1111. -- 防守方
  1112. local pointOccupyInfo = {targetCityId = targetCityId, targetPointIdx = targetPointIdx}
  1113. if targetPointData.unionId then
  1114. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  1115. pointOccupyInfo.occupySrvId = occupyUnionData.serverId
  1116. pointOccupyInfo.occupyPlayerUuid = targetPointData.playerUuid
  1117. -- 防守方的士气信息
  1118. if occupyUnionData.moraleData then
  1119. if occupyUnionData.moraleData.moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1120. pointOccupyInfo.defMoraleAttrIdx = occupyUnionData.moraleData.moraleAttrIdx
  1121. end
  1122. end
  1123. end
  1124. -- 进攻方集结信息
  1125. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1126. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1127. pointOccupyInfo.isGather = 1
  1128. end
  1129. -- 进攻方士气信息
  1130. if myUnionData.moraleData then
  1131. if myUnionData.moraleData.moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1132. pointOccupyInfo.atkMoraleAttrIdx = myUnionData.moraleData.moraleAttrIdx
  1133. end
  1134. end
  1135. -- 更新据点被挑战时间
  1136. targetPointData.battleTime = now
  1137. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1138. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_ISCAN_CHALLENGE
  1139. msgData.playerUuid = playerUuid
  1140. msgData.pointInfo = pointOccupyInfo
  1141. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1142. InnerMsg.sendMsg(fd, msgData)
  1143. end
  1144. -- 玩家挑战据点胜利
  1145. local function challenge_Win(msg)
  1146. local playerUuid = msg.playerUuid
  1147. local myUnionId = msg.myUnionId
  1148. local targetCityId = msg.targetCityId
  1149. local targetPointIdx = msg.targetPointIdx
  1150. local playerShowInfo = msg.playerShowInfo
  1151. if not isRunning() then
  1152. return
  1153. end
  1154. local groupId = getMyUnionGourpId(myUnionId)
  1155. if not groupId then
  1156. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1157. end
  1158. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1159. if not myUnionData then
  1160. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1161. end
  1162. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1163. local pointData = cityData.pointArr[targetPointIdx]
  1164. local now = os.time()
  1165. local cityNum, pointNum = 0, 1
  1166. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1167. -- 如果城池之前被占领了, 则删除占领城池的公会Id
  1168. if cityData.occupyUnion then
  1169. cityData.occupyUnion = nil
  1170. end
  1171. -- 防守方是真实玩家
  1172. if pointData.unionId and pointData.playerUuid then
  1173. -- 更新防守方公会数据
  1174. local defUnionData = AnotherWorldBattleDB.GetUnionData(pointData.unionId)
  1175. local defOccupCityList = defUnionData.occupCityList
  1176. local defTargerCityData = defOccupCityList[targetCityId]
  1177. -- 更新防守方公会对该据点的占领状态和最后占领时间
  1178. if defTargerCityData and defTargerCityData.occupyPointList and defTargerCityData.occupyPointList[targetPointIdx] then
  1179. local defTargetPointData = defTargerCityData.occupyPointList[targetPointIdx]
  1180. if defTargetPointData then
  1181. -- 删除占领该据点的玩家uuid
  1182. defTargetPointData.playerUuid = nil
  1183. -- 更新对该据点最后占领时间段的结束时间
  1184. if defTargetPointData.occupyTimeArr then
  1185. local lastOccupyTimeTb = defTargetPointData.occupyTimeArr[#defTargetPointData.occupyTimeArr]
  1186. lastOccupyTimeTb[2] = now
  1187. end
  1188. end
  1189. end
  1190. -- 删除防守方玩家的防守阵容数据
  1191. local defPlayData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  1192. if defPlayData then
  1193. defPlayData.heroList[targetCityId][targetPointIdx] = nil
  1194. AnotherWorldBattleDB.UpdatePlayerData(pointData.playerUuid, defPlayData)
  1195. end
  1196. -- 如果防守方公会之前占领了城池, 则修改占领状态
  1197. if defTargerCityData and defTargerCityData.isOccupy then
  1198. defTargerCityData.isOccupy = false
  1199. cityNum = -1
  1200. end
  1201. -- 更新防守方公会的排行榜数据
  1202. AnotherWorldBattleDB.UpdateUnionRankList(groupId, pointData.unionId, cityNum, pointNum)
  1203. -- 更新防守方玩家的排行榜数据
  1204. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, pointData.playerUuid, -pointNum, -pointWeight)
  1205. -- 通知玩家
  1206. pointLose(defUnionData.serverId, pointData.playerUuid, targetCityId, targetPointIdx)
  1207. -- 更新防守方公会数据
  1208. AnotherWorldBattleDB.UpdateUnionData(pointData.unionId, defUnionData)
  1209. end
  1210. -- 更新据点最新占领者数据
  1211. pointData.unionId = myUnionId
  1212. pointData.playerUuid = playerUuid
  1213. -- 重置据点被挑战标识
  1214. pointData.battleTime = nil
  1215. -- 更新进攻方公会数据—— 据点占有者, 最新的据点占有时间
  1216. myUnionData.occupCityList = myUnionData.occupCityList or {}
  1217. local occupCityList = myUnionData.occupCityList
  1218. occupCityList[targetCityId] = occupCityList[targetCityId] or {}
  1219. occupCityList[targetCityId].occupyPointList = occupCityList[targetCityId].occupyPointList or {}
  1220. occupCityList[targetCityId].occupyPointList[targetPointIdx] = occupCityList[targetCityId].occupyPointList[targetPointIdx] or {}
  1221. local atkPointInfo = occupCityList[targetCityId].occupyPointList[targetPointIdx]
  1222. atkPointInfo.playerUuid = playerUuid
  1223. atkPointInfo.occupyTimeArr = atkPointInfo.occupyTimeArr or {}
  1224. atkPointInfo.occupyTimeArr[#atkPointInfo.occupyTimeArr+1] = { now }
  1225. --更新进攻者的展示数据
  1226. local atkPlayerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1227. atkPlayerData = atkPlayerData or {}
  1228. atkPlayerData.name = playerShowInfo.name
  1229. atkPlayerData.lv = playerShowInfo.lv
  1230. atkPlayerData.head = playerShowInfo.head
  1231. atkPlayerData.headFrame = playerShowInfo.headFrame
  1232. atkPlayerData.power = playerShowInfo.power
  1233. atkPlayerData.unionId = myUnionId
  1234. -- 更新挑战次数
  1235. -- atkPlayerData.challengeTimes = (atkPlayerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES) - 1
  1236. -- if not atkPlayerData.lastTime or atkPlayerData.lastTime == 0 then
  1237. -- atkPlayerData.lastTime = os.time()
  1238. -- end
  1239. local pChallengeTimes, pLastTime = getPlayerChallengeTimesInfo(playerUuid)
  1240. atkPlayerData.challengeTimes = pChallengeTimes - 1
  1241. if pLastTime <= 0 then
  1242. pLastTime = os.time()
  1243. end
  1244. atkPlayerData.lastTime = pLastTime
  1245. -- 增加防守阵容数据
  1246. atkPlayerData.heroList = atkPlayerData.heroList or {}
  1247. atkPlayerData.heroList[targetCityId] = atkPlayerData.heroList[targetCityId] or {}
  1248. atkPlayerData.heroList[targetCityId][targetPointIdx] = playerShowInfo.heroArr
  1249. cityNum = 0
  1250. -- 进攻方完全占领城池了
  1251. if isCompleteOccupy(cityData, myUnionId) then
  1252. -- 更新城池的占领公会
  1253. cityData.occupyUnion = myUnionId
  1254. -- 更新进攻方公会对该城池的占领状态
  1255. occupCityList[targetCityId].isOccupy = true
  1256. -- 如果占领的是发起集结的城池, 那么取消集结
  1257. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1258. myUnionData.gatherInfo.gatherCity = 0
  1259. end
  1260. cityNum = 1
  1261. end
  1262. -- 更新进攻方公会数据
  1263. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1264. -- 更新进攻方玩家数据
  1265. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, atkPlayerData)
  1266. -- 更新进攻方公会的排行榜数据
  1267. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1268. -- 更新进攻方玩家的排行榜数据
  1269. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, pointWeight, playerShowInfo.power)
  1270. -- 更新城池数据
  1271. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1272. end
  1273. -- 玩家挑战据点失败
  1274. local function challenge_Fail(msg)
  1275. local playerUuid = msg.playerUuid
  1276. local myUnionId = msg.myUnionId
  1277. local targetCityId = msg.targetCityId
  1278. local targetPointIdx = msg.targetPointIdx
  1279. local playerShowInfo = msg.playerShowInfo
  1280. local groupId = getMyUnionGourpId(myUnionId)
  1281. if not groupId then
  1282. return
  1283. end
  1284. -- 据点挑战结束
  1285. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1286. if cityData and cityData.pointArr[targetPointIdx] then
  1287. local targetPointData = cityData.pointArr[targetPointIdx]
  1288. targetPointData.battleTime = nil
  1289. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1290. end
  1291. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1292. playerData = playerData or {}
  1293. playerData.name = playerShowInfo.name
  1294. playerData.lv = playerShowInfo.lv
  1295. playerData.power = playerShowInfo.power
  1296. playerData.unionId = myUnionId
  1297. -- playerData.challengeTimes = (playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES) - 1
  1298. -- if not playerData.lastTime or playerData.lastTime == 0 then
  1299. -- playerData.lastTime = os.time()
  1300. -- end
  1301. local pChallengeTimes, pLastTime = getPlayerChallengeTimesInfo(playerUuid)
  1302. playerData.challengeTimes = pChallengeTimes - 1
  1303. if pLastTime <= 0 then
  1304. pLastTime = os.time()
  1305. end
  1306. playerData.lastTime = pLastTime
  1307. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1308. -- 加入个人排行榜
  1309. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1310. end
  1311. --挑战据点结束的处理函数
  1312. function N2C_PointChallengeEnd_Handle(msg)
  1313. if msg.challengeRes == CombatDefine.RESULT_WIN then
  1314. challenge_Win(msg)
  1315. else
  1316. challenge_Fail(msg)
  1317. end
  1318. end
  1319. -- 更新玩家数据
  1320. function N2C_UpdatePlayerData_Req(msg)
  1321. if not isRunning() then
  1322. return
  1323. end
  1324. local playerUuid = msg.playerUuid
  1325. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1326. if not playerData then
  1327. return
  1328. end
  1329. local groupId = getMyUnionGourpId(playerData.unionId)
  1330. if not groupId then
  1331. return
  1332. end
  1333. local isUpdate = false
  1334. for k,v in pairs(msg.updateData) do
  1335. if k == "power" or k == "name" then
  1336. isUpdate = true
  1337. end
  1338. playerData[k] = v
  1339. end
  1340. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1341. -- 如果是战力/名字更新, 更新玩家排行榜
  1342. if isUpdate then
  1343. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1344. end
  1345. end
  1346. -- 更新公会数据
  1347. function N2C_UpdateUnionData_Req(msg)
  1348. if not isRunning() then
  1349. return
  1350. end
  1351. local myUnionId = msg.myUnionId
  1352. local unionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1353. if not unionData then
  1354. return
  1355. end
  1356. local groupId = getMyUnionGourpId(myUnionId)
  1357. if not groupId then
  1358. return
  1359. end
  1360. local isUpdate = false
  1361. for k,v in pairs(msg.updateData) do
  1362. if k == "power" or k == "name" then
  1363. isUpdate = true
  1364. end
  1365. unionData[k] = v
  1366. end
  1367. AnotherWorldBattleDB.UpdateUnionData(myUnionId, unionData)
  1368. -- 如果是战力更新, 更新公会排行榜
  1369. if isUpdate then
  1370. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, 0, 0, unionData.power)
  1371. end
  1372. end
  1373. -- 更新玩家据点防守阵容数据
  1374. function N2C_UpdatePointLineup_Req(msg)
  1375. if not isRunning() then
  1376. return
  1377. end
  1378. local playerUuid = msg.playerUuid
  1379. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1380. if not playerData or not playerData.heroList then
  1381. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1382. end
  1383. local groupId = getMyUnionGourpId(playerData.unionId)
  1384. if not groupId then
  1385. return
  1386. end
  1387. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1388. local heroList = playerData.heroList
  1389. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1390. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1391. end
  1392. heroList[targetCityId][targetPointIdx] = msg.heroArr
  1393. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1394. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UPDATE_POINT_LINEIP
  1395. msgData.playerUuid = playerUuid
  1396. msgData.targetCityId = targetCityId
  1397. msgData.targetPointIdx = targetPointIdx
  1398. msgData.formation = msg.formation
  1399. msgData.heroList = msg.heroList
  1400. msgData.helpList = msg.helpList
  1401. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1402. InnerMsg.sendMsg(fd, msgData)
  1403. end
  1404. -- 更新玩家英雄数据
  1405. function N2C_UpdateHeroData_Req(msg)
  1406. if not isRunning() then
  1407. return
  1408. end
  1409. local playerUuid = msg.playerUuid
  1410. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1411. if not playerData then
  1412. return
  1413. end
  1414. local heroList = playerData.heroList
  1415. if not heroList then
  1416. return
  1417. end
  1418. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1419. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1420. return
  1421. end
  1422. local isUpdate = false
  1423. local newHeroData = msg.heroData
  1424. local heroArr = heroList[targetCityId][targetPointIdx]
  1425. for _, heroData in ipairs(heroArr) do
  1426. if heroData.heroUuid == newHeroData.heroUuid then
  1427. for k, v in pairs(newHeroData) do
  1428. heroData[k] = v
  1429. end
  1430. isUpdate = true
  1431. break
  1432. end
  1433. end
  1434. if isUpdate then
  1435. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1436. end
  1437. end
  1438. -- 放弃据点
  1439. function N2C_LeavePoint_Req(msg)
  1440. local playerUuid = msg.playerUuid
  1441. local myUnionId = msg.myUnionId
  1442. local targetCityId = msg.targetCityId
  1443. local targetPointIdx = msg.targetPointIdx
  1444. if not isRunning() then
  1445. return
  1446. end
  1447. local groupId = getMyUnionGourpId(myUnionId)
  1448. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1449. local targetPointData = cityData.pointArr[targetPointIdx]
  1450. if not targetPointData.unionId or targetPointData.unionId ~= myUnionId or targetPointData.playerUuid ~= playerUuid then
  1451. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1452. end
  1453. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1454. if not playerData then
  1455. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1456. end
  1457. local heroList = playerData.heroList
  1458. if not heroList or not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1459. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1460. end
  1461. local defUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  1462. if not defUnionData then
  1463. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1464. end
  1465. local defOccupCityList = defUnionData.occupCityList
  1466. if not defOccupCityList then
  1467. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1468. end
  1469. local defTargerCityData = defOccupCityList[targetCityId]
  1470. if not defTargerCityData or not defTargerCityData.occupyPointList or not defTargerCityData.occupyPointList[targetPointIdx] then
  1471. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1472. end
  1473. local cityNum, pointNum = 0, -1
  1474. -- 更新城池数据, 如果城池之前被本公会占领了, 则删除
  1475. if cityData.occupyUnion and cityData.occupyUnion == myUnionId then
  1476. cityData.occupyUnion = nil
  1477. end
  1478. -- 更新据点数据, 删除据点的 unionId, playerUuid
  1479. targetPointData.unionId = nil
  1480. targetPointData.playerUuid = nil
  1481. -- 更新公会数据, 删除公会数据中占领该据点的玩家uuid
  1482. local defTargetPointData = defTargerCityData.occupyPointList[targetPointIdx]
  1483. defTargetPointData.playerUuid = nil
  1484. -- 更新公会数据, 更新该据点最后占领时间段的结束时间
  1485. if defTargetPointData.occupyTimeArr then
  1486. local lastOccupyTimeTb = defTargetPointData.occupyTimeArr[#defTargetPointData.occupyTimeArr]
  1487. lastOccupyTimeTb[2] = os.time()
  1488. end
  1489. -- 更新公会数据, 如果本公会之前占领了城池, 则修改占领状态
  1490. if defTargerCityData and defTargerCityData.isOccupy then
  1491. defTargerCityData.isOccupy = false
  1492. cityNum = -1
  1493. end
  1494. -- 更新玩家数据, 删除玩家数据中该据点的防守数据
  1495. heroList[targetCityId][targetPointIdx] = nil
  1496. if not next(heroList[targetCityId]) then
  1497. heroList[targetCityId] = nil
  1498. end
  1499. -- 更新公会数据
  1500. AnotherWorldBattleDB.UpdateUnionData(myUnionId, defUnionData)
  1501. -- 更新玩家数据
  1502. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1503. -- 更新公会的排行榜数据
  1504. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1505. -- 更新玩家的排行榜数据
  1506. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1507. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, -pointWeight)
  1508. -- 更新城池数据
  1509. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1510. -- 通知普通服, 放弃据点成功
  1511. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_LEAVE_POINT
  1512. msgData.playerUuid = playerUuid
  1513. msgData.targetCityId = targetCityId
  1514. msgData.targetPointIdx = targetPointIdx
  1515. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1516. InnerMsg.sendMsg(fd, msgData)
  1517. N2C_GetAllCity_Req(msg)
  1518. end
  1519. -- 获取本公会士气信息
  1520. function N2C_GetMyUnionMoraleInfo_Req(msg)
  1521. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1522. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GET_MYUIONMORALE
  1523. msgData.playerUuid = msg.playerUuid
  1524. msgData.moraleState = 0
  1525. msgData.moraleAttrIdx = 1
  1526. msgData.moraleStartTime = 0
  1527. if not isRunning() then
  1528. return InnerMsg.sendMsg(fd, msgData)
  1529. end
  1530. if not moraleCondTimeCheck() then
  1531. msgData.moraleState = -1
  1532. return InnerMsg.sendMsg(fd, msgData)
  1533. end
  1534. local myUnionData = AnotherWorldBattleDB.GetUnionData(msg.myUnionId)
  1535. if not myUnionData then
  1536. return InnerMsg.sendMsg(fd, msgData)
  1537. end
  1538. local now = os.time()
  1539. if myUnionData.moraleData then
  1540. -- 有士气加成且士气加成还没结束
  1541. local moraleStartTime = myUnionData.moraleData.moraleStartTime or 0
  1542. if moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1543. msgData.moraleState = 2
  1544. msgData.moraleStartTime = moraleStartTime
  1545. msgData.moraleAttrIdx = myUnionData.moraleData.moraleAttrIdx or 1
  1546. return InnerMsg.sendMsg(fd, msgData)
  1547. end
  1548. end
  1549. local myRank = getUnionRank(msg.myUnionId)
  1550. if myRank > 1 then --占领据点数量第一的公会不能加士气
  1551. msgData.moraleState = 1
  1552. msgData.moraleAttrIdx = myRank
  1553. end
  1554. InnerMsg.sendMsg(fd, msgData)
  1555. end
  1556. -- 给本公会加士气
  1557. function N2C_UnionMorale_Do_Req(msg)
  1558. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1559. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UIONMORALE_DO
  1560. msgData.playerUuid = msg.playerUuid
  1561. msgData.opRes = -1
  1562. if not isRunning() then
  1563. return InnerMsg.sendMsg(fd, msgData)
  1564. end
  1565. if not moraleCondTimeCheck() then
  1566. return InnerMsg.sendMsg(fd, msgData)
  1567. end
  1568. local myUnionData = AnotherWorldBattleDB.GetUnionData(msg.myUnionId)
  1569. if not myUnionData then
  1570. return InnerMsg.sendMsg(fd, msgData)
  1571. end
  1572. local now = os.time()
  1573. if myUnionData.moraleData then
  1574. -- 有士气加成且士气加成还没结束
  1575. local moraleStartTime = myUnionData.moraleData.moraleStartTime or 0
  1576. if moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1577. msgData.opRes = -2
  1578. return InnerMsg.sendMsg(fd, msgData)
  1579. end
  1580. end
  1581. local myRank = getUnionRank(msg.myUnionId)
  1582. if myRank <= 1 then
  1583. return InnerMsg.sendMsg(fd, msgData)
  1584. end
  1585. myUnionData.moraleData = myUnionData.moraleData or {}
  1586. myUnionData.moraleData.moraleStartTime = now
  1587. myUnionData.moraleData.moraleAttrIdx = myRank
  1588. AnotherWorldBattleDB.UpdateUnionData(msg.myUnionId, myUnionData)
  1589. msgData.opRes = 0
  1590. InnerMsg.sendMsg(fd, msgData)
  1591. N2C_GetMyUnionMoraleInfo_Req(msg)
  1592. end