AnotherWorldBattleCS.lua 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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. end
  556. end
  557. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  558. for _, unionData in ipairs(joinUnionArr or {}) do
  559. if unionData.unionId == msg.myUnionId then
  560. msgData.joinState = 1
  561. end
  562. end
  563. local sourceServerId = msg.sourceServerId
  564. local num = calcSvrUnionJoinNum(sourceServerId)
  565. if num >= AnotherWorldBattleDefine.AB_SRV_UNION_MAX_NUM then
  566. msgData.joinState = 2
  567. end
  568. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  569. InnerMsg.sendMsg(fd, msgData)
  570. end
  571. -- 报名
  572. function N2C_Join(msg)
  573. local sourceServerId = msg.sourceServerId
  574. local playerUuid = msg.playerUuid
  575. local unionInfo = msg.unionInfo
  576. local state = isCanJoin(sourceServerId, unionInfo.unionId)
  577. if state == 2 then
  578. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  579. elseif state == 1 then
  580. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_8)
  581. end
  582. -- 更新参赛列表数据
  583. local joinUnionArr = AnotherWorldBattleDB.GetJoinUnionArr()
  584. joinUnionArr = joinUnionArr or {}
  585. joinUnionArr[#joinUnionArr+1] = {
  586. unionId = unionInfo.unionId,
  587. power = unionInfo.power,
  588. serverId = sourceServerId
  589. }
  590. AnotherWorldBattleDB.UpdateJoinUnionArr(joinUnionArr)
  591. -- 更新公会列表数据
  592. local newUnionData = {
  593. serverId = sourceServerId,
  594. power = unionInfo.power,
  595. name = unionInfo.name,
  596. }
  597. AnotherWorldBattleDB.UpdateUnionData(unionInfo.unionId, newUnionData)
  598. -- 通知报名成功
  599. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_JOIN
  600. msgData.playerUuid = playerUuid
  601. msgData.myUnionId = unionInfo.unionId
  602. local fd = MiddleManager.getFDBySvrIndex(sourceServerId)
  603. InnerMsg.sendMsg(fd, msgData)
  604. end
  605. -- 查询所有城池信息
  606. function N2C_AllCity_Query(msg)
  607. local myUnionId = msg.myUnionId
  608. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_ALLCITY_QUERY
  609. msgData.cityArr = {}
  610. msgData.playerUuid = msg.playerUuid
  611. msgData.myUnionBaseCityId = 0
  612. msgData.myOccupyCityArr = {}
  613. msgData.hasPointNum = 0
  614. msgData.challengeTimes = 0
  615. msgData.lastTime = 0
  616. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  617. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  618. if not groupId then
  619. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  620. local stage = AnotherWorldBattleDB.GetStage()
  621. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  622. return InnerMsg.sendMsg(fd, msgData)
  623. end
  624. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  625. end
  626. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  627. if not cityList then
  628. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  629. end
  630. local cityArrMsg = msgData.cityArr
  631. local unionList = AnotherWorldBattleDB.GetUnionList()
  632. msgData.myUnionBaseCityId = unionList[myUnionId] and unionList[myUnionId].baseCityId or 0
  633. for cityId, cityInfo in ipairs(cityList) do
  634. cityArrMsg[cityId] = { occupyPointNum = 0 }
  635. if cityInfo.occupyUnion then
  636. cityArrMsg[cityId].occupyUnionName = unionList[cityInfo.occupyUnion] and unionList[cityInfo.occupyUnion].name
  637. end
  638. for _, pointInfo in ipairs(cityInfo.pointArr) do
  639. if pointInfo.unionId and pointInfo.unionId == myUnionId then
  640. cityArrMsg[cityId].occupyPointNum = cityArrMsg[cityId].occupyPointNum + 1
  641. end
  642. end
  643. cityArrMsg[cityId].cityState = getCityState(cityInfo)
  644. end
  645. msgData.myOccupyCityArr = getUnionOccupyArr(myUnionId)
  646. msgData.hasPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  647. local challengeTimes, leftTime = getPlayerChallengeTimesInfo(msg.playerUuid)
  648. msgData.challengeTimes = challengeTimes
  649. msgData.lastTime = leftTime
  650. InnerMsg.sendMsg(fd, msgData)
  651. end
  652. -- 查询某个城池的详细信息
  653. function N2C_CityDetailed_Query(msg)
  654. local myUnionId = msg.myUnionId
  655. local playerUuid = msg.playerUuid
  656. local targetCityId = msg.targetCityId
  657. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  658. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_CITYDETAILED_QUERY
  659. msgData.playerUuid = playerUuid
  660. msgData.targetCityId = targetCityId
  661. msgData.myUnionOccupyArr = {}
  662. msgData.pointArr = {}
  663. msgData.gatherState = 0
  664. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  665. if not groupId then
  666. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  667. local stage = AnotherWorldBattleDB.GetStage()
  668. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  669. return InnerMsg.sendMsg(fd, msgData)
  670. end
  671. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  672. end
  673. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  674. if not cityList or not cityList[targetCityId] then
  675. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  676. end
  677. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  678. local occupCityList = union and union.occupCityList
  679. local myUnionOccupyArr = calcOccupyPointArr(occupCityList)
  680. if union then
  681. myUnionOccupyArr[#myUnionOccupyArr+1] = union.baseCityId
  682. end
  683. msgData.myUnionOccupyArr = myUnionOccupyArr
  684. local cityData = cityList[targetCityId]
  685. local pointArrMsg = msgData.pointArr
  686. for pointIdx, occupyInfo in ipairs(cityData.pointArr) do
  687. pointArrMsg[pointIdx] = {}
  688. local state = isCanChallengePoint(targetCityId, pointIdx, myUnionId, playerUuid)
  689. pointArrMsg[pointIdx].state = state < 0 and 0 or state
  690. if occupyInfo.unionId and occupyInfo.playerUuid then
  691. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(occupyInfo.unionId)
  692. local occupyPlayerData = AnotherWorldBattleDB.GetPlayerData(occupyInfo.playerUuid)
  693. if occupyUnionData and occupyPlayerData then
  694. pointArrMsg[pointIdx].occupyUnionName = occupyUnionData.name
  695. pointArrMsg[pointIdx].occupyPlayerName = occupyPlayerData.name
  696. pointArrMsg[pointIdx].power = 0
  697. local heroList = occupyPlayerData.heroList
  698. if heroList and heroList[targetCityId] and heroList[targetCityId][pointIdx] then
  699. pointArrMsg[pointIdx].power = calcHerosPower(heroList[targetCityId][pointIdx])
  700. end
  701. end
  702. end
  703. end
  704. local isOk = isRunning()
  705. if isOk then
  706. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  707. local occupyCityArr = getUnionOccupyArr(myUnionId)
  708. isOk = isadJoin(occupyCityArr, targetCityId)
  709. if isOk then
  710. if not cityData.occupyUnion or cityData.occupyUnion ~= myUnionId then
  711. if not myUnionData.gatherInfo then
  712. msgData.gatherState = 1
  713. else
  714. if myUnionData.gatherInfo.gatherCity == targetCityId then
  715. msgData.gatherState = 2
  716. else
  717. local now = os.time()
  718. if now - myUnionData.gatherInfo.gatherTime >= AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  719. msgData.gatherState = 1
  720. end
  721. end
  722. end
  723. end
  724. end
  725. end
  726. InnerMsg.sendMsg(fd, msgData)
  727. end
  728. -- 查询某个据点信息
  729. function N2C_PointDetailed_Query(msg)
  730. local myUnionId = msg.myUnionId
  731. local targetCityId = msg.targetCityId
  732. local targetPointIdx = msg.targetPointIdx
  733. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  734. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINTDETAILED_QUERY
  735. msgData.playerUuid = msg.playerUuid
  736. msgData.targetCityId = targetCityId
  737. msgData.targetPointIdx = targetPointIdx
  738. msgData.pointInfo = {}
  739. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  740. if not groupId then
  741. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  742. local stage = AnotherWorldBattleDB.GetStage()
  743. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  744. return InnerMsg.sendMsg(fd, msgData)
  745. end
  746. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  747. end
  748. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  749. if not cityList or not cityList[targetCityId] then
  750. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  751. end
  752. local cityData = cityList[targetCityId]
  753. local pointData = cityData.pointArr[targetPointIdx]
  754. if not pointData then
  755. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  756. end
  757. local pointInfoMsg = msgData.pointInfo
  758. if pointData.unionId and pointData.playerUuid then
  759. local playerData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  760. if not playerData then
  761. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  762. end
  763. pointInfoMsg.name = playerData.name
  764. pointInfoMsg.head = playerData.head
  765. pointInfoMsg.headFrame = playerData.headFrame
  766. pointInfoMsg.defLv = playerData.lv
  767. local targetHeroArr = playerData.heroList[targetCityId][targetPointIdx]
  768. if not targetHeroArr then
  769. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_4)
  770. end
  771. pointInfoMsg.power = calcHerosPower(targetHeroArr)
  772. pointInfoMsg.heroArr = {}
  773. for i, heroInfo in ipairs(targetHeroArr) do
  774. pointInfoMsg.heroArr[i] = {
  775. heroBody = heroInfo.heroBody,
  776. heroStar = heroInfo.heroStar,
  777. heroLv = heroInfo.heroLevel,
  778. heroCamp = heroInfo.heroCamp,
  779. heroIcon = heroInfo.heroIcon,
  780. heroId = heroInfo.heroId,
  781. heroQuality = heroInfo.heroQuality,
  782. }
  783. end
  784. end
  785. local state = isCanChallengePoint(targetCityId, targetPointIdx, myUnionId, msg.playerUuid)
  786. pointInfoMsg.state = state < 0 and 0 or state
  787. InnerMsg.sendMsg(fd, msgData)
  788. end
  789. -- 查询公会出生点信息
  790. function N2C_BaseCity_Query(msg)
  791. local myUnionId = msg.myUnionId
  792. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  793. if not groupId then
  794. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  795. end
  796. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_BASECITY_QUERY
  797. msgData.playerUuid = msg.playerUuid
  798. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  799. if not union then
  800. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  801. end
  802. local baseCityInfo = {}
  803. msgData.baseCityInfo = baseCityInfo
  804. baseCityInfo.cityId = union.baseCityId
  805. local pointInfoArr = calcOccupyPointArr(union.occupCityList)
  806. baseCityInfo.occupyPointNum = #pointInfoArr
  807. baseCityInfo.myUnionOccupyArr = calcOccupyPointArr(union.occupCityList)
  808. baseCityInfo.myUnionOccupyArr[#baseCityInfo.myUnionOccupyArr+1] = baseCityInfo.cityId
  809. baseCityInfo.occupyCityLv2Num = calcOccupyCityNum(union.occupCityList, 2)
  810. baseCityInfo.occupyCityLv3Num = calcOccupyCityNum(union.occupCityList, 3)
  811. baseCityInfo.occupyCityLv4Num = calcOccupyCityNum(union.occupCityList, 4)
  812. baseCityInfo.occupyCityLv5Num = calcOccupyCityNum(union.occupCityList, 5)
  813. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  814. InnerMsg.sendMsg(fd, msgData)
  815. end
  816. -- 查询玩家占领的所有据点信息
  817. function N2C_PlayerOccupyPoint_Query(msg)
  818. local myUnionId = msg.myUnionId
  819. local playerUuid = msg.playerUuid
  820. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  821. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYEROCCUPYPOINT_QUERY
  822. msgData.playerUuid = playerUuid
  823. msgData.occupyPointArr = {}
  824. local union = AnotherWorldBattleDB.GetUnionData(myUnionId)
  825. if not union then
  826. -- 报名阶段, 或者活动开未开启过处于默认的结束状态
  827. local stage = AnotherWorldBattleDB.GetStage()
  828. if stage == AnotherWorldBattleDefine.AB_STATE_JOIN or stage == AnotherWorldBattleDefine.AB_STATE_END then
  829. return InnerMsg.sendMsg(fd, msgData)
  830. end
  831. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  832. end
  833. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  834. if not playerData then
  835. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  836. end
  837. local heroList = playerData.heroList
  838. if not heroList then
  839. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  840. end
  841. local occupyPointArrMsg = msgData.occupyPointArr
  842. for cityId, pointList in pairs(heroList) do
  843. for pointIdx, heroInfoArr in pairs(pointList) do
  844. occupyPointArrMsg[#occupyPointArrMsg+1] = {
  845. cityId = cityId,
  846. pointIdx = pointIdx,
  847. power = calcHerosPower(heroInfoArr),
  848. heroArr = {},
  849. }
  850. local targetHeroArr = occupyPointArrMsg[#occupyPointArrMsg].heroArr
  851. for _, heroInfo in ipairs(heroInfoArr) do
  852. targetHeroArr[#targetHeroArr+1] = {
  853. heroBody = heroInfo.heroBody,
  854. heroStar = heroInfo.heroStar,
  855. heroLv = heroInfo.heroLevel,
  856. heroCamp = heroInfo.heroCamp,
  857. heroIcon = heroInfo.heroIcon,
  858. heroId = heroInfo.heroId,
  859. heroQuality = heroInfo.heroQuality
  860. }
  861. end
  862. end
  863. end
  864. InnerMsg.sendMsg(fd, msgData)
  865. end
  866. -- 查询公会排行榜
  867. function N2C_UnionRank_Query(msg)
  868. local myUnionId = msg.myUnionId
  869. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  870. if not groupId then
  871. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  872. end
  873. local unionRankList = AnotherWorldBattleDB.GetUnionRankList(groupId)
  874. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UNIONRANK_QUERY
  875. msgData.playerUuid = msg.playerUuid
  876. msgData.myUnionRank = 0
  877. msgData.unionRankArr = {}
  878. for rank, randkData in ipairs(unionRankList) do
  879. msgData.unionRankArr[rank] = {
  880. name = randkData.name,
  881. power = randkData.power,
  882. cityNum = randkData.occupyCityNum,
  883. pointNum = randkData.occupyPointNum,
  884. }
  885. if randkData.guildId == myUnionId then
  886. msgData.myUnionRank = rank
  887. end
  888. end
  889. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  890. InnerMsg.sendMsg(fd, msgData)
  891. end
  892. -- 查询玩家排行榜
  893. function N2C_PlayerRank_Query(msg)
  894. local myUnionId = msg.myUnionId
  895. local groupId = getGroupId(myUnionId, msg.sourceServerId)
  896. if not groupId then
  897. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  898. end
  899. local playerRankList = AnotherWorldBattleDB.GetPlayerRankList(groupId)
  900. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_PLAYERRANK_QUERY
  901. msgData.playerUuid = msg.playerUuid
  902. msgData.myRank = 0
  903. msgData.playerRankArr = {}
  904. for rank, randkData in ipairs(playerRankList) do
  905. if rank > AnotherWorldBattleDefine.AB_RANK_MAX_NUM then
  906. break
  907. end
  908. msgData.playerRankArr[rank] = {
  909. name = randkData.name,
  910. power = randkData.power,
  911. pointNum = randkData.pointNum,
  912. pointWeight = randkData.pointAllWeight,
  913. }
  914. if randkData.playerId == msg.playerUuid then
  915. msgData.myRank = rank
  916. end
  917. end
  918. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  919. InnerMsg.sendMsg(fd, msgData)
  920. end
  921. -- 集结
  922. function N2C_Gather(msg)
  923. local sourceServerId = msg.sourceServerId
  924. local playerUuid = msg.playerUuid
  925. local myUnionId = msg.myUnionId
  926. if not isRunning() then
  927. return errTips(sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_1)
  928. end
  929. local groupId = getMyUnionGourpId(myUnionId)
  930. if not groupId then
  931. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  932. end
  933. local targetCityId = msg.targetCityId
  934. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  935. if not myUnionData then
  936. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  937. end
  938. local gatherInfo = myUnionData.gatherInfo
  939. if msg.opType == 1 then --集结
  940. -- 集结冷却时间判断
  941. local now = os.time()
  942. if gatherInfo then
  943. local gatherTime = gatherInfo.gatherTime
  944. if now - gatherTime < AnotherWorldBattleDefine.AB_GATHER_CD_SEC then
  945. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_6)
  946. end
  947. end
  948. -- 城池是否相邻判断
  949. local occupCityyArr = getUnionOccupyArr(myUnionId)
  950. if not isadJoin(occupCityyArr, targetCityId) then
  951. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_7)
  952. end
  953. myUnionData.gatherInfo = myUnionData.gatherInfo or {}
  954. myUnionData.gatherInfo.gatherTime = now
  955. myUnionData.gatherInfo.gatherCity = targetCityId
  956. else
  957. -- 取消集结
  958. if not gatherInfo then
  959. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  960. end
  961. if targetCityId ~= gatherInfo.gatherCity then
  962. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_9)
  963. end
  964. gatherInfo.gatherCity = 0
  965. end
  966. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  967. -- 推送城池数据给客户端,刷新界面
  968. N2C_CityDetailed_Query(msg)
  969. -- 发给本地服, 用于在公会聊天频道生成链接
  970. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_GATHER
  971. msgData.playerUuid = msg.playerUuid
  972. msgData.targetCityId = targetCityId
  973. msgData.opType = msg.opType
  974. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  975. InnerMsg.sendMsg(fd, msgData)
  976. end
  977. -- 查询据点是否可以被挑战
  978. function N2C_Try_Challengde_Point(msg)
  979. local playerUuid = msg.playerUuid
  980. local myUnionId = msg.myUnionId
  981. local targetCityId = msg.targetCityId
  982. local targetPointIdx = msg.targetPointIdx
  983. local state = isCanChallengePoint(targetCityId, msg.targetPointIdx, msg.myUnionId, playerUuid)
  984. if state ~= 1 then
  985. return
  986. end
  987. local occupyPointNum = calcPlayerOccupyPointNum(msg.playerUuid)
  988. if occupyPointNum >= AnotherWorldBattleDefine.AB_PLAYER_OCCUPY_POINT_MAX_NUM then
  989. return errTips(msg.sourceServerId, playerUuid, AnotherWorldBattleDefine.ERR_CODE_10)
  990. end
  991. local groupId = getMyUnionGourpId(myUnionId)
  992. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  993. local targetPointData = cityList[targetCityId].pointArr[targetPointIdx]
  994. local pointOccupyInfo = {targetCityId = targetCityId, targetPointIdx = targetPointIdx}
  995. if targetPointData.unionId then
  996. local occupyUnionData = AnotherWorldBattleDB.GetUnionData(targetPointData.unionId)
  997. pointOccupyInfo.occupySrvId = occupyUnionData.serverId
  998. pointOccupyInfo.occupyPlayerUuid = targetPointData.playerUuid
  999. end
  1000. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1001. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1002. pointOccupyInfo.isGather = 1
  1003. end
  1004. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_POINT_ISCAN_CHALLENGE
  1005. msgData.playerUuid = playerUuid
  1006. msgData.targetCityId = targetCityId
  1007. msgData.targetPointIdx = targetPointIdx
  1008. msgData.pointInfo = pointOccupyInfo
  1009. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1010. InnerMsg.sendMsg(fd, msgData)
  1011. end
  1012. -- 玩家挑战据点胜利
  1013. local function challenge_Win(msg)
  1014. local playerUuid = msg.playerUuid
  1015. local myUnionId = msg.myUnionId
  1016. local targetCityId = msg.targetCityId
  1017. local targetPointIdx = msg.targetPointIdx
  1018. local playerShowInfo = msg.playerShowInfo
  1019. if not isRunning() then
  1020. return
  1021. end
  1022. local groupId = getMyUnionGourpId(myUnionId)
  1023. if not groupId then
  1024. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1025. end
  1026. local myUnionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1027. if not myUnionData then
  1028. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_3)
  1029. end
  1030. local cityList = AnotherWorldBattleDB.GetCityListByGroupId(groupId)
  1031. local cityData = cityList[targetCityId]
  1032. local pointData = cityData.pointArr[targetPointIdx]
  1033. local now = os.time()
  1034. local cityNum, pointNum = 0, 1
  1035. local pointWeight = AnotherWorldBattleConfig.city[targetCityId].pointWeight
  1036. -- 防守方是真实玩家
  1037. if pointData.unionId and pointData.playerUuid then
  1038. -- 更新防守方公会数据
  1039. local defUnionData = AnotherWorldBattleDB.GetUnionData(pointData.unionId)
  1040. local defOccupCityList = defUnionData.occupCityList
  1041. -- 防守方之前占领了城池
  1042. if defOccupCityList[targetCityId].isOccupy then
  1043. -- 修改防守方的占领状态
  1044. defOccupCityList[targetCityId].isOccupy = false
  1045. -- 更新防守方对该城池的最后占领时间
  1046. local defOccupyPointList = defOccupCityList[targetCityId].occupyPointList
  1047. local defPointInfo = defOccupyPointList[targetPointIdx]
  1048. local defOccupyTimeArr = defPointInfo.occupyTimeArr
  1049. defOccupyTimeArr[#defOccupyTimeArr][2] = now
  1050. -- 删除防守方的该据点的占领者
  1051. defPointInfo.playerUuid = nil
  1052. -- 删除防守方玩家的防守阵容数据
  1053. local defPlayData = AnotherWorldBattleDB.GetPlayerData(pointData.playerUuid)
  1054. if defPlayData then
  1055. defPlayData.heroList[targetCityId][targetPointIdx] = nil
  1056. AnotherWorldBattleDB.UpdatePlayerData(pointData.playerUuid, defPlayData)
  1057. end
  1058. cityNum = -1
  1059. end
  1060. -- 更新防守方公会的排行榜数据
  1061. AnotherWorldBattleDB.UpdateUnionRankList(groupId, pointData.unionId, cityNum, pointNum)
  1062. -- 更新防守方玩家的排行榜数据
  1063. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, -pointNum, -pointWeight)
  1064. -- 通知玩家
  1065. pointLose(defUnionData.serverId, pointData.playerUuid, targetCityId, targetPointIdx)
  1066. -- 更新防守方公会数据
  1067. AnotherWorldBattleDB.UpdateUnionData(pointData.unionId, defUnionData)
  1068. end
  1069. -- 更新据点最新占领者数据
  1070. pointData.unionId = myUnionId
  1071. pointData.playerUuid = playerUuid
  1072. -- 更新进攻方公会数据—— 据点占有者, 最新的据点占有时间
  1073. myUnionData.occupCityList = myUnionData.occupCityList or {}
  1074. local occupCityList = myUnionData.occupCityList
  1075. occupCityList[targetCityId] = occupCityList[targetCityId] or {}
  1076. occupCityList[targetCityId].occupyPointList = occupCityList[targetCityId].occupyPointList or {}
  1077. occupCityList[targetCityId].occupyPointList[targetPointIdx] = occupCityList[targetCityId].occupyPointList[targetPointIdx] or {}
  1078. local atkPointInfo = occupCityList[targetCityId].occupyPointList[targetPointIdx]
  1079. atkPointInfo.playerUuid = playerUuid
  1080. atkPointInfo.occupyTimeArr = atkPointInfo.occupyTimeArr or {}
  1081. atkPointInfo.occupyTimeArr[#atkPointInfo.occupyTimeArr+1] = { now }
  1082. --更新进攻者的展示数据
  1083. local atkPlayerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1084. atkPlayerData = atkPlayerData or {}
  1085. atkPlayerData.name = playerShowInfo.name
  1086. atkPlayerData.lv = playerShowInfo.lv
  1087. atkPlayerData.head = playerShowInfo.head
  1088. atkPlayerData.headFrame = playerShowInfo.headFrame
  1089. atkPlayerData.power = playerShowInfo.power
  1090. atkPlayerData.unionId = myUnionId
  1091. -- 更新挑战次数
  1092. atkPlayerData.challengeTimes = (atkPlayerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1093. if not atkPlayerData.lastTime or atkPlayerData.lastTime == 0 then
  1094. atkPlayerData.lastTime = os.time()
  1095. end
  1096. -- 增加防守阵容数据
  1097. atkPlayerData.heroList = atkPlayerData.heroList or {}
  1098. atkPlayerData.heroList[targetCityId] = atkPlayerData.heroList[targetCityId] or {}
  1099. atkPlayerData.heroList[targetCityId][targetPointIdx] = playerShowInfo.heroArr
  1100. cityNum = 0
  1101. -- 进攻方完全占领城池了
  1102. if isCompleteOccupy(cityData, myUnionId) then
  1103. -- 更新城池的占领公会
  1104. cityData.occupyUnion = myUnionId
  1105. -- 更新进攻方公会对该城池的占领状态
  1106. occupCityList[targetCityId].isOccupy = true
  1107. -- 如果占领的是发起集结的城池, 那么取消集结
  1108. if myUnionData.gatherInfo and myUnionData.gatherInfo.gatherCity == targetCityId then
  1109. myUnionData.gatherInfo.gatherCity = 0
  1110. end
  1111. cityNum = 1
  1112. end
  1113. -- 更新进攻方公会数据
  1114. AnotherWorldBattleDB.UpdateUnionData(myUnionId, myUnionData)
  1115. -- 更新进攻方玩家数据
  1116. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, atkPlayerData)
  1117. -- 更新进攻方公会的排行榜数据
  1118. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, cityNum, pointNum)
  1119. -- 更新进攻方玩家的排行榜数据
  1120. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, pointNum, pointWeight)
  1121. -- 更新城池数据
  1122. AnotherWorldBattleDB.UpdateCityList(groupId, cityList)
  1123. end
  1124. -- 玩家挑战据点失败
  1125. local function challenge_Fail(msg)
  1126. local playerUuid = msg.playerUuid
  1127. local myUnionId = msg.myUnionId
  1128. local groupId = getMyUnionGourpId(myUnionId)
  1129. if not groupId then
  1130. return
  1131. end
  1132. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1133. playerData = playerData or {}
  1134. playerData.name = playerData.name
  1135. playerData.lv = playerData.lv
  1136. playerData.power = playerData.power
  1137. playerData.unionId = myUnionId
  1138. playerData.challengeTimes = (playerData.challengeTimes or AnotherWorldBattleDefine.AB_PLAYER_CHALLENGE_TIMES) - 1
  1139. if not playerData.lastTime or playerData.lastTime == 0 then
  1140. playerData.lastTime = os.time()
  1141. end
  1142. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1143. -- 加入个人排行榜
  1144. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0)
  1145. end
  1146. --挑战据点结束的处理函数
  1147. function N2C_Point_Challenge_End_Handle(msg)
  1148. if msg.challengeRes == CombatDefine.RESULT_WIN then
  1149. challenge_Win(msg)
  1150. else
  1151. challenge_Fail(msg)
  1152. end
  1153. end
  1154. -- 玩家数据更新
  1155. function N2C_Update_Player_Data(msg)
  1156. if not isRunning() then
  1157. return
  1158. end
  1159. local playerUuid = msg.playerUuid
  1160. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1161. if not playerData then
  1162. return
  1163. end
  1164. local groupId = getMyUnionGourpId(playerData.unionId)
  1165. if not groupId then
  1166. return
  1167. end
  1168. local isUpdatePower = false
  1169. for k,v in pairs(msg.updateData) do
  1170. if k == "power" then
  1171. isUpdatePower = true
  1172. end
  1173. playerData[k] = v
  1174. end
  1175. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1176. -- 如果是战力更新, 更新玩家排行榜
  1177. if isUpdatePower then
  1178. AnotherWorldBattleDB.UpdatePlayerRankList(groupId, playerUuid, 0, 0, playerData.power)
  1179. end
  1180. end
  1181. -- 公会数据更新
  1182. function N2C_Update_Union_Data(msg)
  1183. if not isRunning() then
  1184. return
  1185. end
  1186. local myUnionId = msg.myUnionId
  1187. local unionData = AnotherWorldBattleDB.GetUnionData(myUnionId)
  1188. if not unionData then
  1189. return
  1190. end
  1191. local groupId = getMyUnionGourpId(myUnionId)
  1192. if not groupId then
  1193. return
  1194. end
  1195. local isUpdatePower = false
  1196. for k,v in pairs(msg.updateData) do
  1197. if k == "power" then
  1198. isUpdatePower = true
  1199. end
  1200. unionData[k] = v
  1201. end
  1202. AnotherWorldBattleDB.UpdateUnionData(myUnionId, unionData)
  1203. -- 如果是战力更新, 更新公会排行榜
  1204. if isUpdatePower then
  1205. AnotherWorldBattleDB.UpdateUnionRankList(groupId, myUnionId, 0, 0, unionData.power)
  1206. end
  1207. end
  1208. -- 更新玩家据点防守阵容数据
  1209. function N2C_Update_Point_Lineup(msg)
  1210. if not isRunning() then
  1211. return
  1212. end
  1213. local playerUuid = msg.playerUuid
  1214. local playerData = AnotherWorldBattleDB.GetPlayerData(playerUuid)
  1215. if not playerData or not playerData.heroList then
  1216. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1217. end
  1218. local groupId = getMyUnionGourpId(playerData.unionId)
  1219. if not groupId then
  1220. return
  1221. end
  1222. local targetCityId, targetPointIdx = msg.targetCityId, msg.targetPointIdx
  1223. local heroList = playerData.heroList
  1224. if not heroList[targetCityId] or not heroList[targetCityId][targetPointIdx] then
  1225. return errTips(msg.sourceServerId, msg.playerUuid, AnotherWorldBattleDefine.ERR_CODE_5)
  1226. end
  1227. heroList[targetCityId][targetPointIdx] = msg.heroArr
  1228. AnotherWorldBattleDB.UpdatePlayerData(playerUuid, playerData)
  1229. local msgData = InnerMsg.wl.WL_ANOTHERWORLDBATTLE_UPDATE_POINT_LINEIP
  1230. msgData.playerUuid = playerUuid
  1231. msgData.targetCityId = targetCityId
  1232. msgData.targetPointIdx = targetPointIdx
  1233. msgData.formation = msg.formation
  1234. msgData.heroList = msg.heroList
  1235. msgData.helpList = msg.helpList
  1236. local fd = MiddleManager.getFDBySvrIndex(msg.sourceServerId)
  1237. InnerMsg.sendMsg(fd, msgData)
  1238. end