WeekendLoopActCard.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. WeekActCard_WriteLog(human, "WeekActCard_UseCard", "玩家进行了抽卡回调开始 nAddNum = "..nAddNum.." 类型 nType = "..nType)
  242. local nNowType = WeekActCard_GetCardType(human)
  243. print("[WeekActCard_UseCard] nNowType = "..nNowType.." nType = "..nType)
  244. if nNowType ~= nType then
  245. return
  246. end
  247. local DBID = human.db._id
  248. local szName = human.db.name
  249. local tConfig = WeekActCard_GetConfigByHuman(human)
  250. if not tConfig then
  251. print("[WeekActCard_UseCard] 获取不到配置 DBID = "..DBID.." name = "..szName)
  252. return
  253. end
  254. WeekActCard_SetCardNum(human, nAddNum)
  255. local nNowCard = WeekActCard_GetCardNum(human)
  256. if -1 >= nNowCard then
  257. WeekActCard_WriteLog(human, "[WeekActCard_UseCard]", "获取玩家当前抽卡次数失败 nAddNum = "..nAddNum)
  258. print("[WeekActCard_UseCard] 获取不到抽卡次数 DBID = "..DBID.." name = "..szName.." nAddNum = "..nAddNum)
  259. return
  260. end
  261. local bChange = false
  262. for nID, v in pairs(tConfig) do
  263. local nStatus = WeekActCard_GetPrizeStatus(human, nID)
  264. if nNowCard >= v.nNum and WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
  265. bChange = true
  266. local bRet = WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
  267. if false == bRet then
  268. print("[WeekActCard_UseCard] 设置失败 nID = "..nID.." nStatus = "..nStatus.." num = "..v.nNum)
  269. end
  270. end
  271. end
  272. if true == bChange then
  273. WeekActCard_Query(human)
  274. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  275. end
  276. WeekActCard_WriteLog(human, "WeekActCard_UseCard", "玩家进行了抽卡回调处理结束 nAddNum = "..nAddNum.." 类型 nType = "..nType)
  277. end
  278. -- 获取名称
  279. function GetName(human)
  280. if not human then
  281. return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
  282. end
  283. local nNowCardType = WeekActCard_GetCardType(human)
  284. if -1 >= nNowCardType then
  285. return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
  286. end
  287. if not WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[nNowCardType] then
  288. return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[1]
  289. end
  290. return WeekLoopActDef.TWEEKACT_CARD_PANELID_NAME[nNowCardType]
  291. end
  292. ----------------------------------------- 客户端请求 -------------------------------------
  293. -- 客户端请求-抽卡信息
  294. function WeekActCard_Query(human)
  295. if not human then
  296. return
  297. end
  298. local DBID = human.db._id
  299. local szName = human.db.name
  300. local tConfig = WeekActCard_GetConfigByHuman(human)
  301. if not tConfig then
  302. print("[WeekActCard_Query] 获取不到配置 DBID = "..DBID.." name = "..szName)
  303. return
  304. end
  305. local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_CARDQUERY
  306. tMsgData.byType = WeekActCard_GetCardType(human)
  307. tMsgData.nNowNum = WeekActCard_GetCardNum(human)
  308. tMsgData.list[0] = 0
  309. for nID, v in pairs(tConfig) do
  310. tMsgData.list[0] = tMsgData.list[0] + 1
  311. local tData = tMsgData.list[tMsgData.list[0]]
  312. tData.nNum = v.nNum
  313. tData.nID = nID
  314. tData.desc = v.szdesc
  315. tData.nState = WeekActCard_GetPrizeStatus(human, nID)
  316. tData.item[0] = #v.prize
  317. for nIndex, tItem in ipairs(v.prize) do
  318. local nGoodsID = tItem[1]
  319. local nGoodsNum = tItem[2]
  320. Grid.makeItem(tData.item[nIndex], nGoodsID, nGoodsNum)
  321. end
  322. end
  323. WeekActCard_SendData(tMsgData, human.fd)
  324. end
  325. -- 客户端请求领取奖励
  326. function WeekActCard_GetPrize(human)
  327. if not human then
  328. return
  329. end
  330. local DBID = human.db._id
  331. local szName = human.db.name
  332. local tConfig = WeekActCard_GetConfigByHuman(human)
  333. if not tConfig then
  334. print("[WeekActCard_GetPrize] 获取不到配置 DBID = "..DBID.." name = "..szName)
  335. return
  336. end
  337. local tItemList = {}
  338. for nID, v in pairs(tConfig) do
  339. if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActCard_GetPrizeStatus(human, nID) then
  340. if false == WeekActCard_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
  341. print("[WeekActCard_GetPrize] 奖励领取失败 nID = "..nID)
  342. WeekActCard_WriteLog(human, "[WeekActCard_GetPrize]", "领取奖励, 设置奖励状态失败 nID = "..nID)
  343. break
  344. end
  345. for _, data in ipairs(v.prize) do
  346. local nItemID = data[1]
  347. local nItemNum = data[2]
  348. tItemList[nItemID] = tItemList[nItemID] or 0
  349. tItemList[nItemID] = tItemList[nItemID] + nItemNum
  350. end
  351. end
  352. end
  353. if nil ~= _G.next(tItemList) then
  354. local tGoodsInfo = {}
  355. for k, v in pairs(tItemList) do
  356. table.insert(tGoodsInfo, {k,v})
  357. -- 获取奖励写日志
  358. WeekActCard_WriteLog(human, "[WeekActCard_GetPrize]", "玩家获取奖励 nGoodsID = "..k.." nGoodsNum = "..v)
  359. end
  360. BagLogic.addItemList(human, tGoodsInfo, "week_loop_act")
  361. BagLogic.sendItemGetList(human, tItemList, "week_loop_act")
  362. end
  363. WeekActCard_Query(human)
  364. WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
  365. end