RoleDBLogic.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. ----------------------------------
  2. -- 角色DB相关逻辑
  3. -- isNameExistInDB 角色名是否存在
  4. -- getDb 根据uuid取角色信息(优先取内存)
  5. -- getDbByName 根据名字取角色信息(优先取内存)
  6. -- getDbByAccount 根据账号取角色信息(优先取内存)
  7. -- loadRole 根据账号取db
  8. -- saveRole 保存角色整个db
  9. -- saveRoleSset 仅对角色db某些字段进行修改
  10. -- createDefaultRole 初始化角色db信息
  11. ----------------------------------
  12. local LuaMongo = _G.lua_mongo
  13. local Config = require("Config")
  14. local DB = require("common.DB")
  15. local ObjHuman = require("core.ObjHuman")
  16. local HeroDefine = require("hero.HeroDefine")
  17. local CreateRole = require("role.CreateRole")
  18. local CombatDefine = require("combat.CombatDefine")
  19. local RoleLogic = require("role.RoleLogic")
  20. local RoleHeadLogic = require("role.RoleHeadLogic")
  21. local Util = require("common.Util")
  22. local QueryByAccount = {} --按帐号查询
  23. local QueryByName = {} --按角色名查询
  24. local QueryByUuid = {} --按uuid查询
  25. local QueryByIdentity = {} --按identity查询
  26. local QueryUpdateChar = {}
  27. local FieldName = {name = 1}
  28. local FieldAccount = {account = 1}
  29. local tempData = {}
  30. function isNameExistInDB(name)
  31. QueryByName.name = name
  32. tempData.name = nil
  33. LuaMongo.find(DB.db_char, QueryByName, FieldName)
  34. return LuaMongo.next(tempData) and tempData
  35. end
  36. function isAccountExistInDB(account)
  37. QueryByAccount.account = account
  38. tempData.account = nil
  39. LuaMongo.find(DB.db_char, QueryByAccount, FieldAccount)
  40. return LuaMongo.next(tempData) and tempData
  41. end
  42. function isUuidExistInDB(uuid)
  43. QueryByUuid._id = uuid
  44. tempData.account = nil
  45. LuaMongo.find(DB.db_char, QueryByUuid, FieldAccount)
  46. return LuaMongo.next(tempData) and tempData
  47. end
  48. local f = {}
  49. function getDb(uuid, fields)
  50. local t = ObjHuman.onlineUuid[uuid]
  51. if t then
  52. return t.db, t.fd ~= nil
  53. end
  54. QueryByUuid._id = uuid
  55. local data = {}
  56. if type(fields) == "string" then
  57. local k = next(f)
  58. if k then f[k] = nil end
  59. f[fields] = 1
  60. fields = f
  61. end
  62. LuaMongo.find(DB.db_char, QueryByUuid, fields)
  63. return LuaMongo.next(data) and data, nil
  64. end
  65. function getDbByName(name, fields)
  66. if not name then
  67. assert()
  68. end
  69. local t = ObjHuman.onlineHuman[name]
  70. if t then
  71. return t.db, t.fd ~= nil
  72. end
  73. QueryByName.name = name
  74. local data = {}
  75. if type(fields) == "string" then
  76. local k = next(f)
  77. if k then f[k] = nil end
  78. f[fields] = 1
  79. fields = f
  80. end
  81. LuaMongo.find(DB.db_char, QueryByName, fields)
  82. return LuaMongo.next(data) and data, nil
  83. end
  84. function getDbByNameRegex(name, fields)
  85. QueryByName.name = {["$regex"] = name}
  86. local data = {}
  87. if type(fields) == "string" then
  88. local k = next(f)
  89. if k then f[k] = nil end
  90. f[fields] = 1
  91. fields = f
  92. end
  93. RoleLogic.makeRoleBaseFields(fields)
  94. LuaMongo.find(DB.db_char, QueryByName, fields,50)
  95. local userList = {}
  96. local len = 0
  97. while true do
  98. local data = {}
  99. if not LuaMongo.next(data) then
  100. break
  101. end
  102. len = len + 1
  103. userList[len] = {}
  104. userList[len].name = data.name
  105. userList[len].uuid = data._id
  106. userList[len].account = data.account
  107. userList[len].identity = data.identity
  108. userList[len].lv = data.lv
  109. userList[len].head = data.head
  110. userList[len].headFrame = data.headFrame
  111. userList[len].unionUuid = data.unionUuid
  112. userList[len].blue = data.blue
  113. userList[len].yellow = data.yellow
  114. userList[len].zhandouli = data.zhandouli
  115. end
  116. return userList,#userList
  117. end
  118. function getDbByAccount(account, fields)
  119. local t = ObjHuman.onlineAccount[account]
  120. if t then
  121. return t.db, t.fd ~= nil
  122. end
  123. QueryByAccount.account = account
  124. local data = {}
  125. if type(fields) == "string" then
  126. local k = next(f)
  127. if k then f[k] = nil end
  128. f[fields] = 1
  129. fields = f
  130. end
  131. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  132. return LuaMongo.next(data) and data, nil
  133. end
  134. function loadRole(account)
  135. QueryByAccount.account = account
  136. local data = {}
  137. LuaMongo.find(DB.db_char, QueryByAccount)
  138. if LuaMongo.next(data) then
  139. return data
  140. end
  141. end
  142. function getDbByIdentity(identity,fields)
  143. QueryByIdentity.identity = identity
  144. local data = {}
  145. if type(fields) == "string" then
  146. local k = next(f)
  147. if k then f[k] = nil end
  148. f[fields] = 1
  149. fields = f
  150. end
  151. LuaMongo.find(DB.db_char, QueryByIdentity, fields)
  152. return LuaMongo.next(data) and data, nil
  153. end
  154. function saveRole(db)
  155. QueryUpdateChar._id = db._id
  156. LuaMongo.update(DB.db_char, QueryUpdateChar, db)
  157. end
  158. local tempTb = {}
  159. function saveRoleSset(db, sunset)
  160. QueryUpdateChar._id = db._id
  161. db._id = nil
  162. tempTb["$set"] = db
  163. tempTb["$unset"] = sunset
  164. LuaMongo.update(DB.db_char, QueryUpdateChar, tempTb)
  165. db._id = QueryUpdateChar._id
  166. end
  167. -------------------- humandb 初始化 start ----------------------
  168. function createDefaultRole(account)
  169. local db = {
  170. svrIndex = Config.SVR_INDEX,
  171. identity = nil, -- 11位全局不重复的数字id
  172. account = account, -- 帐号名
  173. name = nil, -- 角色名
  174. changeNameCnt = nil, -- 修改名字次数
  175. changeBase = nil, -- 修改基础信息次数
  176. birthDay = nil, -- 生日
  177. signature = nil, -- 个性签名
  178. lv = 1, -- 等级
  179. exp = 0,
  180. zuanshi = 0, -- 钻石
  181. jinbi = 0, -- 金币
  182. createTime = nil, -- 角色创建时间
  183. lastLoginTime = nil, -- 上一次登录时间
  184. lastLogoutTime = nil, -- 最近登出时间
  185. update_daily_time = nil, -- 每天更新时间
  186. onlineTime = nil, -- 在线时长
  187. onlineTimeDay = nil, -- 本日在线时长
  188. onlineTimeDayReport = nil,
  189. heroBag = {[0] = HeroDefine.HERO_BAG_CNT}, -- 英雄背包[index] ={id,lv等级,quality品阶}
  190. heroLevelUpgrade = 0, -- 英雄手动升级最大值
  191. bag = {}, -- 道具背包 {itemID->itemCnt}
  192. fuwenBag = {}, -- 符文背包 [index] = {id, 属性, 技能}
  193. equipBag = {}, -- 装备背包 [index] = {id, 属性, 技能}
  194. combatHero = {}, -- 出战英雄[type] = {[pos] = uuid}
  195. buyCapCnt = nil, -- 购买背包容量次数
  196. heroBook = nil, -- 获得过的图鉴英雄[id]= true
  197. shop = nil, -- 商店购买物品次数记录 shop[shopType][itemID] = cnt
  198. guajiID = 0, -- 已通关关卡/挂机关卡
  199. battleID = 1, -- 战役即将战斗id
  200. battleHis = nil, -- 战役历史记录(用于新手任务第五关特殊处理)
  201. battleRewards = nil, -- 征战奖励领取记录
  202. battleOut = nil, -- 挂机收益{expTs1--经验开始产出时间戳,expTs2--经验产出结算时间戳,itemTs1--道具产出时间戳,itemTs2--道具产出结算时间戳,exp,jinbi,greenExp,items,itemsHalf}
  203. copy = nil, -- 活动副本 [1] ={cnt今日已挑战次数,buyCnt已购买次数}
  204. dailyTask = nil, -- 每日任务
  205. bar = nil, -- [taskID] = {isLock是否锁定, star任务星级, nameID阿辛多, actionID委托, itemID, itemCnt, ts开始做任务时间戳, hero={index}上阵英雄, camp = {1,2}任务要求, job = {1}任务要求}
  206. zhuanpan = nil, -- [type] = {ts1[强制刷新时间戳],ts2[免费刷新时间戳],[1] = {id抽奖id, itemID奖品ID,itemCnt奖品数量,getCnt = nil抽到次数[该抽奖有次数限制才记录]
  207. tower = nil, -- 幻境之塔{当前关卡lv, 恢复时间ts, 当前体力值tili}
  208. drawCard = {jifen=0,list={[1]={},[2]={},[3]={},[4]={}}},--召唤法阵
  209. combatSpeed = nil, -- 战斗速度
  210. jinbiExchange = nil, -- {[1][2][3]对应档位是否领取 ts刷新时间戳}
  211. vipLv = nil, -- vip等级
  212. vipExp = nil, -- vip经验(注意这个经验是累计的 升级不会清0)
  213. vipLibao = nil, -- vip礼包标识
  214. vipYueka = nil, -- vip月卡额外奖励
  215. jjcBestRank = nil, -- 个人竞技场最佳排名
  216. jjcDailyFight = nil, -- 个人竞技场每日战斗次数
  217. chengjiu = {}, -- 成就
  218. onlineReward = nil, -- 在线奖励 {id, ts}
  219. combatQuick = {}, -- 快速战斗
  220. systemSet = nil, -- 系统设置 位与操作 第4位 不展示vip 第5位 屏蔽私聊
  221. chatBan = nil, -- 屏蔽聊天的玩家
  222. actMonthEndTime = nil, -- 月度活动结束时间
  223. actMonthTask = nil, -- 月度活动进度
  224. -- jifen hzz竞技积分 trial hzz皇家试炼积分 buyNum 破碎徽章购买 drunkery = {[id] = num} 酒馆活动 hecheng={[id] = num}
  225. fundFlag = nil, -- 钻石基金标志 nil未购买 1已购买未领完 2已购买已领完
  226. friendHeart = 0, -- 友情值
  227. sendHeart = nil, -- 每日赠送红心好友列表{uuid->1}
  228. getHeartCnt = nil, -- 每日领取红心次数
  229. sendHeartCnt = nil, -- 每日赠送红心次数
  230. friendBlack = nil, -- 黑名单
  231. buy = {}, -- 购买记录
  232. absAct = {}, -- 绝对时间活动数据
  233. loginGiveTime = nil, -- 登录赠送 (限时活动) 记录获得赠送时间
  234. signInCnt = nil, -- 登陆奖励次数
  235. signIn = nil, -- 登陆奖励状态,0 未登录,1,已登录未领取,2已登录已领取
  236. signInTime = nil, -- 登陆时间,用于判断是否是同一天登陆
  237. unionUuid = nil, -- 工会标志 如果玩家不属于任何工会,unionUuid为nil,否则unionUuid为工会唯一id
  238. ectypeCnt = nil, -- 公会副本每日通关次数
  239. ectypeLike = nil , -- 公会副本点赞次数
  240. ectypLikeUuid = nil, -- 公会副本点赞人
  241. ectypHurt = nil, -- 公会副本每日伤害记录
  242. combatVideos = nil, -- 战斗记录列表
  243. technology = nil, -- 公会科技信息
  244. ip = nil, -- 登录IP
  245. killEctype = nil, -- 参与击杀Boss等级
  246. phpChanelID = nil, -- 渠道导入id
  247. dailyShareTask = nil, -- 每日公共任务
  248. openServerGift = nil, -- 开服有礼
  249. mopupDoCnt = nil, -- 战役已扫荡次数
  250. mopupFreeCnt = nil, -- 战役已免费扫荡次数
  251. topupAcount = nil, -- 玩家充值总额
  252. topupAcountDaily = nil, -- 玩家每日充值总额
  253. topupAccountMonth = nil, -- 玩家每月充值总额
  254. firstCharge = nil, -- 超值首充 6元 100元
  255. godDailyTopup = nil, -- 成神之路每日礼包
  256. kingworld = nil, -- 国王君临
  257. superFund = nil, -- 超值基金 大小基金 [type] = {}
  258. sex = 1, -- 性别 1男2女
  259. head = RoleHeadLogic.DEFAULT_HEAD_MALE_ID, -- 头像
  260. headList = nil, -- 已激活头像列表(除默认赠送外)
  261. headFrame = RoleHeadLogic.DEFAULT_HEADFREAM_ID, -- 头像框
  262. headFrameList = nil, -- 已激活头像框列表(除默认赠送外)
  263. body = nil, -- 形象
  264. bodyList = nil, -- 已激活形象
  265. chenghao = nil, -- 称号
  266. chenghaoList = nil, -- 已激活称号
  267. headHasNewFlag = nil, -- 头像1/头像框2/形象4/称号8有新的flag
  268. animation = nil, -- 立绘
  269. animationList = nil, -- 已激活的立绘列表
  270. background = RoleHeadLogic.DEFAULT_BACKGROUND_ID, -- 背景
  271. backgroundList = nil, -- 已激活的背景列表
  272. throneTime = nil, -- 挑战失败记录时间
  273. banSay = nil, -- 被举报
  274. yellow = nil, -- 黄钻贵族
  275. blue = nil, -- 蓝钻贵族
  276. txHall = nil, -- 腾讯大厅
  277. qqZone = nil, -- QQ空间
  278. fuwenRefreshCnt = nil, -- 符文刷新次数
  279. helpCnt = nil, -- 公会秘境助战次数
  280. personMail = nil, -- 私人郵件次數
  281. skinBag = {[0] = HeroDefine.HERO_SKIN_CNT},
  282. mailtips = {}, -- 邮件提醒
  283. skinLog = {}, --皮肤获取记录
  284. lookGl = nil, -- 是否看过攻略
  285. tujianCP = nil, -- 图鉴CP
  286. personalrecord = nil, -- 炼狱个人记录
  287. middleFlag = nil, -- 跨服游戏中
  288. middleOptions = nil, -- 跨服操作事件
  289. leichongHaoli = nil, -- 累充好礼
  290. dailyLeiChong = nil, -- 每日累充 奖励发放 dailyLeiChong[1] = true
  291. battleExtraReward = nil, -- 征战关卡额外奖励
  292. plShare = nil, -- 微信小程序分享
  293. tuiSongLiBao = nil, -- 推送礼包
  294. heroResetCnt = nil, -- 英雄每日重置次数
  295. moshou = nil, -- 魔兽
  296. equipLogs = nil, -- 装备合成日志
  297. jjcGodWar = nil, -- 众神之战
  298. unionLive = nil, -- 公会活跃
  299. moShouPingFen = nil, -- 魔兽评分
  300. unionDonate = nil, -- 公会捐献
  301. dailyBanggong = nil, -- 每日帮贡
  302. totalBanggong = nil, -- 总帮贡
  303. donateReward = nil, -- 公会捐献达标奖励
  304. combatBackup = nil, -- 援军激活条件
  305. liLian = nil, -- 历练
  306. billboard = nil, -- 排行榜-上榜时间
  307. billboardAim = nil, -- 排行榜-进度奖励
  308. lianyu = nil, -- 绝望深渊
  309. lianyuCache = nil, -- 绝望深渊战斗信息
  310. battleVideoUuid = nil, -- 战役挂机Uuid
  311. chatRead = nil, -- 聊天记录阅读到第几条
  312. dailyLibao = nil, -- 充值-每日礼包
  313. richangLibao = nil, -- 充值-每周/每月礼包
  314. tequanShop = nil, -- 充值-特权商店
  315. tequanMopup = nil, -- 扫荡特权 截止时间
  316. weekendFuli = nil, -- 周末福利
  317. unionWar = nil, -- 玩家 公会战 相关数据
  318. jjcLadder = nil, -- 天梯争霸 相关数据
  319. valleyTask = nil, -- 荣耀峡谷(龙族战场)目标
  320. jjcNewestRecord = nil, -- 个人竞技场最新战斗记录时间
  321. jjcWorship = nil, -- 个人竞技场每日膜拜
  322. jjcBeWorship = nil, -- 个人竞技场总被膜拜次数
  323. relationKey = nil, -- 推广码
  324. relationBind= {}, -- 推广关系
  325. relationId = nil, -- 推广人
  326. relationRew = {}, -- 奖励领取状态
  327. warReport = {}, -- 战报收藏列表
  328. warAdmire = nil, -- 战报点赞
  329. drill = nil, -- 勇者试炼
  330. welfareGift = {}, -- 超值礼包
  331. redBagCnt = nil, -- 公会红包次数
  332. monthCard = nil, -- 贵族月卡,王者月卡
  333. limitBuy = nil, -- 限时抢购
  334. leijiChongzhi = nil, -- 累计充值
  335. giftPack = nil, -- 成长礼包
  336. heroRise = nil, -- 英雄崛起
  337. leaveUnionLimit = nil, -- 离开公会后,加入或创建公会时间限制
  338. roleDot = nil, -- 红点
  339. chongJi = nil, -- 创角冲级活动
  340. cdkFix = nil, -- 固定兑换码
  341. cdk = {},
  342. roleSys = nil, -- 系统开放标识
  343. roleSysOpen = nil, -- 系统点击标识
  344. xingYaoGongMing = nil, -- 星耀之门-星耀共鸣
  345. guide = nil, -- 指引
  346. lvGuide = nil, -- 等级指引
  347. sendPfEmail = nil, --开服邮件
  348. limitMangHe = nil, -- 开服 -限时幸运盲盒
  349. fpsTb = nil, -- fps信息
  350. systemSound = nil, -- 系统设置
  351. relic = {}, -- 圣器
  352. adRewardCnt = nil , --每日广告观看次数
  353. adHatchRewardCnt = nil , --每日广告观看加速孵化次数
  354. isTip = nil , --钻石加速孵化今日是否提示
  355. mergeInfo = {}, --融合信息 {mergeStartTime: 0 , mergeTime: 0, mergeEndTime: 0, heroId: 0}
  356. gift = {
  357. unlock = {},
  358. online = {}
  359. }, -- 弹窗礼包
  360. warOrder = { -- 战令相关
  361. battleOrder = {
  362. exp = 0,
  363. finish = {},
  364. unlock = 0,
  365. upgradeFinish = {}
  366. },
  367. devilOrder = {
  368. exp = 0,
  369. finish = {},
  370. unlock = 0,
  371. upgradeFinish = {}
  372. },
  373. clanOrder = {
  374. exp = 0,
  375. finish = {},
  376. unlock = 0,
  377. upgradeFinish = {}
  378. },
  379. arenaOrder = {
  380. exp = 0,
  381. finish = {},
  382. unlock = 0,
  383. upgradeFinish = {}
  384. },
  385. },
  386. -- 注意 以后再加字段如初始化为非nil 要在下面的newAddDBData同步加,否则老号会报错。
  387. }
  388. db.createTime = os.time()
  389. LuaMongo.insert(DB.db_char, db)
  390. return db
  391. end
  392. local newAddDBData = {
  393. heroLevelUpgrade = 0, -- 英雄手动升级最大值
  394. gift = {
  395. unlock = {},
  396. online = {}
  397. }, -- 弹窗礼包
  398. warOrder = {
  399. battleOrder = {
  400. exp = 0,
  401. finish = {},
  402. unlock = 0,
  403. upgradeFinish = {}
  404. },
  405. devilOrder = {
  406. exp = 0,
  407. finish = {},
  408. unlock = 0,
  409. upgradeFinish = {}
  410. },
  411. clanOrder = {
  412. exp = 0,
  413. finish = {},
  414. unlock = 0,
  415. upgradeFinish = {}
  416. },
  417. arenaOrder = {
  418. exp = 0,
  419. finish = {},
  420. unlock = 0,
  421. upgradeFinish = {}
  422. },
  423. }, -- 战令相关
  424. adRewardCnt = nil,
  425. adHatchRewardCnt = nil,
  426. mergeInfo = {},
  427. cdk = {},
  428. isTip = nil
  429. }
  430. local roleDBchanged
  431. local function handleNew(orgDB, newTable)
  432. for k, v in pairs(newTable) do
  433. if not orgDB[k] then
  434. orgDB[k] = v
  435. roleDBchanged = true
  436. elseif type(v) == "table" then
  437. handleNew(orgDB[k],v)
  438. end
  439. end
  440. end
  441. function roleDBInit()
  442. _G.collectgarbage("step", 1000000)
  443. LuaMongo.find(DB.db_char)
  444. local list = {}
  445. local all = 0
  446. while true do
  447. local data = {}
  448. if not LuaMongo.next(data) then
  449. break
  450. end
  451. all = all + 1
  452. roleDBchanged = nil
  453. handleNew(data, newAddDBData)
  454. if roleDBchanged then
  455. list[#list + 1] = data._id
  456. end
  457. end
  458. print(#list, "/", all)
  459. _G.collectgarbage("step", 1000000)
  460. local query = {}
  461. for _, v in ipairs(list) do
  462. query._id = v
  463. LuaMongo.find(DB.db_char, query)
  464. local data = {}
  465. if not LuaMongo.next(data) then
  466. assert()
  467. end
  468. roleDBchanged = nil
  469. handleNew(data, newAddDBData)
  470. if not roleDBchanged then
  471. assert()
  472. end
  473. LuaMongo.update(DB.db_char, query, data)
  474. end
  475. end
  476. -------------------- humandb 初始化 end ----------------------