UnionDBLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 MemberPowerChange(union)
  179. if not union then return end
  180. union.zhandouli = calcUnionZhandouli(union)
  181. updateUnionData(union)
  182. BRoleLogic.updateData(BillboardDefine.TYPE_UNION, union)
  183. end
  184. -- 本日踢人数目
  185. function getKickCntToday(union)
  186. if union.kickTime and
  187. not Util.isSameDay(union.kickTime) then
  188. union.kickCntToday = nil
  189. union.kickTime = nil
  190. end
  191. return union.kickCntToday or 0
  192. end
  193. -- 根据uuid获取公会成员信息
  194. function getUnionMember(union, uuid)
  195. if not union then return end
  196. return union.member[uuid]
  197. end
  198. -- 计算公会战力
  199. function calcUnionZhandouli(union)
  200. if not union then return 0 end
  201. local zhandouli = 0
  202. for uuid in pairs(union.member) do
  203. local db = RoleDBLogic.getDb(uuid, "zhandouli")
  204. zhandouli = zhandouli + (db and db.zhandouli or 0)
  205. end
  206. return zhandouli
  207. end
  208. -- 推荐公会列表
  209. function getRecommendList()
  210. local count = LuaMongo.count(Config.DB_NAME, "union")
  211. if count < 1 then return end
  212. local skip = 0
  213. local limit = count
  214. if count > UNION_DEFAULT_QUERY_LEN then
  215. limit = UNION_DEFAULT_QUERY_LEN
  216. skip = math.random(0, count - UNION_DEFAULT_QUERY_LEN)
  217. end
  218. LuaMongo.find(DB.db_union, nil, FieldBaseUnion, limit, skip)
  219. local unionList = {}
  220. while true do
  221. local union = {}
  222. if not LuaMongo.next(union) then
  223. break
  224. end
  225. unionList[#unionList + 1] = union
  226. end
  227. return unionList
  228. end
  229. -- 后台查询
  230. function getUnionAdmin(start)
  231. local unionList = {}
  232. local all = LuaMongo.count(Config.DB_NAME, "union")
  233. LuaMongo.find(DB.db_union, {["$orderby"] = { lv = - 1 } }, FieldBaseUnion, 10, start)
  234. while true do
  235. if not LuaMongo.next(tempData) then
  236. break
  237. end
  238. local union = {}
  239. union.name = tempData.name
  240. union.lv = tempData.lv
  241. union.id = tempData.id
  242. unionList[#unionList + 1] = union
  243. end
  244. return #unionList, unionList, all
  245. end
  246. -- 日志
  247. function addLog(unionUuid, content,classify)
  248. local union = getUnion(unionUuid, FieldLog)
  249. if not union then return end
  250. local log = {}
  251. log.content = content
  252. log.time = os.time()
  253. log.classify = classify
  254. union.logs = union.logs or {}
  255. table.insert(union.logs, 1, log)
  256. union.logs[UNION_LOG_MAXCNT + 1] = nil
  257. upsetUnionData(union._id, "logs", union.logs)
  258. end
  259. -- 是否有申请
  260. function hasApply(union)
  261. if not union.apply then
  262. return
  263. end
  264. return next(union.apply)
  265. end
  266. -- 是否已申请
  267. function isApply(union, uuid)
  268. if not union.apply then
  269. return
  270. end
  271. return union.apply[uuid]
  272. end
  273. -- 添加申请
  274. function addApply(union, uuid)
  275. if isApply(union, uuid) then
  276. return
  277. end
  278. union.apply = union.apply or {}
  279. union.apply[uuid] = os.time()
  280. upsetUnionData(union._id, "apply", union.apply)
  281. end
  282. -- 删除申请
  283. function delApply(union, uuid)
  284. if not isApply(union, uuid) then
  285. return
  286. end
  287. union.apply[uuid] = nil
  288. upsetUnionData(union._id, "apply", union.apply)
  289. end
  290. -- 清空申请
  291. function resetApply(union)
  292. if union.apply == nil then return end
  293. union.apply = nil
  294. upsetUnionData(union._id, "apply", nil)
  295. end
  296. -- 设置职位
  297. function setPost(union, uuid, post)
  298. local member = getUnionMember(union, uuid)
  299. if not member then return end
  300. member.post = post
  301. upsetUnionData(union._id, "member", union.member)
  302. end
  303. -- 获取职位人数
  304. function getPostCnt(union, post)
  305. if not union then return 0 end
  306. local cnt = 0
  307. for _, member in pairs(union.member) do
  308. if member.post == post then
  309. cnt = cnt + 1
  310. end
  311. end
  312. return cnt
  313. end
  314. -- 设置公告
  315. function setNotice(union, notice)
  316. union.notice = notice
  317. union.noticeTime = os.time()
  318. updateUnionData(union)
  319. end
  320. -- 设置旗帜
  321. function setBannerID(union, bannerID)
  322. union.bannerID = bannerID
  323. updateUnionData(union)
  324. end
  325. -- 设置入会要求
  326. function setJoinLimit(union, needLv, needAgree)
  327. union.needLv = needLv
  328. union.needAgree = needAgree
  329. updateUnionData(union)
  330. end
  331. -- 更换会长
  332. function setPresident(union, targetUuid)
  333. local oldMember = getUnionMember(union, union.presidentUuid)
  334. if oldMember then
  335. oldMember.post = UnionDefine.POST_MEMBER
  336. end
  337. local newMember = getUnionMember(union, targetUuid)
  338. newMember.post = UnionDefine.POST_PRESIDENT
  339. union.presidentUuid = targetUuid
  340. updateUnionData(union)
  341. end
  342. -- 本日签到数目
  343. function getSignInCnt(union)
  344. if union.signInTime and
  345. not Util.isSameDay(union.signInTime) then
  346. union.signInCnt = nil
  347. union.signInTime = nil
  348. end
  349. return union.signInCnt or 0
  350. end
  351. -- 设置公会名称
  352. function setUnionName(union, name)
  353. union.name = name
  354. union.nameTime = os.time()
  355. updateUnionData(union)
  356. end
  357. -- 本日公会邮件数目
  358. function getMailCnt(union)
  359. if union.mailTs and
  360. not Util.isSameDay(union.mailTs) then
  361. union.mailCnt = nil
  362. union.mailTs = nil
  363. end
  364. return union.mailCnt or 0
  365. end
  366. -- 更新公会副本等级
  367. function updateBossLv(union, level)
  368. level = tonumber(level)
  369. if not level then return end
  370. upsetUnionData(union._id, "bossLv", level)
  371. end
  372. -- 获取工坊等级
  373. function getMillLv(union)
  374. return union.mill and union.mill.lv or 1
  375. end
  376. -- 获取工坊经验
  377. function getMillDonate(union)
  378. return union.mill and union.mill.donate or 1
  379. end
  380. -- 初始工坊信息
  381. function initMillData(union)
  382. union.mill = {}
  383. union.mill.lv = 1
  384. union.mill.donate = 0
  385. end
  386. -- 回收头像框
  387. function cleanUnionHeadFrame()
  388. LuaMongo.find(DB.db_union, QueryByHeadFrame, FieldBaseUnion)
  389. local list = {}
  390. while true do
  391. local union = { }
  392. if not LuaMongo.next(union) then
  393. break
  394. end
  395. list[#list + 1] = union._id
  396. end
  397. for _, unionUuid in ipairs(list) do
  398. upsetUnionData(unionUuid, "headFrame", nil)
  399. end
  400. end
  401. -- 更新公会 全体BUF
  402. function updateEctypeBuf(union)
  403. if union.copyBuff == nil then return end
  404. upsetUnionData(union._id, "copyBuff", union.copyBuff)
  405. end
  406. function updateJihuo(union)
  407. upsetUnionData(union._id, "jihuo", union.jihuo)
  408. upsetUnionData(union._id, "jihuoTime", union.jihuoTime)
  409. end
  410. -- 公会红包
  411. -- 发送公会改DB
  412. function insertSendRedBag(unionUuid,redBagID,redBagName,sender,senderPost,config,index)
  413. local now = os.time()
  414. QueryByUnionUuid._id = unionUuid
  415. -- 更新发红包次数,红包ID
  416. local updateTb = { ["$inc"]= {["redBagID"] = 1}}
  417. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  418. -- 更新红包总额排行榜
  419. local updateTb = { ["$inc"]= {
  420. ["redBagRank."..sender.uuid..".totalMoney"] = config.price,
  421. ["redBagRank."..sender.uuid..".bagCnt"] = 1,
  422. }}
  423. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  424. -- 更新红包数据
  425. local obj = {}
  426. obj.id = redBagID
  427. obj.type = config.type
  428. obj.name = config.name
  429. obj.sender = sender
  430. obj.senderPost = senderPost
  431. obj.bagCnt = config.bagCnt
  432. obj.nowCnt = 0
  433. obj.itemID = config.content[1][1]
  434. obj.itemCnt = config.content[1][2]
  435. obj.leftCnt = config.content[1][2]
  436. obj.time = now + config.time*60*60
  437. obj.index = config.index
  438. updateTb = {
  439. ["redBag."..redBagID] = obj
  440. }
  441. local tb = { }
  442. tb["$set"] = updateTb
  443. LuaMongo.update(DB.db_union, QueryByUnionUuid, tb)
  444. end
  445. function updateRedBagRecord(unionUuid,uuid,redBagID,itemID,itemCnt)
  446. local now = os.time()
  447. QueryByUnionUuid._id = unionUuid
  448. -- 更新红包记录
  449. local role = {}
  450. RoleLogic.getRoleBaseByUuid(uuid,role)
  451. local obj = {}
  452. obj.itemID = itemID
  453. obj.itemCnt = itemCnt
  454. obj.role = role
  455. obj.time = now
  456. local updateTb = {
  457. ["redBag."..redBagID..".record."..uuid] = obj
  458. }
  459. local tb = { }
  460. tb["$set"] = updateTb
  461. LuaMongo.update(DB.db_union, QueryByUnionUuid, tb)
  462. updateTb = { ["$inc"]= {["redBag."..redBagID..".leftCnt"] = -itemCnt,["redBag."..redBagID..".nowCnt"] = 1}}
  463. LuaMongo.update(DB.db_union, QueryByUnionUuid, updateTb)
  464. end
  465. function cleanRedBagCnt(human)
  466. human.db.redBagCnt = nil
  467. end