AnotherWorldBattleCS.lua 50 KB

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