-- 异界之战(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, --当前是否占领 -- occupyTimeArr = { {startTime1, endTime1}, {startTime2, endTime2} }, -- occupyPointList = { --占领本城池中的据点列表 -- [pointId1] = playerUuid, -- [pointId2] = playerUuid, -- }, -- }, -- }, -- playerList = { -- [playerUuid] = { -- name = "", -- lv = 200, -- head = 1234, -- headFrame = 1234, -- power = 999, -- heroList = { -- [cityId] = { -- [pointIdx] = { -- { -- heroUuid = "sdgjdjdj", -- heroStar = 111, -- heroLevel = 111, -- heroCamp = 1, -- heroBody = 111, -- heroIcon = 111, -- heroPower = 11111, -- }, -- }, -- }, -- } -- }, -- }, -- gatherInfo = { --集结信息 -- gatherTime = 0, -- gatherCity = 123, -- }, -- }, -- }, } -- 所有分组的城池信息, 启动服务器后生成, 用于快速获取城池信息 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 = { -- [group1] = { -- {unionId = "123", occupyCityNum = 1, occupyPointNum = 0, power = 999}, -- {unionId = "456", occupyCityNum = 0, occupyPointNum = 0, power = 999}, -- }, } -- 所有分组的玩家排行榜, 启动服务器后生成 Group_2_PlayerRankList = { -- [group1] = { -- {unionId = '1244', playerUuid = "123", pointNum = 2, pointAllWeight = 999, power = 999}, -- {unionId = '1244', playerUuid = "777", pointNum = 1, pointAllWeight = 888, power = 999}, -- }, } -- 公会 - 所属分组 映射表 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 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] then return unionId, pointList[targetPointIdx] end end end end for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do net[cityId] = { pointArr = {} } local unionId = getUnionByCityId(cityId) if unionId then net[cityId].occupyUnion = unionId end local pointArr = net[cityId].pointArr for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do pointArr[i] = {} local unionId1, playerUuid = getPointOccupyPlayer(cityId, i) if unionId1 and playerUuid then pointArr[i].unionId = unionId1 pointArr[i].playerUuid = playerUuid end end end end -- 公会排行榜排序算法 local function unionRankSortFunc(unionRankList) table.sort(unionRankList, function (a, b) if a.occupyCityNum > b.occupyCityNum then return true elseif a.occupyCityNum == b.occupyCityNum then if a.occupyPointNum > b.occupyCityNum then return true elseif a.occupyPointNum == b.occupyCityNum then return a.power > b.power else return false end else return false end end) end -- 个人排行榜排序算法 local function playerRankSortFunc(playerRankList) table.sort(playerRankList, function (a, b) if a.pointNum > b.pointNum then return true elseif a.pointNum == b.pointNum then if a.pointAllWeight > b.pointAllWeight then return true elseif a.pointAllWeight == b.pointAllWeight then return a.power > b.power else return false end else return false end end) end -- 生成公会排行榜 local function genGroupUnionRankList(net, 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 unionInfo = AnotherWorldBattleData.unionList[unionId] if unionInfo then local occupyCityNum, occupyPointNum, power = getUnionRankVal(unionInfo) net[#net+1] = { unionId = unionId, occupyCityNum = occupyCityNum, occupyPointNum = occupyPointNum, power = power } end end unionRankSortFunc(net) -- while #net > AnotherWorldBattleDefine.AB_RANK_MAX_NUM do -- table.remove(net) -- end end -- 生成个人排行榜 local function genGroupPlayerRankList(net, unionIdArr) local playerList = {} for _, unionId in ipairs(unionIdArr) do local unionInfo = AnotherWorldBattleData.unionList[unionId] if unionInfo and unionInfo.occupCityList then for cityId, v in pairs(unionInfo.occupCityList) do for pointId, playerUuid in pairs(v.occupyPointList or {}) do playerList[playerUuid] = playerUuid[playerUuid] or { playerUuid = playerUuid, pointNum = 0, pointAllWeight = 0, power = 0, unionId = unionId} playerList[playerUuid].pointNum = playerList[playerUuid].pointNum + 1 local cityCfg = AnotherWorldBattleConfig.city[cityId] playerList[playerUuid].pointAllWeight = playerList[playerUuid].pointAllWeight + cityCfg.pointWeight if unionInfo.playerList and unionInfo.playerList[playerUuid] then playerList[playerUuid].power = unionInfo.playerList[playerUuid].power or 0 end end end end end for _, playerRankInfo in pairs(playerList) do net[#net+1] = playerRankInfo end playerRankSortFunc(net) -- while #net > AnotherWorldBattleDefine.AB_RANK_MAX_NUM do -- table.remove(net) -- end end -- 生成 "公会-公会所属分组Id" 映射表 local function genUnion2GroupList(groupId, unionIdArr) for _, unionId in ipairs(unionIdArr) do Union_2_Group[unionId] = groupId end end -- 生成各组的缓存数据 local function genGroupCache() if not AnotherWorldBattleData.groupArray then return end for groupId, unionIdArr in ipairs(AnotherWorldBattleData.groupArray) do -- 城池 Group_2_CityList[groupId] = {} genCityList(Group_2_CityList[groupId], unionIdArr) -- 公会排行榜 Group_2_UnionRankList[groupId] = {} genGroupUnionRankList(Group_2_UnionRankList[groupId], unionIdArr) -- 个人排行榜 Group_2_PlayerRankList[groupId] = {} genGroupPlayerRankList(Group_2_PlayerRankList[groupId], unionIdArr) -- 公会-公会所属分组 映射表 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 = {} AnotherWorldBattleData.lastRoundStartTime = os.time() -- AnotherWorldBattleData.stage = AnotherWorldBattleDefine.AB_STATE_JOIN AnotherWorldBattleData.joinUnionArr = nil AnotherWorldBattleData.groupArray = nil AnotherWorldBattleData.unionList = nil dbUpdate._id = AnotherWorldBattleData._id LuaMongo.update(DB.db_anotherWorldBattle, dbUpdate, AnotherWorldBattleData) 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 GetUnionList() return AnotherWorldBattleData.unionList end function UpdateUnionList(newUnionList) -- 待确定 AnotherWorldBattleData.unionList = newUnionList updateValue("unionList", newUnionList) 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) AnotherWorldBattleData.unionList[unionId] = NewUnionData updateValue("unionList"..".".. unionId, NewUnionData) --待确认 end function GetPlayerName(unionId, playerUuid) if AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId] then local playerList = AnotherWorldBattleData.unionList[unionId].playerList if playerList and playerList[playerUuid] then return playerList[playerUuid].name end end return "" end -- 获取公会所在分组的Id function GetUnionGroupId(unionId) return Union_2_Group[unionId] end -- 根据分组Id, 获取本组的城池数据 function GetCityListByGroupId(groupId) return Group_2_CityList[groupId] end function UpdateCityList(groupId, newCityList) Group_2_CityList[groupId] = newCityList end -- 根据分组Id, 获取本组的公会排行数据 function GetUnionRankList(groupId) return Group_2_UnionRankList[groupId] end function UpdateUnionRankList(groupId, newRankList) Group_2_UnionRankList[groupId] = newRankList unionRankSortFunc(Group_2_UnionRankList[groupId]) end -- 根据分组Id, 获取本组的玩家排行数据 function GetPlayerRankList(groupId) return Group_2_PlayerRankList[groupId] end function UpdatePlayerRankList(groupId, newRankList) Group_2_PlayerRankList[groupId] = newRankList playerRankSortFunc(Group_2_PlayerRankList[groupId]) end