AnotherWorldBattleCS.lua 59 KB

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