AnotherWorldBattleDB.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. -- 异界之战(DB)
  2. local LuaMongo = _G.lua_mongo
  3. local DB = require("common.DB")
  4. local AnotherWorldBattleDefine = require("anotherWorldBattle.AnotherWorldBattleDefine")
  5. local AnotherWorldBattleConfig = require("excel.anotherWorldBattle")
  6. local dbUpdate = {_id=nil}
  7. local dbUpdateField = {}
  8. -- db
  9. AnotherWorldBattleData = {
  10. -- lastRoundStartTime = 0, -- 上一轮活动开启时间
  11. -- stage = 1, -- 当前阶段
  12. -- joinUnionArr = {
  13. -- { unionId = "", serverId = 123, power = 9999},
  14. -- },
  15. -- groupArray = {
  16. -- [1] = {unionId1, unionId2, unionId3, unionId4, unionId5, unionId6, unionId7, unionId8},
  17. -- [2] = {unionId9, unionId10,...},
  18. -- },
  19. -- unionList = {
  20. -- [unionId1] = {
  21. -- serverId = 123, -- 公会所在服务器Id
  22. -- power = 9999,
  23. -- name = "111",
  24. -- baseCityId = 123, -- 出生城池
  25. -- baseCityStartTime = nil, -- 出生城的开始占领时间
  26. -- occupCityList = { -- 城池列表(包括曾占领过, 当前成功占领, 当前只占领过一个或多个据点)
  27. -- [cityId1] = {
  28. -- isOccupy = false, --当前是否占领
  29. -- occupyTimeArr = { {startTime1, endTime1}, {startTime2, endTime2} },
  30. -- occupyPointList = { --占领本城池中的据点列表
  31. -- [pointId1] = playerUuid,
  32. -- [pointId2] = playerUuid,
  33. -- },
  34. -- },
  35. -- },
  36. -- playerList = {
  37. -- [playerUuid] = {
  38. -- name = "",
  39. -- lv = 200,
  40. -- head = 1234,
  41. -- headFrame = 1234,
  42. -- power = 999,
  43. -- heroList = {
  44. -- [cityId] = {
  45. -- [pointIdx] = {
  46. -- {
  47. -- heroUuid = "sdgjdjdj",
  48. -- heroStar = 111,
  49. -- heroLevel = 111,
  50. -- heroCamp = 1,
  51. -- heroBody = 111,
  52. -- heroIcon = 111,
  53. -- heroPower = 11111,
  54. -- },
  55. -- },
  56. -- },
  57. -- }
  58. -- },
  59. -- },
  60. -- gatherInfo = { --集结信息
  61. -- gatherTime = 0,
  62. -- gatherCity = 123,
  63. -- },
  64. -- },
  65. -- },
  66. }
  67. -- 所有分组的城池信息, 启动服务器后生成, 用于快速获取城池信息
  68. Group_2_CityList = {
  69. -- [groupId] = {
  70. -- [cityId] = {
  71. -- occupyUnion = { -- 占领公会信息, 没有公会占领时为nil
  72. -- serverId = 124,
  73. -- unionId = "123",
  74. -- },
  75. -- pointArr = { -- 城池中所有据点信息, 没人占领时, unionId, playerUuid为nil
  76. -- {unionId = nil, playerUuid = nil, },
  77. -- {unionId = nil, playerUuid = nil, },
  78. -- }
  79. -- },
  80. -- },
  81. }
  82. -- 所有分组的公会排行榜, 启动服务器后生成
  83. Group_2_UnionRankList = {
  84. -- [group1] = {
  85. -- {unionId = "123", occupyCityNum = 1, occupyPointNum = 0, power = 999},
  86. -- {unionId = "456", occupyCityNum = 0, occupyPointNum = 0, power = 999},
  87. -- },
  88. }
  89. -- 所有分组的玩家排行榜, 启动服务器后生成
  90. Group_2_PlayerRankList = {
  91. -- [group1] = {
  92. -- {unionId = '1244', playerUuid = "123", pointNum = 2, pointAllWeight = 999, power = 999},
  93. -- {unionId = '1244', playerUuid = "777", pointNum = 1, pointAllWeight = 888, power = 999},
  94. -- },
  95. }
  96. -- 公会 - 所属分组 映射表
  97. Union_2_Group = {
  98. -- [unionId] = groupId,
  99. }
  100. local function initData()
  101. AnotherWorldBattleData.lastRoundStartTime = 0
  102. AnotherWorldBattleData.stage = AnotherWorldBattleDefine.AB_STATE_END
  103. LuaMongo.insert(DB.db_anotherWorldBattle, AnotherWorldBattleData)
  104. end
  105. local function loadData()
  106. LuaMongo.find(DB.db_anotherWorldBattle)
  107. local data = {}
  108. if LuaMongo.next(data) then
  109. AnotherWorldBattleData = data
  110. else
  111. initData()
  112. end
  113. end
  114. -- 修改db单个字段
  115. local function updateValue(key, value)
  116. if not key then return end
  117. if value then
  118. dbUpdateField["$set"] = {[key]=value}
  119. dbUpdateField["$unset"] = nil
  120. else
  121. dbUpdateField["$set"] = nil
  122. dbUpdateField["$unset"] = {[key]=1}
  123. end
  124. dbUpdate._id = AnotherWorldBattleData._id
  125. LuaMongo.update(DB.db_anotherWorldBattle, dbUpdate, dbUpdateField)
  126. end
  127. -- 生成城池数据
  128. local function genCityList(net, unionIdArr)
  129. local function getUnionByCityId(targetCityId)
  130. for _, unionId in ipairs(unionIdArr) do
  131. local unionInfo = AnotherWorldBattleData.unionList[unionId]
  132. -- 其他城池
  133. if unionInfo and unionInfo.occupCityList then
  134. for occupyCityId, occupyCityinfo in pairs(unionInfo.occupCityList) do
  135. if occupyCityId == targetCityId and occupyCityinfo.isOccupy then
  136. return unionId
  137. end
  138. end
  139. end
  140. -- 出生城池
  141. if unionInfo and unionInfo.baseCityId == targetCityId then
  142. return unionId
  143. end
  144. end
  145. end
  146. local function getPointOccupyPlayer(targetCityId, targetPointIdx)
  147. for _, unionId in ipairs(unionIdArr) do
  148. local unionInfo = AnotherWorldBattleData.unionList[unionId]
  149. if unionInfo and unionInfo.occupCityList and unionInfo.occupCityList[targetCityId] then
  150. local pointList = unionInfo.occupCityList[targetCityId].occupyPointList
  151. if pointList and pointList[targetPointIdx] then
  152. return unionId, pointList[targetPointIdx]
  153. end
  154. end
  155. end
  156. end
  157. for cityId, cityCfg in ipairs(AnotherWorldBattleConfig.city) do
  158. net[cityId] = { pointArr = {} }
  159. local unionId = getUnionByCityId(cityId)
  160. if unionId then
  161. net[cityId].occupyUnion = unionId
  162. end
  163. local pointArr = net[cityId].pointArr
  164. for i=1, AnotherWorldBattleDefine.AB_POINT_MAX_NUM do
  165. pointArr[i] = {}
  166. local unionId1, playerUuid = getPointOccupyPlayer(cityId, i)
  167. if unionId1 and playerUuid then
  168. pointArr[i].unionId = unionId1
  169. pointArr[i].playerUuid = playerUuid
  170. end
  171. end
  172. end
  173. end
  174. -- 公会排行榜排序算法
  175. local function unionRankSortFunc(unionRankList)
  176. table.sort(unionRankList, function (a, b)
  177. if a.occupyCityNum > b.occupyCityNum then
  178. return true
  179. elseif a.occupyCityNum == b.occupyCityNum then
  180. if a.occupyPointNum > b.occupyCityNum then
  181. return true
  182. elseif a.occupyPointNum == b.occupyCityNum then
  183. return a.power > b.power
  184. else
  185. return false
  186. end
  187. else
  188. return false
  189. end
  190. end)
  191. end
  192. -- 个人排行榜排序算法
  193. local function playerRankSortFunc(playerRankList)
  194. table.sort(playerRankList, function (a, b)
  195. if a.pointNum > b.pointNum then
  196. return true
  197. elseif a.pointNum == b.pointNum then
  198. if a.pointAllWeight > b.pointAllWeight then
  199. return true
  200. elseif a.pointAllWeight == b.pointAllWeight then
  201. return a.power > b.power
  202. else
  203. return false
  204. end
  205. else
  206. return false
  207. end
  208. end)
  209. end
  210. -- 生成公会排行榜
  211. local function genGroupUnionRankList(net, unionIdArr)
  212. local function getUnionRankVal(union)
  213. local occupyCityNum, occupyPointNum, power = 0, 0, 0
  214. power = union.power or 0
  215. for _,v in pairs(union.occupCityList or {}) do
  216. if v.isOccupy then
  217. occupyCityNum = occupyCityNum + 1
  218. end
  219. for _,_ in pairs(v.occupyPointList or {}) do
  220. occupyPointNum = occupyPointNum + 1
  221. end
  222. end
  223. return occupyCityNum, occupyPointNum, power
  224. end
  225. for _, unionId in ipairs(unionIdArr) do
  226. local unionInfo = AnotherWorldBattleData.unionList[unionId]
  227. if unionInfo then
  228. local occupyCityNum, occupyPointNum, power = getUnionRankVal(unionInfo)
  229. net[#net+1] = {
  230. unionId = unionId,
  231. occupyCityNum = occupyCityNum,
  232. occupyPointNum = occupyPointNum,
  233. power = power
  234. }
  235. end
  236. end
  237. unionRankSortFunc(net)
  238. -- while #net > AnotherWorldBattleDefine.AB_RANK_MAX_NUM do
  239. -- table.remove(net)
  240. -- end
  241. end
  242. -- 生成个人排行榜
  243. local function genGroupPlayerRankList(net, unionIdArr)
  244. local playerList = {}
  245. for _, unionId in ipairs(unionIdArr) do
  246. local unionInfo = AnotherWorldBattleData.unionList[unionId]
  247. if unionInfo and unionInfo.occupCityList then
  248. for cityId, v in pairs(unionInfo.occupCityList) do
  249. for pointId, playerUuid in pairs(v.occupyPointList or {}) do
  250. playerList[playerUuid] = playerUuid[playerUuid] or { playerUuid = playerUuid, pointNum = 0, pointAllWeight = 0, power = 0, unionId = unionId}
  251. playerList[playerUuid].pointNum = playerList[playerUuid].pointNum + 1
  252. local cityCfg = AnotherWorldBattleConfig.city[cityId]
  253. playerList[playerUuid].pointAllWeight = playerList[playerUuid].pointAllWeight + cityCfg.pointWeight
  254. if unionInfo.playerList and unionInfo.playerList[playerUuid] then
  255. playerList[playerUuid].power = unionInfo.playerList[playerUuid].power or 0
  256. end
  257. end
  258. end
  259. end
  260. end
  261. for _, playerRankInfo in pairs(playerList) do
  262. net[#net+1] = playerRankInfo
  263. end
  264. playerRankSortFunc(net)
  265. -- while #net > AnotherWorldBattleDefine.AB_RANK_MAX_NUM do
  266. -- table.remove(net)
  267. -- end
  268. end
  269. -- 生成 "公会-公会所属分组Id" 映射表
  270. local function genUnion2GroupList(groupId, unionIdArr)
  271. for _, unionId in ipairs(unionIdArr) do
  272. Union_2_Group[unionId] = groupId
  273. end
  274. end
  275. -- 生成各组的缓存数据
  276. local function genGroupCache()
  277. if not AnotherWorldBattleData.groupArray then
  278. return
  279. end
  280. for groupId, unionIdArr in ipairs(AnotherWorldBattleData.groupArray) do
  281. -- 城池
  282. Group_2_CityList[groupId] = {}
  283. genCityList(Group_2_CityList[groupId], unionIdArr)
  284. -- 公会排行榜
  285. Group_2_UnionRankList[groupId] = {}
  286. genGroupUnionRankList(Group_2_UnionRankList[groupId], unionIdArr)
  287. -- 个人排行榜
  288. Group_2_PlayerRankList[groupId] = {}
  289. genGroupPlayerRankList(Group_2_PlayerRankList[groupId], unionIdArr)
  290. -- 公会-公会所属分组 映射表
  291. genUnion2GroupList(groupId, unionIdArr)
  292. end
  293. end
  294. function initAfterStart()
  295. if _G.is_middle ~= true then return end
  296. loadData()
  297. genGroupCache()
  298. end
  299. -- 重置数据
  300. function ResetData()
  301. Group_2_CityList = {}
  302. Group_2_UnionRankList = {}
  303. Group_2_PlayerRankList = {}
  304. Union_2_Group = {}
  305. AnotherWorldBattleData.lastRoundStartTime = os.time()
  306. -- AnotherWorldBattleData.stage = AnotherWorldBattleDefine.AB_STATE_JOIN
  307. AnotherWorldBattleData.joinUnionArr = nil
  308. AnotherWorldBattleData.groupArray = nil
  309. AnotherWorldBattleData.unionList = nil
  310. dbUpdate._id = AnotherWorldBattleData._id
  311. LuaMongo.update(DB.db_anotherWorldBattle, dbUpdate, AnotherWorldBattleData)
  312. end
  313. function GetStage()
  314. return AnotherWorldBattleData.stage
  315. end
  316. function UpdateStage(newStage)
  317. AnotherWorldBattleData.stage = newStage
  318. updateValue("stage", newStage)
  319. if newStage == AnotherWorldBattleDefine.AB_STATE_BATTLE then
  320. genGroupCache()
  321. end
  322. end
  323. function GetLastRoundStartTime()
  324. return AnotherWorldBattleData.lastRoundStartTime
  325. end
  326. function UpdateLastRoundStartTime(newTime)
  327. AnotherWorldBattleData.lastRoundStartTime = newTime
  328. updateValue("lastRoundStartTime", newTime)
  329. end
  330. function GetJoinUnionArr()
  331. return AnotherWorldBattleData.joinUnionArr
  332. end
  333. function UpdateJoinUnionArr(newJoinUnionArr)
  334. AnotherWorldBattleData.joinUnionArr = newJoinUnionArr
  335. updateValue("joinUnionArr", newJoinUnionArr)
  336. end
  337. function GetGroupArray()
  338. return AnotherWorldBattleData.groupArray
  339. end
  340. function UpdateGroupArray(newGroupArray)
  341. AnotherWorldBattleData.groupArray = newGroupArray
  342. updateValue("groupArray", newGroupArray)
  343. end
  344. function GetUnionList()
  345. return AnotherWorldBattleData.unionList
  346. end
  347. function UpdateUnionList(newUnionList) -- 待确定
  348. AnotherWorldBattleData.unionList = newUnionList
  349. updateValue("unionList", newUnionList)
  350. end
  351. function GetUnionName(unionId)
  352. return AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId].name or ""
  353. end
  354. function GetUnionData(unionId)
  355. return AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId]
  356. end
  357. function UpdateUnionData(unionId, NewUnionData)
  358. AnotherWorldBattleData.unionList[unionId] = NewUnionData
  359. updateValue("unionList"..".".. unionId, NewUnionData) --待确认
  360. end
  361. function GetPlayerName(unionId, playerUuid)
  362. if AnotherWorldBattleData.unionList and AnotherWorldBattleData.unionList[unionId] then
  363. local playerList = AnotherWorldBattleData.unionList[unionId].playerList
  364. if playerList and playerList[playerUuid] then
  365. return playerList[playerUuid].name
  366. end
  367. end
  368. return ""
  369. end
  370. -- 获取公会所在分组的Id
  371. function GetUnionGroupId(unionId)
  372. return Union_2_Group[unionId]
  373. end
  374. -- 根据分组Id, 获取本组的城池数据
  375. function GetCityListByGroupId(groupId)
  376. return Group_2_CityList[groupId]
  377. end
  378. function UpdateCityList(groupId, newCityList)
  379. Group_2_CityList[groupId] = newCityList
  380. end
  381. -- 根据分组Id, 获取本组的公会排行数据
  382. function GetUnionRankList(groupId)
  383. return Group_2_UnionRankList[groupId]
  384. end
  385. function UpdateUnionRankList(groupId, newRankList)
  386. Group_2_UnionRankList[groupId] = newRankList
  387. unionRankSortFunc(Group_2_UnionRankList[groupId])
  388. end
  389. -- 根据分组Id, 获取本组的玩家排行数据
  390. function GetPlayerRankList(groupId)
  391. return Group_2_PlayerRankList[groupId]
  392. end
  393. function UpdatePlayerRankList(groupId, newRankList)
  394. Group_2_PlayerRankList[groupId] = newRankList
  395. playerRankSortFunc(Group_2_PlayerRankList[groupId])
  396. end