UnionRedBagLogic.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. local Msg = require("core.Msg")
  2. local PanelDefine = require("broadcast.PanelDefine")
  3. local UnionDBLogic = require("union.UnionDBLogic")
  4. local UnionDefine = require("union.UnionDefine")
  5. local UnionExcel = require("excel.union")
  6. local Grid = require("bag.Grid")
  7. local BagLogic = require("bag.BagLogic")
  8. local RoleLogic = require("role.RoleLogic")
  9. local BuyLogic = require("topup.BuyLogic")
  10. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  11. local ChatUnion = require("chat.ChatUnion")
  12. local LiLianLogic = require("dailyTask.LiLianLogic")
  13. local UnionLivenessLogic = require("union.UnionLivenessLogic")
  14. -- 红包记录查询
  15. function unionRedBagQuery(human)
  16. -- 判断玩家是否有公会
  17. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  18. if union == nil then
  19. return
  20. end
  21. local now = os.time()
  22. local msgRet = Msg.gc.GC_UNION_RED_BAG_QUERY
  23. if union.redBag == nil then
  24. -- 无公会红包记录
  25. msgRet.redBagList[0] = 0
  26. else
  27. -- 有公会红包记录
  28. local cnt = 0
  29. for k,v in pairs(union.redBag) do
  30. cnt = cnt + 1
  31. local net = msgRet.redBagList[cnt]
  32. local config = v.index and UnionExcel.redBag[v.index]
  33. net.id = k
  34. net.name = v.name
  35. net.type = v.type
  36. RoleLogic.makeRoleBase(v.sender,net.sender)
  37. net.senderPost = v.senderPost
  38. net.bagCnt = v.bagCnt
  39. net.nowCnt = v.nowCnt
  40. net.time = v.time - now
  41. -- 已过期
  42. if now > v.time then
  43. net.statu = 3
  44. else
  45. if v.record ~= nil and v.record[human.db._id] ~= nil then
  46. net.statu = 2 -- 已领取
  47. elseif v.nowCnt >= v.bagCnt then
  48. net.statu = 4 -- 已领完
  49. else
  50. net.statu = 1 -- 可领
  51. end
  52. end
  53. net.desc = config and config.desc or ""
  54. net.item[0] = config and #config.content or 0
  55. for i = 1, net.item[0] do
  56. local itemID = config.content[i][1]
  57. local itemCnt = config.content[i][2]
  58. Grid.makeItem(net.item[i], itemID, itemCnt)
  59. end
  60. end
  61. msgRet.redBagList[0] = cnt
  62. end
  63. Msg.send(msgRet,human.fd)
  64. end
  65. -- 单个红包查询
  66. function singleRedBag(human,id)
  67. -- 判断玩家是否有公会
  68. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  69. if union == nil then
  70. return
  71. end
  72. -- 校验红包是否存在
  73. if id == nil or union.redBag[id] == nil then
  74. return
  75. end
  76. local msgRet = Msg.gc.GC_UNION_SINGLE_RED_BAG_QUERY
  77. if union.redBag[id].record == nil then
  78. msgRet.getRedBagList[0] = 0
  79. else
  80. local cnt = 0
  81. for k,v in pairs(union.redBag[id].record) do
  82. cnt = cnt + 1
  83. local net = msgRet.getRedBagList[cnt]
  84. RoleLogic.getRoleBaseByUuid(k,net.role)
  85. net.time = v.time
  86. Grid.makeItem(net.item,v.itemID,v.itemCnt)
  87. end
  88. msgRet.getRedBagList[0] = cnt
  89. end
  90. Msg.send(msgRet,human.fd)
  91. end
  92. -- 单个红包手气最佳
  93. function mostLucky(human,id)
  94. -- 判断玩家是否有公会
  95. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  96. if union == nil then
  97. return
  98. end
  99. -- 校验红包是否存在
  100. if id == nil or union.redBag[id] == nil then
  101. return
  102. end
  103. local uuid = nil
  104. local luckCnt = 0
  105. if union.redBag[id].record == nil then
  106. return
  107. else
  108. for k,v in pairs(union.redBag[id].record) do
  109. cnt = cnt + 1
  110. if luckCnt == 0 or luckCnt < v.itemCnt then
  111. luckCnt = v.itemCnt
  112. uuid = k
  113. end
  114. end
  115. end
  116. return uuid,luckCnt
  117. end
  118. -- 发红包查询
  119. function sendRedBagQuery(human)
  120. -- 判断玩家是否有公会
  121. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  122. if union == nil then
  123. return
  124. end
  125. local msgRet = Msg.gc.GC_UNION_SEND_RED_BAG_QUERY
  126. local config = UnionExcel.redBag
  127. local len = #config
  128. for i = 1,len do
  129. local net = msgRet.redBag[i]
  130. local v = config[i]
  131. net.type = v.type
  132. net.index = i
  133. net.name = v.name
  134. if human.db.redBagCnt == nil or human.db.redBagCnt[v.type] == nil then
  135. net.maxSendCnt = v.maxSendCnt
  136. else
  137. net.maxSendCnt = v.maxSendCnt - human.db.redBagCnt[v.type]
  138. end
  139. net.bagCnt = v.bagCnt
  140. net.desc = v.desc
  141. net.buttonTxt = v.buttonTxt
  142. net.rewardTxt = v.rewardTxt
  143. -- 红包发送奖励
  144. local rewardLen = #v.reward
  145. for j = 1,rewardLen do
  146. Grid.makeItem(net.reward[j],v.reward[j][1],v.reward[j][2])
  147. end
  148. net.reward[0] = rewardLen
  149. -- 红包内容
  150. local contentLen = #v.content
  151. for j = 1,contentLen do
  152. Grid.makeItem(net.item[j],v.content[j][1],v.content[j][2])
  153. end
  154. net.item[0] = contentLen
  155. -- 红包buyMsg
  156. BuyLogic.fontBuyItem(human, net.buyItem, v.buyID)
  157. end
  158. msgRet.redBag[0] = len
  159. Msg.send(msgRet,human.fd)
  160. end
  161. -- 发红包回调
  162. function sendRedBagCallBack(human,index)
  163. -- 判断玩家是否有公会
  164. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  165. if union == nil then
  166. return
  167. end
  168. local config = UnionExcel.redBag[index]
  169. -- 发送失败 次数上限
  170. if human.db.redBagCnt ~= nil
  171. and human.db.redBagCnt[config.type] ~= nil
  172. and human.db.redBagCnt[config.type] + config.cnt > config.maxSendCnt then
  173. return
  174. end
  175. -- 发送成功
  176. -- 写数据库
  177. human.db.redBagCnt = human.db.redBagCnt or {}
  178. human.db.redBagCnt[config.type] = human.db.redBagCnt[config.type] or 0
  179. human.db.redBagCnt[config.type] = human.db.redBagCnt[config.type] + config.cnt
  180. local roleBase = {}
  181. RoleLogic.getRoleBase(human,roleBase)
  182. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  183. if not member then return end
  184. local redBagID = union.redBagID and union.redBagID + 1 or 1
  185. local txtID = config.type..redBagID -- 由于用数字做id,在刷新公会信息时,会使数据库红包数据由object 变为Array ,id会被改变
  186. UnionDBLogic.insertSendRedBag(union._id,txtID,config.name,roleBase,member.post,config,index)
  187. -- 发送发红包者奖励
  188. BagLogic.addItemList(human, config.reward, "send_redbag")
  189. -- 发送公会通知
  190. ChatUnion.chatUnionRedBag(human)
  191. -- 跑马灯
  192. -- todo
  193. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE24,PanelDefine.PANEL_ID_201)
  194. -- 查询
  195. unionRedBagQuery(human)
  196. local price = config.price
  197. LiLianLogic.onCallback(human,LiLianLogic.LILIAN_OUTID32,price)
  198. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_RED_BAG,1)
  199. end
  200. function getRedBag(human,id)
  201. -- 判断玩家是否有公会
  202. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  203. if union == nil then
  204. return
  205. end
  206. -- 校验红包是否存在
  207. if id == nil or union.redBag[id] == nil then
  208. return
  209. end
  210. -- 判断是否能抢
  211. local now = os.time()
  212. local net = union.redBag[id]
  213. -- 已超时
  214. if now > union.redBag[id].time then
  215. return
  216. end
  217. -- 已领取
  218. if net.record ~= nil and net.record[human.db._id] ~= nil then
  219. return
  220. end
  221. -- 已领完
  222. if net.nowCnt >= net.bagCnt then
  223. return
  224. end
  225. -- 计算抢到多少
  226. local leftCnt = net.bagCnt - net.nowCnt
  227. local leftItem = net.leftCnt
  228. local getCnt = 0
  229. if leftCnt == 1 then
  230. getCnt = leftItem
  231. else
  232. getCnt = math.random(1,leftItem)
  233. end
  234. -- 防止某人手气太好,剩余数量不足以发剩余红包
  235. local temoLeftItem = leftItem - getCnt
  236. if temoLeftItem < leftCnt - 1 then
  237. temoLeftItem = leftCnt - 1
  238. getCnt = leftItem - temoLeftItem
  239. leftItem = temoLeftItem
  240. end
  241. -- 写数据库
  242. UnionDBLogic.updateRedBagRecord(human.db.unionUuid,human.db._id,id,net.itemID,getCnt)
  243. -- 发送物品
  244. -- BagLogic.addItem(human,net.itemID,getCnt,"get_redbag")
  245. BagLogic.cleanMomentItemList()
  246. BagLogic.updateMomentItem(1, net.itemID,getCnt)
  247. BagLogic.addMomentItemList(human, "get_redbag")
  248. -- 查询
  249. singleRedBag(human,id)
  250. union = UnionDBLogic.getUnion(human.db.unionUuid)
  251. local net = union.redBag[id]
  252. if net.nowCnt >= net.bagCnt then
  253. local mostUuid = mostLucky(human,id)
  254. if mostUuid then
  255. LiLianLogic.onCallbackByUuid(mostUuid,LiLianLogic.LILIAN_OUTID31,1)
  256. end
  257. end
  258. end
  259. --红包榜查询
  260. function redBagBillboardQuery(human)
  261. -- 判断玩家是否有公会
  262. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  263. if union == nil then
  264. return
  265. end
  266. local msgRet = Msg.gc.GC_UNION_RED_BAG_BILLBOARD
  267. if union.redBagRank == nil then
  268. msgRet.redBagRank[0] = 0
  269. else
  270. local rankTb = union.redBagRank
  271. table.sort(rankTb,function (a,b)
  272. return a.totalMoney > b.totalMoney
  273. end)
  274. local cnt = 0
  275. for uuid,v in pairs(rankTb) do
  276. cnt = cnt + 1
  277. local net = msgRet.redBagRank[cnt]
  278. local member = UnionDBLogic.getUnionMember(union, uuid)
  279. RoleLogic.getRoleBaseByUuid(uuid,net.role)
  280. net.totalMoney = v.totalMoney
  281. net.bagCnt = v.bagCnt
  282. net.post = member and member.post or UnionDefine.POST_MEMBER
  283. end
  284. msgRet.redBagRank[0] = cnt
  285. end
  286. Msg.send(msgRet,human.fd)
  287. end
  288. -- 每日更新红包次数
  289. function updateDaily(human)
  290. UnionDBLogic.cleanRedBagCnt(human)
  291. end