WeekendLoopActCard.lua 13 KB

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