JjcDB.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. ----------------------------------------------------------------
  2. -- 竞技场db
  3. ----------------------------------------------------------------
  4. local JjcExcel = require("excel.jjc")
  5. local LuaMongo = _G.lua_mongo
  6. local Config = require("Config")
  7. local DB = require("common.DB")
  8. local Util = require("common.Util")
  9. local CommonDB = require("common.CommonDB")
  10. local ObjHuman = require("core.ObjHuman")
  11. local JjcLogic = require("jjc.JjcLogic")
  12. local RoleDefine = require("role.RoleDefine")
  13. local CombatDefine = require("combat.CombatDefine")
  14. local CombatPosLogic = require("combat.CombatPosLogic")
  15. local CombatVideo = require("combat.CombatVideo")
  16. local CreateRole = require("role.CreateRole")
  17. UUID_2_RANK = UUID_2_RANK or {}
  18. RANK_2_JJCDATA = RANK_2_JJCDATA or {}
  19. ROBOT_LIST = ROBOT_LIST or {}
  20. MAX_RECORD_CNT = 5 -- 保留5条对战记录
  21. SEASON_KEEP_DAY = 7 -- 7天一个赛季
  22. ROBOT_LIST_MAXLEN = 100 -- 100个机器人
  23. JJC_INIT_POINT = 1000 -- 竞技场初始分
  24. local QueryByUuid = {_id = nil}
  25. function getRank(uuid)
  26. local index = UUID_2_RANK[uuid]
  27. return index or 0
  28. end
  29. function getJjcData(uuid)
  30. local rank = UUID_2_RANK[uuid]
  31. if not rank then return end
  32. return RANK_2_JJCDATA[rank]
  33. end
  34. function getJjcPoint(uuid)
  35. local jjcData = getJjcData(uuid)
  36. if not jjcData then
  37. return JJC_INIT_POINT
  38. end
  39. return jjcData.point
  40. end
  41. function getWorshipCnt(uuid)
  42. local jjcData = getJjcData(uuid)
  43. if not jjcData then
  44. return 0
  45. end
  46. return jjcData.worshipCnt
  47. end
  48. function getJjcTotalFight(uuid)
  49. local jjcData = getJjcData(uuid)
  50. if not jjcData then
  51. return 0
  52. end
  53. return jjcData.jjcTotalFight or 0
  54. end
  55. function getTotalFightCnt(uuid)
  56. local jjcData = getJjcData(uuid)
  57. if not jjcData then
  58. return 0
  59. end
  60. return jjcData.totalFightCnt or 0
  61. end
  62. function getJjcSeasonBox(uuid)
  63. local jjcData = getJjcData(uuid)
  64. if not jjcData then
  65. return {}
  66. end
  67. return jjcData.jjcSeasonBox or {}
  68. end
  69. function getJjcRival(uuid)
  70. local jjcData = getJjcData(uuid)
  71. if not jjcData then
  72. return {}
  73. end
  74. return jjcData.rival or {}
  75. end
  76. function isNpc(jjcData)
  77. if jjcData.point and jjcData.monsterOutID then
  78. return true
  79. end
  80. end
  81. function getSeasonCnt()
  82. local openDay = CommonDB.getServerOpenDay()
  83. if openDay == nil then return end
  84. return math.ceil(openDay / SEASON_KEEP_DAY)
  85. end
  86. function getSeasonEndTime()
  87. local openDay = CommonDB.getServerOpenDay()
  88. local openServerTime = CommonDB.getServerOpenTime()
  89. if openDay == nil then return 0 end
  90. local championEndTime = CommonDB.getChampionEndTime()
  91. local nowTime = os.time()
  92. if championEndTime == 0 then
  93. local dayStartTime = Util.getDayStartTime(openServerTime)
  94. local endTime = dayStartTime + SEASON_KEEP_DAY * 86400 - 10800
  95. CommonDB.setChampionEndTime(endTime)
  96. championEndTime = endTime
  97. end
  98. local startTime = championEndTime - SEASON_KEEP_DAY * 86400
  99. local firstEndTime = SEASON_KEEP_DAY * 86400 + openServerTime - 10800
  100. if openDay <= SEASON_KEEP_DAY and firstEndTime == championEndTime then
  101. startTime = openServerTime
  102. end
  103. return championEndTime,startTime
  104. end
  105. function setSeasonEndTime()
  106. local openDay = CommonDB.getServerOpenDay()
  107. local openServerTime = CommonDB.getServerOpenTime()
  108. if openDay == nil then return end
  109. local championEndTime = CommonDB.getChampionEndTime()
  110. if championEndTime == 0 then
  111. local nowTime = os.time()
  112. local dayStartTime = Util.getDayStartTime(openServerTime)
  113. championEndTime = dayStartTime + SEASON_KEEP_DAY * 86400 - 10800
  114. else
  115. championEndTime = championEndTime + SEASON_KEEP_DAY * 86400
  116. end
  117. CommonDB.setChampionEndTime(championEndTime)
  118. end
  119. function getGodSeasonEndTime()
  120. local openTime = CommonDB.getServerOpenTime()
  121. if openTime == 0 then
  122. return
  123. end
  124. local nowTime = os.time()
  125. local dayStartTime = Util.getDayStartTime(openTime)
  126. local time = math.max(nowTime - dayStartTime, 1)
  127. local openDay = math.ceil(time / 86400)
  128. local seasonCnt = math.ceil(openDay / SEASON_KEEP_DAY) - 1
  129. local startTime = dayStartTime + seasonCnt * SEASON_KEEP_DAY * 86400
  130. local seasonStartTime = startTime - 3 * 3600
  131. local seasonEndTime = seasonStartTime + SEASON_KEEP_DAY * 86400
  132. if nowTime > seasonEndTime then
  133. seasonStartTime = seasonEndTime
  134. seasonEndTime = seasonStartTime + SEASON_KEEP_DAY * 86400
  135. end
  136. return seasonEndTime, math.max(seasonStartTime, openTime), startTime
  137. end
  138. function updateJJCData(jjcData)
  139. if not jjcData._id then return end
  140. QueryByUuid._id = jjcData._id
  141. LuaMongo.update(DB.db_jjc, QueryByUuid, jjcData)
  142. end
  143. function removeJJCData(uuid)
  144. if not uuid then return end
  145. QueryByUuid._id = uuid
  146. LuaMongo.remove(DB.db_jjc, QueryByUuid)
  147. end
  148. function initJJCDB()
  149. RANK_2_JJCDATA = {}
  150. UUID_2_RANK = {}
  151. ROBOT_LIST = {}
  152. resetJJCDB(true)
  153. end
  154. function isEmpty()
  155. LuaMongo.find(DB.db_jjc)
  156. local data = {}
  157. if not LuaMongo.next(data) then
  158. return true
  159. end
  160. end
  161. function resetJJCDB(init)
  162. -- 删除假人 留下真人
  163. ROBOT_LIST = {}
  164. UUID_2_RANK = {}
  165. local count = #RANK_2_JJCDATA
  166. local reallyCount = 0
  167. for i = 1, count do
  168. local jjcData = RANK_2_JJCDATA[i]
  169. RANK_2_JJCDATA[i] = nil
  170. if isNpc(jjcData) and init then
  171. removeJJCData(jjcData._id)
  172. else
  173. jjcData.point = math.floor(jjcData.point*7/10)
  174. if jjcData.point < JJC_INIT_POINT then
  175. jjcData.point = JJC_INIT_POINT
  176. end
  177. jjcData.recordList = nil
  178. jjcData.jjcSeasonBox = {}
  179. jjcData.rival = nil
  180. jjcData.jjcTotalFight = 0
  181. jjcData.worshipCnt = 0
  182. reallyCount = reallyCount + 1
  183. RANK_2_JJCDATA[reallyCount] = jjcData
  184. updateJJCData(jjcData)
  185. end
  186. end
  187. CombatVideo.cleanJJCVideo()
  188. -- 插入假人
  189. if init then
  190. for _, npcConfig in ipairs(JjcExcel.npc) do
  191. for i = 1, npcConfig.cnt do
  192. addNpc(npcConfig)
  193. end
  194. end
  195. end
  196. sortRank()
  197. end
  198. function loadJJCData()
  199. LuaMongo.find(DB.db_jjc)
  200. while true do
  201. local data = {}
  202. if not LuaMongo.next(data) then
  203. break
  204. end
  205. if data.point then
  206. RANK_2_JJCDATA[#RANK_2_JJCDATA + 1] = data
  207. else
  208. return
  209. end
  210. end
  211. sortRank()
  212. for i = 1, #RANK_2_JJCDATA do
  213. if #ROBOT_LIST >= ROBOT_LIST_MAXLEN then
  214. break
  215. end
  216. local jjcData = RANK_2_JJCDATA[i]
  217. if isNpc(jjcData) then
  218. ROBOT_LIST[#ROBOT_LIST + 1] = jjcData
  219. end
  220. end
  221. return true
  222. end
  223. function initAfterStart()
  224. if _G.is_middle == true then return end
  225. -- 检查赛季
  226. checkReset()
  227. checkSeasonTime()
  228. if isEmpty() then
  229. initJJCDB()
  230. else
  231. loadJJCData()
  232. end
  233. end
  234. local function ownCmp(a, b)
  235. if a.point ~= b.point then
  236. return a.point > b.point
  237. end
  238. return a.time < b.time
  239. end
  240. function sortRank()
  241. if #RANK_2_JJCDATA > 1 then
  242. table.sort(RANK_2_JJCDATA, ownCmp)
  243. end
  244. for rank = 1, #RANK_2_JJCDATA do
  245. local uuid = RANK_2_JJCDATA[rank]._id
  246. UUID_2_RANK[uuid] = rank
  247. end
  248. end
  249. function addPlayer(human)
  250. local rank = UUID_2_RANK[human.db._id]
  251. if rank then return end
  252. local jjcData = {}
  253. jjcData._id = human.db._id
  254. jjcData.point = JJC_INIT_POINT
  255. jjcData.recordList = nil
  256. jjcData.totalFightCnt = 0 -- 为所有赛季战斗次数总和,超过20次则不再累计,赛季结束不清0
  257. jjcData.time = os.time()
  258. LuaMongo.insert(DB.db_jjc, jjcData)
  259. RANK_2_JJCDATA[#RANK_2_JJCDATA + 1] = jjcData
  260. sortRank()
  261. end
  262. function addNpc(config)
  263. local jjcData = {}
  264. local r = math.random(1, #config.monsterOutID)
  265. jjcData.monsterOutID = config.monsterOutID[r]
  266. jjcData.lv = 20
  267. jjcData.head = CreateRole.getRandomHead()
  268. jjcData.name = CreateRole.getRandomName()
  269. jjcData.body = CreateRole.getRandomBody()
  270. jjcData.identity = CreateRole.getFakeIdentityMax()
  271. jjcData.point = config.initPoint
  272. jjcData.zhandouli = math.random(config.zhandouli[1], config.zhandouli[2])
  273. jjcData.time = os.time()
  274. LuaMongo.insert(DB.db_jjc, jjcData)
  275. RANK_2_JJCDATA[#RANK_2_JJCDATA + 1] = jjcData
  276. if #ROBOT_LIST < ROBOT_LIST_MAXLEN then
  277. ROBOT_LIST[#ROBOT_LIST + 1] = jjcData
  278. end
  279. end
  280. function updatePlayerPoint(uuid, point, nosort)
  281. local rank = UUID_2_RANK[uuid]
  282. if not rank then return end
  283. local jjcData = RANK_2_JJCDATA[rank]
  284. if jjcData.point == point then
  285. return
  286. end
  287. jjcData.point = point
  288. jjcData.time = os.time()
  289. updateJJCData(jjcData)
  290. if not nosort then
  291. sortRank()
  292. end
  293. end
  294. -- 更新总战斗次数
  295. function updateTotalFight(uuid)
  296. local rank = UUID_2_RANK[uuid]
  297. if not rank then return end
  298. local jjcData = RANK_2_JJCDATA[rank]
  299. jjcData.jjcTotalFight = jjcData.jjcTotalFight or 0
  300. jjcData.jjcTotalFight = jjcData.jjcTotalFight + 1
  301. updateJJCData(jjcData)
  302. end
  303. -- 更新所有赛季总战斗次数
  304. function updateTotalFightCnt(uuid)
  305. local rank = UUID_2_RANK[uuid]
  306. if not rank then return end
  307. local jjcData = RANK_2_JJCDATA[rank]
  308. jjcData.totalFightCnt = jjcData.totalFightCnt or 0
  309. if jjcData.totalFightCnt >= 20 then
  310. return
  311. end
  312. jjcData.totalFightCnt = jjcData.totalFightCnt + 1
  313. updateJJCData(jjcData)
  314. end
  315. -- 更新总膜拜次数
  316. function updateWorshipCnt(uuid)
  317. local rank = UUID_2_RANK[uuid]
  318. if not rank then return end
  319. local jjcData = RANK_2_JJCDATA[rank]
  320. jjcData.worshipCnt = jjcData.worshipCnt or 0
  321. jjcData.worshipCnt = jjcData.worshipCnt + 1
  322. updateJJCData(jjcData)
  323. end
  324. -- 更新赛季盒子信息
  325. function updateSeasonBox(uuid,index)
  326. local rank = UUID_2_RANK[uuid]
  327. if not rank then return end
  328. local jjcData = RANK_2_JJCDATA[rank]
  329. if jjcData.jjcSeasonBox and jjcData.jjcSeasonBox[index] ~= nil then
  330. return
  331. end
  332. jjcData.jjcSeasonBox = jjcData.jjcSeasonBox or {}
  333. jjcData.jjcSeasonBox[index] = 2
  334. updateJJCData(jjcData)
  335. end
  336. -- 更新对手
  337. function updateRival(uuid,index,uuid2)
  338. local rank = UUID_2_RANK[uuid]
  339. if not rank then return end
  340. local jjcData = RANK_2_JJCDATA[rank]
  341. jjcData.rival = jjcData.rival or {}
  342. jjcData.rival[index] = uuid2
  343. updateJJCData(jjcData)
  344. end
  345. -- 清空对手
  346. function cleanRival(uuid)
  347. local rank = UUID_2_RANK[uuid]
  348. if not rank then return end
  349. local jjcData = RANK_2_JJCDATA[rank]
  350. if jjcData.rival and jjcData.rival[index] ~= nil then
  351. return
  352. end
  353. jjcData.rival = nil
  354. updateJJCData(jjcData)
  355. end
  356. local function addRocord(jjcData, videoUuid)
  357. if isNpc(jjcData) then return end
  358. jjcData.recordList = jjcData.recordList or {}
  359. table.insert(jjcData.recordList, 1, videoUuid)
  360. local removeUuid = jjcData.recordList[MAX_RECORD_CNT + 1]
  361. if removeUuid then
  362. jjcData.recordList[MAX_RECORD_CNT + 1] = nil
  363. CombatVideo.delJJCVideoCnt(removeUuid)
  364. end
  365. updateJJCData(jjcData)
  366. end
  367. local QueryByUuidAtk = {}
  368. function addJjcRecord(atkUuid, defUuid, combatInfo)
  369. local atkData = getJjcData(atkUuid)
  370. local defData = getJjcData(defUuid)
  371. if not atkData or not defData then return end
  372. local cnt = (not isNpc(atkData)) and 1 or 0
  373. cnt = cnt + ((not isNpc(defData)) and 1 or 0 )
  374. if cnt < 1 then return end
  375. local combatVideo = CombatVideo.createCombatVideo(CombatVideo.VIDEOTYPE_JJC, combatInfo, cnt)
  376. if not combatVideo then return end
  377. addRocord(atkData, combatVideo._id)
  378. addRocord(defData, combatVideo._id)
  379. end
  380. function getJjcRecordList(uuid)
  381. local jjcData = getJjcData(uuid)
  382. if not jjcData then return end
  383. return jjcData.recordList
  384. end
  385. function checkReset()
  386. local endTime = getSeasonEndTime()
  387. local now = os.time()
  388. if now >= endTime then
  389. resetJJCDB()
  390. setSeasonEndTime()
  391. end
  392. end
  393. function checkSeasonTime()
  394. local endTime = getSeasonEndTime()
  395. local openServerTime = CommonDB.getServerOpenTime()
  396. if openServerTime == 0 then
  397. -- 备服,服务器启动中,但服务器未开放
  398. -- 清除已计算的竞技场时间
  399. CommonDB.setChampionEndTime()
  400. return
  401. end
  402. local now = os.time()
  403. local day = Util.diffDayByTimes(openServerTime,now)
  404. local trueDay = day + 1
  405. local day2 = trueDay % SEASON_KEEP_DAY
  406. local day3 = 0
  407. if day2 ~= 0 then
  408. day3 = SEASON_KEEP_DAY - day2
  409. end
  410. local dayStartTime = Util.getDayStartTime(now)
  411. local trueEndTime = dayStartTime + day3*86400 + 21*60*60
  412. CommonDB.setChampionEndTime(trueEndTime)
  413. end
  414. function cleanOldVideos()
  415. local delTime = os.time() - 3*86400 -- 删除三天前的记录
  416. local field = {["combatInfo.time"]=1}
  417. for rank, jjcData in ipairs(RANK_2_JJCDATA) do
  418. local recordLen = jjcData.recordList and #jjcData.recordList or 0
  419. local realRecordLen = 0
  420. for i = 1, recordLen do
  421. local videoUuid = jjcData.recordList[i]
  422. jjcData.recordList[i] = nil
  423. local combatVideo = CombatVideo.getCombatVideo(videoUuid, field)
  424. if combatVideo and combatVideo.combatInfo.time > delTime then
  425. realRecordLen = realRecordLen + 1
  426. jjcData.recordList[realRecordLen] = videoUuid
  427. else
  428. CombatVideo.removeCombatVideo(videoUuid)
  429. end
  430. end
  431. if recordLen ~= realRecordLen then
  432. updateJJCData(jjcData)
  433. end
  434. end
  435. end