AnotherWorldBattleCS.lua 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. local stage = AnotherWorldBattleDB.GetStage()
  653. msgData.systemState = stage
  654. if stage == 0 then
  655. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  656. if lastRoundStartTime == 0 then
  657. msgData.systemState = 0
  658. else
  659. msgData.systemState = 3
  660. end
  661. end
  662. local sourceServerId = msg.sourceServerId
  663. local num = calcSvrUnionJoinNum(sourceServerId)
  664. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  665. msgData.joinState = 2
  666. end
  667. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  668. for _, unionData in ipairs(joinUnionArr or {}) do
  669. if unionData.unionId == msg.myUnionId then
  670. msgData.joinState = 1
  671. end
  672. end
  673. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  674. InnerMsg.sendMsg(fd, msgData)
  675. end
  676. -- 报名
  677. function N2C_Join_Req(msg)
  678. local sourceServerId = msg.sourceServerId
  679. local playerUuid = msg.playerUuid
  680. local unionInfo = msg.unionInfo
  681. local state = isCanJoin(sourceServerId, unionInfo.unionId)
  682. if state == 2 then
  683. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  684. elseif state == 1 then
  685. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_8)
  686. end
  687. -- 更新参赛列表数据
  688. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  689. joinUnionArr = joinUnionArr or {}
  690. joinUnionArr[#joinUnionArr+1] = {
  691. unionId = unionInfo.unionId,
  692. power = unionInfo.power,
  693. serverId = sourceServerId
  694. }
  695. AnotherWorldBattleDB.UpdateJoinUnionArr(joinUnionArr)
  696. -- 更新公会列表数据
  697. local newUnionData = {
  698. serverId = sourceServerId,
  699. power = unionInfo.power,
  700. name = unionInfo.name,
  701. }
  702. AnotherWorldBattleDB.UpdateUnionData(unionInfo.unionId, newUnionData)
  703. -- 通知报名成功
  704. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_JOIN
  705. msgData.playerUuid = playerUuid
  706. msgData.myUnionId = unionInfo.unionId
  707. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  708. InnerMsg.sendMsg(fd, msgData)
  709. end
  710. -- 查询所有城池信息
  711. function N2C_GetAllCity_Req(msg)
  712. local myUnionId = msg.myUnionId
  713. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ALLCITY_QUERY
  714. msgData.cityArr = {}
  715. msgData.playerUuid = msg.playerUuid
  716. msgData.myUnionBaseCityId = 0
  717. msgData.myOccupyCityArr = {}
  718. msgData.hasPointNum = 0
  719. msgData.challengeTimes = 0
  720. msgData.lastTime = 0
  721. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  722. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  723. if not groupId then
  724. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  725. local stage = AnotherWorldBattleDB.GetStage()
  726. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  727. return InnerMsg.sendMsg(fd, msgData)
  728. end
  729. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  730. end
  731. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  732. if not cityList then
  733. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  734. end
  735. local cityArrMsg = msgData.cityArr
  736. local unionList = AnotherWorldBattleDB.GetUnionList()
  737. msgData.myUnionBaseCityId = unionList[myUnionId] and unionList[myUnionId].baseCityId or 0
  738. for cityId, cityInfo in ipairs(cityList) do
  739. cityArrMsg[cityId] = { occupyPointNum = 0 }
  740. if cityInfo.occupyUnion then
  741. cityArrMsg[cityId].occupyUnionName = unionList[cityInfo.occupyUnion] and unionList[cityInfo.occupyUnion].name
  742. end
  743. for _, pointInfo in ipairs(cityInfo.pointArr) do
  744. if pointInfo.unionId and pointInfo.unionId == myUnionId then
  745. cityArrMsg[cityId].occupyPointNum = cityArrMsg[cityId].occupyPointNum + 1
  746. end
  747. end
  748. cityArrMsg[cityId].cityState = getCityState(cityInfo)
  749. end
  750. msgData.myOccupyCityArr = getUnionOccupyArr(myUnionId)
  751. msgData.hasPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  752. local challengeTimes, leftTime = getPlayerChallengeTimesInfo(msg.playerUuid)
  753. msgData.challengeTimes = challengeTimes
  754. msgData.lastTime = leftTime
  755. msgData.occupyPointData = getOccupPointData(myUnionId)
  756. InnerMsg.sendMsg(fd, msgData)
  757. end
  758. -- 查询某个城池的详细信息
  759. function N2C_GetCityDetailed_Req(msg)
  760. local myUnionId = msg.myUnionId
  761. local playerUuid = msg.playerUuid
  762. local targetCityId = msg.targetCityId
  763. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  764. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_CITYDETAILED_QUERY
  765. msgData.playerUuid = playerUuid
  766. msgData.targetCityId = targetCityId
  767. msgData.myUnionOccupyArr = {}
  768. msgData.pointArr = {}
  769. msgData.gatherState = 0
  770. msgData.gatherTime = -1
  771. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  772. if not groupId then
  773. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  774. local stage = AnotherWorldBattleDB.GetStage()
  775. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  776. return InnerMsg.sendMsg(fd, msgData)
  777. end
  778. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  779. end
  780. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  781. if not cityData then
  782. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  783. end
  784. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  785. local occupCityList = union and union.occupCityList
  786. local myUnionOccupyArr = calcOccupyPointArr(occupCityList)
  787. if union then
  788. myUnionOccupyArr[#myUnionOccupyArr+1] = union.baseCityId
  789. end
  790. msgData.myUnionOccupyArr = myUnionOccupyArr
  791. local pointArrMsg = msgData.pointArr
  792. for pointIdx, occupyInfo in ipairs(cityData.pointArr) do
  793. pointArrMsg[pointIdx] = {}
  794. local state = isCanChallengePoint(targetCityId, pointIdx, myUnionId, playerUuid)
  795. pointArrMsg[pointIdx].state = state < 0 and 0 or state
  796. if occupyInfo.unionId and occupyInfo.playerUuid then
  797. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(occupyInfo.unionId)
  798. local occupyPlayerData = AnotherWorldBattleDB.GetPlayerData(occupyInfo.playerUuid)
  799. if occupyUnionData and occupyPlayerData then
  800. pointArrMsg[pointIdx].occupyUnionName = occupyUnionData.name
  801. pointArrMsg[pointIdx].occupyPlayerName = occupyPlayerData.name
  802. pointArrMsg[pointIdx].power = 0
  803. local heroList = occupyPlayerData.heroList
  804. if heroList and heroList[targetCityId] and heroList[targetCityId][pointIdx] then
  805. pointArrMsg[pointIdx].power = calcHerosPower(heroList[targetCityId][pointIdx])
  806. end
  807. end
  808. end
  809. end
  810. local isOk = isRunning()
  811. if isOk then
  812. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  813. local occupyCityArr = getUnionOccupyArr(myUnionId)
  814. isOk = isadJoin(occupyCityArr, targetCityId)
  815. if isOk then
  816. if not cityData.occupyUnion or cityData.occupyUnion ~= myUnionId then
  817. if not myUnionData.gatherInfo then
  818. msgData.gatherState = 1
  819. else
  820. if myUnionData.gatherInfo.gatherCity == targetCityId then
  821. msgData.gatherState = 2
  822. msgData.gatherTime = myUnionData.gatherInfo.gatherTime
  823. else
  824. local now = os.time()
  825. if now - myUnionData.gatherInfo.gatherTime >= AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  826. msgData.gatherState = 1
  827. end
  828. end
  829. end
  830. end
  831. end
  832. end
  833. InnerMsg.sendMsg(fd, msgData)
  834. end
  835. -- 查询某个据点信息
  836. function N2C_GetPointDetailed_Req(msg)
  837. local myUnionId = msg.myUnionId
  838. local targetCityId = msg.targetCityId
  839. local targetPointIdx = msg.targetPointIdx
  840. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  841. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINTDETAILED_QUERY
  842. msgData.playerUuid = msg.playerUuid
  843. msgData.targetCityId = targetCityId
  844. msgData.targetPointIdx = targetPointIdx
  845. msgData.pointInfo = {}
  846. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  847. if not groupId then
  848. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  849. local stage = AnotherWorldBattleDB.GetStage()
  850. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  851. return InnerMsg.sendMsg(fd, msgData)
  852. end
  853. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  854. end
  855. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  856. if not cityData then
  857. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  858. end
  859. local pointData = cityData.pointArr[targetPointIdx]
  860. if not pointData then
  861. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  862. end
  863. local pointInfoMsg = msgData.pointInfo
  864. if pointData.unionId and pointData.playerUuid then
  865. local playerData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  866. if not playerData then
  867. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  868. end
  869. pointInfoMsg.name = playerData.name
  870. pointInfoMsg.head = playerData.head
  871. pointInfoMsg.headFrame = playerData.headFrame
  872. pointInfoMsg.defLv = playerData.lv
  873. local targetHeroArr = playerData.heroList[targetCityId][targetPointIdx]
  874. if not targetHeroArr then
  875. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  876. end
  877. pointInfoMsg.power = calcHerosPower(targetHeroArr)
  878. pointInfoMsg.heroArr = {}
  879. for i, heroInfo in ipairs(targetHeroArr) do
  880. pointInfoMsg.heroArr[i] = {
  881. heroBody = heroInfo.heroBody,
  882. heroStar = heroInfo.heroStar,
  883. heroLv = heroInfo.heroLevel,
  884. heroCamp = heroInfo.heroCamp,
  885. heroIcon = heroInfo.heroIcon,
  886. heroId = heroInfo.heroId,
  887. heroQuality = heroInfo.heroQuality,
  888. }
  889. end
  890. end
  891. local state = isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, msg.playerUuid)
  892. pointInfoMsg.state = state < 0 and 0 or state
  893. InnerMsg.sendMsg(fd, msgData)
  894. end
  895. -- 查询公会出生点信息
  896. function N2C_GetBaseCity_Req(msg)
  897. local myUnionId = msg.myUnionId
  898. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  899. if not groupId then
  900. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  901. end
  902. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_BASECITY_QUERY
  903. msgData.playerUuid = msg.playerUuid
  904. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  905. if not union then
  906. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  907. end
  908. local baseCityInfo = {}
  909. msgData.baseCityInfo = baseCityInfo
  910. baseCityInfo.cityId = union.baseCityId
  911. local pointInfoArr = calcOccupyPointArr(union.occupCityList)
  912. baseCityInfo.occupyPointNum = #pointInfoArr
  913. baseCityInfo.myUnionOccupyArr = calcOccupyPointArr(union.occupCityList)
  914. baseCityInfo.myUnionOccupyArr[#baseCityInfo.myUnionOccupyArr+1] = baseCityInfo.cityId
  915. baseCityInfo.occupyCityLv2Num = calcOccupyCityNum(union.occupCityList, 2)
  916. baseCityInfo.occupyCityLv3Num = calcOccupyCityNum(union.occupCityList, 3)
  917. baseCityInfo.occupyCityLv4Num = calcOccupyCityNum(union.occupCityList, 4)
  918. baseCityInfo.occupyCityLv5Num = calcOccupyCityNum(union.occupCityList, 5)
  919. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  920. InnerMsg.sendMsg(fd, msgData)
  921. end
  922. -- 查询玩家占领的所有据点信息
  923. function N2C_GetPlayerOccupyPoint_Req(msg)
  924. local myUnionId = msg.myUnionId
  925. local playerUuid = msg.playerUuid
  926. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  927. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYEROCCUPYPOINT_QUERY
  928. msgData.playerUuid = playerUuid
  929. msgData.occupyPointArr = {}
  930. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  931. if not union then
  932. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  933. local stage = AnotherWorldBattleDB.GetStage()
  934. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  935. return InnerMsg.sendMsg(fd, msgData)
  936. end
  937. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  938. end
  939. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  940. if not playerData then
  941. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  942. end
  943. local heroList = playerData.heroList
  944. if not heroList then
  945. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  946. end
  947. local occupyPointArrMsg = msgData.occupyPointArr
  948. for cityId, pointList in pairs(heroList) do
  949. for pointIdx, heroInfoArr in pairs(pointList) do
  950. occupyPointArrMsg[#occupyPointArrMsg+1] = {
  951. cityId = cityId,
  952. pointIdx = pointIdx,
  953. power = calcHerosPower(heroInfoArr),
  954. heroArr = {},
  955. }
  956. local targetHeroArr = occupyPointArrMsg[#occupyPointArrMsg].heroArr
  957. for _, heroInfo in ipairs(heroInfoArr) do
  958. targetHeroArr[#targetHeroArr+1] = {
  959. heroBody = heroInfo.heroBody,
  960. heroStar = heroInfo.heroStar,
  961. heroLv = heroInfo.heroLevel,
  962. heroCamp = heroInfo.heroCamp,
  963. heroIcon = heroInfo.heroIcon,
  964. heroId = heroInfo.heroId,
  965. heroQuality = heroInfo.heroQuality
  966. }
  967. end
  968. end
  969. end
  970. InnerMsg.sendMsg(fd, msgData)
  971. end
  972. -- 查询公会排行榜
  973. function N2C_GetUnionRank_Req(msg)
  974. local myUnionId = msg.myUnionId
  975. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  976. if not groupId then
  977. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  978. end
  979. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  980. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UNIONRANK_QUERY
  981. msgData.playerUuid = msg.playerUuid
  982. msgData.myUnionRank = 0
  983. msgData.unionRankArr = {}
  984. for rank, randkData in ipairs(unionRankList) do
  985. msgData.unionRankArr[rank] = {
  986. name = randkData.name,
  987. power = randkData.power,
  988. cityNum = randkData.occupyCityNum,
  989. pointNum = randkData.occupyPointNum,
  990. }
  991. if randkData.guildId == myUnionId then
  992. msgData.myUnionRank = rank
  993. end
  994. end
  995. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  996. InnerMsg.sendMsg(fd, msgData)
  997. end
  998. -- 查询玩家排行榜
  999. function N2C_GetPlayerRank_Req(msg)
  1000. local myUnionId = msg.myUnionId
  1001. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  1002. if not groupId then
  1003. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1004. end
  1005. local playerRankList = AnotherWorldBattleDB.GetPlayerRankList(groupId)
  1006. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYERRANK_QUERY
  1007. msgData.playerUuid = msg.playerUuid
  1008. msgData.myRank = 0
  1009. msgData.playerRankArr = {}
  1010. for rank, randkData in ipairs(playerRankList) do
  1011. if rank > AnotherWorldBattleDefine.AB_RANK_MAX_NUM then
  1012. break
  1013. end
  1014. msgData.playerRankArr[rank] = {
  1015. name = randkData.name,
  1016. power = randkData.power,
  1017. pointNum = randkData.pointNum,
  1018. pointWeight = randkData.pointAllWeight,
  1019. }
  1020. if randkData.playerId == msg.playerUuid then
  1021. msgData.myRank = rank
  1022. end
  1023. end
  1024. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1025. InnerMsg.sendMsg(fd, msgData)
  1026. end
  1027. -- 集结/取消集结
  1028. function N2C_Gather_Req(msg)
  1029. local sourceServerId = msg.sourceServerId
  1030. local playerUuid = msg.playerUuid
  1031. local myUnionId = msg.myUnionId
  1032. if not isRunning() then
  1033. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  1034. end
  1035. local groupId = getMyUnionGourpId(myUnionId)
  1036. if not groupId then
  1037. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1038. end
  1039. local targetCityId = msg.targetCityId
  1040. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1041. if not myUnionData then
  1042. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1043. end
  1044. local gatherInfo = myUnionData.gatherInfo
  1045. if msg.opType == 1 then --集结
  1046. -- 集结冷却时间判断
  1047. local now = os.time()
  1048. if gatherInfo then
  1049. local gatherTime = gatherInfo.gatherTime
  1050. if now - gatherTime < AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  1051. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_6)
  1052. end
  1053. end
  1054. -- 城池是否相邻判断
  1055. local occupCityyArr = getUnionOccupyArr(myUnionId)
  1056. if not isadJoin(occupCityyArr, targetCityId) then
  1057. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_7)
  1058. end
  1059. myUnionData.gatherInfo = myUnionData.gatherInfo or {}
  1060. myUnionData.gatherInfo.gatherTime = now
  1061. myUnionData.gatherInfo.gatherCity = targetCityId
  1062. else
  1063. -- 取消集结
  1064. if not gatherInfo then
  1065. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  1066. end
  1067. if targetCityId ~= gatherInfo.gatherCity then
  1068. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  1069. end
  1070. gatherInfo.gatherCity = 0
  1071. end
  1072. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1073. -- 推送城池数据给客户端,刷新界面
  1074. N2C_GetCityDetailed_Req(msg)
  1075. -- 发给本地服, 用于在公会聊天频道生成链接
  1076. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GATHER
  1077. msgData.playerUuid = msg.playerUuid
  1078. msgData.targetCityId = targetCityId
  1079. msgData.opType = msg.opType
  1080. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1081. InnerMsg.sendMsg(fd, msgData)
  1082. end
  1083. -- 查询据点是否可以被挑战
  1084. function N2C_TryChallengdePoint_Req(msg)
  1085. local playerUuid = msg.playerUuid
  1086. local myUnionId = msg.myUnionId
  1087. local targetCityId = msg.targetCityId
  1088. local targetPointIdx = msg.targetPointIdx
  1089. local state = isCanChallengePoint(targetCityId, msg.targetPointIdx, msg.myUnionId, playerUuid)
  1090. if state ~= 1 then
  1091. return
  1092. end
  1093. local occupyPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  1094. if occupyPointNum >= AnotherWorldBattleDefine.AB_PLAYER_OCCUPY_POINT_MAX_NUM then
  1095. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_10)
  1096. end
  1097. local now = os.time()
  1098. local groupId = getMyUnionGourpId(myUnionId)
  1099. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1100. local targetPointData = cityData.pointArr[targetPointIdx]
  1101. -- 检查据点是否有人挑战
  1102. local pointBattleTime = targetPointData.battleTime
  1103. if pointBattleTime and now - pointBattleTime < AnotherWorldBattleDefine.AB_POINT_BATTLE_TIME then
  1104. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_11)
  1105. end
  1106. -- 防守方
  1107. local pointOccupyInfo = {targetCityId = targetCityId, targetPointIdx = targetPointIdx}
  1108. if targetPointData.unionId then
  1109. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  1110. pointOccupyInfo.occupySrvId = occupyUnionData.serverId
  1111. pointOccupyInfo.occupyPlayerUuid = targetPointData.playerUuid
  1112. -- 防守方的士气信息
  1113. if occupyUnionData.moraleData then
  1114. if occupyUnionData.moraleData.moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1115. pointOccupyInfo.defMoraleAttrIdx = occupyUnionData.moraleData.moraleAttrIdx
  1116. end
  1117. end
  1118. end
  1119. -- 进攻方集结信息
  1120. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1121. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1122. pointOccupyInfo.isGather = 1
  1123. end
  1124. -- 进攻方士气信息
  1125. if myUnionData.moraleData then
  1126. if myUnionData.moraleData.moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1127. pointOccupyInfo.atkMoraleAttrIdx = myUnionData.moraleData.moraleAttrIdx
  1128. end
  1129. end
  1130. -- 更新据点被挑战时间
  1131. targetPointData.battleTime = now
  1132. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1133. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_ISCAN_CHALLENGE
  1134. msgData.playerUuid = playerUuid
  1135. msgData.pointInfo = pointOccupyInfo
  1136. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1137. InnerMsg.sendMsg(fd, msgData)
  1138. end
  1139. -- 玩家挑战据点胜利
  1140. local function challenge_Win(msg)
  1141. local playerUuid = msg.playerUuid
  1142. local myUnionId = msg.myUnionId
  1143. local targetCityId = msg.targetCityId
  1144. local targetPointIdx = msg.targetPointIdx
  1145. local playerShowInfo = msg.playerShowInfo
  1146. if not isRunning() then
  1147. return
  1148. end
  1149. local groupId = getMyUnionGourpId(myUnionId)
  1150. if not groupId then
  1151. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1152. end
  1153. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1154. if not myUnionData then
  1155. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1156. end
  1157. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1158. local pointData = cityData.pointArr[targetPointIdx]
  1159. local now = os.time()
  1160. local cityNum, pointNum = 0, 1
  1161. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1162. -- 如果城池之前被占领了, 则删除占领城池的公会Id
  1163. if cityData.occupyUnion then
  1164. cityData.occupyUnion = nil
  1165. end
  1166. -- 防守方是真实玩家
  1167. if pointData.unionId and pointData.playerUuid then
  1168. -- 更新防守方公会数据
  1169. local defUnionData = AnotherWorldBattleDB.GetUnionData(pointData.unionId)
  1170. local defOccupCityList = defUnionData.occupCityList
  1171. local defTargerCityData = defOccupCityList[targetCityId]
  1172. -- 更新防守方公会对该据点的占领状态和最后占领时间
  1173. if defTargerCityData and defTargerCityData.occupyPointList and defTargerCityData.occupyPointList[targetPointIdx] then
  1174. local defTargetPointData = defTargerCityData.occupyPointList[targetPointIdx]
  1175. if defTargetPointData then
  1176. -- 删除占领该据点的玩家uuid
  1177. defTargetPointData.playerUuid = nil
  1178. -- 更新对该据点最后占领时间段的结束时间
  1179. if defTargetPointData.occupyTimeArr then
  1180. local lastOccupyTimeTb = defTargetPointData.occupyTimeArr[#defTargetPointData.occupyTimeArr]
  1181. lastOccupyTimeTb[2] = now
  1182. end
  1183. end
  1184. end
  1185. -- 删除防守方玩家的防守阵容数据
  1186. local defPlayData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  1187. if defPlayData then
  1188. defPlayData.heroList[targetCityId][targetPointIdx] = nil
  1189. AnotherWorldBattleDB.UpdatePlayerData(pointData.playerUuid, defPlayData)
  1190. end
  1191. -- 如果防守方公会之前占领了城池, 则修改占领状态
  1192. if defTargerCityData and defTargerCityData.isOccupy then
  1193. defTargerCityData.isOccupy = false
  1194. cityNum = -1
  1195. end
  1196. -- 更新防守方公会的排行榜数据
  1197. AnotherWorldBattleDB.UpdateUnionRankList(groupId, pointData.unionId, cityNum, pointNum)
  1198. -- 更新防守方玩家的排行榜数据
  1199. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, pointData.playerUuid, -pointNum, -pointWeight)
  1200. -- 通知玩家
  1201. pointLose(defUnionData.serverId, pointData.playerUuid, targetCityId, targetPointIdx)
  1202. -- 更新防守方公会数据
  1203. AnotherWorldBattleDB.UpdateUnionData(pointData.unionId, defUnionData)
  1204. end
  1205. -- 更新据点最新占领者数据
  1206. pointData.unionId = myUnionId
  1207. pointData.playerUuid = playerUuid
  1208. -- 重置据点被挑战标识
  1209. pointData.battleTime = nil
  1210. -- 更新进攻方公会数据—— 据点占有者, 最新的据点占有时间
  1211. myUnionData.occupCityList = myUnionData.occupCityList or {}
  1212. local occupCityList = myUnionData.occupCityList
  1213. occupCityList[targetCityId] = occupCityList[targetCityId] or {}
  1214. occupCityList[targetCityId].occupyPointList = occupCityList[targetCityId].occupyPointList or {}
  1215. occupCityList[targetCityId].occupyPointList[targetPointIdx] = occupCityList[targetCityId].occupyPointList[targetPointIdx] or {}
  1216. local atkPointInfo = occupCityList[targetCityId].occupyPointList[targetPointIdx]
  1217. atkPointInfo.playerUuid = playerUuid
  1218. atkPointInfo.occupyTimeArr = atkPointInfo.occupyTimeArr or {}
  1219. atkPointInfo.occupyTimeArr[#atkPointInfo.occupyTimeArr+1] = { now }
  1220. --更新进攻者的展示数据
  1221. local atkPlayerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1222. atkPlayerData = atkPlayerData or {}
  1223. atkPlayerData.name = playerShowInfo.name
  1224. atkPlayerData.lv = playerShowInfo.lv
  1225. atkPlayerData.head = playerShowInfo.head
  1226. atkPlayerData.headFrame = playerShowInfo.headFrame
  1227. atkPlayerData.power = playerShowInfo.power
  1228. atkPlayerData.unionId = myUnionId
  1229. -- 更新挑战次数
  1230. -- atkPlayerData.challengeTimes = (atkPlayerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES) - 1
  1231. -- if not atkPlayerData.lastTime or atkPlayerData.lastTime == 0 then
  1232. -- atkPlayerData.lastTime = os.time()
  1233. -- end
  1234. local pChallengeTimes, pLastTime = getPlayerChallengeTimesInfo(playerUuid)
  1235. atkPlayerData.challengeTimes = pChallengeTimes - 1
  1236. if pLastTime == 0 then
  1237. pLastTime= os.time()
  1238. end
  1239. atkPlayerData.lastTime = pLastTime
  1240. -- 增加防守阵容数据
  1241. atkPlayerData.heroList = atkPlayerData.heroList or {}
  1242. atkPlayerData.heroList[targetCityId] = atkPlayerData.heroList[targetCityId] or {}
  1243. atkPlayerData.heroList[targetCityId][targetPointIdx] = playerShowInfo.heroArr
  1244. cityNum = 0
  1245. -- 进攻方完全占领城池了
  1246. if isCompleteOccupy(cityData, myUnionId) then
  1247. -- 更新城池的占领公会
  1248. cityData.occupyUnion = myUnionId
  1249. -- 更新进攻方公会对该城池的占领状态
  1250. occupCityList[targetCityId].isOccupy = true
  1251. -- 如果占领的是发起集结的城池, 那么取消集结
  1252. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1253. myUnionData.gatherInfo.gatherCity = 0
  1254. end
  1255. cityNum = 1
  1256. end
  1257. -- 更新进攻方公会数据
  1258. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1259. -- 更新进攻方玩家数据
  1260. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, atkPlayerData)
  1261. -- 更新进攻方公会的排行榜数据
  1262. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1263. -- 更新进攻方玩家的排行榜数据
  1264. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, pointWeight, playerShowInfo.power)
  1265. -- 更新城池数据
  1266. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1267. end
  1268. -- 玩家挑战据点失败
  1269. local function challenge_Fail(msg)
  1270. local playerUuid = msg.playerUuid
  1271. local myUnionId = msg.myUnionId
  1272. local targetCityId = msg.targetCityId
  1273. local targetPointIdx = msg.targetPointIdx
  1274. local playerShowInfo = msg.playerShowInfo
  1275. local groupId = getMyUnionGourpId(myUnionId)
  1276. if not groupId then
  1277. return
  1278. end
  1279. -- 据点挑战结束
  1280. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1281. if cityData and cityData.pointArr[targetPointIdx] then
  1282. local targetPointData = cityData.pointArr[targetPointIdx]
  1283. targetPointData.battleTime = nil
  1284. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1285. end
  1286. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1287. playerData = playerData or {}
  1288. playerData.name = playerShowInfo.name
  1289. playerData.lv = playerShowInfo.lv
  1290. playerData.power = playerShowInfo.power
  1291. playerData.unionId = myUnionId
  1292. -- playerData.challengeTimes = (playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_INIT_TIMES) - 1
  1293. -- if not playerData.lastTime or playerData.lastTime == 0 then
  1294. -- playerData.lastTime = os.time()
  1295. -- end
  1296. local pChallengeTimes, pLastTime = getPlayerChallengeTimesInfo(playerUuid)
  1297. playerData.challengeTimes = pChallengeTimes - 1
  1298. if pLastTime == 0 then
  1299. pLastTime= os.time()
  1300. end
  1301. playerData.lastTime = pLastTime
  1302. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1303. -- 加入个人排行榜
  1304. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1305. end
  1306. --挑战据点结束的处理函数
  1307. function N2C_PointChallengeEnd_Handle(msg)
  1308. if msg.challengeRes == CombatDefine.RESULT_WIN then
  1309. challenge_Win(msg)
  1310. else
  1311. challenge_Fail(msg)
  1312. end
  1313. end
  1314. -- 更新玩家数据
  1315. function N2C_UpdatePlayerData_Req(msg)
  1316. if not isRunning() then
  1317. return
  1318. end
  1319. local playerUuid = msg.playerUuid
  1320. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1321. if not playerData then
  1322. return
  1323. end
  1324. local groupId = getMyUnionGourpId(playerData.unionId)
  1325. if not groupId then
  1326. return
  1327. end
  1328. local isUpdate = false
  1329. for k,v in pairs(msg.updateData) do
  1330. if k == "power" or k == "name" then
  1331. isUpdate = true
  1332. end
  1333. playerData[k] = v
  1334. end
  1335. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1336. -- 如果是战力/名字更新, 更新玩家排行榜
  1337. if isUpdate then
  1338. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1339. end
  1340. end
  1341. -- 更新公会数据
  1342. function N2C_UpdateUnionData_Req(msg)
  1343. if not isRunning() then
  1344. return
  1345. end
  1346. local myUnionId = msg.myUnionId
  1347. local unionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1348. if not unionData then
  1349. return
  1350. end
  1351. local groupId = getMyUnionGourpId(myUnionId)
  1352. if not groupId then
  1353. return
  1354. end
  1355. local isUpdate = false
  1356. for k,v in pairs(msg.updateData) do
  1357. if k == "power" or k == "name" then
  1358. isUpdate = true
  1359. end
  1360. unionData[k] = v
  1361. end
  1362. AnotherWorldBattleDB.UpdateUnionData(myUnionId, unionData)
  1363. -- 如果是战力更新, 更新公会排行榜
  1364. if isUpdate then
  1365. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, 0, 0, unionData.power)
  1366. end
  1367. end
  1368. -- 更新玩家据点防守阵容数据
  1369. function N2C_UpdatePointLineup_Req(msg)
  1370. if not isRunning() then
  1371. return
  1372. end
  1373. local playerUuid = msg.playerUuid
  1374. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1375. if not playerData or not playerData.heroList then
  1376. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1377. end
  1378. local groupId = getMyUnionGourpId(playerData.unionId)
  1379. if not groupId then
  1380. return
  1381. end
  1382. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1383. local heroList = playerData.heroList
  1384. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1385. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1386. end
  1387. heroList[targetCityId][targetPointIdx] = msg.heroArr
  1388. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1389. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UPDATE_POINT_LINEIP
  1390. msgData.playerUuid = playerUuid
  1391. msgData.targetCityId = targetCityId
  1392. msgData.targetPointIdx = targetPointIdx
  1393. msgData.formation = msg.formation
  1394. msgData.heroList = msg.heroList
  1395. msgData.helpList = msg.helpList
  1396. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1397. InnerMsg.sendMsg(fd, msgData)
  1398. end
  1399. -- 更新玩家英雄数据
  1400. function N2C_UpdateHeroData_Req(msg)
  1401. if not isRunning() then
  1402. return
  1403. end
  1404. local playerUuid = msg.playerUuid
  1405. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1406. if not playerData then
  1407. return
  1408. end
  1409. local heroList = playerData.heroList
  1410. if not heroList then
  1411. return
  1412. end
  1413. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1414. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1415. return
  1416. end
  1417. local isUpdate = false
  1418. local newHeroData = msg.heroData
  1419. local heroArr = heroList[targetCityId][targetPointIdx]
  1420. for _, heroData in ipairs(heroArr) do
  1421. if heroData.heroUuid == newHeroData.heroUuid then
  1422. for k, v in pairs(newHeroData) do
  1423. heroData[k] = v
  1424. end
  1425. isUpdate = true
  1426. break
  1427. end
  1428. end
  1429. if isUpdate then
  1430. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1431. end
  1432. end
  1433. -- 放弃据点
  1434. function N2C_LeavePoint_Req(msg)
  1435. local playerUuid = msg.playerUuid
  1436. local myUnionId = msg.myUnionId
  1437. local targetCityId = msg.targetCityId
  1438. local targetPointIdx = msg.targetPointIdx
  1439. if not isRunning() then
  1440. return
  1441. end
  1442. local groupId = getMyUnionGourpId(myUnionId)
  1443. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1444. local targetPointData = cityData.pointArr[targetPointIdx]
  1445. if not targetPointData.unionId or targetPointData.unionId ~= myUnionId or targetPointData.playerUuid ~= playerUuid then
  1446. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1447. end
  1448. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1449. if not playerData then
  1450. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1451. end
  1452. local heroList = playerData.heroList
  1453. if not heroList or not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1454. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1455. end
  1456. local defUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  1457. if not defUnionData then
  1458. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1459. end
  1460. local defOccupCityList = defUnionData.occupCityList
  1461. if not defOccupCityList then
  1462. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1463. end
  1464. local defTargerCityData = defOccupCityList[targetCityId]
  1465. if not defTargerCityData or not defTargerCityData.occupyPointList or not defTargerCityData.occupyPointList[targetPointIdx] then
  1466. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  1467. end
  1468. local cityNum, pointNum = 0, -1
  1469. -- 更新城池数据, 如果城池之前被本公会占领了, 则删除
  1470. if cityData.occupyUnion and cityData.occupyUnion == myUnionId then
  1471. cityData.occupyUnion = nil
  1472. end
  1473. -- 更新据点数据, 删除据点的 unionId, playerUuid
  1474. targetPointData.unionId = nil
  1475. targetPointData.playerUuid = nil
  1476. -- 更新公会数据, 删除公会数据中占领该据点的玩家uuid
  1477. local defTargetPointData = defTargerCityData.occupyPointList[targetPointIdx]
  1478. defTargetPointData.playerUuid = nil
  1479. -- 更新公会数据, 更新该据点最后占领时间段的结束时间
  1480. if defTargetPointData.occupyTimeArr then
  1481. local lastOccupyTimeTb = defTargetPointData.occupyTimeArr[#defTargetPointData.occupyTimeArr]
  1482. lastOccupyTimeTb[2] = os.time()
  1483. end
  1484. -- 更新公会数据, 如果本公会之前占领了城池, 则修改占领状态
  1485. if defTargerCityData and defTargerCityData.isOccupy then
  1486. defTargerCityData.isOccupy = false
  1487. cityNum = -1
  1488. end
  1489. -- 更新玩家数据, 删除玩家数据中该据点的防守数据
  1490. heroList[targetCityId][targetPointIdx] = nil
  1491. if not next(heroList[targetCityId]) then
  1492. heroList[targetCityId] = nil
  1493. end
  1494. -- 更新公会数据
  1495. AnotherWorldBattleDB.UpdateUnionData(myUnionId, defUnionData)
  1496. -- 更新玩家数据
  1497. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1498. -- 更新公会的排行榜数据
  1499. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1500. -- 更新玩家的排行榜数据
  1501. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1502. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, -pointWeight)
  1503. -- 更新城池数据
  1504. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1505. -- 通知普通服, 放弃据点成功
  1506. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_LEAVE_POINT
  1507. msgData.playerUuid = playerUuid
  1508. msgData.targetCityId = targetCityId
  1509. msgData.targetPointIdx = targetPointIdx
  1510. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1511. InnerMsg.sendMsg(fd, msgData)
  1512. N2C_GetAllCity_Req(msg)
  1513. end
  1514. -- 获取本公会士气信息
  1515. function N2C_GetMyUnionMoraleInfo_Req(msg)
  1516. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1517. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GET_MYUIONMORALE
  1518. msgData.playerUuid = msg.playerUuid
  1519. msgData.moraleState = 0
  1520. msgData.moraleAttrIdx = 1
  1521. msgData.moraleStartTime = 0
  1522. if not isRunning() then
  1523. return InnerMsg.sendMsg(fd, msgData)
  1524. end
  1525. if not moraleCondTimeCheck() then
  1526. msgData.moraleState = -1
  1527. return InnerMsg.sendMsg(fd, msgData)
  1528. end
  1529. local myUnionData = AnotherWorldBattleDB.GetUnionData(msg.myUnionId)
  1530. if not myUnionData then
  1531. return InnerMsg.sendMsg(fd, msgData)
  1532. end
  1533. local now = os.time()
  1534. if myUnionData.moraleData then
  1535. -- 有士气加成且士气加成还没结束
  1536. local moraleStartTime = myUnionData.moraleData.moraleStartTime or 0
  1537. if moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1538. msgData.moraleState = 2
  1539. msgData.moraleStartTime = moraleStartTime
  1540. msgData.moraleAttrIdx = myUnionData.moraleData.moraleAttrIdx or 1
  1541. return InnerMsg.sendMsg(fd, msgData)
  1542. end
  1543. end
  1544. local myRank = getUnionRank(msg.myUnionId)
  1545. if myRank > 1 then --占领据点数量第一的公会不能加士气
  1546. msgData.moraleState = 1
  1547. msgData.moraleAttrIdx = myRank
  1548. end
  1549. InnerMsg.sendMsg(fd, msgData)
  1550. end
  1551. -- 给本公会加士气
  1552. function N2C_UnionMorale_Do_Req(msg)
  1553. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1554. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UIONMORALE_DO
  1555. msgData.playerUuid = msg.playerUuid
  1556. msgData.opRes = -1
  1557. if not isRunning() then
  1558. return InnerMsg.sendMsg(fd, msgData)
  1559. end
  1560. if not moraleCondTimeCheck() then
  1561. return InnerMsg.sendMsg(fd, msgData)
  1562. end
  1563. local myUnionData = AnotherWorldBattleDB.GetUnionData(msg.myUnionId)
  1564. if not myUnionData then
  1565. return InnerMsg.sendMsg(fd, msgData)
  1566. end
  1567. local now = os.time()
  1568. if myUnionData.moraleData then
  1569. -- 有士气加成且士气加成还没结束
  1570. local moraleStartTime = myUnionData.moraleData.moraleStartTime or 0
  1571. if moraleStartTime + AnotherWorldBattleDefine.AB_MORALE_DURATION >= now then
  1572. msgData.opRes = -2
  1573. return InnerMsg.sendMsg(fd, msgData)
  1574. end
  1575. end
  1576. local myRank = getUnionRank(msg.myUnionId)
  1577. if myRank <= 1 then
  1578. return InnerMsg.sendMsg(fd, msgData)
  1579. end
  1580. myUnionData.moraleData = myUnionData.moraleData or {}
  1581. myUnionData.moraleData.moraleStartTime = now
  1582. myUnionData.moraleData.moraleAttrIdx = myRank
  1583. AnotherWorldBattleDB.UpdateUnionData(msg.myUnionId, myUnionData)
  1584. msgData.opRes = 0
  1585. InnerMsg.sendMsg(fd, msgData)
  1586. N2C_GetMyUnionMoraleInfo_Req(msg)
  1587. end