CommonDB.lua 12 KB

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