CommonDB.lua 15 KB

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