UnionDonateLogic.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. local UnionDBLogic = require("union.UnionDBLogic")
  2. local UnionExcel = require("excel.union")
  3. local Msg = require("core.Msg")
  4. local Util = require("common.Util")
  5. local Lang = require("common.Lang")
  6. local Grid = require("bag.Grid")
  7. local BagLogic = require("bag.BagLogic")
  8. local Broadcast = require("broadcast.Broadcast")
  9. local UnionLogic = require("union.UnionLogic")
  10. local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
  11. local ItemDefine = require("bag.ItemDefine")
  12. local LiLianLogic = require("dailyTask.LiLianLogic")
  13. local UnionLivenessLogic = require("union.UnionLivenessLogic")
  14. local UnionDefine = require("union.UnionDefine")
  15. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  16. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  17. local MengxinLogic = require("present.MengxinLogic")
  18. local YunYingLogic = require("yunying.YunYingLogic")
  19. local TriggerDefine = require("trigger.TriggerDefine")
  20. local TriggerLogic = require("trigger.TriggerLogic")
  21. function unionDonateQuery(human)
  22. -- 判断玩家是否有工会
  23. -- 判断玩家公会是否存在
  24. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  25. if union == nil then
  26. return
  27. end
  28. local unionLv = math.min(union.lv, #UnionExcel.union)
  29. local nextLv = unionLv + 1
  30. local config = UnionExcel.union[nextLv]
  31. -- 写协议数据
  32. local msgRet = Msg.gc.GC_UNION_DONATE_QUERY
  33. msgRet.unionLv = unionLv
  34. msgRet.unionExp = union.exp
  35. msgRet.lvUpExp = UnionExcel.union[unionLv].exp
  36. msgRet.nextMemCnt = config and config.maxCnt or 0
  37. msgRet.todayExp = union.donate and union.donate[1] or 0
  38. -- 捐赠类型
  39. local len = #UnionExcel.donate
  40. for i = 1,len do
  41. local net = msgRet.donate[i]
  42. local v = UnionExcel.donate[i]
  43. net.unionExp = v.exp
  44. net.type = v.type
  45. Grid.makeItem(net.unionContr,v.contr[1][1],v.contr[1][2])
  46. Grid.makeItem(net.needCost,v.cost[1][1],v.cost[1][2])
  47. if human.db.unionDonate ~= nil and human.db.unionDonate[1] == v.type then
  48. net.state = 2
  49. else
  50. if human.db.unionDonate == nil then
  51. net.state = 1
  52. else
  53. net.state = 0
  54. end
  55. end
  56. end
  57. msgRet.donate[0] = len
  58. -- 捐赠奖励
  59. len = #UnionExcel.donateReward
  60. for i = 1,len do
  61. local net = msgRet.reach[i]
  62. local v = UnionExcel.donateReward[i]
  63. net.id = i
  64. net.needExp = v.needExp
  65. local rewardLen = #v.reward
  66. for j = 1,rewardLen do
  67. Grid.makeItem(net.reward[j],v.reward[j][1],v.reward[j][2])
  68. end
  69. net.reward[0] = rewardLen
  70. if human.db.donateReward == nil or human.db.donateReward[i] == nil then
  71. if union.donate ~= nil and union.donate[1] >= v.needExp then
  72. net.state = 1
  73. else
  74. net.state = 0
  75. end
  76. else
  77. net.state = 2
  78. end
  79. end
  80. msgRet.reach[0] = len
  81. -- 发送协议
  82. Msg.send(msgRet,human.fd)
  83. end
  84. function donateDo(human,type)
  85. -- 判断玩家是否有工会
  86. -- 判断玩家公会是否存在
  87. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  88. if union == nil then
  89. return
  90. end
  91. -- 验证是否可以捐赠
  92. if human.db.unionDonate ~= nil then
  93. return Broadcast.sendErr(human, Lang.UNION_DONATE_ERR_HAD)
  94. end
  95. -- 验证捐赠类型是否合法
  96. if type == nil then
  97. return
  98. end
  99. local config = UnionExcel.donate[type]
  100. if config == nil then
  101. return
  102. end
  103. -- 验证捐赠花费是否足够
  104. if not BagLogic.checkItemCnt(human, config.cost[1][1],config.cost[1][2]) then
  105. return
  106. end
  107. -- 扣除花费
  108. BagLogic.delItem(human, config.cost[1][1],config.cost[1][2], "union_donate")
  109. -- 写DB
  110. human.db.unionDonate = {}
  111. human.db.unionDonate[1] = config.type -- 捐赠类型
  112. human.db.unionDonate[2] = config.exp -- 当日获得经验
  113. human.db.unionDonate[3] = config.contr[1][2] -- 当日捐赠
  114. human.db.dailyBanggong = human.db.dailyBanggong or 0
  115. human.db.dailyBanggong = human.db.dailyBanggong + config.contr[1][2]
  116. human.db.totalBanggong = human.db.totalBanggong or 0
  117. human.db.totalBanggong = human.db.totalBanggong + config.contr[1][2]
  118. -- 公会经验
  119. UnionLogic.addUnionExp(union,config.exp)
  120. -- 日清,当日公会获得经验
  121. union.donate = union.donate or {}
  122. union.donate[1] = union.donate[1] or 0
  123. union.donate[1] = union.donate[1] + config.exp
  124. union.donate[2] = os.time()
  125. -- 公会总捐赠
  126. union.totalDonate = union.totalDonate or 0
  127. union.totalDonate = union.totalDonate + config.contr[1][2]
  128. -- 公会捐赠列表
  129. union.member[human.db._id].donate = union.member[human.db._id].donate or 0
  130. union.member[human.db._id].donate = union.member[human.db._id].donate + config.contr[1][2]
  131. union.member[human.db._id].lastDonateTime = os.time()
  132. UnionDBLogic.updateUnionData(union)
  133. UnionLogic.addUnionLog(union._id, 8, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_DONATE,config.param)
  134. -- 给道具
  135. BagLogic.cleanMomentItemList()
  136. BagLogic.updateMomentItem(1, config.contr[1][1], config.contr[1][2])
  137. BagLogic.addMomentItemList(human, "union_donate")
  138. DailyTaskLogic.recordDailyTaskFinishCnt(human, DailyTaskLogic.DAILY_TASK_ID_7, 1)
  139. if config.cost[1][2] == ItemDefine.ITEM_ZUANSHI_ID then
  140. LiLianLogic.onCallback(human,LiLianLogic.LILIAN_OUTID30,cnt)
  141. end
  142. MengxinLogic.onCallBack(human,MengxinLogic.MX_TASK_TYPE_10,1)
  143. YunYingLogic.onCallBack(human, "onUnionDonate",1)
  144. --荣耀历程,仅当至尊捐献时触发
  145. if type ==3 then
  146. TriggerLogic.PublishEvent(TriggerDefine.UNION_DONATE, human.db._id, 1)
  147. end
  148. TriggerLogic.PublishEvent(TriggerDefine.UNIONS_ALL_TYPE_DONATE, human.db._id, 1)
  149. UnionLogic.sendUnionChange(union)
  150. unionDonateQuery(human)
  151. Broadcast.sendErr(human, Lang.UNION_DONATE_OK)
  152. if type == 1 then
  153. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_JB_DONATE,1)
  154. elseif type == 2 then
  155. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_ZS_DONATE,1)
  156. elseif type == 3 then
  157. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_ZZZS_DONATE,1)
  158. end
  159. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1003)
  160. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  161. end
  162. function getDonateReward(human,id)
  163. -- 判断玩家是否有工会
  164. -- 判断玩家公会是否存在
  165. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  166. if union == nil then
  167. return
  168. end
  169. -- 验证id是否合法
  170. if id == nil then
  171. return
  172. end
  173. local config = UnionExcel.donateReward[id]
  174. if config == nil then
  175. return
  176. end
  177. -- 验证公会当日是否获得足够的经验
  178. if union.donate == nil or union.donate[1] < config.needExp then
  179. return
  180. end
  181. -- 验证玩家是否已领奖励
  182. if human.db.donateReward ~= nil and human.db.donateReward[id] ~= nil then
  183. return
  184. end
  185. -- 领取奖励
  186. -- 写DB
  187. human.db.donateReward = human.db.donateReward or {}
  188. human.db.donateReward[id] = 1
  189. -- 发放物品
  190. BagLogic.addItemList(human, config.reward, "union_donate")
  191. unionDonateQuery(human)
  192. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1003)
  193. end
  194. function updateDaily(human)
  195. -- 清理玩家公会当日捐赠
  196. human.db.unionDonate = nil
  197. human.db.donateReward = nil
  198. human.db.dailyBanggong = 0
  199. -- 判断玩家是否有工会
  200. -- 判断玩家公会是否存在
  201. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  202. if union == nil then
  203. return
  204. end
  205. local now = os.time()
  206. -- 清理公会当日数据
  207. if union.donate == nil then
  208. return
  209. end
  210. local isSameDay = Util.isSameDayByTimes(now,union.donate[2])
  211. -- 不是同一天,清理
  212. if isSameDay ~= true then
  213. union.donate = nil
  214. end
  215. UnionDBLogic.updateUnionData(union)
  216. end
  217. function cleanUnionDonateList(union,uuid)
  218. if union.donateList == nil or union.donateList[uuid] == nil then
  219. return
  220. end
  221. union.donateList[uuid] = nil
  222. end
  223. HUOYUE_TIME = 172800
  224. function getHuoyueCnt(union)
  225. local now = os.time()
  226. local cnt = 0
  227. for k,v in pairs(union.member) do
  228. if v.lastDonateTime and now - v.lastDonateTime <= HUOYUE_TIME then
  229. cnt = cnt + 1
  230. end
  231. end
  232. return cnt
  233. end
  234. function isDot(human)
  235. -- 有可捐赠次数
  236. if human.db.unionDonate == nil then
  237. print("[isDot] 公会可捐赠直接返回")
  238. return true
  239. end
  240. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  241. if union == nil then
  242. return
  243. end
  244. -- 有可领取奖励
  245. local len = #UnionExcel.donateReward
  246. for i = 1,len do
  247. local v = UnionExcel.donateReward[i]
  248. if human.db.donateReward == nil or human.db.donateReward[i] == nil then
  249. if union.donate ~= nil and union.donate[1] >= v.needExp then
  250. print("[isDot] 公会可捐赠直接返回 i = "..i.." union.donate[1] = "..union.donate[1].." needExp = "..v.needExp)
  251. return true
  252. end
  253. end
  254. end
  255. return false
  256. end