JjcDB.lua 13 KB

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