AnotherWorldBattleCS.lua 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. -- 周三23点过后暂时不算结束
  51. local now = os.time()
  52. local toDayStartTime = getTodayStartTime()
  53. if wDay == AnotherWorldBattleDefine.AB_OPEN_WDAY_AREA[2] and now > (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  54. return false
  55. end
  56. return true
  57. end
  58. -- 进入新一轮的处理
  59. local function newRoundHandle()
  60. local stage = AnotherWorldBattleDB.GetStage()
  61. -- 上一轮奖励没有发放, 在新一轮开始时, 先发奖
  62. if stage == AnotherWorldBattleDefine.AB_STATE_AWARD then
  63. IssueRewardManager()
  64. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  65. end
  66. -- 重置数据
  67. AnotherWorldBattleDB.ResetData()
  68. -- 更新状态
  69. AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_JOIN)
  70. -- 更新活动开始时间
  71. local now = os.time()
  72. AnotherWorldBattleDB.UpdateLastRoundStartTime(now)
  73. end
  74. -- 能否进入新一轮的报名阶段
  75. local function joinStageCheck()
  76. local stage = AnotherWorldBattleDB.GetStage()
  77. if stage ~= AnotherWorldBattleDefine.AB_STATE_END and stage ~= AnotherWorldBattleDefine.AB_STATE_AWARD then
  78. return
  79. end
  80. -- local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  81. -- local diffDays = Util.diffDay(lastRoundStartTime)
  82. -- 上一轮活动结束时间为0 或 当前时间 - 上一轮活动开启时间 > 9天, 开启新一轮活动
  83. -- if lastRoundStartTime == 0 or diffDays > AnotherWorldBattleDefine.AB_SUB_DAY then
  84. newRoundHandle()
  85. -- end
  86. end
  87. -- 公会分组算法
  88. local function groupingAlgorithm(unionArray, len)
  89. local usedTb = {}
  90. local groupArr = {}
  91. local function getNextUnuseIdx(nowIdx)
  92. for i=nowIdx, len do
  93. if not usedTb[i] then
  94. return i
  95. end
  96. end
  97. end
  98. local function checkRandIdx(startIdx, endIdx)
  99. local tbl= {}
  100. for i= startIdx, endIdx do
  101. if not usedTb[i] then
  102. table.insert(tbl, i)
  103. end
  104. end
  105. return tbl
  106. end
  107. local currentIdx = 1
  108. local selectNum, cnt = 0, 0
  109. while currentIdx <= len do
  110. currentIdx = getNextUnuseIdx(currentIdx)
  111. if not currentIdx then
  112. break
  113. end
  114. local startIdx = currentIdx + 1
  115. local endIdx = math.min(currentIdx + 19, len)
  116. groupArr[#groupArr+1] = { unionArray[currentIdx] }
  117. selectNum = selectNum + 1
  118. cnt = math.min(AnotherWorldBattleDefine.AB_GROUP_UNION_NUM - 1, len - selectNum )
  119. for i=1, cnt do
  120. local correctTb = checkRandIdx(startIdx, endIdx)
  121. local matchIdx = correctTb[math.random(1, #correctTb)]
  122. usedTb[currentIdx] = true
  123. usedTb[matchIdx] = true
  124. table.insert(groupArr[#groupArr], unionArray[matchIdx])
  125. selectNum = selectNum + 1
  126. end
  127. currentIdx = currentIdx + 1
  128. end
  129. return groupArr
  130. end
  131. -- 分组
  132. local function grouping()
  133. local function genUnionIdArray(sourceUnionArr, targetUnionArr)
  134. for k, v in ipairs(sourceUnionArr) do
  135. targetUnionArr[k] = v.unionId
  136. end
  137. end
  138. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  139. if not joinUnionArr then
  140. -- 没有公会参加, 本轮活动结束
  141. local now = os.time()
  142. AnotherWorldBattleDB.UpdateLastRoundStartTime(now)
  143. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  144. end
  145. local len = #joinUnionArr
  146. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  147. table.sort(joinUnionArr, function (a, b)
  148. return a.power > b.power
  149. end)
  150. end
  151. local unionIdArr = {}
  152. genUnionIdArray(joinUnionArr, unionIdArr)
  153. local newGroupArray
  154. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  155. newGroupArray = groupingAlgorithm(unionIdArr, len)
  156. else
  157. newGroupArray = { unionIdArr }
  158. end
  159. AnotherWorldBattleDB.UpdateGroupArray(newGroupArray)
  160. end
  161. -- 给各个分组中的公会随机分配出生点
  162. local function randomBaseCity()
  163. local baseCityIdArr = {}
  164. for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do
  165. if cityCfg.isBaseCity == 1 then
  166. baseCityIdArr[#baseCityIdArr+1] = cityId
  167. end
  168. end
  169. local now = os.time()
  170. local groupArray = AnotherWorldBattleDB.GetGroupArray()
  171. for _, unionIdArr in ipairs(groupArray) do
  172. -- 乱序
  173. table.shuffle(baseCityIdArr)
  174. for i, unionId in ipairs(unionIdArr) do
  175. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  176. if union then
  177. union.baseCityId = baseCityIdArr[i]
  178. union.baseCityStartTime = now
  179. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  180. end
  181. end
  182. end
  183. end
  184. -- 统计公会的占领情况, 用于发放奖励
  185. local function genUnionOccupyInfo(unionId, union, playerListData)
  186. local occupyTb = {
  187. occupyCityArr = {}, -- 当前还占领的城池列表
  188. point2CityIdArr = {}, -- 曾占领/当前还占领的据点的所属城池Id列表
  189. occuoyPointNum = 0,
  190. playerUuidArr = {},
  191. unionId = unionId,
  192. }
  193. local now = os.time()
  194. local t1 = occupyTb.occupyCityArr
  195. local t2 = occupyTb.point2CityIdArr
  196. t1[#t1+1] = union.baseCityId
  197. for cityId, cityIno in pairs(union.occupCityList or {}) do
  198. if cityIno.isOccupy then
  199. t1[#t1+1] = cityId
  200. end
  201. for _, pointInfo in pairs(cityIno.occupyPointList) do
  202. if pointInfo.playerUuid then
  203. if not cityIno.isOccupy then
  204. occupyTb.occuoyPointNum = occupyTb.occuoyPointNum + 1
  205. end
  206. -- 更新据点最新占领时间段的结束时间
  207. local occupyTimeArr = pointInfo.occupyTimeArr
  208. local lastTimeTb = occupyTimeArr[#occupyTimeArr]
  209. if #lastTimeTb == 1 then
  210. occupyTimeArr[#occupyTimeArr][2] = now
  211. end
  212. end
  213. t2[#t2+1] = {cityId, pointInfo.occupyTimeArr}
  214. end
  215. end
  216. -- 出生点算5个据点
  217. local baseCiyuTimeArr = { {union.baseCityStartTime, union.baseCityEndTime or now } }
  218. for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
  219. t2[#t2+1] = {union.baseCityId, baseCiyuTimeArr}
  220. end
  221. for playerUuid, playerInfo in pairs(playerListData) do
  222. if playerInfo.unionId == unionId then
  223. occupyTb.playerUuidArr[#occupyTb.playerUuidArr+1] = playerUuid
  224. end
  225. end
  226. -- 防止本次没有正常发放奖励,后续补发时, 结束时间异常的情况
  227. union.baseCityEndTime = now
  228. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  229. return occupyTb
  230. end
  231. -- 发奖
  232. local function issueReward(sourceServerId, occupyInfo)
  233. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ISSUEREWARD
  234. msgData.unionOccupyInfo = occupyInfo
  235. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  236. InnerMsg.sendMsg(fd, msgData)
  237. end
  238. -- 奖励发放管理函数
  239. function IssueRewardManager()
  240. local delay_sec = 0
  241. local unionList = AnotherWorldBattleDB.GetUnionList()
  242. local playerListData = AnotherWorldBattleDB.GetPlayerList()
  243. for unionId, union in pairs(unionList) do
  244. local occupyInfo = genUnionOccupyInfo(unionId, union, playerListData)
  245. delay_sec = delay_sec + 5
  246. Timer.addLater(delay_sec, issueReward, union.serverId, occupyInfo)
  247. end
  248. end
  249. -- 检测活动各阶段状态及相关处理
  250. local function timedStageHandle()
  251. -- 处于报名阶段
  252. -- if table.find(AnotherWorldBattleDefine.AB_JOIN_WDAY_AREA, wDay) then
  253. -- joinStageCheck()
  254. -- end
  255. if wDay == AnotherWorldBattleDefine.AB_JOIN_WDAY then
  256. joinStageCheck()
  257. end
  258. local now = os.time()
  259. -- 报名阶段 -> 战斗阶段
  260. if wDay >= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[1] and wDay <= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] then
  261. local stage = AnotherWorldBattleDB.GetStage()
  262. local toDayStartTime = getTodayStartTime()
  263. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_START_SEC) then
  264. -- 分组
  265. grouping()
  266. -- 给各个分组中的公会随机分配出生点
  267. randomBaseCity()
  268. -- 改为战斗阶段
  269. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_BATTLE)
  270. end
  271. end
  272. -- 战斗阶段 -> 发奖阶段
  273. local toDayStartTime = getTodayStartTime()
  274. if wDay == AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  275. local stage = AnotherWorldBattleDB.GetStage()
  276. if stage == AnotherWorldBattleDefine.AB_STATE_BATTLE or stage == AnotherWorldBattleDefine.AB_STATE_AWARD then
  277. -- 改为发奖阶段
  278. AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_AWARD)
  279. -- 开始发奖
  280. IssueRewardManager()
  281. -- 改为结束阶段
  282. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  283. end
  284. end
  285. end
  286. function oneMin()
  287. if _G.is_middle ~= true then return end
  288. if not isOpen() then
  289. return
  290. end
  291. -- 与 onHour() 处理错开
  292. if Util.getMin() == 0 then
  293. return
  294. end
  295. timedStageHandle()
  296. end
  297. function onHour(hour)
  298. if _G.is_middle ~= true then return end
  299. if hour == 0 or not wDay then
  300. updateWDay()
  301. end
  302. if not isOpen() then
  303. return
  304. end
  305. timedStageHandle()
  306. end
  307. ------------------------------------C2N---------------------------------------------------
  308. -- 错误提示
  309. local function errTips(sourceServerId, playerUuid, errCode)
  310. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_TIPS
  311. msgData.playerUuid = playerUuid
  312. msgData.errCode = errCode
  313. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  314. InnerMsg.sendMsg(fd, msgData)
  315. end
  316. -- 通知玩家,据点被别的玩家占领了
  317. local function pointLose(sourceServerId, playerUuid, loseCityId, losePointIdx)
  318. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_LOSE
  319. msgData.playerUuid = playerUuid
  320. msgData.loseCityId = loseCityId
  321. msgData.losePointIdx = losePointIdx
  322. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  323. InnerMsg.sendMsg(fd, msgData)
  324. end
  325. ------------------------------------N2C---------------------------------------------------
  326. -- 获取自己公会的组Id
  327. local function getMyUnionGourpId(myUnionId)
  328. local groupId = AnotherWorldBattleDB.GetUnionGroupId(myUnionId)
  329. return groupId
  330. end
  331. -- 获取自己公会/所在区服第一公会的组Id
  332. local function getGroupId(myUnionId, myServerId)
  333. local groupId = getMyUnionGourpId(myUnionId)
  334. if not groupId then
  335. groupId = AnotherWorldBattleDB.GetGroupIdByServerId(myServerId)
  336. end
  337. return groupId
  338. end
  339. -- 统计单个服公会报名数量
  340. local function calcSvrUnionJoinNum(serverId)
  341. local num = 0
  342. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  343. if not joinUnionArr then
  344. return num
  345. end
  346. for _, unionInfo in ipairs(joinUnionArr) do
  347. if unionInfo.serverId == serverId then
  348. num = num + 1
  349. end
  350. end
  351. return num
  352. end
  353. -- 计算多个英雄的总战力
  354. local function calcHerosPower(heroArr)
  355. local power = 0
  356. for _, v in ipairs(heroArr) do
  357. power = power + v.heroPower
  358. end
  359. return power
  360. end
  361. -- 统计公会占领不同等级的城池数量
  362. local function calcOccupyCityNum(occupCityList, cityLv)
  363. if not occupCityList then
  364. return 0
  365. end
  366. local num = 0
  367. for cityId, occupyInfo in pairs(occupCityList) do
  368. if occupyInfo.isOccupy then
  369. local cityCfg = AnotherWorldBattleConfig.city[cityId]
  370. if cityCfg.cityLv == cityLv then
  371. num = num + 1
  372. end
  373. end
  374. end
  375. return num
  376. end
  377. -- 统计公会占领的据点所属城池数组
  378. local function calcOccupyPointArr(occupCityList)
  379. local pointInfoArr = {}
  380. if not occupCityList then
  381. return pointInfoArr
  382. end
  383. for cityId, occupyInfo in pairs(occupCityList) do
  384. for _, pointInfo in pairs(occupyInfo.occupyPointList) do
  385. if pointInfo.playerUuid then
  386. pointInfoArr[#pointInfoArr+1] = cityId
  387. end
  388. end
  389. end
  390. return pointInfoArr
  391. end
  392. -- 获取公会占领的城池列表
  393. local function getUnionOccupyArr(unionId)
  394. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  395. if not union then
  396. return
  397. end
  398. local occupyCityArr = {}
  399. occupyCityArr[#occupyCityArr+1] = union.baseCityId
  400. for cityId, occupyInfo in pairs(union.occupCityList or {}) do
  401. if occupyInfo.isOccupy then
  402. occupyCityArr[#occupyCityArr+1] = cityId
  403. end
  404. end
  405. return occupyCityArr
  406. end
  407. -- 检查某个城池与公会占领的城池是否相邻
  408. local function isadJoin(cityIdArr, targetCityId)
  409. if not cityIdArr then
  410. return false
  411. end
  412. local targetCityCfg = AnotherWorldBattleConfig.city[targetCityId]
  413. if not targetCityCfg then
  414. return false
  415. end
  416. for _, cityId in ipairs(targetCityCfg.adJoinCityArr) do
  417. if table.find(cityIdArr, cityId) then
  418. return true
  419. end
  420. end
  421. return false
  422. end
  423. -- 统计玩家占据的据点数量
  424. local function calcPlayerOccupyPointNum(playerUuid)
  425. local playerOccupyPonitNum = 0
  426. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  427. if playerData and playerData.heroList then
  428. for _, pointList in pairs(playerData.heroList) do
  429. for _, _ in pairs(pointList) do
  430. playerOccupyPonitNum = playerOccupyPonitNum + 1
  431. end
  432. end
  433. end
  434. return playerOccupyPonitNum
  435. end
  436. -- 检查某个据点是否能被玩家挑战
  437. local function isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, playerUuid)
  438. -- 活动未开启
  439. if not isRunning() then
  440. return -1
  441. end
  442. -- 公会没有参加活动
  443. local groupId = getMyUnionGourpId(myUnionId)
  444. if not groupId then
  445. return -2
  446. end
  447. local tagetCityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  448. if not tagetCityData then
  449. return -3
  450. end
  451. local targetPointData = tagetCityData.pointArr[targetPointIdx]
  452. if not targetPointData then
  453. return -5
  454. end
  455. -- 自己占领了该据点
  456. if targetPointData.unionId and targetPointData.unionId == myUnionId and targetPointData.playerUuid == playerUuid then
  457. return 2
  458. end
  459. -- 据点已被本公会占领
  460. if tagetCityData.occupyUnion and tagetCityData.occupyUnion == myUnionId then
  461. return -4
  462. end
  463. -- 据点已被本公会占领
  464. if targetPointData.unionId and targetPointData.unionId == myUnionId then
  465. return -6
  466. end
  467. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  468. if not myUnionData then
  469. return -7
  470. end
  471. -- 城池是否相邻
  472. local occupyCityArr = getUnionOccupyArr(myUnionId)
  473. if not isadJoin(occupyCityArr, targetCityId) then
  474. return -8
  475. end
  476. return 1
  477. end
  478. -- 公会是否能报名
  479. local function isCanJoin(sourceServerId, myUnionId)
  480. if not isRunning() then
  481. return 2
  482. end
  483. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  484. for _, unionData in ipairs(joinUnionArr or {}) do
  485. if unionData.unionId == myUnionId then
  486. return 1
  487. end
  488. end
  489. local num = calcSvrUnionJoinNum(sourceServerId)
  490. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  491. return 2
  492. end
  493. return 0
  494. end
  495. -- 检查城池是否被己方公会完全占领
  496. local function isCompleteOccupy(cityData, myUnionId)
  497. for _, pointInfo in ipairs(cityData.pointArr) do
  498. if not pointInfo.unionId or pointInfo.unionId ~= myUnionId then
  499. return false
  500. end
  501. end
  502. return true
  503. end
  504. -- 获取玩家挑战次数相关数据
  505. local function getPlayerChallengeTimesInfo(playerUuid)
  506. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  507. if not playerData then
  508. return AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES, 0
  509. end
  510. local now = os.time()
  511. local subSex = now - playerData.lastTime
  512. if subSex >= AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC then
  513. local addTimes = math.floor(subSex / AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC)
  514. playerData.challengeTimes = math.min(playerData.challengeTimes + addTimes, AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES)
  515. playerData.lastTime = playerData.lastTime + addTimes* AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC
  516. if playerData.challengeTimes == AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES then
  517. playerData.lastTime = 0
  518. end
  519. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  520. end
  521. return playerData.challengeTimes, playerData.lastTime
  522. end
  523. -- 获取城池被占领状态
  524. local function getCityState(cityData)
  525. local state = 0
  526. if cityData.occupyUnion then
  527. state = 1
  528. return state
  529. end
  530. local occupyPlayerUuid
  531. for _, pointInfo in ipairs(cityData.pointArr) do
  532. if pointInfo.playerUuid then
  533. if not occupyPlayerUuid then
  534. occupyPlayerUuid = pointInfo.playerUuid
  535. end
  536. if pointInfo.playerUuid ~= occupyPlayerUuid then
  537. state = 2
  538. break
  539. end
  540. end
  541. end
  542. return state
  543. end
  544. -- 查询状态
  545. function N2C_Get_State(msg)
  546. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GET_STATE
  547. msgData.playerUuid = msg.playerUuid
  548. msgData.systemState = 0
  549. msgData.joinState = 0
  550. local stage = AnotherWorldBattleDB.GetStage()
  551. msgData.systemState = stage
  552. if stage == 0 then
  553. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  554. if lastRoundStartTime == 0 then
  555. msgData.systemState = 0
  556. else
  557. msgData.systemState = 3
  558. end
  559. end
  560. local sourceServerId = msg.sourceServerId
  561. local num = calcSvrUnionJoinNum(sourceServerId)
  562. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  563. msgData.joinState = 2
  564. end
  565. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  566. for _, unionData in ipairs(joinUnionArr or {}) do
  567. if unionData.unionId == msg.myUnionId then
  568. msgData.joinState = 1
  569. end
  570. end
  571. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  572. InnerMsg.sendMsg(fd, msgData)
  573. end
  574. -- 报名
  575. function N2C_Join(msg)
  576. local sourceServerId = msg.sourceServerId
  577. local playerUuid = msg.playerUuid
  578. local unionInfo = msg.unionInfo
  579. local state = isCanJoin(sourceServerId, unionInfo.unionId)
  580. if state == 2 then
  581. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  582. elseif state == 1 then
  583. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_8)
  584. end
  585. -- 更新参赛列表数据
  586. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  587. joinUnionArr = joinUnionArr or {}
  588. joinUnionArr[#joinUnionArr+1] = {
  589. unionId = unionInfo.unionId,
  590. power = unionInfo.power,
  591. serverId = sourceServerId
  592. }
  593. AnotherWorldBattleDB.UpdateJoinUnionArr(joinUnionArr)
  594. -- 更新公会列表数据
  595. local newUnionData = {
  596. serverId = sourceServerId,
  597. power = unionInfo.power,
  598. name = unionInfo.name,
  599. }
  600. AnotherWorldBattleDB.UpdateUnionData(unionInfo.unionId, newUnionData)
  601. -- 通知报名成功
  602. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_JOIN
  603. msgData.playerUuid = playerUuid
  604. msgData.myUnionId = unionInfo.unionId
  605. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  606. InnerMsg.sendMsg(fd, msgData)
  607. end
  608. -- 查询所有城池信息
  609. function N2C_AllCity_Query(msg)
  610. local myUnionId = msg.myUnionId
  611. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ALLCITY_QUERY
  612. msgData.cityArr = {}
  613. msgData.playerUuid = msg.playerUuid
  614. msgData.myUnionBaseCityId = 0
  615. msgData.myOccupyCityArr = {}
  616. msgData.hasPointNum = 0
  617. msgData.challengeTimes = 0
  618. msgData.lastTime = 0
  619. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  620. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  621. if not groupId then
  622. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  623. local stage = AnotherWorldBattleDB.GetStage()
  624. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  625. return InnerMsg.sendMsg(fd, msgData)
  626. end
  627. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  628. end
  629. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  630. if not cityList then
  631. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  632. end
  633. local cityArrMsg = msgData.cityArr
  634. local unionList = AnotherWorldBattleDB.GetUnionList()
  635. msgData.myUnionBaseCityId = unionList[myUnionId] and unionList[myUnionId].baseCityId or 0
  636. for cityId, cityInfo in ipairs(cityList) do
  637. cityArrMsg[cityId] = { occupyPointNum = 0 }
  638. if cityInfo.occupyUnion then
  639. cityArrMsg[cityId].occupyUnionName = unionList[cityInfo.occupyUnion] and unionList[cityInfo.occupyUnion].name
  640. end
  641. for _, pointInfo in ipairs(cityInfo.pointArr) do
  642. if pointInfo.unionId and pointInfo.unionId == myUnionId then
  643. cityArrMsg[cityId].occupyPointNum = cityArrMsg[cityId].occupyPointNum + 1
  644. end
  645. end
  646. cityArrMsg[cityId].cityState = getCityState(cityInfo)
  647. end
  648. msgData.myOccupyCityArr = getUnionOccupyArr(myUnionId)
  649. msgData.hasPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  650. local challengeTimes, leftTime = getPlayerChallengeTimesInfo(msg.playerUuid)
  651. msgData.challengeTimes = challengeTimes
  652. msgData.lastTime = leftTime
  653. InnerMsg.sendMsg(fd, msgData)
  654. end
  655. -- 查询某个城池的详细信息
  656. function N2C_CityDetailed_Query(msg)
  657. local myUnionId = msg.myUnionId
  658. local playerUuid = msg.playerUuid
  659. local targetCityId = msg.targetCityId
  660. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  661. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_CITYDETAILED_QUERY
  662. msgData.playerUuid = playerUuid
  663. msgData.targetCityId = targetCityId
  664. msgData.myUnionOccupyArr = {}
  665. msgData.pointArr = {}
  666. msgData.gatherState = 0
  667. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  668. if not groupId then
  669. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  670. local stage = AnotherWorldBattleDB.GetStage()
  671. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  672. return InnerMsg.sendMsg(fd, msgData)
  673. end
  674. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  675. end
  676. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  677. if not cityData then
  678. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  679. end
  680. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  681. local occupCityList = union and union.occupCityList
  682. local myUnionOccupyArr = calcOccupyPointArr(occupCityList)
  683. if union then
  684. myUnionOccupyArr[#myUnionOccupyArr+1] = union.baseCityId
  685. end
  686. msgData.myUnionOccupyArr = myUnionOccupyArr
  687. local pointArrMsg = msgData.pointArr
  688. for pointIdx, occupyInfo in ipairs(cityData.pointArr) do
  689. pointArrMsg[pointIdx] = {}
  690. local state = isCanChallengePoint(targetCityId, pointIdx, myUnionId, playerUuid)
  691. pointArrMsg[pointIdx].state = state < 0 and 0 or state
  692. if occupyInfo.unionId and occupyInfo.playerUuid then
  693. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(occupyInfo.unionId)
  694. local occupyPlayerData = AnotherWorldBattleDB.GetPlayerData(occupyInfo.playerUuid)
  695. if occupyUnionData and occupyPlayerData then
  696. pointArrMsg[pointIdx].occupyUnionName = occupyUnionData.name
  697. pointArrMsg[pointIdx].occupyPlayerName = occupyPlayerData.name
  698. pointArrMsg[pointIdx].power = 0
  699. local heroList = occupyPlayerData.heroList
  700. if heroList and heroList[targetCityId] and heroList[targetCityId][pointIdx] then
  701. pointArrMsg[pointIdx].power = calcHerosPower(heroList[targetCityId][pointIdx])
  702. end
  703. end
  704. end
  705. end
  706. local isOk = isRunning()
  707. if isOk then
  708. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  709. local occupyCityArr = getUnionOccupyArr(myUnionId)
  710. isOk = isadJoin(occupyCityArr, targetCityId)
  711. if isOk then
  712. if not cityData.occupyUnion or cityData.occupyUnion ~= myUnionId then
  713. if not myUnionData.gatherInfo then
  714. msgData.gatherState = 1
  715. else
  716. if myUnionData.gatherInfo.gatherCity == targetCityId then
  717. msgData.gatherState = 2
  718. else
  719. local now = os.time()
  720. if now - myUnionData.gatherInfo.gatherTime >= AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  721. msgData.gatherState = 1
  722. end
  723. end
  724. end
  725. end
  726. end
  727. end
  728. InnerMsg.sendMsg(fd, msgData)
  729. end
  730. -- 查询某个据点信息
  731. function N2C_PointDetailed_Query(msg)
  732. local myUnionId = msg.myUnionId
  733. local targetCityId = msg.targetCityId
  734. local targetPointIdx = msg.targetPointIdx
  735. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  736. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINTDETAILED_QUERY
  737. msgData.playerUuid = msg.playerUuid
  738. msgData.targetCityId = targetCityId
  739. msgData.targetPointIdx = targetPointIdx
  740. msgData.pointInfo = {}
  741. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  742. if not groupId then
  743. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  744. local stage = AnotherWorldBattleDB.GetStage()
  745. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  746. return InnerMsg.sendMsg(fd, msgData)
  747. end
  748. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  749. end
  750. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  751. if not cityData then
  752. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  753. end
  754. local pointData = cityData.pointArr[targetPointIdx]
  755. if not pointData then
  756. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  757. end
  758. local pointInfoMsg = msgData.pointInfo
  759. if pointData.unionId and pointData.playerUuid then
  760. local playerData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  761. if not playerData then
  762. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  763. end
  764. pointInfoMsg.name = playerData.name
  765. pointInfoMsg.head = playerData.head
  766. pointInfoMsg.headFrame = playerData.headFrame
  767. pointInfoMsg.defLv = playerData.lv
  768. local targetHeroArr = playerData.heroList[targetCityId][targetPointIdx]
  769. if not targetHeroArr then
  770. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  771. end
  772. pointInfoMsg.power = calcHerosPower(targetHeroArr)
  773. pointInfoMsg.heroArr = {}
  774. for i, heroInfo in ipairs(targetHeroArr) do
  775. pointInfoMsg.heroArr[i] = {
  776. heroBody = heroInfo.heroBody,
  777. heroStar = heroInfo.heroStar,
  778. heroLv = heroInfo.heroLevel,
  779. heroCamp = heroInfo.heroCamp,
  780. heroIcon = heroInfo.heroIcon,
  781. heroId = heroInfo.heroId,
  782. heroQuality = heroInfo.heroQuality,
  783. }
  784. end
  785. end
  786. local state = isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, msg.playerUuid)
  787. pointInfoMsg.state = state < 0 and 0 or state
  788. InnerMsg.sendMsg(fd, msgData)
  789. end
  790. -- 查询公会出生点信息
  791. function N2C_BaseCity_Query(msg)
  792. local myUnionId = msg.myUnionId
  793. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  794. if not groupId then
  795. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  796. end
  797. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_BASECITY_QUERY
  798. msgData.playerUuid = msg.playerUuid
  799. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  800. if not union then
  801. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  802. end
  803. local baseCityInfo = {}
  804. msgData.baseCityInfo = baseCityInfo
  805. baseCityInfo.cityId = union.baseCityId
  806. local pointInfoArr = calcOccupyPointArr(union.occupCityList)
  807. baseCityInfo.occupyPointNum = #pointInfoArr
  808. baseCityInfo.myUnionOccupyArr = calcOccupyPointArr(union.occupCityList)
  809. baseCityInfo.myUnionOccupyArr[#baseCityInfo.myUnionOccupyArr+1] = baseCityInfo.cityId
  810. baseCityInfo.occupyCityLv2Num = calcOccupyCityNum(union.occupCityList, 2)
  811. baseCityInfo.occupyCityLv3Num = calcOccupyCityNum(union.occupCityList, 3)
  812. baseCityInfo.occupyCityLv4Num = calcOccupyCityNum(union.occupCityList, 4)
  813. baseCityInfo.occupyCityLv5Num = calcOccupyCityNum(union.occupCityList, 5)
  814. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  815. InnerMsg.sendMsg(fd, msgData)
  816. end
  817. -- 查询玩家占领的所有据点信息
  818. function N2C_PlayerOccupyPoint_Query(msg)
  819. local myUnionId = msg.myUnionId
  820. local playerUuid = msg.playerUuid
  821. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  822. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYEROCCUPYPOINT_QUERY
  823. msgData.playerUuid = playerUuid
  824. msgData.occupyPointArr = {}
  825. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  826. if not union then
  827. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  828. local stage = AnotherWorldBattleDB.GetStage()
  829. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  830. return InnerMsg.sendMsg(fd, msgData)
  831. end
  832. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  833. end
  834. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  835. if not playerData then
  836. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  837. end
  838. local heroList = playerData.heroList
  839. if not heroList then
  840. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  841. end
  842. local occupyPointArrMsg = msgData.occupyPointArr
  843. for cityId, pointList in pairs(heroList) do
  844. for pointIdx, heroInfoArr in pairs(pointList) do
  845. occupyPointArrMsg[#occupyPointArrMsg+1] = {
  846. cityId = cityId,
  847. pointIdx = pointIdx,
  848. power = calcHerosPower(heroInfoArr),
  849. heroArr = {},
  850. }
  851. local targetHeroArr = occupyPointArrMsg[#occupyPointArrMsg].heroArr
  852. for _, heroInfo in ipairs(heroInfoArr) do
  853. targetHeroArr[#targetHeroArr+1] = {
  854. heroBody = heroInfo.heroBody,
  855. heroStar = heroInfo.heroStar,
  856. heroLv = heroInfo.heroLevel,
  857. heroCamp = heroInfo.heroCamp,
  858. heroIcon = heroInfo.heroIcon,
  859. heroId = heroInfo.heroId,
  860. heroQuality = heroInfo.heroQuality
  861. }
  862. end
  863. end
  864. end
  865. InnerMsg.sendMsg(fd, msgData)
  866. end
  867. -- 查询公会排行榜
  868. function N2C_UnionRank_Query(msg)
  869. local myUnionId = msg.myUnionId
  870. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  871. if not groupId then
  872. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  873. end
  874. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  875. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UNIONRANK_QUERY
  876. msgData.playerUuid = msg.playerUuid
  877. msgData.myUnionRank = 0
  878. msgData.unionRankArr = {}
  879. for rank, randkData in ipairs(unionRankList) do
  880. msgData.unionRankArr[rank] = {
  881. name = randkData.name,
  882. power = randkData.power,
  883. cityNum = randkData.occupyCityNum,
  884. pointNum = randkData.occupyPointNum,
  885. }
  886. if randkData.guildId == myUnionId then
  887. msgData.myUnionRank = rank
  888. end
  889. end
  890. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  891. InnerMsg.sendMsg(fd, msgData)
  892. end
  893. -- 查询玩家排行榜
  894. function N2C_PlayerRank_Query(msg)
  895. local myUnionId = msg.myUnionId
  896. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  897. if not groupId then
  898. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  899. end
  900. local playerRankList = AnotherWorldBattleDB.GetPlayerRankList(groupId)
  901. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYERRANK_QUERY
  902. msgData.playerUuid = msg.playerUuid
  903. msgData.myRank = 0
  904. msgData.playerRankArr = {}
  905. for rank, randkData in ipairs(playerRankList) do
  906. if rank > AnotherWorldBattleDefine.AB_RANK_MAX_NUM then
  907. break
  908. end
  909. msgData.playerRankArr[rank] = {
  910. name = randkData.name,
  911. power = randkData.power,
  912. pointNum = randkData.pointNum,
  913. pointWeight = randkData.pointAllWeight,
  914. }
  915. if randkData.playerId == msg.playerUuid then
  916. msgData.myRank = rank
  917. end
  918. end
  919. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  920. InnerMsg.sendMsg(fd, msgData)
  921. end
  922. -- 集结
  923. function N2C_Gather(msg)
  924. local sourceServerId = msg.sourceServerId
  925. local playerUuid = msg.playerUuid
  926. local myUnionId = msg.myUnionId
  927. if not isRunning() then
  928. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  929. end
  930. local groupId = getMyUnionGourpId(myUnionId)
  931. if not groupId then
  932. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  933. end
  934. local targetCityId = msg.targetCityId
  935. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  936. if not myUnionData then
  937. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  938. end
  939. local gatherInfo = myUnionData.gatherInfo
  940. if msg.opType == 1 then --集结
  941. -- 集结冷却时间判断
  942. local now = os.time()
  943. if gatherInfo then
  944. local gatherTime = gatherInfo.gatherTime
  945. if now - gatherTime < AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  946. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_6)
  947. end
  948. end
  949. -- 城池是否相邻判断
  950. local occupCityyArr = getUnionOccupyArr(myUnionId)
  951. if not isadJoin(occupCityyArr, targetCityId) then
  952. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_7)
  953. end
  954. myUnionData.gatherInfo = myUnionData.gatherInfo or {}
  955. myUnionData.gatherInfo.gatherTime = now
  956. myUnionData.gatherInfo.gatherCity = targetCityId
  957. else
  958. -- 取消集结
  959. if not gatherInfo then
  960. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  961. end
  962. if targetCityId ~= gatherInfo.gatherCity then
  963. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  964. end
  965. gatherInfo.gatherCity = 0
  966. end
  967. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  968. -- 推送城池数据给客户端,刷新界面
  969. N2C_CityDetailed_Query(msg)
  970. -- 发给本地服, 用于在公会聊天频道生成链接
  971. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GATHER
  972. msgData.playerUuid = msg.playerUuid
  973. msgData.targetCityId = targetCityId
  974. msgData.opType = msg.opType
  975. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  976. InnerMsg.sendMsg(fd, msgData)
  977. end
  978. -- 查询据点是否可以被挑战
  979. function N2C_Try_Challengde_Point(msg)
  980. local playerUuid = msg.playerUuid
  981. local myUnionId = msg.myUnionId
  982. local targetCityId = msg.targetCityId
  983. local targetPointIdx = msg.targetPointIdx
  984. local state = isCanChallengePoint(targetCityId, msg.targetPointIdx, msg.myUnionId, playerUuid)
  985. if state ~= 1 then
  986. return
  987. end
  988. local occupyPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  989. if occupyPointNum >= AnotherWorldBattleDefine.AB_PLAYER_OCCUPY_POINT_MAX_NUM then
  990. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_10)
  991. end
  992. local groupId = getMyUnionGourpId(myUnionId)
  993. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  994. local targetPointData = cityData.pointArr[targetPointIdx]
  995. local pointOccupyInfo = {targetCityId = targetCityId, targetPointIdx = targetPointIdx}
  996. if targetPointData.unionId then
  997. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  998. pointOccupyInfo.occupySrvId = occupyUnionData.serverId
  999. pointOccupyInfo.occupyPlayerUuid = targetPointData.playerUuid
  1000. end
  1001. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1002. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1003. pointOccupyInfo.isGather = 1
  1004. end
  1005. -- 检查据点是否有人挑战
  1006. local now = os.time()
  1007. local pointBattleTime = targetPointData.battleTime
  1008. if pointBattleTime and now - pointBattleTime < AnotherWorldBattleDefine.AB_POINT_BATTLE_TIME then
  1009. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_10)
  1010. end
  1011. -- 更新据点被挑战时间
  1012. targetPointData.battleTime = now
  1013. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1014. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_ISCAN_CHALLENGE
  1015. msgData.playerUuid = playerUuid
  1016. msgData.targetCityId = targetCityId
  1017. msgData.targetPointIdx = targetPointIdx
  1018. msgData.pointInfo = pointOccupyInfo
  1019. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1020. InnerMsg.sendMsg(fd, msgData)
  1021. end
  1022. -- 玩家挑战据点胜利
  1023. local function challenge_Win(msg)
  1024. local playerUuid = msg.playerUuid
  1025. local myUnionId = msg.myUnionId
  1026. local targetCityId = msg.targetCityId
  1027. local targetPointIdx = msg.targetPointIdx
  1028. local playerShowInfo = msg.playerShowInfo
  1029. if not isRunning() then
  1030. return
  1031. end
  1032. local groupId = getMyUnionGourpId(myUnionId)
  1033. if not groupId then
  1034. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1035. end
  1036. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1037. if not myUnionData then
  1038. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1039. end
  1040. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1041. local pointData = cityData.pointArr[targetPointIdx]
  1042. local now = os.time()
  1043. local cityNum, pointNum = 0, 1
  1044. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1045. -- 防守方是真实玩家
  1046. if pointData.unionId and pointData.playerUuid then
  1047. -- 更新防守方公会数据
  1048. local defUnionData = AnotherWorldBattleDB.GetUnionData(pointData.unionId)
  1049. local defOccupCityList = defUnionData.occupCityList
  1050. -- 防守方之前占领了城池
  1051. if defOccupCityList[targetCityId].isOccupy then
  1052. -- 修改防守方的占领状态
  1053. defOccupCityList[targetCityId].isOccupy = false
  1054. -- 更新防守方对该城池的最后占领时间
  1055. local defOccupyPointList = defOccupCityList[targetCityId].occupyPointList
  1056. local defPointInfo = defOccupyPointList[targetPointIdx]
  1057. local defOccupyTimeArr = defPointInfo.occupyTimeArr
  1058. defOccupyTimeArr[#defOccupyTimeArr][2] = now
  1059. -- 删除防守方的该据点的占领者
  1060. defPointInfo.playerUuid = nil
  1061. -- 删除防守方玩家的防守阵容数据
  1062. local defPlayData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  1063. if defPlayData then
  1064. defPlayData.heroList[targetCityId][targetPointIdx] = nil
  1065. AnotherWorldBattleDB.UpdatePlayerData(pointData.playerUuid, defPlayData)
  1066. end
  1067. cityNum = -1
  1068. end
  1069. -- 更新防守方公会的排行榜数据
  1070. AnotherWorldBattleDB.UpdateUnionRankList(groupId, pointData.unionId, cityNum, pointNum)
  1071. -- 更新防守方玩家的排行榜数据
  1072. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, pointData.playerUuid, -pointNum, -pointWeight)
  1073. -- 通知玩家
  1074. pointLose(defUnionData.serverId, pointData.playerUuid, targetCityId, targetPointIdx)
  1075. -- 更新防守方公会数据
  1076. AnotherWorldBattleDB.UpdateUnionData(pointData.unionId, defUnionData)
  1077. end
  1078. -- 更新据点最新占领者数据
  1079. pointData.unionId = myUnionId
  1080. pointData.playerUuid = playerUuid
  1081. -- 据点挑战结束
  1082. pointData.battleTime = nil
  1083. -- 更新进攻方公会数据—— 据点占有者, 最新的据点占有时间
  1084. myUnionData.occupCityList = myUnionData.occupCityList or {}
  1085. local occupCityList = myUnionData.occupCityList
  1086. occupCityList[targetCityId] = occupCityList[targetCityId] or {}
  1087. occupCityList[targetCityId].occupyPointList = occupCityList[targetCityId].occupyPointList or {}
  1088. occupCityList[targetCityId].occupyPointList[targetPointIdx] = occupCityList[targetCityId].occupyPointList[targetPointIdx] or {}
  1089. local atkPointInfo = occupCityList[targetCityId].occupyPointList[targetPointIdx]
  1090. atkPointInfo.playerUuid = playerUuid
  1091. atkPointInfo.occupyTimeArr = atkPointInfo.occupyTimeArr or {}
  1092. atkPointInfo.occupyTimeArr[#atkPointInfo.occupyTimeArr+1] = { now }
  1093. --更新进攻者的展示数据
  1094. local atkPlayerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1095. atkPlayerData = atkPlayerData or {}
  1096. atkPlayerData.name = playerShowInfo.name
  1097. atkPlayerData.lv = playerShowInfo.lv
  1098. atkPlayerData.head = playerShowInfo.head
  1099. atkPlayerData.headFrame = playerShowInfo.headFrame
  1100. atkPlayerData.power = playerShowInfo.power
  1101. atkPlayerData.unionId = myUnionId
  1102. -- 更新挑战次数
  1103. atkPlayerData.challengeTimes = (atkPlayerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1104. if not atkPlayerData.lastTime or atkPlayerData.lastTime == 0 then
  1105. atkPlayerData.lastTime = os.time()
  1106. end
  1107. -- 增加防守阵容数据
  1108. atkPlayerData.heroList = atkPlayerData.heroList or {}
  1109. atkPlayerData.heroList[targetCityId] = atkPlayerData.heroList[targetCityId] or {}
  1110. atkPlayerData.heroList[targetCityId][targetPointIdx] = playerShowInfo.heroArr
  1111. cityNum = 0
  1112. -- 进攻方完全占领城池了
  1113. if isCompleteOccupy(cityData, myUnionId) then
  1114. -- 更新城池的占领公会
  1115. cityData.occupyUnion = myUnionId
  1116. -- 更新进攻方公会对该城池的占领状态
  1117. occupCityList[targetCityId].isOccupy = true
  1118. -- 如果占领的是发起集结的城池, 那么取消集结
  1119. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1120. myUnionData.gatherInfo.gatherCity = 0
  1121. end
  1122. cityNum = 1
  1123. end
  1124. -- 更新进攻方公会数据
  1125. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1126. -- 更新进攻方玩家数据
  1127. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, atkPlayerData)
  1128. -- 更新进攻方公会的排行榜数据
  1129. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1130. -- 更新进攻方玩家的排行榜数据
  1131. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, pointWeight)
  1132. -- 更新城池数据
  1133. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1134. end
  1135. -- 玩家挑战据点失败
  1136. local function challenge_Fail(msg)
  1137. local playerUuid = msg.playerUuid
  1138. local myUnionId = msg.myUnionId
  1139. local targetCityId = msg.targetCityId
  1140. local targetPointIdx = msg.targetPointIdx
  1141. local groupId = getMyUnionGourpId(myUnionId)
  1142. if not groupId then
  1143. return
  1144. end
  1145. -- 据点挑战结束
  1146. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1147. if cityData and cityData.pointArr[targetPointIdx] then
  1148. local targetPointData = cityData.pointArr[targetPointIdx]
  1149. targetPointData.battleTime = nil
  1150. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1151. end
  1152. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1153. playerData = playerData or {}
  1154. playerData.name = playerData.name
  1155. playerData.lv = playerData.lv
  1156. playerData.power = playerData.power
  1157. playerData.unionId = myUnionId
  1158. playerData.challengeTimes = (playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1159. if not playerData.lastTime or playerData.lastTime == 0 then
  1160. playerData.lastTime = os.time()
  1161. end
  1162. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1163. -- 加入个人排行榜
  1164. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0)
  1165. end
  1166. --挑战据点结束的处理函数
  1167. function N2C_Point_Challenge_End_Handle(msg)
  1168. if msg.challengeRes == CombatDefine.RESULT_WIN then
  1169. challenge_Win(msg)
  1170. else
  1171. challenge_Fail(msg)
  1172. end
  1173. end
  1174. -- 玩家数据更新
  1175. function N2C_Update_Player_Data(msg)
  1176. if not isRunning() then
  1177. return
  1178. end
  1179. local playerUuid = msg.playerUuid
  1180. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1181. if not playerData then
  1182. return
  1183. end
  1184. local groupId = getMyUnionGourpId(playerData.unionId)
  1185. if not groupId then
  1186. return
  1187. end
  1188. local isUpdatePower = false
  1189. for k,v in pairs(msg.updateData) do
  1190. if k == "power" then
  1191. isUpdatePower = true
  1192. end
  1193. playerData[k] = v
  1194. end
  1195. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1196. -- 如果是战力更新, 更新玩家排行榜
  1197. if isUpdatePower then
  1198. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1199. end
  1200. end
  1201. -- 公会数据更新
  1202. function N2C_Update_Union_Data(msg)
  1203. if not isRunning() then
  1204. return
  1205. end
  1206. local myUnionId = msg.myUnionId
  1207. local unionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1208. if not unionData then
  1209. return
  1210. end
  1211. local groupId = getMyUnionGourpId(myUnionId)
  1212. if not groupId then
  1213. return
  1214. end
  1215. local isUpdatePower = false
  1216. for k,v in pairs(msg.updateData) do
  1217. if k == "power" then
  1218. isUpdatePower = true
  1219. end
  1220. unionData[k] = v
  1221. end
  1222. AnotherWorldBattleDB.UpdateUnionData(myUnionId, unionData)
  1223. -- 如果是战力更新, 更新公会排行榜
  1224. if isUpdatePower then
  1225. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, 0, 0, unionData.power)
  1226. end
  1227. end
  1228. -- 更新玩家据点防守阵容数据
  1229. function N2C_Update_Point_Lineup(msg)
  1230. if not isRunning() then
  1231. return
  1232. end
  1233. local playerUuid = msg.playerUuid
  1234. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1235. if not playerData or not playerData.heroList then
  1236. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1237. end
  1238. local groupId = getMyUnionGourpId(playerData.unionId)
  1239. if not groupId then
  1240. return
  1241. end
  1242. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1243. local heroList = playerData.heroList
  1244. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1245. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1246. end
  1247. heroList[targetCityId][targetPointIdx] = msg.heroArr
  1248. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1249. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UPDATE_POINT_LINEIP
  1250. msgData.playerUuid = playerUuid
  1251. msgData.targetCityId = targetCityId
  1252. msgData.targetPointIdx = targetPointIdx
  1253. msgData.formation = msg.formation
  1254. msgData.heroList = msg.heroList
  1255. msgData.helpList = msg.helpList
  1256. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1257. InnerMsg.sendMsg(fd, msgData)
  1258. end