|
|
@@ -204,589 +204,136 @@ local function genCityList(net, unionIdArr)
|
|
|
end
|
|
|
|
|
|
-- 工会排行榜
|
|
|
+-- 目前最多8条
|
|
|
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)
|
|
|
+ local guilds = {} -- id -> guildRec(永久表,不删)
|
|
|
+ local rank = {} -- 1..#guilds 复用数组
|
|
|
+
|
|
|
+ local function cmp(a, b)
|
|
|
+ if a.occupyCityNum ~= b.occupyCityNum then
|
|
|
+ return a.occupyCityNum > b.occupyCityNum
|
|
|
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
|
|
|
- }
|
|
|
+ if a.occupyPointNum ~= b.occupyPointNum then
|
|
|
+ return a.occupyPointNum > b.occupyPointNum
|
|
|
end
|
|
|
-
|
|
|
- state.isDirty = false
|
|
|
+ return a.power > b.power
|
|
|
end
|
|
|
|
|
|
- -- 定义接口函数
|
|
|
- function interface.updateGuild(guildId, name, power, occupyCityNum, occupyPointNum)
|
|
|
- if not state.guilds[guildId] then
|
|
|
- state.guilds[guildId] = {
|
|
|
- id = guildId,
|
|
|
- name = name,
|
|
|
- power = power,
|
|
|
+ local function updateGuild(id, name, occupyCityNum, occupyPointNum, power)
|
|
|
+ local g = guilds[id]
|
|
|
+ if not g then
|
|
|
+ g = { id = id, name = name or "",
|
|
|
occupyCityNum = occupyCityNum or 0,
|
|
|
- occupyPointNum = occupyPointNum or 0
|
|
|
- }
|
|
|
+ occupyPointNum = occupyPointNum or 0,
|
|
|
+ power = power or 0 }
|
|
|
+ guilds[id] = g
|
|
|
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
|
|
|
+ g.occupyCityNum = occupyCityNum or g.occupyCityNum
|
|
|
+ g.occupyPointNum = occupyPointNum or g.occupyPointNum
|
|
|
+ g.power = power or g.power
|
|
|
+ if name then g.name = name end
|
|
|
+ end
|
|
|
+
|
|
|
+ -- 全量重建(8 条以内,可忽略性能)
|
|
|
+ local n = 0
|
|
|
+ for _, v in pairs(guilds) do
|
|
|
+ n = n + 1
|
|
|
+ rank[n] = v
|
|
|
+ end
|
|
|
+ table.sort(rank, cmp)
|
|
|
+ for i = n + 1, #rank do rank[i] = nil end -- 清理旧数据
|
|
|
+ end
|
|
|
+
|
|
|
+ local function getRankList()
|
|
|
+ local ret = {}
|
|
|
+ for i = 1, #rank do
|
|
|
+ local g = rank[i]
|
|
|
+ ret[i] =
|
|
|
+ {
|
|
|
+ rank = i, guildId = g.id, name = g.name,
|
|
|
+ occupyCityNum = g.occupyCityNum,
|
|
|
+ occupyPointNum = g.occupyPointNum,
|
|
|
+ power = g.power}
|
|
|
+ end
|
|
|
+ return ret
|
|
|
+ end
|
|
|
+
|
|
|
+ local function getGuildDetail(id) return guilds[id] end
|
|
|
+ -- 在返回表里加
|
|
|
+ return {
|
|
|
+ updateGuild = updateGuild,
|
|
|
+ getRankList = getRankList,
|
|
|
+ getGuildDetail = getGuildDetail
|
|
|
+ }
|
|
|
|
|
|
- return interface
|
|
|
end
|
|
|
|
|
|
-- 玩家排行榜
|
|
|
+-- 当前每个公会最多25人,8个公会为一组,最多200人
|
|
|
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
|
|
|
+ local players = {} -- id -> rec(永久存在,不删)
|
|
|
+ local rank = {} -- 1..RANK_MAX 复用
|
|
|
+ local tmp = {} -- 一次性长到 MAX_PEOPLE
|
|
|
+
|
|
|
+ local function cmp(a, b)
|
|
|
+ if a.pointNum ~= b.pointNum then return a.pointNum > b.pointNum end
|
|
|
+ if a.pointAllWeight ~= b.pointAllWeight then return a.pointAllWeight > b.pointAllWeight end
|
|
|
+ return a.power > b.power
|
|
|
+ end
|
|
|
+
|
|
|
+ -- 预分配 tmp 数组,避免运行时扩容
|
|
|
+ for i = 1, 500 do tmp[i] = nil end
|
|
|
+
|
|
|
+ local function updatePlayer(id, name, power, pNum, pWeight)
|
|
|
+ -- 1. 取出或新建记录
|
|
|
+ local rec = players[id]
|
|
|
+ if not rec then
|
|
|
+ rec = { id = id, name = name or "", power = power,
|
|
|
+ pointNum = pNum or 0, pointAllWeight = pWeight or 0 }
|
|
|
+ players[id] = rec
|
|
|
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
|
|
|
+ -- 原地更新
|
|
|
+ rec.power = power
|
|
|
+ if pNum ~= nil then rec.pointNum = pNum end
|
|
|
+ if pWeight ~= nil then rec.pointAllWeight = pWeight end
|
|
|
+ if name then rec.name = name 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
|
|
|
- })
|
|
|
+ -- 2. 全量收集(只复制引用,不创建新表)
|
|
|
+ local n = 0
|
|
|
+ for _, v in pairs(players) do
|
|
|
+ n = n + 1
|
|
|
+ tmp[n] = v
|
|
|
end
|
|
|
- return result
|
|
|
- end
|
|
|
|
|
|
- function interface.getPlayerRank(playerId)
|
|
|
- local index = state.playerIndex[playerId]
|
|
|
- return index
|
|
|
- end
|
|
|
+ -- 3. 只排序有用部分
|
|
|
+ table.sort(tmp, cmp)
|
|
|
|
|
|
- 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
|
|
|
- }
|
|
|
+ -- 4. 取前 RANK_MAX 到 rank,复用 rank 数组
|
|
|
+ for i = 1, AnotherWorldBattleDefine.AB_RANK_MAX_NUM do
|
|
|
+ rank[i] = tmp[i]
|
|
|
end
|
|
|
- return nil
|
|
|
- end
|
|
|
-
|
|
|
- function interface.hasPlayer(playerId)
|
|
|
- return state.players[playerId] ~= nil
|
|
|
+ for i = AnotherWorldBattleDefine.AB_RANK_MAX_NUM + 1, #rank do rank[i] = nil end
|
|
|
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
|
|
|
+ local function getRankList()
|
|
|
+ local ret = {}
|
|
|
+ for i = 1, #rank do
|
|
|
+ local p = rank[i]
|
|
|
+ ret[i] = { rank = i, playerId = p.id, name = p.name, power = p.power,
|
|
|
+ pointNum = p.pointNum, pointAllWeight = p.pointAllWeight }
|
|
|
end
|
|
|
+ return ret
|
|
|
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
|
|
|
+ local function getPlayerDetail(id) return players[id] end
|
|
|
+ return {
|
|
|
+ updatePlayer = updatePlayer,
|
|
|
+ getRankList = getRankList,
|
|
|
+ getPlayerDetail = getPlayerDetail,
|
|
|
+ }
|
|
|
end
|
|
|
|
|
|
-
|
|
|
-- 生成公会排行榜
|
|
|
local function genGroupUnionRankList(unionRank, unionIdArr)
|
|
|
local function getUnionRankVal(union)
|
|
|
@@ -810,16 +357,14 @@ local function genGroupUnionRankList(unionRank, unionIdArr)
|
|
|
local unionData = AnotherWorldBattleData.unionList[unionId]
|
|
|
if unionData then
|
|
|
local occupyCityNum, occupyPointNum, power = getUnionRankVal(unionData)
|
|
|
- unionRank.updateGuild(unionId, unionData.name, power, occupyCityNum, occupyPointNum )
|
|
|
+ unionRank.updateGuild(unionId, unionData.name, occupyCityNum, occupyPointNum, power)
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
-- 生成个人排行榜
|
|
|
local function genGroupPlayerRankList(playerRank, unionIdArr, playerListData)
|
|
|
- local playerArr = {}
|
|
|
-
|
|
|
- local function insertPlayerArr(unionId)
|
|
|
+ local function insertPlayerRank(unionId)
|
|
|
if not playerListData then
|
|
|
return
|
|
|
end
|
|
|
@@ -835,16 +380,15 @@ local function genGroupPlayerRankList(playerRank, unionIdArr, playerListData)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
- playerArr[#playerArr+1] = {playerId = playerUuid, name = playerInfo.name, power = playerInfo.power, pointNum = pointNum, pointAllWeight = pointAllWeight}
|
|
|
+ playerRank.updatePlayer(playerUuid, playerInfo.name, playerInfo.power, pointNum, pointAllWeight)
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
for _, unionId in ipairs(unionIdArr) do
|
|
|
- insertPlayerArr(unionId)
|
|
|
+ insertPlayerRank(unionId)
|
|
|
end
|
|
|
|
|
|
- playerRank.batchUpdate(playerArr)
|
|
|
end
|
|
|
|
|
|
-- 生成 "公会-公会所属分组Id" 映射表
|
|
|
@@ -1048,16 +592,30 @@ end
|
|
|
|
|
|
|
|
|
-- 获取城池数据
|
|
|
+function GetCityData(groupId, cityId)
|
|
|
+ if Group_2_CityList[groupId] then
|
|
|
+ return Group_2_CityList[groupId][cityId]
|
|
|
+ end
|
|
|
+end
|
|
|
+-- 更新城池数据
|
|
|
+function UpdateCityData(groupId, cityId, newCityData)
|
|
|
+ if Group_2_CityList[groupId] then
|
|
|
+ Group_2_CityList[groupId][cityId] = newCityData
|
|
|
+ end
|
|
|
+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]
|
|
|
@@ -1075,7 +633,7 @@ function UpdateUnionRankList(groupId, unionId, cityVal, pointVal, newPower)
|
|
|
if newPower then
|
|
|
unionRankInfo.power = newPower
|
|
|
end
|
|
|
- unionRank.updateGuild(unionId, unionRankInfo.name, unionRankInfo.power, unionRankInfo.occupyCityNum, unionRankInfo.occupyPointNum)
|
|
|
+ unionRank.updateGuild(unionId, unionRankInfo.name, unionRankInfo.occupyCityNum, unionRankInfo.occupyPointNum, unionRankInfo.power)
|
|
|
end
|
|
|
end
|
|
|
|