WeekendLoopActCard.lua 12 KB

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