AnotherWorldBattleCS.lua 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. if #correctTb == 0 then break end
  122. local matchIdx = correctTb[math.random(1, #correctTb)]
  123. usedTb[currentIdx] = true
  124. usedTb[matchIdx] = true
  125. table.insert(groupArr[#groupArr], unionArray[matchIdx])
  126. selectNum = selectNum + 1
  127. end
  128. currentIdx = currentIdx + 1
  129. end
  130. return groupArr
  131. end
  132. -- 分组
  133. local function grouping()
  134. local function genUnionIdArray(sourceUnionArr, targetUnionArr)
  135. for k, v in ipairs(sourceUnionArr) do
  136. targetUnionArr[k] = v.unionId
  137. end
  138. end
  139. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  140. if not joinUnionArr then
  141. -- 没有公会参加, 本轮活动结束
  142. local now = os.time()
  143. -- AnotherWorldBattleDB.UpdateLastRoundStartTime(now)
  144. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  145. end
  146. local len = #joinUnionArr
  147. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  148. table.sort(joinUnionArr, function (a, b)
  149. return a.power > b.power
  150. end)
  151. end
  152. local unionIdArr = {}
  153. genUnionIdArray(joinUnionArr, unionIdArr)
  154. local newGroupArray
  155. if len > AnotherWorldBattleDefine.AB_GROUP_UNION_NUM then
  156. newGroupArray = groupingAlgorithm(unionIdArr, len)
  157. else
  158. newGroupArray = { unionIdArr }
  159. end
  160. AnotherWorldBattleDB.UpdateGroupArray(newGroupArray)
  161. end
  162. -- 给各个分组中的公会随机分配出生点
  163. local function randomBaseCity()
  164. local baseCityIdArr = {}
  165. for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do
  166. if cityCfg.isBaseCity == 1 then
  167. baseCityIdArr[#baseCityIdArr+1] = cityId
  168. end
  169. end
  170. local now = os.time()
  171. local groupArray = AnotherWorldBattleDB.GetGroupArray()
  172. for _, unionIdArr in ipairs(groupArray) do
  173. -- 乱序
  174. table.shuffle(baseCityIdArr)
  175. for i, unionId in ipairs(unionIdArr) do
  176. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  177. if union then
  178. union.baseCityId = baseCityIdArr[i]
  179. union.baseCityStartTime = now
  180. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  181. end
  182. end
  183. end
  184. end
  185. -- 统计公会的占领情况, 用于发放奖励
  186. local function genUnionOccupyInfo(unionId, union, playerListData)
  187. local occupyTb = {
  188. occupyCityArr = {}, -- 当前还占领的城池列表
  189. point2CityIdArr = {}, -- 曾占领/当前还占领的据点的所属城池Id列表
  190. occuoyPointNum = 0,
  191. playerUuidArr = {},
  192. unionId = unionId,
  193. }
  194. local now = os.time()
  195. local t1 = occupyTb.occupyCityArr
  196. local t2 = occupyTb.point2CityIdArr
  197. t1[#t1+1] = union.baseCityId
  198. for cityId, cityIno in pairs(union.occupCityList or {}) do
  199. if cityIno.isOccupy then
  200. t1[#t1+1] = cityId
  201. end
  202. for _, pointInfo in pairs(cityIno.occupyPointList) do
  203. if pointInfo.playerUuid then
  204. if not cityIno.isOccupy then
  205. occupyTb.occuoyPointNum = occupyTb.occuoyPointNum + 1
  206. end
  207. -- 更新据点最新占领时间段的结束时间
  208. local occupyTimeArr = pointInfo.occupyTimeArr
  209. local lastTimeTb = occupyTimeArr[#occupyTimeArr]
  210. if #lastTimeTb == 1 then
  211. occupyTimeArr[#occupyTimeArr][2] = now
  212. end
  213. end
  214. t2[#t2+1] = {cityId, pointInfo.occupyTimeArr}
  215. end
  216. end
  217. -- 出生点算5个据点
  218. local baseCiyuTimeArr = { {union.baseCityStartTime, union.baseCityEndTime or now } }
  219. for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
  220. t2[#t2+1] = {union.baseCityId, baseCiyuTimeArr}
  221. end
  222. for playerUuid, playerInfo in pairs(playerListData) do
  223. if playerInfo.unionId == unionId then
  224. occupyTb.playerUuidArr[#occupyTb.playerUuidArr+1] = playerUuid
  225. end
  226. end
  227. -- 防止本次没有正常发放奖励,后续补发时, 结束时间异常的情况
  228. union.baseCityEndTime = now
  229. AnotherWorldBattleDB.UpdateUnionData(unionId, union)
  230. return occupyTb
  231. end
  232. -- 发奖
  233. local function issueReward(sourceServerId, occupyInfo)
  234. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ISSUEREWARD
  235. msgData.unionOccupyInfo = occupyInfo
  236. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  237. InnerMsg.sendMsg(fd, msgData)
  238. end
  239. -- 奖励发放管理函数
  240. function IssueRewardManager()
  241. local delay_sec = 0
  242. local unionList = AnotherWorldBattleDB.GetUnionList()
  243. local playerListData = AnotherWorldBattleDB.GetPlayerList()
  244. for unionId, union in pairs(unionList) do
  245. local occupyInfo = genUnionOccupyInfo(unionId, union, playerListData)
  246. delay_sec = delay_sec + 5
  247. Timer.addLater(delay_sec, issueReward, union.serverId, occupyInfo)
  248. end
  249. end
  250. -- 检测活动各阶段状态及相关处理
  251. local function timedStageHandle()
  252. -- 处于报名阶段
  253. -- if table.find(AnotherWorldBattleDefine.AB_JOIN_WDAY_AREA, wDay) then
  254. -- joinStageCheck()
  255. -- end
  256. if wDay == AnotherWorldBattleDefine.AB_JOIN_WDAY then
  257. joinStageCheck()
  258. end
  259. local now = os.time()
  260. -- 报名阶段 -> 战斗阶段
  261. if wDay >= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[1] and wDay <= AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] then
  262. local stage = AnotherWorldBattleDB.GetStage()
  263. local toDayStartTime = getTodayStartTime()
  264. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_START_SEC) then
  265. -- 分组
  266. grouping()
  267. -- 给各个分组中的公会随机分配出生点
  268. randomBaseCity()
  269. -- 改为战斗阶段
  270. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_BATTLE)
  271. end
  272. end
  273. -- 战斗阶段 -> 发奖阶段
  274. local toDayStartTime = getTodayStartTime()
  275. if wDay == AnotherWorldBattleDefine.AB_BATTLE_WDAY_AREA[2] and now >= (toDayStartTime + AnotherWorldBattleDefine.AB_BATTLE_END_SEC) then
  276. local stage = AnotherWorldBattleDB.GetStage()
  277. if stage == AnotherWorldBattleDefine.AB_STATE_BATTLE or stage == AnotherWorldBattleDefine.AB_STATE_AWARD then
  278. -- 改为发奖阶段
  279. AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_AWARD)
  280. -- 开始发奖
  281. IssueRewardManager()
  282. -- 改为结束阶段
  283. return AnotherWorldBattleDB.UpdateStage(AnotherWorldBattleDefine.AB_STATE_END)
  284. end
  285. end
  286. end
  287. function oneMin()
  288. if _G.is_middle ~= true then return end
  289. if not isOpen() then
  290. return
  291. end
  292. -- 与 onHour() 处理错开
  293. if Util.getMin() == 0 then
  294. return
  295. end
  296. timedStageHandle()
  297. end
  298. function onHour(hour)
  299. if _G.is_middle ~= true then return end
  300. if hour == 0 or not wDay then
  301. updateWDay()
  302. end
  303. if not isOpen() then
  304. return
  305. end
  306. timedStageHandle()
  307. end
  308. ------------------------------------C2N---------------------------------------------------
  309. -- 错误提示
  310. local function errTips(sourceServerId, playerUuid, errCode)
  311. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_TIPS
  312. msgData.playerUuid = playerUuid
  313. msgData.errCode = errCode
  314. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  315. InnerMsg.sendMsg(fd, msgData)
  316. end
  317. -- 通知玩家,据点被别的玩家占领了
  318. local function pointLose(sourceServerId, playerUuid, loseCityId, losePointIdx)
  319. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_LOSE
  320. msgData.playerUuid = playerUuid
  321. msgData.loseCityId = loseCityId
  322. msgData.losePointIdx = losePointIdx
  323. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  324. InnerMsg.sendMsg(fd, msgData)
  325. end
  326. ------------------------------------N2C---------------------------------------------------
  327. -- 获取自己公会的组Id
  328. local function getMyUnionGourpId(myUnionId)
  329. local groupId = AnotherWorldBattleDB.GetUnionGroupId(myUnionId)
  330. return groupId
  331. end
  332. -- 获取自己公会/所在区服第一公会的组Id
  333. local function getGroupId(myUnionId, myServerId)
  334. local groupId = getMyUnionGourpId(myUnionId)
  335. if not groupId then
  336. groupId = AnotherWorldBattleDB.GetGroupIdByServerId(myServerId)
  337. end
  338. return groupId
  339. end
  340. -- 统计单个服公会报名数量
  341. local function calcSvrUnionJoinNum(serverId)
  342. local num = 0
  343. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  344. if not joinUnionArr then
  345. return num
  346. end
  347. for _, unionInfo in ipairs(joinUnionArr) do
  348. if unionInfo.serverId == serverId then
  349. num = num + 1
  350. end
  351. end
  352. return num
  353. end
  354. -- 计算多个英雄的总战力
  355. local function calcHerosPower(heroArr)
  356. local power = 0
  357. for _, v in ipairs(heroArr) do
  358. power = power + v.heroPower
  359. end
  360. return power
  361. end
  362. -- 统计公会占领不同等级的城池数量
  363. local function calcOccupyCityNum(occupCityList, cityLv)
  364. if not occupCityList then
  365. return 0
  366. end
  367. local num = 0
  368. for cityId, occupyInfo in pairs(occupCityList) do
  369. if occupyInfo.isOccupy then
  370. local cityCfg = AnotherWorldBattleConfig.city[cityId]
  371. if cityCfg.cityLv == cityLv then
  372. num = num + 1
  373. end
  374. end
  375. end
  376. return num
  377. end
  378. -- 统计公会占领的据点所属城池数组
  379. local function calcOccupyPointArr(occupCityList)
  380. local pointInfoArr = {}
  381. if not occupCityList then
  382. return pointInfoArr
  383. end
  384. for cityId, occupyInfo in pairs(occupCityList) do
  385. for _, pointInfo in pairs(occupyInfo.occupyPointList) do
  386. if pointInfo.playerUuid then
  387. pointInfoArr[#pointInfoArr+1] = cityId
  388. end
  389. end
  390. end
  391. return pointInfoArr
  392. end
  393. -- 获取公会占领的城池列表
  394. local function getUnionOccupyArr(unionId)
  395. local union = AnotherWorldBattleDB.GetUnionData(unionId)
  396. if not union then
  397. return
  398. end
  399. local occupyCityArr = {}
  400. occupyCityArr[#occupyCityArr+1] = union.baseCityId
  401. for cityId, occupyInfo in pairs(union.occupCityList or {}) do
  402. if occupyInfo.isOccupy then
  403. occupyCityArr[#occupyCityArr+1] = cityId
  404. end
  405. end
  406. return occupyCityArr
  407. end
  408. -- 检查某个城池与公会占领的城池是否相邻
  409. local function isadJoin(cityIdArr, targetCityId)
  410. if not cityIdArr then
  411. return false
  412. end
  413. local targetCityCfg = AnotherWorldBattleConfig.city[targetCityId]
  414. if not targetCityCfg then
  415. return false
  416. end
  417. for _, cityId in ipairs(targetCityCfg.adJoinCityArr) do
  418. if table.find(cityIdArr, cityId) then
  419. return true
  420. end
  421. end
  422. return false
  423. end
  424. -- 统计玩家占据的据点数量
  425. local function calcPlayerOccupyPointNum(playerUuid)
  426. local playerOccupyPonitNum = 0
  427. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  428. if playerData and playerData.heroList then
  429. for _, pointList in pairs(playerData.heroList) do
  430. for _, _ in pairs(pointList) do
  431. playerOccupyPonitNum = playerOccupyPonitNum + 1
  432. end
  433. end
  434. end
  435. return playerOccupyPonitNum
  436. end
  437. -- 检查某个据点是否能被玩家挑战
  438. local function isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, playerUuid)
  439. -- 活动未开启
  440. if not isRunning() then
  441. return -1
  442. end
  443. -- 公会没有参加活动
  444. local groupId = getMyUnionGourpId(myUnionId)
  445. if not groupId then
  446. return -2
  447. end
  448. local tagetCityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  449. if not tagetCityData then
  450. return -3
  451. end
  452. local targetPointData = tagetCityData.pointArr[targetPointIdx]
  453. if not targetPointData then
  454. return -5
  455. end
  456. -- 自己占领了该据点
  457. if targetPointData.unionId and targetPointData.unionId == myUnionId and targetPointData.playerUuid == playerUuid then
  458. return 2
  459. end
  460. -- 据点已被本公会占领
  461. if tagetCityData.occupyUnion and tagetCityData.occupyUnion == myUnionId then
  462. return -4
  463. end
  464. -- 据点已被本公会占领
  465. if targetPointData.unionId and targetPointData.unionId == myUnionId then
  466. return -6
  467. end
  468. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  469. if not myUnionData then
  470. return -7
  471. end
  472. -- 城池是否相邻
  473. local occupyCityArr = getUnionOccupyArr(myUnionId)
  474. if not isadJoin(occupyCityArr, targetCityId) then
  475. return -8
  476. end
  477. return 1
  478. end
  479. -- 公会是否能报名
  480. local function isCanJoin(sourceServerId, myUnionId)
  481. if not isRunning() then
  482. return 2
  483. end
  484. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  485. for _, unionData in ipairs(joinUnionArr or {}) do
  486. if unionData.unionId == myUnionId then
  487. return 1
  488. end
  489. end
  490. local num = calcSvrUnionJoinNum(sourceServerId)
  491. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  492. return 2
  493. end
  494. return 0
  495. end
  496. -- 检查城池是否被己方公会完全占领
  497. local function isCompleteOccupy(cityData, myUnionId)
  498. for _, pointInfo in ipairs(cityData.pointArr) do
  499. if not pointInfo.unionId or pointInfo.unionId ~= myUnionId then
  500. return false
  501. end
  502. end
  503. return true
  504. end
  505. -- 获取玩家挑战次数相关数据
  506. local function getPlayerChallengeTimesInfo(playerUuid)
  507. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  508. if not playerData then
  509. return AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES, 0
  510. end
  511. local now = os.time()
  512. local subSex = now - playerData.lastTime
  513. if subSex >= AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC then
  514. local addTimes = math.floor(subSex / AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC)
  515. playerData.challengeTimes = math.min(playerData.challengeTimes + addTimes, AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES)
  516. playerData.lastTime = playerData.lastTime + addTimes* AnotherWorldBattleDefine.AB_PLAYER_CHALLENGETIMES_SEC
  517. if playerData.challengeTimes == AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES then
  518. playerData.lastTime = 0
  519. end
  520. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  521. end
  522. return playerData.challengeTimes, playerData.lastTime
  523. end
  524. -- 获取城池被占领状态
  525. local function getCityState(cityData)
  526. local state = 0
  527. if cityData.occupyUnion then
  528. state = 1
  529. return state
  530. end
  531. local occupyPlayerUuid
  532. for _, pointInfo in ipairs(cityData.pointArr) do
  533. if pointInfo.playerUuid then
  534. if not occupyPlayerUuid then
  535. occupyPlayerUuid = pointInfo.playerUuid
  536. end
  537. if pointInfo.playerUuid ~= occupyPlayerUuid then
  538. state = 2
  539. break
  540. end
  541. end
  542. end
  543. return state
  544. end
  545. -- 查询状态
  546. function N2C_Get_State(msg)
  547. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GET_STATE
  548. msgData.playerUuid = msg.playerUuid
  549. msgData.systemState = 0
  550. msgData.joinState = 0
  551. local stage = AnotherWorldBattleDB.GetStage()
  552. msgData.systemState = stage
  553. if stage == 0 then
  554. local lastRoundStartTime = AnotherWorldBattleDB.GetLastRoundStartTime()
  555. if lastRoundStartTime == 0 then
  556. msgData.systemState = 0
  557. else
  558. msgData.systemState = 3
  559. end
  560. end
  561. local sourceServerId = msg.sourceServerId
  562. local num = calcSvrUnionJoinNum(sourceServerId)
  563. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  564. msgData.joinState = 2
  565. end
  566. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  567. for _, unionData in ipairs(joinUnionArr or {}) do
  568. if unionData.unionId == msg.myUnionId then
  569. msgData.joinState = 1
  570. end
  571. end
  572. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  573. InnerMsg.sendMsg(fd, msgData)
  574. end
  575. -- 报名
  576. function N2C_Join(msg)
  577. local sourceServerId = msg.sourceServerId
  578. local playerUuid = msg.playerUuid
  579. local unionInfo = msg.unionInfo
  580. local state = isCanJoin(sourceServerId, unionInfo.unionId)
  581. if state == 2 then
  582. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  583. elseif state == 1 then
  584. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_8)
  585. end
  586. -- 更新参赛列表数据
  587. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  588. joinUnionArr = joinUnionArr or {}
  589. joinUnionArr[#joinUnionArr+1] = {
  590. unionId = unionInfo.unionId,
  591. power = unionInfo.power,
  592. serverId = sourceServerId
  593. }
  594. AnotherWorldBattleDB.UpdateJoinUnionArr(joinUnionArr)
  595. -- 更新公会列表数据
  596. local newUnionData = {
  597. serverId = sourceServerId,
  598. power = unionInfo.power,
  599. name = unionInfo.name,
  600. }
  601. AnotherWorldBattleDB.UpdateUnionData(unionInfo.unionId, newUnionData)
  602. -- 通知报名成功
  603. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_JOIN
  604. msgData.playerUuid = playerUuid
  605. msgData.myUnionId = unionInfo.unionId
  606. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  607. InnerMsg.sendMsg(fd, msgData)
  608. end
  609. -- 查询所有城池信息
  610. function N2C_AllCity_Query(msg)
  611. local myUnionId = msg.myUnionId
  612. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ALLCITY_QUERY
  613. msgData.cityArr = {}
  614. msgData.playerUuid = msg.playerUuid
  615. msgData.myUnionBaseCityId = 0
  616. msgData.myOccupyCityArr = {}
  617. msgData.hasPointNum = 0
  618. msgData.challengeTimes = 0
  619. msgData.lastTime = 0
  620. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  621. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  622. if not groupId then
  623. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  624. local stage = AnotherWorldBattleDB.GetStage()
  625. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  626. return InnerMsg.sendMsg(fd, msgData)
  627. end
  628. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  629. end
  630. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  631. if not cityList then
  632. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  633. end
  634. local cityArrMsg = msgData.cityArr
  635. local unionList = AnotherWorldBattleDB.GetUnionList()
  636. msgData.myUnionBaseCityId = unionList[myUnionId] and unionList[myUnionId].baseCityId or 0
  637. for cityId, cityInfo in ipairs(cityList) do
  638. cityArrMsg[cityId] = { occupyPointNum = 0 }
  639. if cityInfo.occupyUnion then
  640. cityArrMsg[cityId].occupyUnionName = unionList[cityInfo.occupyUnion] and unionList[cityInfo.occupyUnion].name
  641. end
  642. for _, pointInfo in ipairs(cityInfo.pointArr) do
  643. if pointInfo.unionId and pointInfo.unionId == myUnionId then
  644. cityArrMsg[cityId].occupyPointNum = cityArrMsg[cityId].occupyPointNum + 1
  645. end
  646. end
  647. cityArrMsg[cityId].cityState = getCityState(cityInfo)
  648. end
  649. msgData.myOccupyCityArr = getUnionOccupyArr(myUnionId)
  650. msgData.hasPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  651. local challengeTimes, leftTime = getPlayerChallengeTimesInfo(msg.playerUuid)
  652. msgData.challengeTimes = challengeTimes
  653. msgData.lastTime = leftTime
  654. InnerMsg.sendMsg(fd, msgData)
  655. end
  656. -- 查询某个城池的详细信息
  657. function N2C_CityDetailed_Query(msg)
  658. local myUnionId = msg.myUnionId
  659. local playerUuid = msg.playerUuid
  660. local targetCityId = msg.targetCityId
  661. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  662. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_CITYDETAILED_QUERY
  663. msgData.playerUuid = playerUuid
  664. msgData.targetCityId = targetCityId
  665. msgData.myUnionOccupyArr = {}
  666. msgData.pointArr = {}
  667. msgData.gatherState = 0
  668. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  669. if not groupId then
  670. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  671. local stage = AnotherWorldBattleDB.GetStage()
  672. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  673. return InnerMsg.sendMsg(fd, msgData)
  674. end
  675. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  676. end
  677. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  678. if not cityData then
  679. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  680. end
  681. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  682. local occupCityList = union and union.occupCityList
  683. local myUnionOccupyArr = calcOccupyPointArr(occupCityList)
  684. if union then
  685. myUnionOccupyArr[#myUnionOccupyArr+1] = union.baseCityId
  686. end
  687. msgData.myUnionOccupyArr = myUnionOccupyArr
  688. local pointArrMsg = msgData.pointArr
  689. for pointIdx, occupyInfo in ipairs(cityData.pointArr) do
  690. pointArrMsg[pointIdx] = {}
  691. local state = isCanChallengePoint(targetCityId, pointIdx, myUnionId, playerUuid)
  692. pointArrMsg[pointIdx].state = state < 0 and 0 or state
  693. if occupyInfo.unionId and occupyInfo.playerUuid then
  694. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(occupyInfo.unionId)
  695. local occupyPlayerData = AnotherWorldBattleDB.GetPlayerData(occupyInfo.playerUuid)
  696. if occupyUnionData and occupyPlayerData then
  697. pointArrMsg[pointIdx].occupyUnionName = occupyUnionData.name
  698. pointArrMsg[pointIdx].occupyPlayerName = occupyPlayerData.name
  699. pointArrMsg[pointIdx].power = 0
  700. local heroList = occupyPlayerData.heroList
  701. if heroList and heroList[targetCityId] and heroList[targetCityId][pointIdx] then
  702. pointArrMsg[pointIdx].power = calcHerosPower(heroList[targetCityId][pointIdx])
  703. end
  704. end
  705. end
  706. end
  707. local isOk = isRunning()
  708. if isOk then
  709. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  710. local occupyCityArr = getUnionOccupyArr(myUnionId)
  711. isOk = isadJoin(occupyCityArr, targetCityId)
  712. if isOk then
  713. if not cityData.occupyUnion or cityData.occupyUnion ~= myUnionId then
  714. if not myUnionData.gatherInfo then
  715. msgData.gatherState = 1
  716. else
  717. if myUnionData.gatherInfo.gatherCity == targetCityId then
  718. msgData.gatherState = 2
  719. else
  720. local now = os.time()
  721. if now - myUnionData.gatherInfo.gatherTime >= AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  722. msgData.gatherState = 1
  723. end
  724. end
  725. end
  726. end
  727. end
  728. end
  729. InnerMsg.sendMsg(fd, msgData)
  730. end
  731. -- 查询某个据点信息
  732. function N2C_PointDetailed_Query(msg)
  733. local myUnionId = msg.myUnionId
  734. local targetCityId = msg.targetCityId
  735. local targetPointIdx = msg.targetPointIdx
  736. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  737. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINTDETAILED_QUERY
  738. msgData.playerUuid = msg.playerUuid
  739. msgData.targetCityId = targetCityId
  740. msgData.targetPointIdx = targetPointIdx
  741. msgData.pointInfo = {}
  742. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  743. if not groupId then
  744. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  745. local stage = AnotherWorldBattleDB.GetStage()
  746. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  747. return InnerMsg.sendMsg(fd, msgData)
  748. end
  749. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  750. end
  751. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  752. if not cityData then
  753. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  754. end
  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 cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  995. local targetPointData = cityData.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. -- 检查据点是否有人挑战
  1007. local now = os.time()
  1008. local pointBattleTime = targetPointData.battleTime
  1009. if pointBattleTime and now - pointBattleTime < AnotherWorldBattleDefine.AB_POINT_BATTLE_TIME then
  1010. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_11)
  1011. end
  1012. -- 更新据点被挑战时间
  1013. targetPointData.battleTime = now
  1014. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1015. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_ISCAN_CHALLENGE
  1016. msgData.playerUuid = playerUuid
  1017. msgData.targetCityId = targetCityId
  1018. msgData.targetPointIdx = targetPointIdx
  1019. msgData.pointInfo = pointOccupyInfo
  1020. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1021. InnerMsg.sendMsg(fd, msgData)
  1022. end
  1023. -- 玩家挑战据点胜利
  1024. local function challenge_Win(msg)
  1025. local playerUuid = msg.playerUuid
  1026. local myUnionId = msg.myUnionId
  1027. local targetCityId = msg.targetCityId
  1028. local targetPointIdx = msg.targetPointIdx
  1029. local playerShowInfo = msg.playerShowInfo
  1030. if not isRunning() then
  1031. return
  1032. end
  1033. local groupId = getMyUnionGourpId(myUnionId)
  1034. if not groupId then
  1035. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1036. end
  1037. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1038. if not myUnionData then
  1039. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1040. end
  1041. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1042. local pointData = cityData.pointArr[targetPointIdx]
  1043. local now = os.time()
  1044. local cityNum, pointNum = 0, 1
  1045. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1046. -- 如果城池之前被占领了, 则重置占领城池的公会Id
  1047. if cityData.occupyUnion then
  1048. cityData.occupyUnion = nil
  1049. end
  1050. -- 防守方是真实玩家
  1051. if pointData.unionId and pointData.playerUuid then
  1052. -- 更新防守方公会数据
  1053. local defUnionData = AnotherWorldBattleDB.GetUnionData(pointData.unionId)
  1054. local defOccupCityList = defUnionData.occupCityList
  1055. -- 防守方之前占领了城池
  1056. if defOccupCityList[targetCityId].isOccupy then
  1057. -- 修改防守方的占领状态
  1058. defOccupCityList[targetCityId].isOccupy = false
  1059. -- 更新防守方对该城池的最后占领时间
  1060. local defOccupyPointList = defOccupCityList[targetCityId].occupyPointList
  1061. local defPointInfo = defOccupyPointList[targetPointIdx]
  1062. local defOccupyTimeArr = defPointInfo.occupyTimeArr
  1063. defOccupyTimeArr[#defOccupyTimeArr][2] = now
  1064. -- 删除防守方的该据点的占领者
  1065. defPointInfo.playerUuid = nil
  1066. -- 删除防守方玩家的防守阵容数据
  1067. local defPlayData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  1068. if defPlayData then
  1069. defPlayData.heroList[targetCityId][targetPointIdx] = nil
  1070. AnotherWorldBattleDB.UpdatePlayerData(pointData.playerUuid, defPlayData)
  1071. end
  1072. cityNum = -1
  1073. end
  1074. -- 更新防守方公会的排行榜数据
  1075. AnotherWorldBattleDB.UpdateUnionRankList(groupId, pointData.unionId, cityNum, pointNum)
  1076. -- 更新防守方玩家的排行榜数据
  1077. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, pointData.playerUuid, -pointNum, -pointWeight)
  1078. -- 通知玩家
  1079. pointLose(defUnionData.serverId, pointData.playerUuid, targetCityId, targetPointIdx)
  1080. -- 更新防守方公会数据
  1081. AnotherWorldBattleDB.UpdateUnionData(pointData.unionId, defUnionData)
  1082. end
  1083. -- 更新据点最新占领者数据
  1084. pointData.unionId = myUnionId
  1085. pointData.playerUuid = playerUuid
  1086. -- 据点挑战结束
  1087. pointData.battleTime = nil
  1088. -- 更新进攻方公会数据—— 据点占有者, 最新的据点占有时间
  1089. myUnionData.occupCityList = myUnionData.occupCityList or {}
  1090. local occupCityList = myUnionData.occupCityList
  1091. occupCityList[targetCityId] = occupCityList[targetCityId] or {}
  1092. occupCityList[targetCityId].occupyPointList = occupCityList[targetCityId].occupyPointList or {}
  1093. occupCityList[targetCityId].occupyPointList[targetPointIdx] = occupCityList[targetCityId].occupyPointList[targetPointIdx] or {}
  1094. local atkPointInfo = occupCityList[targetCityId].occupyPointList[targetPointIdx]
  1095. atkPointInfo.playerUuid = playerUuid
  1096. atkPointInfo.occupyTimeArr = atkPointInfo.occupyTimeArr or {}
  1097. atkPointInfo.occupyTimeArr[#atkPointInfo.occupyTimeArr+1] = { now }
  1098. --更新进攻者的展示数据
  1099. local atkPlayerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1100. atkPlayerData = atkPlayerData or {}
  1101. atkPlayerData.name = playerShowInfo.name
  1102. atkPlayerData.lv = playerShowInfo.lv
  1103. atkPlayerData.head = playerShowInfo.head
  1104. atkPlayerData.headFrame = playerShowInfo.headFrame
  1105. atkPlayerData.power = playerShowInfo.power
  1106. atkPlayerData.unionId = myUnionId
  1107. -- 更新挑战次数
  1108. atkPlayerData.challengeTimes = (atkPlayerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1109. if not atkPlayerData.lastTime or atkPlayerData.lastTime == 0 then
  1110. atkPlayerData.lastTime = os.time()
  1111. end
  1112. -- 增加防守阵容数据
  1113. atkPlayerData.heroList = atkPlayerData.heroList or {}
  1114. atkPlayerData.heroList[targetCityId] = atkPlayerData.heroList[targetCityId] or {}
  1115. atkPlayerData.heroList[targetCityId][targetPointIdx] = playerShowInfo.heroArr
  1116. cityNum = 0
  1117. -- 进攻方完全占领城池了
  1118. if isCompleteOccupy(cityData, myUnionId) then
  1119. -- 更新城池的占领公会
  1120. cityData.occupyUnion = myUnionId
  1121. -- 更新进攻方公会对该城池的占领状态
  1122. occupCityList[targetCityId].isOccupy = true
  1123. -- 如果占领的是发起集结的城池, 那么取消集结
  1124. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1125. myUnionData.gatherInfo.gatherCity = 0
  1126. end
  1127. cityNum = 1
  1128. end
  1129. -- 更新进攻方公会数据
  1130. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1131. -- 更新进攻方玩家数据
  1132. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, atkPlayerData)
  1133. -- 更新进攻方公会的排行榜数据
  1134. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1135. -- 更新进攻方玩家的排行榜数据
  1136. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, pointWeight, playerShowInfo.power)
  1137. -- 更新城池数据
  1138. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1139. end
  1140. -- 玩家挑战据点失败
  1141. local function challenge_Fail(msg)
  1142. local playerUuid = msg.playerUuid
  1143. local myUnionId = msg.myUnionId
  1144. local targetCityId = msg.targetCityId
  1145. local targetPointIdx = msg.targetPointIdx
  1146. local groupId = getMyUnionGourpId(myUnionId)
  1147. if not groupId then
  1148. return
  1149. end
  1150. -- 据点挑战结束
  1151. local cityData = AnotherWorldBattleDB.GetCityData(groupId, targetCityId)
  1152. if cityData and cityData.pointArr[targetPointIdx] then
  1153. local targetPointData = cityData.pointArr[targetPointIdx]
  1154. targetPointData.battleTime = nil
  1155. AnotherWorldBattleDB.UpdateCityData(groupId, targetCityId, cityData)
  1156. end
  1157. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1158. playerData = playerData or {}
  1159. playerData.name = playerData.name
  1160. playerData.lv = playerData.lv
  1161. playerData.power = playerData.power
  1162. playerData.unionId = myUnionId
  1163. playerData.challengeTimes = (playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1164. if not playerData.lastTime or playerData.lastTime == 0 then
  1165. playerData.lastTime = os.time()
  1166. end
  1167. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1168. -- 加入个人排行榜
  1169. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1170. end
  1171. --挑战据点结束的处理函数
  1172. function N2C_Point_Challenge_End_Handle(msg)
  1173. if msg.challengeRes == CombatDefine.RESULT_WIN then
  1174. challenge_Win(msg)
  1175. else
  1176. challenge_Fail(msg)
  1177. end
  1178. end
  1179. -- 玩家数据更新
  1180. function N2C_Update_Player_Data(msg)
  1181. if not isRunning() then
  1182. return
  1183. end
  1184. local playerUuid = msg.playerUuid
  1185. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1186. if not playerData then
  1187. return
  1188. end
  1189. local groupId = getMyUnionGourpId(playerData.unionId)
  1190. if not groupId then
  1191. return
  1192. end
  1193. local isUpdatePower = false
  1194. for k,v in pairs(msg.updateData) do
  1195. if k == "power" then
  1196. isUpdatePower = true
  1197. end
  1198. playerData[k] = v
  1199. end
  1200. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1201. -- 如果是战力更新, 更新玩家排行榜
  1202. if isUpdatePower then
  1203. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1204. end
  1205. end
  1206. -- 公会数据更新
  1207. function N2C_Update_Union_Data(msg)
  1208. if not isRunning() then
  1209. return
  1210. end
  1211. local myUnionId = msg.myUnionId
  1212. local unionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1213. if not unionData then
  1214. return
  1215. end
  1216. local groupId = getMyUnionGourpId(myUnionId)
  1217. if not groupId then
  1218. return
  1219. end
  1220. local isUpdatePower = false
  1221. for k,v in pairs(msg.updateData) do
  1222. if k == "power" then
  1223. isUpdatePower = true
  1224. end
  1225. unionData[k] = v
  1226. end
  1227. AnotherWorldBattleDB.UpdateUnionData(myUnionId, unionData)
  1228. -- 如果是战力更新, 更新公会排行榜
  1229. if isUpdatePower then
  1230. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, 0, 0, unionData.power)
  1231. end
  1232. end
  1233. -- 更新玩家据点防守阵容数据
  1234. function N2C_Update_Point_Lineup(msg)
  1235. if not isRunning() then
  1236. return
  1237. end
  1238. local playerUuid = msg.playerUuid
  1239. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1240. if not playerData or not playerData.heroList then
  1241. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1242. end
  1243. local groupId = getMyUnionGourpId(playerData.unionId)
  1244. if not groupId then
  1245. return
  1246. end
  1247. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1248. local heroList = playerData.heroList
  1249. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1250. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1251. end
  1252. heroList[targetCityId][targetPointIdx] = msg.heroArr
  1253. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1254. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UPDATE_POINT_LINEIP
  1255. msgData.playerUuid = playerUuid
  1256. msgData.targetCityId = targetCityId
  1257. msgData.targetPointIdx = targetPointIdx
  1258. msgData.formation = msg.formation
  1259. msgData.heroList = msg.heroList
  1260. msgData.helpList = msg.helpList
  1261. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1262. InnerMsg.sendMsg(fd, msgData)
  1263. end