CommonDB.lua 15 KB

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