UnionDBLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. ----------------------------
  2. -- 工会模块DB
  3. -- getUnion 根据公会uuid获取公会union
  4. -- getUnionByName 根据公会名字获取公会union
  5. -- getUnionByNameRegex 根据名字模糊查询公会列表
  6. -- addUnion 创建公会
  7. -- delUnion 删除公会
  8. -- updateUnionData 刷新公会信息
  9. -- calcUnionZhandouli 计算公会战力
  10. -- addUnionMember 添加公会成员
  11. -- delUnionMember 删除公会成员
  12. ---------------------------
  13. local UnionExcel = require("excel.union")
  14. local Config = require("Config")
  15. local LuaMongo = _G.lua_mongo
  16. local DB = require("common.DB")
  17. local Util = require("common.Util")
  18. local CommonDB = require("common.CommonDB")
  19. local UnionDefine = require("union.UnionDefine")
  20. local RoleDBLogic = require("role.RoleDBLogic")
  21. local RoleLogic = require("role.RoleLogic")
  22. local BRoleLogic = require("billboard.BRoleLogic")
  23. local BillboardDefine = require("billboard.BillboardDefine")
  24. QueryByName = {name = nil}
  25. QueryByUnionUuid = {_id = nil}
  26. QueryByHeadFrame = {headFrame = {["$exists"] = 1}}
  27. FieldBaseUnion = {name = 1, lv = 1, curCnt = 1, id = 1, bannerID = 1, notice = 1, exp = 1, headFrame = 1, apply = 1,presidentUuid = 1,needLv = 1}
  28. FieldLog = {logs = 1}
  29. UNION_DEFAULT_QUERY_LEN = 10 -- 推荐公会个数
  30. UNION_LOG_MAXCNT = 50 -- 公会操作日志最大条数
  31. -- 查询数据库中是否已注册这个工会名
  32. local tempData = {}
  33. function isNameExistInDB(name)
  34. QueryByName.name = name
  35. tempData.name = nil
  36. LuaMongo.find(DB.db_union, QueryByName)
  37. return LuaMongo.next(tempData) and tempData
  38. end
  39. -- 根据uuid获取公会数据
  40. function getUnion(unionUuid, fields)
  41. if unionUuid == nil or unionUuid == "" then
  42. return
  43. end
  44. local union = {}
  45. QueryByUnionUuid._id = unionUuid
  46. LuaMongo.find(DB.db_union, QueryByUnionUuid, fields)
  47. return LuaMongo.next(union) and union
  48. end
  49. -- 根据公会名字获取公会信息
  50. function getUnionByName(name)
  51. local union = {}
  52. QueryByName.name = name
  53. LuaMongo.find(DB.db_union, QueryByName)
  54. return LuaMongo.next(union) and union
  55. end
  56. -- 模糊查询 获取公会列表
  57. function getUnionByNameRegex(name)
  58. QueryByName.name = {["$regex"] = name}
  59. LuaMongo.find(DB.db_union, QueryByName, nil, 10)
  60. local unionList = {}
  61. while true do
  62. local union = {}
  63. if not LuaMongo.next(union) then
  64. break
  65. end
  66. unionList[#unionList + 1] = union
  67. end
  68. return unionList
  69. end
  70. -- 刷新公会信息
  71. function updateUnionData(union)
  72. if not union or not union._id then return end
  73. QueryByUnionUuid._id = union._id
  74. LuaMongo.update(DB.db_union, QueryByUnionUuid, union)
  75. end
  76. -- 更新某些字段
  77. local DBUpdateField = {} -- 更新域
  78. function upsetUnionData(unionUuid, key, value)
  79. if unionUuid == nil then return end
  80. if key == nil then return end
  81. if value then
  82. DBUpdateField["$set"] = {[key]=value}
  83. DBUpdateField["$unset"] = nil
  84. else
  85. DBUpdateField["$set"] = nil
  86. DBUpdateField["$unset"] = {[key]=1}
  87. end
  88. QueryByUnionUuid._id = unionUuid
  89. LuaMongo.update(DB.db_union, QueryByUnionUuid, DBUpdateField)
  90. end
  91. -- 生成一个11位的全局不重复的数字组成的id
  92. -- 规则 前6位服务器index 后五位自增id
  93. local function getIdentity()
  94. local firstStr = Config.SVR_INDEX
  95. local id = CommonDB.getUnionIdentityMax() + 1
  96. CommonDB.setUnionIdentityMax(id)
  97. return tostring(firstStr .. "" .. id)
  98. end
  99. -- 创建公会
  100. function addUnion(human, name, bannerID, notice, needLv, needAgree)
  101. local unionId = getIdentity()
  102. if not unionId then assert(nil) end
  103. local uuid = human.db._id
  104. local union = {}
  105. union.id = unionId -- 公会编号,全数字字符串
  106. union.createTime = os.time() -- 创建时间
  107. union.name = name -- 公会名字
  108. union.nameTime = nil -- 公会改名时间
  109. union.lv = 1 -- 等级
  110. union.exp = 0 -- 经验
  111. union.presidentUuid = uuid -- 会长
  112. union.zhandouli = 0 -- 公会战力
  113. union.member = {} -- 成员 [uuid] = {post = nil}
  114. union.curCnt = 0 -- 公会人数
  115. union.kickCntToday = nil -- 本日踢除人数(每日踢人是有上限的,防止恶意踢人?)
  116. union.kickTime = nil -- 最近一次踢人时间
  117. union.notice = notice -- 公告
  118. union.noticeTime = nil -- 公告修改时间
  119. union.bannerID = bannerID -- 旗子
  120. union.needLv = needLv -- 入会等级要求
  121. union.needAgree = needAgree -- 是否需要验证
  122. union.signInCnt = 0 -- 签到人数
  123. union.signInTime = nil -- 签到时间戳
  124. union.apply = nil -- 申请列表
  125. union.bossLv = 1 -- 公会副本boss当前等级
  126. union.logs = nil -- 公会日志
  127. union.mailCnt = nil -- 公会邮件发送数量
  128. union.mailTs = nil -- 公会邮件发送时间戳
  129. union.mill = nil -- 公会磨坊信息 {lv,donate}
  130. union.headFrame = nil -- 公会头像框(公会战给与,暂时屏蔽)
  131. union.copyBuff = nil -- 公会副本BUF
  132. union.shopLv = 0 -- 公会商店等级
  133. union.shopExp = 0 -- 公会商店经验
  134. union.recruit = nil -- 公会招募标记
  135. union.lastTreaAtkResult = nil -- 上次宝藏掠夺战 攻击别人的结果 {[1]掠夺成功为1/掠夺失败为nil [2] [3]}
  136. union.billboard = nil -- 各种公会秘境迷宫的排行榜
  137. addUnionMember(union, uuid, UnionDefine.POST_PRESIDENT)
  138. LuaMongo.insert(DB.db_union, union)
  139. BRoleLogic.updateData(BillboardDefine.TYPE_UNION, union)
  140. return union
  141. end
  142. -- 解散公会
  143. function delUnion(unionUuid)
  144. if unionUuid == nil then return end
  145. QueryByUnionUuid._id = unionUuid
  146. LuaMongo.remove(DB.db_union, QueryByUnionUuid)
  147. end
  148. -- 添加公会成员
  149. function addUnionMember(union, uuid, post)
  150. if union.member[uuid] then
  151. return
  152. end
  153. local member = {}
  154. member.post = post or UnionDefine.POST_MEMBER -- 职位
  155. member.donate = nil -- 捐献
  156. union.member[uuid] = member
  157. union.curCnt = union.curCnt + 1
  158. union.zhandouli = calcUnionZhandouli(union)
  159. if union._id then
  160. updateUnionData(union)
  161. BRoleLogic.updateData(BillboardDefine.TYPE_UNION, union)
  162. end
  163. end
  164. -- 删除成员
  165. function delUnionMember(union, uuid, isKict)
  166. if not union then return end
  167. union.member[uuid] = nil
  168. union.curCnt = union.curCnt - 1
  169. union.zhandouli = calcUnionZhandouli(union)
  170. if isKict then
  171. union.kickCntToday = (union.kickCntToday or 0) + 1
  172. union.kickTime = union.kickTime or os.time()
  173. end
  174. updateUnionData(union)
  175. BRoleLogic.updateData(BillboardDefine.TYPE_UNION, union)
  176. end
  177. -- 本日踢人数目
  178. function getKickCntToday(union)
  179. if union.kickTime and
  180. not Util.isSameDay(union.kickTime) then
  181. union.kickCntToday = nil
  182. union.kickTime = nil
  183. end
  184. return union.kickCntToday or 0
  185. end
  186. -- 根据uuid获取公会成员信息
  187. function getUnionMember(union, uuid)
  188. if not union then return end
  189. return union.member[uuid]
  190. end
  191. -- 计算公会战力
  192. function calcUnionZhandouli(union)
  193. if not union then return 0 end
  194. local zhandouli = 0
  195. for uuid in pairs(union.member) do
  196. local db = RoleDBLogic.getDb(uuid, "zhandouli")
  197. zhandouli = zhandouli + (db and db.zhandouli or 0)
  198. end
  199. return zhandouli
  200. end
  201. -- 推荐公会列表
  202. function getRecommendList()
  203. local count = LuaMongo.count(Config.DB_NAME, "union")
  204. if count < 1 then return end
  205. local skip = 0
  206. local limit = count
  207. if count > UNION_DEFAULT_QUERY_LEN then
  208. limit = UNION_DEFAULT_QUERY_LEN
  209. skip = math.random(0, count - UNION_DEFAULT_QUERY_LEN)
  210. end
  211. LuaMongo.find(DB.db_union, nil, FieldBaseUnion, limit, skip)
  212. local unionList = {}
  213. while true do
  214. local union = {}
  215. if not LuaMongo.next(union) then
  216. break
  217. end
  218. unionList[#unionList + 1] = union
  219. end
  220. return unionList
  221. end
  222. -- 后台查询
  223. function getUnionAdmin(start)
  224. local unionList = {}
  225. local all = LuaMongo.count(Config.DB_NAME, "union")
  226. LuaMongo.find(DB.db_union, {["$orderby"] = { lv = - 1 } }, FieldBaseUnion, 10, start)
  227. while true do
  228. if not LuaMongo.next(tempData) then
  229. break
  230. end
  231. local union = {}
  232. union.name = tempData.name
  233. union.lv = tempData.lv
  234. union.id = tempData.id
  235. unionList[#unionList + 1] = union
  236. end
  237. return #unionList, unionList, all
  238. end
  239. -- 日志
  240. function addLog(unionUuid, content,classify)
  241. local union = getUnion(unionUuid, FieldLog)
  242. if not union then return end
  243. local log = {}
  244. log.content = content
  245. log.time = os.time()
  246. log.classify = classify
  247. union.logs = union.logs or {}
  248. table.insert(union.logs, 1, log)
  249. union.logs[UNION_LOG_MAXCNT + 1] = nil
  250. upsetUnionData(union._id, "logs", union.logs)
  251. end
  252. -- 是否有申请
  253. function hasApply(union)
  254. if not union.apply then
  255. return
  256. end
  257. return next(union.apply)
  258. end
  259. -- 是否已申请
  260. function isApply(union, uuid)
  261. if not union.apply then
  262. return
  263. end
  264. return union.apply[uuid]
  265. end
  266. -- 添加申请
  267. function addApply(union, uuid)
  268. if isApply(union, uuid) then
  269. return
  270. end
  271. union.apply = union.apply or {}
  272. union.apply[uuid] = os.time()
  273. upsetUnionData(union._id, "apply", union.apply)
  274. end
  275. -- 删除申请
  276. function delApply(union, uuid)
  277. if not isApply(union, uuid) then
  278. return
  279. end
  280. union.apply[uuid] = nil
  281. upsetUnionData(union._id, "apply", union.apply)
  282. end
  283. -- 清空申请
  284. function resetApply(union)
  285. if union.apply == nil then return end
  286. union.apply = nil
  287. upsetUnionData(union._id, "apply", nil)
  288. end
  289. -- 设置职位
  290. function setPost(union, uuid, post)
  291. local member = getUnionMember(union, uuid)
  292. if not member then return end
  293. member.post = post
  294. upsetUnionData(union._id, "member", union.member)
  295. end
  296. -- 获取职位人数
  297. function getPostCnt(union, post)
  298. if not union then return 0 end
  299. local cnt = 0
  300. for _, member in pairs(union.member) do
  301. if member.post == post then
  302. cnt = cnt + 1
  303. end
  304. end
  305. return cnt
  306. end
  307. -- 设置公告
  308. function setNotice(union, notice)
  309. union.notice = notice
  310. union.noticeTime = os.time()
  311. updateUnionData(union)
  312. end
  313. -- 设置旗帜
  314. function setBannerID(union, bannerID)
  315. union.bannerID = bannerID
  316. updateUnionData(union)
  317. end
  318. -- 设置入会要求
  319. function setJoinLimit(union, needLv, needAgree)
  320. union.needLv = needLv
  321. union.needAgree = needAgree
  322. updateUnionData(union)
  323. end
  324. -- 更换会长
  325. function setPresident(union, targetUuid)
  326. local oldMember = getUnionMember(union, union.presidentUuid)
  327. if oldMember then
  328. oldMember.post = UnionDefine.POST_MEMBER
  329. end
  330. local newMember = getUnionMember(union, targetUuid)
  331. newMember.post = UnionDefine.POST_PRESIDENT
  332. union.presidentUuid = targetUuid
  333. updateUnionData(union)
  334. end
  335. -- 本日签到数目
  336. function getSignInCnt(union)
  337. if union.signInTime and
  338. not Util.isSameDay(union.signInTime) then
  339. union.signInCnt = nil
  340. union.signInTime = nil
  341. end
  342. return union.signInCnt or 0
  343. end
  344. -- 设置公会名称
  345. function setUnionName(union, name)
  346. union.name = name
  347. union.nameTime = os.time()
  348. updateUnionData(union)
  349. end
  350. -- 本日公会邮件数目
  351. function getMailCnt(union)
  352. if union.mailTs and
  353. not Util.isSameDay(union.mailTs) then
  354. union.mailCnt = nil
  355. union.mailTs = nil
  356. end
  357. return union.mailCnt or 0
  358. end
  359. -- 更新公会副本等级
  360. function updateBossLv(union, level)
  361. level = tonumber(level)
  362. if not level then return end
  363. upsetUnionData(union._id, "bossLv", level)
  364. end
  365. -- 获取工坊等级
  366. function getMillLv(union)
  367. return union.mill and union.mill.lv or 1
  368. end
  369. -- 获取工坊经验
  370. function getMillDonate(union)
  371. return union.mill and union.mill.donate or 1
  372. end
  373. -- 初始工坊信息
  374. function initMillData(union)
  375. union.mill = {}
  376. union.mill.lv = 1
  377. union.mill.donate = 0
  378. end
  379. -- 回收头像框
  380. function cleanUnionHeadFrame()
  381. LuaMongo.find(DB.db_union, QueryByHeadFrame, FieldBaseUnion)
  382. local list = {}
  383. while true do
  384. local union = { }
  385. if not LuaMongo.next(union) then
  386. break
  387. end
  388. list[#list + 1] = union._id
  389. end
  390. for _, unionUuid in ipairs(list) do
  391. upsetUnionData(unionUuid, "headFrame", nil)
  392. end
  393. end
  394. -- 更新公会 全体BUF
  395. function updateEctypeBuf(union)
  396. if union.copyBuff == nil then return end
  397. upsetUnionData(union._id, "copyBuff", union.copyBuff)
  398. end
  399. function updateJihuo(union)
  400. upsetUnionData(union._id, "jihuo", union.jihuo)
  401. upsetUnionData(union._id, "jihuoTime", union.jihuoTime)
  402. end
  403. -- 公会红包
  404. -- 发送公会改DB
  405. function insertSendRedBag(unionUuid,redBagID,redBagName,sender,senderPost,config,index)
  406. local now = os.time()
  407. QueryByUnionUuid._id = unionUuid
  408. -- 更新发红包次数,红包ID
  409. local updateTb = { ["$inc"]= {["redBagID"] = 1}}
  410. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  411. -- 更新红包总额排行榜
  412. local updateTb = { ["$inc"]= {
  413. ["redBagRank."..sender.uuid..".totalMoney"] = config.price,
  414. ["redBagRank."..sender.uuid..".bagCnt"] = 1,
  415. }}
  416. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  417. -- 更新红包数据
  418. local obj = {}
  419. obj.id = redBagID
  420. obj.type = config.type
  421. obj.name = config.name
  422. obj.sender = sender
  423. obj.senderPost = senderPost
  424. obj.bagCnt = config.bagCnt
  425. obj.nowCnt = 0
  426. obj.itemID = config.content[1][1]
  427. obj.itemCnt = config.content[1][2]
  428. obj.leftCnt = config.content[1][2]
  429. obj.time = now + config.time*60*60
  430. obj.index = config.index
  431. updateTb = {
  432. ["redBag."..redBagID] = obj
  433. }
  434. local tb = { }
  435. tb["$set"] = updateTb
  436. LuaMongo.update(DB.db_union, QueryByUnionUuid, tb)
  437. end
  438. function updateRedBagRecord(unionUuid,uuid,redBagID,itemID,itemCnt)
  439. local now = os.time()
  440. QueryByUnionUuid._id = unionUuid
  441. -- 更新红包记录
  442. local role = {}
  443. RoleLogic.getRoleBaseByUuid(uuid,role)
  444. local obj = {}
  445. obj.itemID = itemID
  446. obj.itemCnt = itemCnt
  447. obj.role = role
  448. obj.time = now
  449. local updateTb = {
  450. ["redBag."..redBagID..".record."..uuid] = obj
  451. }
  452. local tb = { }
  453. tb["$set"] = updateTb
  454. LuaMongo.update(DB.db_union, QueryByUnionUuid, tb)
  455. updateTb = { ["$inc"]= {["redBag."..redBagID..".leftCnt"] = -itemCnt,["redBag."..redBagID..".nowCnt"] = 1}}
  456. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  457. end
  458. function cleanRedBagCnt(human)
  459. human.db.redBagCnt = nil
  460. end