CommonDB.lua 11 KB

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