CommonDB.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. local DB = require("common.DB")
  2. local LuaMongo = _G.lua_mongo
  3. local Config = require("Config")
  4. local Util = require("common.Util")
  5. local ObjHuman = require("core.ObjHuman")
  6. local YunYingLogic = require("yunying.YunYingLogic")
  7. local OpenAct = require("present.OpenAct")
  8. local DBUpdate = {_id=nil} -- 更新
  9. local DBUpdateField = {} -- 更新域
  10. local nVoucherInflateLen = 20 -- 代金券记录信息长度
  11. KEY_SERVER_START_TIME = "serverStartTime" -- 开服时间
  12. KEY_IDENTITY_MAX = "identityMax" -- 自增的最大的数字 从1开始
  13. KEY_UNION_IDENTITY_MAX = "unionIdentityMax" -- 自增的最大的数字 从1开始
  14. KEY_REWARD_INDEX = "rewardIndex" -- 好友排行榜奖励索引rewardIndex
  15. -- KEY_BAN_IP = "banIp" -- 被禁IP
  16. KEY_CHONGJI_ACT = "chongJiAct" -- 全服冲级活动 领取情况
  17. KEY_ACT_MONTH = "actMonth" -- 当前计算的真实月份
  18. KEY_UNION_WAR = "unionWar" -- 公会战
  19. KEY_ACT_MONTH_LIST = "actMonthList" -- 现实活动
  20. KEY_RESET_TIME = "resetTime"
  21. KEY_DRILL = "drill" -- 过关斩将信息(本期开始时间等)
  22. KEY_SHARE_PLATFORM_BOARD = "sharePlatformBoard" -- 分享排行
  23. KEY_BILLBOARD_AIM = "billboardAim" -- 排行榜达标记录
  24. KEY_JJC_LADDER = "jjcLadder" -- 天梯争霸信息
  25. KEY_VALLEY = "valley" -- 荣耀峡谷信息
  26. KEY_LIANYU = "lianyu" -- 绝望深渊信息
  27. KEY_MIDDLE_INFO = "middleInfo" -- 跨服链接信息
  28. KEY_CHAMPION_END_TIME = "championEndTime" -- 冠军竞技场本赛季结束时间
  29. KEY_DRAGON_NEST_CITIAO = "dragonNestCiTiao" -- 冰龙巢穴词条
  30. KEY_WORK_MAN = "workMan" -- 打工人活动
  31. KEY_OPEN_SERVER_TOPUP_TOP = "openServerTopupTop" -- 开服充值排名活动
  32. KEY_CIYUAN_MOZHU = "ciyuanMoZhu" -- 次元魔蛛
  33. KEY_YJ_TIME = "yjTreasureTime" -- 遗迹探险时间
  34. KEY_CDK_BATCH = "cdkBatchIdx" -- CDK批次序号
  35. KEY_WEEKLOOP_CARD_BATCH = "weekLoopCardBatch" -- 周活动-抽卡批次
  36. KEY_VOUCHER_INFLATE = "tVoucherInflateInfo" -- 代金券膨胀信息
  37. kEY_RECOMMENDLINEUP = "recommendlineup" -- 推荐阵容
  38. KEY_FIREWORKBONUS_TIME="fireWorksBonusTime" -- 烟花加成时间
  39. KEY_COMMERCEACT_INFO = "commerceActInfo" -- 跨服活动信息
  40. KEY_JJCLADDER_INFO = "jjcNewladderInfo" -- 天梯赛信息
  41. KEY_COMMERCEACT_MIDDLE_INFO = "commerceMiddleActInfo" -- 中心服跨服商业活动信息
  42. KEY_BAN_DATA = "banData" -- 封禁数据
  43. KEY_DESKTOP_DATA = "desktopData" -- 桌面添加数据(账号级)
  44. KEY_REBATE_DATA = "rebateData" -- 渠道返利数据
  45. KEY_ANOTHERWORLDBATTLE_START_TI = "anotherWorldBattleStart" -- 最近一轮异界之战开启时间
  46. -- KEY_BIGRPLAYER_SHOW_DATA = "bigRPlayerShowData" -- 大R玩家实力展示相关数据
  47. db = db or {
  48. serverStartTime = nil, -- 开服时间
  49. zhuanpanRecord = nil, -- 转盘中奖记录
  50. identityMax = nil, -- 自增的最大的数字 从1开始
  51. rewardIndex = nil, -- 好友排行榜奖励索引rewardIndex
  52. unionIdentityMax = nil, -- 自增的最大的数字 从1开始
  53. mergeSvrIndex = nil, -- 合服之后对应的服务器索引 比如如果10001,10003,100004合服了 这里的数据结构是{10001,10003,10004}
  54. -- banIp = {}, -- 被禁IP
  55. chongJiAct = {}, -- 全服冲级活动 领取情况
  56. actMonth = nil, -- 当前计算的真实月份
  57. unionWarState = nil, -- 当前工会战状态
  58. actMonthList = nil, -- 现实活动
  59. resetTime = nil, -- 确认是否重置数据
  60. middleInfo = {}, -- 跨服链接信息
  61. championEndTime = nil, -- 冠军竞技场本赛季结束时间
  62. dragonNestCiTiao = {}, -- 冰龙巢穴词条
  63. workMan = {}, -- 打工人活动
  64. ciyuanMoZhu = nil, -- 次元魔蛛
  65. yjTreasureTime = nil, -- 遗迹探险时间
  66. cdkBatchIdx = nil, -- CDK批次序号
  67. weekLoopCardBatch = nil, -- 周活动-抽卡批次
  68. tVoucherInflateInfo = {}, -- 代金券膨胀信息
  69. recommendlineup = {}, -- 推荐阵容
  70. fireWorksBonusTime = nil, -- 烟花加成时间
  71. commerceActInfo = {}, -- 跨服商业活动信息
  72. jjcNewladderInfo = {}, -- 天梯赛信息
  73. commerceMiddleActInfo = {}, -- 中心服跨服商业活动信息
  74. -- bigRPlayerShowData = {},
  75. }
  76. --普通服缓存的封禁数据
  77. local banList = {}
  78. local isInit = false
  79. function init()
  80. if isInit then
  81. return
  82. end
  83. LuaMongo.find(DB.db_common)
  84. if not LuaMongo.next(db) then
  85. LuaMongo.insert(DB.db_common, db)
  86. end
  87. isInit = true
  88. end
  89. -- 修改db单个字段
  90. function updateValue(key, value)
  91. if not key then return end
  92. if not db then return end
  93. if value then
  94. DBUpdateField["$set"] = {[key]=value}
  95. DBUpdateField["$unset"] = nil
  96. else
  97. DBUpdateField["$set"] = nil
  98. DBUpdateField["$unset"] = {[key]=1}
  99. end
  100. DBUpdate._id = db._id
  101. db[key] = value
  102. LuaMongo.update(DB.db_common, DBUpdate, DBUpdateField)
  103. end
  104. function getCDKBatch()
  105. if not db then
  106. return
  107. end
  108. return db[KEY_CDK_BATCH] or 0
  109. end
  110. function setCDKBatch(idx)
  111. idx = idx + 1
  112. updateValue(KEY_CDK_BATCH,idx)
  113. end
  114. function getValueByKey(key)
  115. if not db then return end
  116. return db[key]
  117. end
  118. -- 同步db内容
  119. function reset()
  120. LuaMongo.find(DB.db_common)
  121. if not LuaMongo.next(db) then
  122. db = {banIp = {},}
  123. end
  124. print("db.serverStartTime:",db.serverStartTime)
  125. end
  126. -- 设置开服时间
  127. function setServerOpenTime(time)
  128. db.serverStartTime = time
  129. updateValue(KEY_SERVER_START_TIME, time)
  130. YunYingLogic.onServerOpenTime()
  131. OpenAct.onServerOpenTime()
  132. end
  133. function getServerOpenTime()
  134. if not db.serverStartTime and not isInit then
  135. init()
  136. end
  137. if not db.serverStartTime then
  138. local t = Util.getDayStartTime()
  139. setServerOpenTime(t)
  140. end
  141. return db.serverStartTime
  142. end
  143. -- 获得当前是开服第几天(如果是开服第一天返回1)
  144. function getServerOpenDay()
  145. local openTime = getServerOpenTime()
  146. if openTime == 0 then
  147. return
  148. end
  149. local startTime = Util.getDayStartTime(openTime)
  150. local now = os.time()
  151. return math.ceil((now - startTime + 1) / 24 / 3600)
  152. end
  153. function getIdentityMax()
  154. if db.identityMax == nil then
  155. db.identityMax = math.random(10001, 20000)
  156. setIdentityMax(db.identityMax)
  157. elseif db.identityMax < 10001 then
  158. db.identityMax = math.random(10001, 20000) -- 兼容老规则下数据
  159. setIdentityMax(db.identityMax)
  160. end
  161. return db.identityMax
  162. end
  163. -- 虚拟ID,假的
  164. function getFakeIdentityMax()
  165. return math.random(1, 9999)
  166. end
  167. function setIdentityMax(idMax)
  168. updateValue(KEY_IDENTITY_MAX, idMax)
  169. end
  170. function getUnionIdentityMax()
  171. if db.unionIdentityMax == nil then
  172. db.unionIdentityMax = math.random(10001, 20000)
  173. setUnionIdentityMax(db.unionIdentityMax)
  174. elseif db.unionIdentityMax < 10001 then
  175. db.unionIdentityMax = math.random(10001, 20000) -- 兼容老规则下数据
  176. setUnionIdentityMax(db.unionIdentityMax)
  177. end
  178. return db.unionIdentityMax
  179. end
  180. function setUnionIdentityMax(idMax)
  181. updateValue(KEY_UNION_IDENTITY_MAX, idMax)
  182. end
  183. function getRewardIndex()
  184. return db.rewardIndex or 0
  185. end
  186. function setRewardIndex(rewardIndex)
  187. updateValue(KEY_REWARD_INDEX, rewardIndex)
  188. end
  189. function getMergeSvrIndex()
  190. return db.mergeSvrIndex
  191. end
  192. -- cnt = 1
  193. -- function setBanIp(ip,time)
  194. -- db.banIp[cnt] =db.banIp[cnt] or {}
  195. -- db.banIp[cnt].ip = ip
  196. -- db.banIp[cnt].time = time
  197. -- cnt = cnt + 1
  198. -- updateValue(KEY_BAN_IP, db.banIp)
  199. -- end
  200. -- function getBanIp(ip)
  201. -- for k,v in pairs(db.banIp) do
  202. -- if v.ip == ip then
  203. -- return v.ip,v.time
  204. -- end
  205. -- end
  206. -- return
  207. -- end
  208. -- function delBanIp(ip)
  209. -- for k,v in pairs(db.banIp) do
  210. -- if v.ip == ip then
  211. -- db.banIp[k] = nil
  212. -- end
  213. -- end
  214. -- updateValue(KEY_BAN_IP, db.banIp)
  215. -- end
  216. --跨服才用的函数
  217. function UpdateBanData(newBanData)
  218. --db.banData = newBanData
  219. updateValue(KEY_BAN_DATA, newBanData)
  220. end
  221. -- 普通服用的函数
  222. function GetBanCache()
  223. return banList
  224. end
  225. -- 普通服用的函数
  226. function UpdateBanCache(banTag, banTime)
  227. if banTime == 0 then
  228. banList[banTag] = nil
  229. else
  230. banList[banTag] = banTime
  231. end
  232. end
  233. -- 全服创号 冲级活动
  234. function getSvrChongJiGet(id)
  235. local num = 0
  236. if not db.chongJiAct then
  237. return num
  238. end
  239. if db.chongJiAct[id] then
  240. num = db.chongJiAct[id] or 0
  241. end
  242. return num
  243. end
  244. function addSvrChongJiGet(id)
  245. db.chongJiAct = db.chongJiAct or {}
  246. db.chongJiAct[id] = db.chongJiAct[id] or 0
  247. db.chongJiAct[id] = db.chongJiAct[id] + 1
  248. updateValue(KEY_CHONGJI_ACT, db.chongJiAct)
  249. end
  250. function getActMonth()
  251. return db.actMonth or 0
  252. end
  253. function setActMonth(month)
  254. updateValue(KEY_ACT_MONTH, month)
  255. end
  256. function getActMonthList(id)
  257. local act = nil
  258. if not db.actMonthList then
  259. return act
  260. end
  261. if db.actMonthList[id] then
  262. act = db.actMonthList[id] or 0
  263. end
  264. return act
  265. end
  266. function setActMonthList(id, endTime, month)
  267. db.actMonthList = db.actMonthList or {}
  268. db.actMonthList[id] = db.actMonthList[id] or {}
  269. db.actMonthList[id].endTime = endTime
  270. db.actMonthList[id].month = month
  271. updateValue(KEY_ACT_MONTH_LIST, db.actMonthList)
  272. end
  273. function getResetTime()
  274. return db.resetTime or 0
  275. end
  276. function setResetTime(time)
  277. updateValue(KEY_RESET_TIME, time)
  278. end
  279. function getMiddleInfo()
  280. return db.middleInfo
  281. end
  282. function setMiddleInfo(ip, port, host)
  283. db.middleInfo.ip = ip
  284. db.middleInfo.port = port
  285. db.middleInfo.host = host
  286. updateValue(KEY_MIDDLE_INFO, db.middleInfo)
  287. end
  288. function getChampionEndTime()
  289. return db.championEndTime or 0
  290. end
  291. function setChampionEndTime(time)
  292. updateValue(KEY_CHAMPION_END_TIME, time)
  293. end
  294. function getDragonNestCitiao()
  295. return db.dragonNestCiTiao
  296. end
  297. function setDragonNestCitiao(list, time)
  298. db.dragonNestCiTiao.citiao = Util.copyTable(list)
  299. db.dragonNestCiTiao.time = time
  300. updateValue(KEY_DRAGON_NEST_CITIAO, db.dragonNestCiTiao)
  301. end
  302. function setWorkManState(state)
  303. db.workMan.state = state
  304. updateValue(KEY_WORK_MAN, db.workMan)
  305. end
  306. function setWorkManGroup(group)
  307. db.workMan.group = group
  308. updateValue(KEY_WORK_MAN, db.workMan)
  309. end
  310. function setWorkManTs(ts)
  311. db.workMan.time = ts
  312. updateValue(KEY_WORK_MAN, db.workMan)
  313. end
  314. function getWorkMan()
  315. return db.workMan
  316. end
  317. -- 获取合服时间
  318. function getMergeServerTime()
  319. return db.mergeServerTime or 0
  320. end
  321. -- 获得当前是合服第几天(如果是合服第一天返回1)
  322. function getServerMergeDay()
  323. local mergeTime = getMergeServerTime()
  324. if mergeTime == 0 then
  325. return
  326. end
  327. local startTime = Util.getDayStartTime(mergeTime)
  328. local now = os.time()
  329. return math.ceil((now - startTime + 1) / 24 / 3600)
  330. end
  331. function setMergeTime(ts)
  332. db.mergeServerTime = ts
  333. updateValue("mergeServerTime", db.mergeServerTime)
  334. end
  335. function getYjTreasureEndTime()
  336. return db.yjTreasureTime or 0
  337. end
  338. function setYjTreasureEndTime(time)
  339. updateValue(KEY_YJ_TIME, time)
  340. end
  341. function getWeekCardBatch()
  342. return db.weekLoopCardBatch or 0
  343. end
  344. function setWeekCardBatch(nValue)
  345. updateValue(KEY_WEEKLOOP_CARD_BATCH, nValue)
  346. end
  347. function getVoucherInflate()
  348. return db.tVoucherInflateInfo
  349. end
  350. function setVoucherInflate(szText, nTime)
  351. if not szText or 0 >= nTime then
  352. return
  353. end
  354. local nNowLen = #db.tVoucherInflateInfo
  355. if nNowLen >= nVoucherInflateLen then
  356. local nDelLen = nNowLen - nVoucherInflateLen + 1
  357. for i = nDelLen, 1, -1 do
  358. table.remove(db.tVoucherInflateInfo, i)
  359. end
  360. end
  361. table.insert(db.tVoucherInflateInfo, {szText = szText, nTime = nTime})
  362. updateValue(KEY_VOUCHER_INFLATE, db.tVoucherInflateInfo)
  363. end
  364. -- 获取阵容数据
  365. function getRecommendLineUp()
  366. return db.recommendlineup
  367. end
  368. -- 修改阵容数据
  369. function SetRecommendLineUp(tData)
  370. db.recommendlineup = tData
  371. updateValue(kEY_RECOMMENDLINEUP, db.recommendlineup)
  372. end
  373. -- 获取烟花加成时间
  374. function getFireWorksBonusTime()
  375. return db.fireWorksBonusTime or 0
  376. end
  377. -- 设置烟花加成时间
  378. function setFireWorkBonusTime(nValue)
  379. updateValue(KEY_FIREWORKBONUS_TIME,nValue)
  380. end
  381. function GetInFireWork()
  382. -- 获取当前时间
  383. local nNowTime = os.time()
  384. local currentDuration = getFireWorksBonusTime()
  385. -- 检查是否在加成内
  386. if nNowTime <= currentDuration then
  387. return true
  388. end
  389. return false
  390. end
  391. -- 获取跨服商业活动信息
  392. function GetCommerceActInfo()
  393. return db.commerceActInfo
  394. end
  395. -- 设置跨服商业活动信息
  396. --[[
  397. db.commerceActInfo ={
  398. nBeginTime = nNowTime, -- 开始时间
  399. nEndTime = nEndTime, -- 结束时间
  400. nPoint = 0, -- 全服点数
  401. nSendRankMail = false, -- 是否发送全服邮件
  402. nBatchID = 1 -- 批次ID
  403. }
  404. ]]
  405. function SetCommerceActInfo(tData)
  406. db.commerceActInfo = tData
  407. updateValue(KEY_COMMERCEACT_INFO, db.commerceActInfo)
  408. end
  409. function GetCommerceActInfo_Point()
  410. return db.commerceActInfo.nPoint or 0
  411. end
  412. function SetCommerceActInfo_Point(nValue)
  413. db.commerceActInfo.nPoint = nValue
  414. updateValue(KEY_COMMERCEACT_INFO, db.commerceActInfo)
  415. end
  416. -- 是否发送邮件
  417. function GetCommerceActInfo_SendServerMail()
  418. return db.commerceActInfo.nSendRankMail
  419. end
  420. function SetCommerceActInfo_SendServerMail(tData)
  421. db.commerceActInfo = tData
  422. updateValue(KEY_COMMERCEACT_INFO, db.commerceActInfo)
  423. end
  424. -- 天梯赛结束时间
  425. function GetJjcLadderEndTime()
  426. return db.jjcNewladderInfo.nEndTime or 0
  427. end
  428. -- 设置天梯赛结束时间
  429. function SetJjcLadderEndTime(nEndTime)
  430. db.jjcNewladderInfo.nEndTime = nEndTime
  431. updateValue(KEY_JJCLADDER_INFO, db.jjcNewladderInfo)
  432. end
  433. -- 设置天梯赛前三数据
  434. function SetJjcLadderLast3RankInfo(tData)
  435. db.jjcNewladderInfo.tLast3Rank = tData
  436. updateValue(KEY_JJCLADDER_INFO, db.jjcNewladderInfo)
  437. end
  438. -- 获取天梯赛前三数据
  439. function GetJjcLadderLast3RankInfo()
  440. return db.jjcNewladderInfo.tLast3Rank
  441. end
  442. -- 获取中心服跨服商业活动信息
  443. --[[
  444. -- 战区所属服务器key
  445. [nKey] =
  446. {
  447. nBatchID, -- 批次
  448. nBeginTime, -- 开始时间
  449. nEndTime, -- 结束时间
  450. }
  451. ]]
  452. function GetCommerceMiddleAct()
  453. return db.commerceMiddleActInfo
  454. end
  455. -- 设置中心服跨服商业活动信息
  456. function SetCommerceMiddleAct(tData)
  457. db.commerceMiddleActInfo = tData
  458. updateValue(KEY_COMMERCEACT_MIDDLE_INFO, db.commerceMiddleActInfo)
  459. end
  460. -- 获取中心服跨服商业某个服的信息
  461. function GetCommerceMiddleAct_ByKey(nKey)
  462. return db.commerceMiddleActInfo[nKey] or nil
  463. end
  464. -- 设置中心服跨服商业某个服的信息
  465. function SetCommerceMiddleAct_ByKey(nKey, tData)
  466. db.commerceMiddleActInfo[nKey] = tData
  467. updateValue(KEY_COMMERCEACT_MIDDLE_INFO, db.commerceMiddleActInfo)
  468. end
  469. -- 桌面添加数据(账号级,多区服共享)
  470. -- 结构:{ [newUniqueTag] = { ["channelId_platform"] = true, ... }, ... }
  471. local function makeDesktopAccountKey(channelId, serverId, account)
  472. return tostring(channelId) .. "|" .. tostring(serverId) .. "|" .. tostring(account)
  473. end
  474. function GetDesktopData(channelId, serverId, account)
  475. local data = db[KEY_DESKTOP_DATA]
  476. if not data then return nil end
  477. local accountKey = makeDesktopAccountKey(channelId, serverId, account)
  478. return data[accountKey]
  479. end
  480. function SetDesktopData(channelId, serverId, account, platform)
  481. local accountKey = makeDesktopAccountKey(channelId, serverId, account)
  482. local platformKey = tostring(channelId) .. "_" .. tostring(platform)
  483. local data = db[KEY_DESKTOP_DATA] or {}
  484. data[accountKey] = data[accountKey] or {}
  485. data[accountKey][platformKey] = true
  486. db[KEY_DESKTOP_DATA] = data
  487. updateValue(KEY_DESKTOP_DATA, data)
  488. end
  489. function IsDesktopAdded(channelId, serverId, account, platform)
  490. local accountData = GetDesktopData(channelId, serverId, account)
  491. if not accountData then return false end
  492. local platformKey = tostring(channelId) .. "_" .. tostring(platform)
  493. return accountData[platformKey] == true
  494. end