AnotherWorldBattleCS.lua 64 KB

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