CommonDB.lua 14 KB

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