UnionDonateLogic.lua 9.2 KB

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