AnotherWorldBattleCS.lua 64 KB

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