CommonDB.lua 15 KB

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