| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132 |
- -- 异界之战(DB)
- local LuaMongo = _G.lua_mongo
- local DB = require("common.DB")
- local AnotherWorldBattleDefine = require("anotherWorldBattle.AnotherWorldBattleDefine")
- local AnotherWorldBattleConfig = require("excel.anotherWorldBattle")
- local dbUpdate = {_id=nil}
- local dbUpdateField = {}
- -- db
- AnotherWorldBattleData = {
- -- lastRoundStartTime = 0, -- 上一轮活动开启时间
- -- stage = 1, -- 当前阶段
- -- joinUnionArr = {
- -- { unionId = "", serverId = 123, power = 9999},
- -- },
- -- groupArray = {
- -- [1] = {unionId1, unionId2, unionId3, unionId4, unionId5, unionId6, unionId7, unionId8},
- -- [2] = {unionId9, unionId10,...},
- -- },
- -- unionList = {
- -- [unionId1] = {
- -- serverId = 123, -- 公会所在服务器Id
- -- power = 9999,
- -- name = "111",
- -- baseCityId = 123, -- 出生城池
- -- baseCityStartTime = nil, -- 出生城的开始占领时间
- -- occupCityList = { -- 城池列表(包括曾占领过, 当前成功占领, 当前只占领过一个或多个据点)
- -- [cityId1] = {
- -- isOccupy = false, --当前是否占领
- -- occupyPointList = { --占领本城池中的据点列表
- -- [pointId1] = { playerUuid = "", occupyTimeArr = { {startTime1, endTime1}, {startTime2, endTime2}}, },
- -- },
- -- },
- -- },
- -- gatherInfo = { --集结信息
- -- gatherTime = 0,
- -- gatherCity = 123,
- -- },
- -- },
- -- },
- -- playerList = {
- -- [playerUuid] = {
- -- name = "",
- -- lv = 200,
- -- head = 1234,
- -- headFrame = 1234,
- -- power = 999,
- -- challengeTimes = 10,
- -- lastTime = nil, -- 上一次更新的时间戳
- -- unionId = "",
- -- heroList = {
- -- [cityId] = {
- -- [pointIdx] = {
- -- {
- -- heroUuid = "sdgjdjdj",
- -- heroStar = 111,
- -- heroLevel = 111,
- -- heroCamp = 1,
- -- heroBody = 111,
- -- heroIcon = 111,
- -- heroPower = 11111,
- -- heroId = 111,
- -- heroQuality = 1,
- -- },
- -- },
- -- },
- -- }
- -- },
- -- }
- }
- -- 所有分组的城池信息, 启动服务器后生成, 用于快速获取城池信息
- Group_2_CityList = {
- -- [groupId] = {
- -- [cityId] = {
- -- occupyUnion = { -- 占领公会信息, 没有公会占领时为nil
- -- serverId = 124,
- -- unionId = "123",
- -- },
- -- pointArr = { -- 城池中所有据点信息, 没人占领时, unionId, playerUuid为nil
- -- {unionId = nil, playerUuid = nil, },
- -- {unionId = nil, playerUuid = nil, },
- -- }
- -- },
- -- },
- }
- -- 所有分组的公会排行榜, 启动服务器后生成
- Group_2_UnionRankList = {}
- -- 所有分组的玩家排行榜, 启动服务器后生成
- Group_2_PlayerRankList = {}
- -- 区服Id - 公会列表 映射表
- Server_2_UnionArr = {
- -- [serverId] = {unionId1, unionId2},
- }
- -- 公会 - 所属分组 映射表
- Union_2_Group = {
- -- [unionId] = groupId,
- }
- local function initData()
- AnotherWorldBattleData.lastRoundStartTime = 0
- AnotherWorldBattleData.stage = AnotherWorldBattleDefine.AB_STATE_END
- LuaMongo.insert(DB.db_anotherWorldBattle, AnotherWorldBattleData)
- end
- local function loadData()
- LuaMongo.find(DB.db_anotherWorldBattle)
- local data = {}
- if LuaMongo.next(data) then
- AnotherWorldBattleData = data
- else
- initData()
- end
- end
- -- 修改db单个字段
- local function updateValue(key, value)
- if not key then return end
- if value then
- dbUpdateField["$set"] = {[key]=value}
- dbUpdateField["$unset"] = nil
- else
- dbUpdateField["$set"] = nil
- dbUpdateField["$unset"] = {[key]=1}
- end
- dbUpdate._id = AnotherWorldBattleData._id
- LuaMongo.update(DB.db_anotherWorldBattle, dbUpdate, dbUpdateField)
- end
- -- 生成城池数据
- local function genCityList(net, unionIdArr)
- local function getUnionByCityId(targetCityId)
- for _, unionId in ipairs(unionIdArr) do
- local unionInfo = AnotherWorldBattleData.unionList[unionId]
- -- 其他城池
- if unionInfo and unionInfo.occupCityList then
- for occupyCityId, occupyCityinfo in pairs(unionInfo.occupCityList) do
- if occupyCityId == targetCityId and occupyCityinfo.isOccupy then
- return unionId
- end
- end
- end
- -- 出生城池
- if unionInfo and unionInfo.baseCityId == targetCityId then
- return unionId
- end
- end
- return nil
- end
- local function getPointOccupyPlayer(targetCityId, targetPointIdx)
- for _, unionId in ipairs(unionIdArr) do
- local unionInfo = AnotherWorldBattleData.unionList[unionId]
- if unionInfo and unionInfo.occupCityList and unionInfo.occupCityList[targetCityId] then
- local pointList = unionInfo.occupCityList[targetCityId].occupyPointList
- if pointList and pointList[targetPointIdx] and pointList[targetPointIdx].playerUuid then
- return unionId, pointList[targetPointIdx].playerUuid
- end
- end
- end
- return nil, nil
- end
- for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do
- local cityEntry = { pointArr = {} }
- net[cityId] = cityEntry
- local unionId = getUnionByCityId(cityId)
- if unionId then
- cityEntry.occupyUnion = unionId
- end
- local pointArr = cityEntry.pointArr
- for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
- local unionId1, playerUuid = getPointOccupyPlayer(cityId, i)
- if unionId1 and playerUuid then
- pointArr[i] = {
- unionId = unionId1,
- playerUuid = playerUuid
- }
- else
- pointArr[i] = {}
- end
- end
- end
- end
- -- 工会排行榜
- local function createGuildRank()
- local state = {
- guilds = {}, -- {[guildId] = {id, name, power, occupyCityNum, occupyPointNum, ...}}
- sortedGuilds = {}, -- 排序后的工会数组
- isDirty = true,
- maxSize = 8
- }
-
- -- 创建接口表
- local interface = {}
-
- -- 计算组合键
- local function getGuildSortKey(occupyCityNum, occupyPointNum, power)
- return occupyCityNum * 100000000000000000000 + -- 10^20
- occupyPointNum * 1000000000000 + -- 10^12
- power
- end
-
- -- 使用组合键进行比较
- local function compareGuilds(a, b)
- local keyA = getGuildSortKey(a.occupyCityNum, a.occupyPointNum, a.power)
- local keyB = getGuildSortKey(b.occupyCityNum, b.occupyPointNum, b.power)
-
- if keyA ~= keyB then
- return keyA > keyB -- 组合键大的排名高
- end
-
- -- 组合键相同,按ID排序
- return a.id < b.id
- end
-
- local function quickSort(arr, left, right)
- if left >= right then return end
-
- local pivot = arr[math.floor((left + right) / 2)]
- local i, j = left, right
-
- while i <= j do
- while compareGuilds(arr[i], pivot) do i = i + 1 end
- while compareGuilds(pivot, arr[j]) do j = j - 1 end
-
- if i <= j then
- arr[i], arr[j] = arr[j], arr[i]
- i = i + 1
- j = j - 1
- end
- end
-
- quickSort(arr, left, j)
- quickSort(arr, i, right)
- end
-
- local function refreshRank()
- -- 收集所有工会到数组
- local guildArray = {}
- for _, guild in pairs(state.guilds) do
- table.insert(guildArray, guild)
- end
-
- -- 使用快速排序
- if #guildArray > 0 then
- quickSort(guildArray, 1, #guildArray)
- end
-
- -- 只保留前8名
- state.sortedGuilds = {}
- for i = 1, math.min(state.maxSize, #guildArray) do
- state.sortedGuilds[i] = {
- guildId = guildArray[i].id,
- rank = i,
- power = guildArray[i].power,
- name = guildArray[i].name,
- occupyCityNum = guildArray[i].occupyCityNum,
- occupyPointNum = guildArray[i].occupyPointNum
- }
- end
-
- state.isDirty = false
- end
- -- 定义接口函数
- function interface.updateGuild(guildId, name, power, occupyCityNum, occupyPointNum)
- if not state.guilds[guildId] then
- state.guilds[guildId] = {
- id = guildId,
- name = name,
- power = power,
- occupyCityNum = occupyCityNum or 0,
- occupyPointNum = occupyPointNum or 0
- }
- else
- state.guilds[guildId].power = power
- state.guilds[guildId].occupyCityNum = occupyCityNum or state.guilds[guildId].occupyCityNum
- state.guilds[guildId].occupyPointNum = occupyPointNum or state.guilds[guildId].occupyPointNum
- if name then
- state.guilds[guildId].name = name
- end
- end
- state.isDirty = true
- end
- function interface.updateOccupyCityNum(guildId, occupyCityNum)
- if state.guilds[guildId] then
- state.guilds[guildId].occupyCityNum = occupyCityNum
- state.isDirty = true
- end
- end
- function interface.updateOccupyPointNum(guildId, occupyPointNum)
- if state.guilds[guildId] then
- state.guilds[guildId].occupyPointNum = occupyPointNum
- state.isDirty = true
- end
- end
- function interface.updatePower(guildId, power)
- if state.guilds[guildId] then
- state.guilds[guildId].power = power
- state.isDirty = true
- end
- end
- function interface.disbandGuild(guildId)
- if state.guilds[guildId] then
- state.guilds[guildId] = nil
- state.isDirty = true
- return true
- end
- return false
- end
- function interface.getRankList()
- if state.isDirty then
- refreshRank()
- end
- return state.sortedGuilds
- end
- function interface.getGuildRank(guildId)
- if state.isDirty then
- refreshRank()
- end
-
- for i, guild in ipairs(state.sortedGuilds) do
- if guild.guildId == guildId then
- return i
- end
- end
- return nil
- end
- function interface.getGuildDetail(guildId)
- if state.isDirty then
- refreshRank()
- end
-
- for i, guild in ipairs(state.sortedGuilds) do
- if guild.guildId == guildId then
- return {
- rank = i,
- guildId = guild.guildId,
- name = guild.name,
- power = guild.power,
- occupyCityNum = guild.occupyCityNum,
- occupyPointNum = guild.occupyPointNum
- }
- end
- end
- return nil
- end
- function interface.hasGuild(guildId)
- return state.guilds[guildId] ~= nil
- end
- function interface.getTotalGuilds()
- return tableCount(state.guilds)
- end
- function interface._getState()
- return {
- totalGuilds = tableCount(state.guilds),
- isDirty = state.isDirty
- }
- end
- return interface
- end
- -- 玩家排行榜
- local function createPlayerRank()
- local state = {
- players = {}, -- {[playerId] = {id, name, power, pointNum, pointAllWeight, ...}}
- rankArray = {}, -- 排行榜数组,按组合键排序
- playerIndex = {}, -- {[playerId] = 在rankArray中的索引}
- candidateMap = {}, -- 候选玩家 {[playerId] = true}
- maxSize = AnotherWorldBattleDefine.AB_RANK_MAX_NUM,
- size = 0
- }
-
- -- 创建接口表
- local interface = {}
-
- -- 计算组合键
- local function getPlayerSortKey(pointNum, pointAllWeight, power)
- return pointNum * 10000000000000000 + -- 10^16
- pointAllWeight * 10000000000 + -- 10^10
- power
- end
- -- 使用组合键进行比较
- local function comparePlayers(a, b)
- local keyA = getPlayerSortKey(a.pointNum, a.pointAllWeight, a.power)
- local keyB = getPlayerSortKey(b.pointNum, b.pointAllWeight, b.power)
- if keyA ~= keyB then
- return keyA > keyB -- 组合键大的排名高
- end
-
- -- 组合键相同,按ID排序
- return a.id < b.id
- end
- -- 二分查找插入位置(使用组合键)
- local function findInsertPosition(playerData)
- local left, right = 1, state.size
- local key = getPlayerSortKey(playerData.pointNum, playerData.pointAllWeight, playerData.power)
-
- while left <= right do
- local mid = math.floor((left + right) / 2)
- local midPlayer = state.rankArray[mid]
- local midKey = getPlayerSortKey(midPlayer.pointNum, midPlayer.pointAllWeight, midPlayer.power)
-
- if key > midKey then
- right = mid - 1
- else
- left = mid + 1
- end
- end
-
- return left
- end
-
- -- 插入玩家到排行榜
- local function insertToRank(playerData)
- if state.size == 0 then
- state.rankArray[1] = playerData
- state.playerIndex[playerData.id] = 1
- state.size = 1
- return
- end
-
- local pos = findInsertPosition(playerData)
-
- -- 向后移动元素
- for i = state.size, pos, -1 do
- state.rankArray[i + 1] = state.rankArray[i]
- state.playerIndex[state.rankArray[i].id] = i + 1
- end
-
- -- 插入新元素
- state.rankArray[pos] = playerData
- state.playerIndex[playerData.id] = pos
- state.size = state.size + 1
-
- -- 如果超过最大大小,移除最后一个
- if state.size > state.maxSize then
- local removedPlayer = state.rankArray[state.maxSize + 1]
- if removedPlayer then
- state.playerIndex[removedPlayer.id] = nil
- state.candidateMap[removedPlayer.id] = true
- end
- state.size = state.maxSize
- end
- end
-
- -- 从排行榜移除玩家
- local function removeFromRank(playerId)
- local index = state.playerIndex[playerId]
- if not index then return false end
-
- -- 向前移动元素
- for i = index, state.size - 1 do
- state.rankArray[i] = state.rankArray[i + 1]
- state.playerIndex[state.rankArray[i].id] = i
- end
-
- state.rankArray[state.size] = nil
- state.playerIndex[playerId] = nil
- state.size = state.size - 1
- return true
- end
-
- -- 调整玩家在排行榜中的位置
- local function adjustPlayerPosition(playerId)
- local currentIndex = state.playerIndex[playerId]
- if not currentIndex then return end
-
- local playerData = state.players[playerId]
- if not playerData then return end
-
- -- 先移除,再重新插入(确保位置正确)
- removeFromRank(playerId)
- insertToRank(playerData)
- end
- -- 检查候选玩家
- local function checkCandidates()
- if state.size < state.maxSize then
- -- 排行榜未满,找到最佳候选加入
- local bestCandidate = nil
- local bestKey = -1
-
- for playerId, _ in pairs(state.candidateMap) do
- local player = state.players[playerId]
- if player then
- local key = getPlayerSortKey(player.pointNum, player.pointAllWeight, player.power)
- if key > bestKey then
- bestCandidate = playerId
- bestKey = key
- elseif key == bestKey and player.id < bestCandidate then
- bestCandidate = playerId
- bestKey = key
- end
- end
- end
-
- if bestCandidate then
- state.candidateMap[bestCandidate] = nil
- insertToRank(state.players[bestCandidate])
- end
- else
- -- 检查候选是否应该替换最后一名
- local lastPlayer = state.rankArray[state.size]
- local lastKey = getPlayerSortKey(lastPlayer.pointNum, lastPlayer.pointAllWeight, lastPlayer.power)
- local bestCandidate = nil
- local bestKey = lastKey
-
- for playerId, _ in pairs(state.candidateMap) do
- local player = state.players[playerId]
- if player then
- local key = getPlayerSortKey(player.pointNum, player.pointAllWeight, player.power)
- if key > bestKey then
- bestCandidate = playerId
- bestKey = key
- elseif key == bestKey and player.id < bestCandidate then
- bestCandidate = playerId
- bestKey = key
- end
- end
- end
-
- if bestCandidate then
- state.candidateMap[bestCandidate] = nil
- state.candidateMap[lastPlayer.id] = true
- removeFromRank(lastPlayer.id)
- insertToRank(state.players[bestCandidate])
- end
- end
- end
- -- 完全移除玩家(包括从候选集)
- local function removePlayerCompletely(playerId)
- -- 从排行榜移除
- removeFromRank(playerId)
-
- -- 从候选集移除
- state.candidateMap[playerId] = nil
-
- -- 从玩家数据移除
- state.players[playerId] = nil
-
- return true
- end
- -- 定义接口函数
- function interface.updatePlayer(playerId, name, power, pointNum, pointAllWeight)
- local playerData = state.players[playerId]
-
- if not playerData then
- -- 新玩家
- playerData = {
- id = playerId,
- name = name,
- power = power,
- pointNum = pointNum or 0,
- pointAllWeight = pointAllWeight or 0
- }
- state.players[playerId] = playerData
-
- if state.size < state.maxSize then
- insertToRank(playerData)
- else
- -- 检查是否能直接进入排行榜
- local lastPlayer = state.rankArray[state.size]
- local lastKey = getPlayerSortKey(lastPlayer.pointNum, lastPlayer.pointAllWeight, lastPlayer.power)
- local newKey = getPlayerSortKey(pointNum or 0, pointAllWeight or 0, power)
-
- if newKey > lastKey or (newKey == lastKey and playerId < lastPlayer.id) then
- state.candidateMap[lastPlayer.id] = true
- removeFromRank(lastPlayer.id)
- insertToRank(playerData)
- else
- state.candidateMap[playerId] = true
- end
- end
- else
- -- 已有玩家
- playerData.power = power
- playerData.pointNum = pointNum or playerData.pointNum
- playerData.pointAllWeight = pointAllWeight or playerData.pointAllWeight
- if name then
- playerData.name = name
- end
-
- local currentIndex = state.playerIndex[playerId]
-
- if currentIndex then
- -- 玩家在排行榜中,需要调整位置
- adjustPlayerPosition(playerId)
- else
- -- 玩家不在排行榜中,检查候选集
- if state.candidateMap[playerId] then
- if state.size < state.maxSize then
- state.candidateMap[playerId] = nil
- insertToRank(playerData)
- else
- local lastPlayer = state.rankArray[state.size]
- local lastKey = getPlayerSortKey(lastPlayer.pointNum, lastPlayer.pointAllWeight, lastPlayer.power)
- local newKey = getPlayerSortKey(playerData.pointNum, playerData.pointAllWeight, playerData.power)
-
- if newKey > lastKey or (newKey == lastKey and playerId < lastPlayer.id) then
- state.candidateMap[lastPlayer.id] = true
- state.candidateMap[playerId] = nil
- removeFromRank(lastPlayer.id)
- insertToRank(playerData)
- end
- end
- end
- end
- end
- end
- function interface.updatePointNum(playerId, pointNum)
- if state.players[playerId] then
- state.players[playerId].pointNum = pointNum
-
- local currentIndex = state.playerIndex[playerId]
- if currentIndex then
- adjustPlayerPosition(playerId)
- else
- -- 不在排行榜中,检查候选集
- if state.candidateMap[playerId] then
- checkCandidates()
- end
- end
- end
- end
- function interface.updatePointAllWeight(playerId, pointAllWeight)
- if state.players[playerId] then
- state.players[playerId].pointAllWeight = pointAllWeight
-
- local currentIndex = state.playerIndex[playerId]
- if currentIndex then
- adjustPlayerPosition(playerId)
- else
- -- 不在排行榜中,检查候选集
- if state.candidateMap[playerId] then
- checkCandidates()
- end
- end
- end
- end
- function interface.updatePower(playerId, power)
- if state.players[playerId] then
- state.players[playerId].power = power
-
- local currentIndex = state.playerIndex[playerId]
- if currentIndex then
- adjustPlayerPosition(playerId)
- else
- -- 不在排行榜中,检查候选集
- if state.candidateMap[playerId] then
- checkCandidates()
- end
- end
- end
- end
- function interface.removePlayer(playerId)
- return removePlayerCompletely(playerId)
- end
- function interface.getRankList()
- checkCandidates() -- 每次获取时检查候选
-
- local result = {}
- for i = 1, state.size do
- local player = state.rankArray[i]
- table.insert(result, {
- playerId = player.id,
- name = player.name,
- rank = i,
- power = player.power,
- pointNum = player.pointNum,
- pointAllWeight = player.pointAllWeight
- })
- end
- return result
- end
- function interface.getPlayerRank(playerId)
- local index = state.playerIndex[playerId]
- return index
- end
- function interface.getPlayerDetail(playerId)
- local index = state.playerIndex[playerId]
- if index then
- local player = state.rankArray[index]
- return {
- rank = index,
- playerId = player.id,
- name = player.name,
- power = player.power,
- pointNum = player.pointNum,
- pointAllWeight = player.pointAllWeight
- }
- end
- return nil
- end
- function interface.hasPlayer(playerId)
- return state.players[playerId] ~= nil
- end
- function interface.getTotalPlayers()
- return tableCount(state.players)
- end
- function interface.batchUpdate(updates)
- for _, update in ipairs(updates) do
- if update.playerId then
- interface.updatePlayer(
- update.playerId,
- update.name,
- update.power,
- update.pointNum,
- update.pointAllWeight
- )
- end
- end
- end
- function interface._getState()
- return {
- totalPlayers = tableCount(state.players),
- rankSize = state.size,
- candidateCount = tableCount(state.candidateMap)
- }
- end
- return interface
- end
- -- 辅助函数:计算表的元素数量
- function tableCount(t)
- local count = 0
- for _ in pairs(t) do count = count + 1 end
- return count
- end
- -- 辅助函数:计算表的元素数量
- function tableCount(t)
- local count = 0
- for _ in pairs(t) do count = count + 1 end
- return count
- end
- -- 生成公会排行榜
- local function genGroupUnionRankList(unionRank, unionIdArr)
- local function getUnionRankVal(union)
- local occupyCityNum, occupyPointNum, power = 0, 0, 0
- power = union.power or 0
- for _, v in pairs(union.occupCityList or {}) do
- if v.isOccupy then
- occupyCityNum = occupyCityNum + 1
- end
- for _, _ in pairs(v.occupyPointList or {}) do
- occupyPointNum = occupyPointNum + 1
- end
- end
- return occupyCityNum, occupyPointNum, power
- end
- for _, unionId in ipairs(unionIdArr) do
- local unionData = AnotherWorldBattleData.unionList[unionId]
- if unionData then
- local occupyCityNum, occupyPointNum, power = getUnionRankVal(unionData)
- unionRank.updateGuild(unionId, unionData.name, power, occupyCityNum, occupyPointNum )
- end
- end
- end
- -- 生成个人排行榜
- local function genGroupPlayerRankList(playerRank, unionIdArr, playerListData)
- local playerArr = {}
- local function insertPlayerArr(unionId)
- if not playerListData then
- return
- end
- for playerUuid, playerInfo in pairs(playerListData) do
- if playerInfo.unionId == unionId then
- local pointNum, pointAllWeight = 0, 0
- for cityId, pointList in pairs(playerInfo.heroList or {}) do
- for _, _ in pairs(pointList) do
- pointNum = pointNum + 1
- local cityCfg = AnotherWorldBattleConfig.city[cityId]
- pointAllWeight = pointAllWeight + (cityCfg and cityCfg.pointWeight or 0)
- end
- end
- playerArr[#playerArr+1] = {playerId = playerUuid, name = playerInfo.name, power = playerInfo.power, pointNum = pointNum, pointAllWeight = pointAllWeight}
- end
- end
- end
- for _, unionId in ipairs(unionIdArr) do
- insertPlayerArr(unionId)
- end
- playerRank.batchUpdate(playerArr)
- end
- -- 生成 "公会-公会所属分组Id" 映射表
- local function genUnion2GroupList(groupId, unionIdArr)
- for _, unionId in ipairs(unionIdArr) do
- Union_2_Group[unionId] = groupId
- end
- end
- -- 生成 "serverId - 公会Array" 映射表
- local function genServer2UnionArr(unionRank)
- local rankList = unionRank.getRankList()
- for _, rankInfo in ipairs(rankList) do
- local unionId = rankInfo.guildId
- local unionInfo = AnotherWorldBattleData.unionList[unionId]
- if unionInfo then
- local serverId = unionInfo.serverId
- Server_2_UnionArr[serverId] = Server_2_UnionArr[serverId] or {}
- Server_2_UnionArr[serverId][#Server_2_UnionArr[serverId]+1] = unionId
- end
- end
- end
- -- 生成各组的缓存数据
- local function genGroupCache()
- if not AnotherWorldBattleData.groupArray then
- return
- end
- local playerListData = AnotherWorldBattleData.playerList
- for groupId, unionIdArr in ipairs(AnotherWorldBattleData.groupArray) do
- -- 城池
- Group_2_CityList[groupId] = {}
- genCityList(Group_2_CityList[groupId], unionIdArr)
- -- 公会排行榜
- Group_2_UnionRankList[groupId] = createGuildRank()
- genGroupUnionRankList(Group_2_UnionRankList[groupId], unionIdArr)
- -- "serverId -- unionIdArray" 映射表
- genServer2UnionArr(Group_2_UnionRankList[groupId])
- -- 个人排行榜
- Group_2_PlayerRankList[groupId] = createPlayerRank()
- genGroupPlayerRankList(Group_2_PlayerRankList[groupId], unionIdArr, playerListData)
- -- "公会-公会所属分组" 映射表
- genUnion2GroupList(groupId, unionIdArr)
- end
- end
- function initAfterStart()
- if _G.is_middle ~= true then return end
- loadData()
- genGroupCache()
- end
- -- 重置数据
- function ResetData()
- Group_2_CityList = {}
- Group_2_UnionRankList = {}
- Group_2_PlayerRankList = {}
- Union_2_Group = {}
- Server_2_UnionArr = {}
- AnotherWorldBattleData.lastRoundStartTime = os.time()
- -- AnotherWorldBattleData.stage = AnotherWorldBattleDefine.AB_STATE_JOIN
- AnotherWorldBattleData.joinUnionArr = nil
- AnotherWorldBattleData.groupArray = nil
- AnotherWorldBattleData.unionList = nil
- AnotherWorldBattleData.playerList = nil
- dbUpdate._id = AnotherWorldBattleData._id
- LuaMongo.update(DB.db_anotherWorldBattle, dbUpdate, AnotherWorldBattleData)
- end
- -- 获取公会所在分组的Id
- function GetUnionGroupId(unionId)
- return Union_2_Group[unionId]
- end
- -- 当自己公会没有参赛时, 获取本服第一个公会所在分组Id
- function GetGroupIdByServerId(serverId)
- local unionArr = Server_2_UnionArr[serverId]
- if unionArr and unionArr[1] then
- local unionId = unionArr[1]
- return GetUnionGroupId(unionId)
- end
- end
- -- 获取活动当前阶段
- function GetStage()
- return AnotherWorldBattleData.stage
- end
- -- 更新活动当前阶段
- function UpdateStage(newStage)
- AnotherWorldBattleData.stage = newStage
- updateValue("stage", newStage)
- if newStage == AnotherWorldBattleDefine.AB_STATE_BATTLE then
- genGroupCache()
- end
- end
- -- 获取活动上一轮的开始时间
- function GetLastRoundStartTime()
- return AnotherWorldBattleData.lastRoundStartTime
- end
- function UpdateLastRoundStartTime(newTime)
- AnotherWorldBattleData.lastRoundStartTime = newTime
- updateValue("lastRoundStartTime", newTime)
- end
- -- 获取参赛公会列表
- function GetJoinUnionArr()
- return AnotherWorldBattleData.joinUnionArr
- end
- -- 更新参赛公会列表
- function UpdateJoinUnionArr(newJoinUnionArr)
- AnotherWorldBattleData.joinUnionArr = newJoinUnionArr
- updateValue("joinUnionArr", newJoinUnionArr)
- end
- -- 获取分组列表
- function GetGroupArray()
- return AnotherWorldBattleData.groupArray
- end
- -- 更新分组列表
- function UpdateGroupArray(newGroupArray)
- AnotherWorldBattleData.groupArray = newGroupArray
- updateValue("groupArray", newGroupArray)
- end
- -- 获取公会名字
- function GetUnionName(unionId)
- return AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId].name or ""
- end
- -- 获取某个公会数据
- function GetUnionData(unionId)
- return AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId]
- end
- -- 更新某个公会数据
- function UpdateUnionData(unionId, newUnionData)
- if not AnotherWorldBattleData.unionList then
- AnotherWorldBattleData.unionList = {}
- -- updateValue("unionList", AnotherWorldBattleData.unionList)
- end
- AnotherWorldBattleData.unionList[unionId] = newUnionData
- updateValue("unionList"..".".. unionId, newUnionData)
- end
- -- 获取公会列表
- function GetUnionList()
- return AnotherWorldBattleData.unionList
- end
- -- 获取玩家名字
- function GetPlayerName(playerUuid)
- local playerListData = AnotherWorldBattleData.playerList
- if playerListData and playerListData[playerUuid] then
- return playerListData[playerUuid].name or ""
- end
- return ""
- end
- -- 获取玩家数据
- function GetPlayerData(playerUuid)
- return AnotherWorldBattleData.playerList and AnotherWorldBattleData.playerList[playerUuid]
- end
- -- 更新玩家数据
- function UpdatePlayerData(playerUuid, newPlayerData)
- if not AnotherWorldBattleData.playerList then
- AnotherWorldBattleData.playerList = {}
- -- updateValue("playerList", AnotherWorldBattleData.playerList)
- end
- AnotherWorldBattleData.playerList[playerUuid] = newPlayerData
- updateValue("playerList".. "." .. playerUuid, newPlayerData)
- end
- -- 获取公会列表
- function GetPlayerList()
- return AnotherWorldBattleData.playerList
- end
- -- 获取城池数据
- function GetCityListByGroupId(groupId)
- return Group_2_CityList[groupId]
- end
- -- 更新城池数据
- function UpdateCityList(groupId, newCityList)
- Group_2_CityList[groupId] = newCityList
- end
- -- 获取公会排行数据
- function GetUnionRankList(groupId)
- local unionRank = Group_2_UnionRankList[groupId]
- if unionRank then
- return unionRank.getRankList()
- end
- end
- -- 更新公会的排行榜数据, cityVal, pointVal为变化的值
- function UpdateUnionRankList(groupId, unionId, cityVal, pointVal, newPower)
- local unionRank = Group_2_UnionRankList[groupId]
- if unionRank then
- local unionRankInfo = unionRank.getGuildDetail(unionId)
- unionRankInfo.occupyCityNum = unionRankInfo.occupyCityNum + cityVal
- unionRankInfo.occupyPointNum = unionRankInfo.occupyPointNum + pointVal
- if newPower then
- unionRankInfo.power = newPower
- end
- unionRank.updateGuild(unionId, unionRankInfo.name, unionRankInfo.power, unionRankInfo.occupyCityNum, unionRankInfo.occupyPointNum)
- end
- end
- -- 生成玩家用于排名的数据
- local function genPlayerRankVal(playerUuid)
- local playerListData = AnotherWorldBattleData.playerList
- local name, power, pointNum, pointAllWeight = "", 0, 0, 0
- if playerListData and playerListData[playerUuid] then
- power = playerListData[playerUuid].power or 0
- name = playerListData[playerUuid].name or ""
- for cityId, pointList in pairs(playerListData[playerUuid].heroList or {}) do
- for _, _ in pairs(pointList) do
- pointNum = pointNum + 1
- local cityCfg = AnotherWorldBattleConfig.city[cityId]
- pointAllWeight = pointAllWeight + (cityCfg and cityCfg.pointWeight or 0)
- end
- end
- end
- return name, power, pointNum, pointAllWeight
- end
- -- 获取玩家排行数据
- function GetPlayerRankList(groupId)
- local playerRank = Group_2_PlayerRankList[groupId]
- if playerRank then
- return playerRank.getRankList()
- end
- end
- -- 更新玩家排行数据, pointVal, pointWeightVal为变化的值
- function UpdatePlayerRankList(groupId, playerUuid, pointVal, pointWeightVal, newPower)
- local playerRank = Group_2_PlayerRankList[groupId]
- if playerRank then
- local playerRankData = playerRank.getPlayerDetail(playerUuid)
- if not playerRankData then
- playerRankData = {}
- local name, power, pointNum, pointAllWeight = genPlayerRankVal(playerUuid)
- playerRankData.name = name
- playerRankData.power = power
- playerRankData.pointNum = pointNum
- playerRankData.pointAllWeight = pointAllWeight
- else
- playerRankData.pointNum = playerRankData.pointNum + pointVal
- playerRankData.pointAllWeight = playerRankData.pointAllWeight + pointWeightVal
- if newPower then
- playerRankData.power = newPower
- end
- end
- playerRank.updatePlayer(playerUuid, playerRankData.name, playerRankData.power, playerRankData.pointNum, playerRankData.pointAllWeight)
- end
- end
|