WeekendLoopActCard.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. --------------------------------
  2. -- 文件名 : WeekendLoopActCard.lua
  3. -- 文件说明 : 周末冲刺活动-抽卡
  4. -- 创建时间 : 2024/12/03
  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. tCardPrize = nil
  26. -- 写日志
  27. local function WeekActCard_WriteLog(human, szFuncName, sztext)
  28. Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, szFuncName..sztext.." _id = "..human.db._id.." name = "..human.db.name)
  29. end
  30. -- 下发数据
  31. local function WeekActCard_SendData(tMsgData, fd)
  32. Msg.send(tMsgData, fd)
  33. end
  34. -- 获取配置
  35. local function WeekActCard_GetConfig()
  36. if not WeekLoopActCof then
  37. return nil
  38. end
  39. return WeekLoopActCof.Card
  40. end
  41. -- 获取配置
  42. local function WeekActCard_GetConfigByType(nType)
  43. if not tCardPrize or not tCardPrize[nType] then
  44. return nil
  45. end
  46. return tCardPrize[nType]
  47. end
  48. -- 初始化抽卡类型
  49. local function WeekActCard_ResetCardType(human)
  50. if not human then
  51. print("[WeekActCard_ResetCardType] 参数不正确")
  52. return false
  53. end
  54. if not human.db.nWeekCardType then
  55. human.db.nWeekCardType = 1
  56. else
  57. -- math.fmod()
  58. local nLen = (human.db.nWeekCardType + 1) % WeekLoopActDef.WEEKACT_CARD_TYPE_LEN
  59. human.db.nWeekCardType = (0 == nLen) and WeekLoopActDef.WEEKACT_CARD_TYPE_LEN or nLen
  60. end
  61. return true
  62. end
  63. -- 获取抽卡类型
  64. local function WeekActCard_GetCardType(human)
  65. if not human then
  66. print("[WeekActCard_SetHeroID] 参数不正确")
  67. return -1
  68. end
  69. if not human.db.nWeekCardType then
  70. local bRet = WeekActCard_ResetCardType(human)
  71. if false == bRet then
  72. WeekActCard_WriteLog(human, "[WeekActCard_GetCardType]", "获取抽卡类型,不存在对应字段,重新初始化失败")
  73. return -1
  74. end
  75. WeekActCard_WriteLog(human, "[WeekActCard_GetCardType]", "获取抽卡类型,不存在对应字段,初始化顺序有问题, 重新初始化成功")
  76. end
  77. return human.db.nWeekCardType
  78. end
  79. -- 通过人物抽卡类型获取配置
  80. local function WeekActCard_GetConfigByHuman(human)
  81. if not human then
  82. return nil
  83. end
  84. local nNowType = WeekActCard_GetCardType(human)
  85. if -1 >= nNowType then
  86. return nil
  87. end
  88. return WeekActCard_GetConfigByType(nNowType)
  89. end
  90. -- 初始化奖励信息
  91. local function WeekActCard_ResetPrize(human)
  92. if not human then
  93. return false
  94. end
  95. local nNowType = WeekActCard_GetCardType()
  96. if -1 >= nNowType then
  97. print("[WeekActCard_ResetPrize] 获取抽卡类型失败")
  98. return false
  99. end
  100. local tConfig = WeekActCard_GetConfigByType(nNowType)
  101. if not tConfig then
  102. return false
  103. end
  104. if not human.db.tWeekCardPrize then
  105. human.db.tWeekCardPrize = {}
  106. end
  107. for nID, v in pairs(tConfig) do
  108. human.db.tWeekCardPrize[nID] = WeekLoopActDef.WEEKACT_STATE_NONE
  109. end
  110. return true
  111. end
  112. -- 获取奖励表
  113. local function WeekActCard_GetDBPrize(human)
  114. if not human then
  115. return nil
  116. end
  117. if not human.db.tWeekGuYuPrize then
  118. return nil
  119. end
  120. return human.db.tWeekGuYuPrize
  121. end
  122. -- 获取奖励ID状态
  123. local function WeekActCard_GetPrizeStatus(human, nID)
  124. if not human then
  125. return WeekLoopActDef.WEEKACT_STATE_NONE
  126. end
  127. local tPrize = WeekActCard_GetDBPrize(human)
  128. if not tPrize or not tPrize[nID] then
  129. return WeekLoopActDef.WEEKACT_STATE_NONE
  130. end
  131. return tPrize[nID]
  132. end
  133. -- 设置奖励ID状态
  134. local function WeekActCard_SetPrizeStatus(human, nID, nStatus)
  135. if not human then
  136. return false
  137. end
  138. local tPrize = WeekActCard_GetDBPrize(human)
  139. if not tPrize or not tPrize[nID] then
  140. return false
  141. end
  142. tPrize[nID] = nStatus
  143. return true
  144. end
  145. -- 初始化抽卡数量
  146. local function WeekActCard_ResetCardNum(human)
  147. if not human then
  148. print("[WeekActCard_ResetCardNum] 参数不正确")
  149. return false
  150. end
  151. human.db.nWeekCardNum = 0
  152. return true
  153. end
  154. -- 获取抽卡数量
  155. local function WeekActCard_GetCardNum(human)
  156. if not human then
  157. print("[WeekActCard_SetHeroID] 参数不正确")
  158. return -1
  159. end
  160. if not human.db.nWeekCardNum then
  161. local bRet = WeekActCard_ResetCardNum(human)
  162. if false == bRet then
  163. WeekActCard_WriteLog(human, "[WeekActCard_GetGuYuNum]", "获取抽卡数量,不存在对应字段,重新初始化失败")
  164. return -1
  165. end
  166. WeekActCard_WriteLog(human, "[WeekActCard_GetGuYuNum]", "获取抽卡数量,不存在对应字段,初始化顺序有问题, 重新初始化成功")
  167. end
  168. return human.db.nWeekCardNum
  169. end
  170. -- 设置抽卡数量
  171. local function WeekActCard_SetCardNum(human, nValue)
  172. if not human then
  173. print("[WeekActCard_SetCardNum] 参数不正确")
  174. return
  175. end
  176. local nNowCard = WeekActCard_GetCardNum(human)
  177. if -1 >= nNowCard then
  178. WeekActCard_WriteLog(human, "[WeekActCard_SetCardNum]", "获取古玉数量失败")
  179. return
  180. end
  181. local nNewGuYu = nNowCard + nValue
  182. if 0 > nNewGuYu then
  183. nNewGuYu = 0
  184. end
  185. human.db.nWeekCardNum = nNewGuYu
  186. end
  187. ----------------------------------------- 外部调用 -------------------------------------
  188. function Init()
  189. tCardPrize = {}
  190. local tConfig = WeekActCard_GetConfig()
  191. if not tConfig then
  192. return false
  193. end
  194. for nID, v in pairs(tConfig) do
  195. local nType = v.nType
  196. if not tCardPrize[nType] then
  197. tCardPrize[nType] = {}
  198. end
  199. local nTrueID = nID % WeekLoopActDef.WEEKACT_CARD_PRIZEID_LEN
  200. tCardPrize[nType][nTrueID] = v
  201. end
  202. return true
  203. end
  204. -- 重置数据
  205. function WeekActCard_ResetData(human)
  206. if not human then
  207. return
  208. end
  209. -- 先更新抽卡类型
  210. WeekActCard_ResetCardType(human)
  211. -- 重置抽卡数量
  212. WeekActCard_ResetCardNum(human)
  213. -- 重置奖励信息
  214. if false == WeekActCard_ResetPrize(human) then
  215. print("[WeekActCard_ResetData] 重置抽卡奖励数据失败")
  216. return
  217. end
  218. WeekActCard_WriteLog(human, "[WeekActCard_ResetData]", "抽卡奖励相关重置完成")
  219. print("[WeekActCard_ResetData] 抽卡 数据重置结束 ")
  220. end
  221. -- 是否有红点
  222. function isRed(human)
  223. local tPrize = WeekActCard_GetConfigByHuman(human)
  224. if not tPrize then
  225. return false
  226. end
  227. for nID, v in pairs(tPrize) do
  228. if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActCard_GetPrizeStatus(human, nID) then
  229. return true
  230. end
  231. end
  232. return false
  233. end
  234. -- 抽卡回调
  235. function WeekActCard_UseCard(human, nAddNum, nType)
  236. if not human or 0 >= nAddNum then
  237. return
  238. end
  239. local nNowType = WeekActCard_GetCardType(human)
  240. if nNowType ~= nType then
  241. return
  242. end
  243. local DBID = human.db._id
  244. local szName = human.db.name
  245. local tConfig = WeekActCard_GetConfigByHuman(human)
  246. if not tConfig then
  247. print("[WeekActCard_UseCard] 获取不到配置 DBID = "..DBID.." name = "..szName)
  248. return
  249. end
  250. WeekActCard_SetCardNum(human, nAddNum)
  251. local nNowCard = WeekActCard_GetCardNum(human)
  252. if -1 >= nNowCard then
  253. WeekActCard_WriteLog(human, "[WeekActCard_UseCard]", "获取玩家当前抽卡次数失败 nAddNum = "..nAddNum)
  254. print("[WeekActCard_UseCard] 获取不到抽卡次数 DBID = "..DBID.." name = "..szName.." nAddNum = "..nAddNum)
  255. return
  256. end
  257. local bChange = false
  258. for nID, v in pairs(tConfig) do
  259. local nStatus = WeekActCard_GetPrizeStatus(human, nID)
  260. if nNowCard >= v.nNum and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
  261. bChange = true
  262. WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
  263. end
  264. end
  265. if true == bChange then
  266. WeekActCard_Query(human)
  267. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  268. end
  269. end
  270. ----------------------------------------- 客户端请求 -------------------------------------
  271. -- 客户端请求-抽卡信息
  272. function WeekActCard_Query(human)
  273. if not human then
  274. return
  275. end
  276. local DBID = human.db._id
  277. local szName = human.db.name
  278. local tConfig = WeekActCard_GetConfigByHuman(human)
  279. if not tConfig then
  280. print("[WeekActCard_Query] 获取不到配置 DBID = "..DBID.." name = "..szName)
  281. return
  282. end
  283. local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_CARDQUERY
  284. tMsgData.byType = WeekActCard_GetCardType(human)
  285. tMsgData.nNowNum = WeekActCard_GetCardNum(human)
  286. tMsgData.list[0] = 0
  287. for nID, v in pairs(tConfig) do
  288. tMsgData.list[0] = tMsgData.list[0] + 1
  289. local tData = tMsgData.list[tMsgData.list[0]]
  290. tData.nNum = v.nNum
  291. tData.nID = nID
  292. tData.desc = v.szdesc
  293. tData.nState = WeekActCard_GetPrizeStatus(human, nID)
  294. tData.item[0] = #v.prize
  295. for nIndex, tItem in ipairs(v.prize) do
  296. local nGoodsID = tItem[1]
  297. local nGoodsNum = tItem[2]
  298. Grid.makeItem(tData.item[nIndex], nGoodsID, nGoodsNum)
  299. end
  300. end
  301. WeekActCard_SendData(tMsgData, human.fd)
  302. end
  303. -- 客户端请求领取奖励
  304. function WeekActCard_GetPrize(human)
  305. if not human then
  306. return
  307. end
  308. local DBID = human.db._id
  309. local szName = human.db.name
  310. local tConfig = WeekActCard_GetConfigByHuman()
  311. if not tConfig then
  312. print("[WeekActCard_GetPrize] 获取不到配置 DBID = "..DBID.." name = "..szName)
  313. return
  314. end
  315. local tItemList = {}
  316. for nID, v in pairs(tConfig) do
  317. if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActCard_GetPrizeStatus(human, nID) then
  318. if false == WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
  319. print("[WeekActCard_GetPrize] 奖励领取失败 nID = "..nID)
  320. WeekActCard_WriteLog(human, "[WeekActCard_GetPrize]", "领取奖励, 设置奖励状态失败 nID = "..nID)
  321. break
  322. end
  323. for _, data in ipairs(v.prize) do
  324. local nItemID = data[1]
  325. local nItemNum = data[2]
  326. tItemList[nItemID] = tItemList[nItemID] or 0
  327. tItemList[nItemID] = tItemList[nItemID] + nItemNum
  328. end
  329. end
  330. end
  331. if nil ~= _G.next(tItemList) then
  332. local tGoodsInfo = {}
  333. for k, v in pairs(tItemList) do
  334. table.insert(tGoodsInfo, {k,v})
  335. -- 获取奖励写日志
  336. WeekActCard_WriteLog(human, "[WeekActCard_GetPrize]", "玩家获取奖励 nGoodsID = "..k.." nGoodsNum = "..v)
  337. end
  338. BagLogic.addItemList(human, tGoodsInfo, "week_loop_act")
  339. BagLogic.sendItemGetList(human, tItemList, "week_loop_act")
  340. end
  341. WeekActCard_Query(human)
  342. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  343. end