WeekendLoopActUseGuYu.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. --------------------------------
  2. -- 文件名 : WeekendLoopActUseGuYu.lua
  3. -- 文件说明 : 周末冲刺活动-消费古玉
  4. -- 创建时间 : 2024/12/02
  5. -- 创建人 : FC
  6. --------------------------------
  7. local Util = require("common.Util")
  8. local Lang = require("common.Lang")
  9. local Broadcast = require("broadcast.Broadcast")
  10. local MailExcel = require("excel.mail")
  11. local Msg = require("core.Msg")
  12. local ObjHuman = require("core.ObjHuman")
  13. local WeekLoopActDef = require("WeekendLoopActivity.WeekendLoopActDefine")
  14. local WeekLoopActCof = require("excel.WeekLoopAct")
  15. local CommonDB = require("common.CommonDB")
  16. local MailManager = require("mail.MailManager")
  17. local Grid = require("bag.Grid")
  18. local HeroGrid = require("hero.HeroGrid")
  19. local HeroLogic = require("hero.HeroLogic")
  20. local BagLogic = require("bag.BagLogic")
  21. local Log = require("common.Log")
  22. local HeroExcel = require("excel.hero")
  23. local WeekendLoopActManger = require("WeekendLoopActivity.WeekendLoopActManager")
  24. ----------------------------------------- 内部处理开始 -------------------------------------
  25. -- 写日志
  26. local function WeekActGuYu_WriteLog(human, szFuncName, sztext)
  27. Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, szFuncName..sztext.." _id = "..human.db._id.." name = "..human.db.name)
  28. end
  29. -- 下发数据
  30. local function WeekActGuYu_SendData(tMsgData, fd)
  31. Msg.send(tMsgData, fd)
  32. end
  33. -- 获取配置
  34. local function WeekActGuYu_GetConfig()
  35. if not WeekLoopActCof then
  36. return nil
  37. end
  38. return WeekLoopActCof.GuYu
  39. end
  40. -- 初始化古玉数量
  41. local function WeekActHeroStar_ResetGuYuNum(human)
  42. if not human then
  43. print("[WeekActHeroStar_SetHeroID] 参数不正确")
  44. return false
  45. end
  46. human.db.nWeekUseGuYu = 0
  47. return true
  48. end
  49. -- 获取古玉数量
  50. local function WeekActHeroStar_GetGuYuNum(human)
  51. if not human then
  52. print("[WeekActHeroStar_SetHeroID] 参数不正确")
  53. return -1
  54. end
  55. if not human.db.nWeekUseGuYu then
  56. local bRet = WeekActHeroStar_ResetGuYuNum(human)
  57. if false == bRet then
  58. WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetGuYuNum]", "获取古玉数量,不存在对应字段,重新初始化失败")
  59. return -1
  60. end
  61. WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetGuYuNum]", "获取古玉数量,不存在对应字段,初始化顺序有问题, 重新初始化成功")
  62. end
  63. return human.db.nWeekUseGuYu
  64. end
  65. -- 设置古玉数量
  66. local function WeekActHeroStar_SetGuYuNum(human, nValue)
  67. if not human then
  68. print("[WeekActHeroStar_SetHeroID] 参数不正确")
  69. return
  70. end
  71. local nNowGuYu = WeekActHeroStar_GetGuYuNum(human)
  72. if -1 >= nNowGuYu then
  73. WeekActGuYu_WriteLog(human, "[WeekActHeroStar_SetGuYuNum]", "获取古玉数量失败")
  74. return
  75. end
  76. local nNewGuYu = nNowGuYu + nValue
  77. if 0 > nNewGuYu then
  78. nNewGuYu = 0
  79. end
  80. human.db.nWeekUseGuYu = nNewGuYu
  81. end
  82. -- 初始化奖励信息
  83. local function WeekActGuYu_ResetPrize(human)
  84. if not human then
  85. return false
  86. end
  87. local tConfig = WeekActGuYu_GetConfig()
  88. if not tConfig then
  89. return false
  90. end
  91. if not human.db.tWeekGuYuPrize then
  92. human.db.tWeekGuYuPrize = {}
  93. end
  94. for nID, v in pairs(tConfig) do
  95. human.db.tWeekGuYuPrize[nID] = WeekLoopActDef.WEEKACT_STATE_NONE
  96. end
  97. return true
  98. end
  99. -- 获取奖励表
  100. local function WeekActGuYu_GetDBPrize(human)
  101. if not human then
  102. return nil
  103. end
  104. if not human.db.tWeekGuYuPrize then
  105. return nil
  106. end
  107. return human.db.tWeekGuYuPrize
  108. end
  109. -- 获取奖励ID状态
  110. local function WeekActGuYu_GetPrizeStatus(human, nID)
  111. if not human then
  112. return WeekLoopActDef.WEEKACT_STATE_NONE
  113. end
  114. local tPrize = WeekActGuYu_GetDBPrize(human)
  115. if not tPrize or not tPrize[nID] then
  116. print("[WeekActGuYu_GetPrizeStatus] 不存在对应的ID nID = "..nID)
  117. return WeekLoopActDef.WEEKACT_STATE_NONE
  118. end
  119. return tPrize[nID]
  120. end
  121. -- 设置奖励ID状态
  122. local function WeekActGuYu_SetPrizeStatus(human, nID, nStatus)
  123. if not human then
  124. return false
  125. end
  126. local tPrize = WeekActGuYu_GetDBPrize(human)
  127. if not tPrize or not tPrize[nID] then
  128. return false
  129. end
  130. tPrize[nID] = nStatus
  131. return true
  132. end
  133. ----------------------------------------- 外部调用 -------------------------------------
  134. -- 重置数据
  135. function WeekActGuYu_ResetData(human)
  136. if not human then
  137. return
  138. end
  139. -- 重置奖励信息
  140. if false == WeekActGuYu_ResetPrize(human) then
  141. print("[WeekActGuYu_ResetData] 重置古玉奖励数据失败")
  142. return
  143. end
  144. -- 重置消耗古玉数量
  145. WeekActHeroStar_ResetGuYuNum(human)
  146. WeekActGuYu_WriteLog(human, "[WeekActGuYu_ResetData]", "古玉奖励相关重置完成")
  147. print("[WeekActGuYu_ResetData] 古玉 数据重置结束 ")
  148. end
  149. -- 是否有红点
  150. function isRed(human)
  151. local tPrize = WeekActGuYu_GetConfig()
  152. if not tPrize then
  153. return false
  154. end
  155. for nID, v in pairs(tPrize) do
  156. if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActGuYu_GetPrizeStatus(human, nID) then
  157. return true
  158. end
  159. end
  160. return false
  161. end
  162. -- 消耗古玉回调
  163. function WeekActGuYu_UseGuYu(human, nGuYu)
  164. if not human or 0 >= nGuYu then
  165. return
  166. end
  167. local DBID = human.db._id
  168. local szName = human.db.name
  169. local tConfig = WeekActGuYu_GetConfig()
  170. if not tConfig then
  171. print("[WeekActGuYu_UseGuYu] 获取不到配置 DBID = "..DBID.." name = "..szName)
  172. return
  173. end
  174. WeekActHeroStar_SetGuYuNum(human, nGuYu)
  175. local nNowGuYu = WeekActHeroStar_GetGuYuNum(human)
  176. print("[onDecZuanshi] 消耗古玉回调开始 nNowGuYu = "..nNowGuYu)
  177. local bChange = false
  178. for nID, v in pairs(tConfig) do
  179. local nStatus = WeekActGuYu_GetPrizeStatus(human, nID)
  180. print("[onDecZuanshi] 消耗古玉回调开始 nNowGuYu = "..nNowGuYu.." nStatus = "..nStatus)
  181. if nNowGuYu >= v.guyu and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
  182. bChange = true
  183. WeekActGuYu_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
  184. end
  185. end
  186. print("[onDecZuanshi] 古玉回调处理完成 nNowGuYu = "..nNowGuYu)
  187. if true == bChange then
  188. print("[onDecZuanshi] 古玉回调处理完成 开始请求 nNowGuYu = "..nNowGuYu)
  189. WeekActGuYu_Query(human)
  190. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  191. end
  192. end
  193. ----------------------------------------- 客户端请求 -------------------------------------
  194. -- 客户端请求-古玉信息
  195. function WeekActGuYu_Query(human)
  196. if not human then
  197. return
  198. end
  199. local DBID = human.db._id
  200. local szName = human.db.name
  201. local tConfig = WeekActGuYu_GetConfig()
  202. if not tConfig then
  203. print("[WeekActGuYu_Query] 获取不到配置 DBID = "..DBID.." name = "..szName)
  204. return
  205. end
  206. local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_GUYUQUERY
  207. tMsgData.nGuYu = WeekActHeroStar_GetGuYuNum(human)
  208. tMsgData.list[0] = 0
  209. for nID, v in pairs(tConfig) do
  210. tMsgData.list[0] = tMsgData.list[0] + 1
  211. local tData = tMsgData.list[tMsgData.list[0]]
  212. tData.nID = nID
  213. tData.nGuYu = v.guyu
  214. tData.nState = WeekActGuYu_GetPrizeStatus(human, nID)
  215. tData.item[0] = #v.prize
  216. for nIndex, tItem in ipairs(v.prize) do
  217. local nGoodsID = tItem[1]
  218. local nGoodsNum = tItem[2]
  219. Grid.makeItem(tData.item[nIndex], nGoodsID, nGoodsNum)
  220. end
  221. end
  222. WeekActGuYu_SendData(tMsgData, human.fd)
  223. end
  224. -- 客户端请求领取奖励
  225. function WeekActGuYu_GetPrize(human)
  226. if not human then
  227. return
  228. end
  229. local DBID = human.db._id
  230. local szName = human.db.name
  231. local tConfig = WeekActGuYu_GetConfig()
  232. if not tConfig then
  233. print("[WeekActGuYu_GetPrize] 获取不到配置 DBID = "..DBID.." name = "..szName)
  234. return
  235. end
  236. local tItemList = {}
  237. for nID, v in pairs(tConfig) do
  238. if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActGuYu_GetPrizeStatus(human, nID) then
  239. if false == WeekActGuYu_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
  240. print("[WeekActGuYu_GetPrize] 奖励领取失败 nID = "..nID)
  241. WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetPrize]", "领取奖励, 设置奖励状态失败 nID = "..nID)
  242. break
  243. end
  244. for _, data in ipairs(v.prize) do
  245. local nItemID = data[1]
  246. local nItemNum = data[2]
  247. tItemList[nItemID] = tItemList[nItemID] or 0
  248. tItemList[nItemID] = tItemList[nItemID] + nItemNum
  249. end
  250. end
  251. end
  252. if nil ~= _G.next(tItemList) then
  253. local tGoodsInfo = {}
  254. for k, v in pairs(tItemList) do
  255. table.insert(tGoodsInfo, {k,v})
  256. -- 获取奖励写日志
  257. WeekActGuYu_WriteLog(human, "[WeekActHeroStar_GetPrize]", "玩家获取奖励 nGoodsID = "..k.." nGoodsNum = "..v)
  258. end
  259. BagLogic.addItemList(human, tGoodsInfo, "week_loop_act")
  260. BagLogic.sendItemGetList(human, tItemList, "week_loop_act")
  261. end
  262. WeekActGuYu_Query(human)
  263. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  264. end