ServerCommerceManager.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. --------------------------------
  2. -- 文件名 : ServerCommerceActManger.lua
  3. -- 文件说明 : 跨服商业-活动模板管理
  4. -- 创建时间 : 2025/03/26
  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 MailManager = require("mail.MailManager")
  14. local BagLogic = require("bag.BagLogic")
  15. local Grid = require("bag.Grid")
  16. local PanelDefine = require("broadcast.PanelDefine")
  17. local CommonDB = require("common.CommonDB")
  18. local BuyLogic = require("topup.BuyLogic")
  19. local GuideLogic = require("guide.GuideLogic")
  20. local Log = require("common.Log")
  21. local YunYingLogic = require("yunying.YunYingLogic")
  22. local ServerCommerceCof = require("excel.ServerCommerce")
  23. local ServerCommerceActDefine = require("serverCommerce.ServerCommerceActDefine")
  24. local Timer = require("core.Timer")
  25. local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle")
  26. local ServerCommerceActCharge = require("serverCommerce.ServerCommerceActCharge")
  27. local ServerCommerceActRank = require("serverCommerce.ServerCommerceActRank")
  28. -- 活动信息
  29. tCommerceActInfo = nil
  30. -- {
  31. -- nStartTime = nil, -- 开始时间
  32. -- nEendTime = nil, -- 结束时间
  33. -- isRun = nil, -- 是否在活动中
  34. -- nOpen = nil, -- 是否打开
  35. -- nBatchID = nil, -- 批次
  36. -- }
  37. -- 加载的模块
  38. tCommerceActModuel = {}
  39. ----------------------------------------- 内部处理开始 -------------------------------------
  40. -- 下发数据
  41. local function CommerceAct_SendData(tMsgData, fd)
  42. Msg.send(tMsgData, fd)
  43. end
  44. -- 创建玩家DB数据
  45. function CommerceAct_CreateHumanDB(human)
  46. local nBatchID = 1
  47. local tCommonDBData = CommonDB.GetCommerceActInfo()
  48. if tCommonDBData and nil ~= _G.next(tCommonDBData) then
  49. nBatchID = tCommonDBData.nBatchID
  50. end
  51. human.db.ServerCommerce =
  52. {
  53. nPoint = 0, -- 当前玩家个人积分
  54. Task = {}, -- 任务信息
  55. Charge = {}, -- 连充豪礼
  56. Shop = {}, -- 战区钜惠
  57. nBatchID = nBatchID, -- 批次
  58. bSendPlayerMail = false, -- 是否发送个人邮件
  59. bSendServerMail = false, -- 是否发送全服邮件
  60. }
  61. for nID, module in pairs(tCommerceActModuel) do
  62. if module and module.CreatDB then
  63. local bRet = module.CreatDB(human)
  64. if false == bRet then
  65. print("[CommerceAct_CreateHumanDB] nID 初始化DB 数据失败 nID = "..nID)
  66. end
  67. end
  68. end
  69. end
  70. -- 下发活动数据
  71. function CommerceAct_SendActInfo(human)
  72. if not human or not tCommerceActInfo then
  73. return
  74. end
  75. print("[CommerceAct_SendActInfo] 下发活动数据 开始 ")
  76. local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_ALLINFO
  77. tMsgData.nStartTime = tCommerceActInfo.nStartTime
  78. tMsgData.nEendTime = tCommerceActInfo.nEendTime
  79. tMsgData.tActID[0] = #ServerCommerceCof.CommerceAct
  80. local nIndex = 1
  81. for id, v in pairs(ServerCommerceCof.CommerceAct) do
  82. local tActData = tMsgData.tActID[nIndex]
  83. nIndex = nIndex + 1
  84. tActData.ID = id
  85. tActData.name = v.name
  86. tActData.nSortID = v.sortID
  87. tActData.nIcon = v.icon
  88. tActData.nPanelID = v.panelID
  89. local bRed = false
  90. if tCommerceActModuel[id] and tCommerceActModuel[id].isRed then
  91. bRed = tCommerceActModuel[id].isRed(human)
  92. end
  93. print("[CommerceAct_SendActInfo] name = "..tActData.name)
  94. tActData.nRed = (bRed == true) and 1 or 0
  95. end
  96. YunYingLogic.sendBanner(human)
  97. CommerceAct_SendData(tMsgData, human.fd)
  98. print("[CommerceAct_SendActInfo] 下发活动数据 结束 ")
  99. end
  100. -- 各个子活动重置活动数据
  101. local function CommerceAct_ResetData(human)
  102. if not human then
  103. return
  104. end
  105. end
  106. -- 各个子活动处理结束数据
  107. local function CommerceAct_HandleEndData(human)
  108. if not human then
  109. return
  110. end
  111. end
  112. -- 所有活动初始化数据开始
  113. local function CommerceAct_BeginAllAct(human)
  114. if not human or not tCommerceActInfo then
  115. return
  116. end
  117. CommerceAct_CreateHumanDB(human)
  118. print("[CommerceAct_BeginAllAct] 所有活动初始化数据开始 ")
  119. end
  120. -- 结束所有活动
  121. local function CommerceAct_EndAllAct(human)
  122. -- 各个子活动处理数据
  123. CommerceAct_HandleEndData(human)
  124. end
  125. -- 活动开始
  126. local function CommerceAct_Begin()
  127. if not tCommerceActInfo then
  128. return
  129. end
  130. for nID, module in pairs(tCommerceActModuel) do
  131. if module and module.ClearCache then
  132. module.ClearCache()
  133. end
  134. end
  135. -- 遍历在线玩家
  136. for uuid, human in pairs(ObjHuman.onlineUuid) do
  137. CommerceAct_BeginAllAct(human)
  138. end
  139. end
  140. -- 活动结束
  141. local function CommerceAct_End()
  142. -- 排行榜结束
  143. ServerCommerceMiddle.CommerceMiddle_QueryServerRank(ServerCommerceActDefine.COMMERCEACT_SENDSERVERMAIL)
  144. end
  145. -- 延迟向中心服请求数据
  146. local function CommerceAct_LaterTimeQuery(nOperate)
  147. print("[CommerceAct_LaterTimeQuery] 延迟向中心服请求数据开始")
  148. Timer.addLater(ServerCommerceActDefine.COMMERCEACT_INITSERVERTIME, CommerceAct_GetActOpen, nOperate)
  149. end
  150. -- 延迟10分钟开始
  151. local function CommerceAct_LaterBeginAct()
  152. if not tCommerceActInfo then
  153. return
  154. end
  155. tCommerceActInfo.isRun = true
  156. CommerceAct_Begin()
  157. end
  158. -- 检查活动是否过期
  159. local function CommerceAct_CheckActIsOverTime()
  160. local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
  161. if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
  162. return true
  163. end
  164. local nEndTime = tCommerceDBInfo.nEndTime
  165. local nNowTime = os.time()
  166. return nNowTime >= nEndTime
  167. end
  168. -- 检查是否开启下一轮活动
  169. local function CommerceAct_CanOpenNext()
  170. local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
  171. if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
  172. return true
  173. end
  174. local nDiffDay = Util.diffDay(tCommerceDBInfo.nEndTime)
  175. if nDiffDay >= ServerCommerceActDefine.COMMERCEACT_NEXTDAY then
  176. return true
  177. end
  178. return false
  179. end
  180. -- 创建通用DB数据
  181. local function CommerceAct_CreateCommonDB()
  182. local nNowTime = os.time()
  183. local tCommerceInfo = CommonDB.GetCommerceActInfo()
  184. local nEndTime = nNowTime + ServerCommerceActDefine.COMMERCEACT_LASTDAY * 86400
  185. local tEndDate = os.date("*t",nEndTime)
  186. tEndDate.hour = ServerCommerceActDefine.COMMERCEACT_ENDTIME
  187. tEndDate.min = 0
  188. tEndDate.sec = 0
  189. nEndTime = os.time(tEndDate)
  190. local tDBData = {
  191. nBeginTime = nNowTime,
  192. nEndTime = nEndTime,
  193. nPoint = 0,
  194. nSendRankMail = false,
  195. }
  196. if not tCommerceInfo or nil == _G.next(tCommerceInfo) then
  197. tDBData.nBatchID = 1
  198. else
  199. tDBData.nBatchID = tDBData.nBatchID + 1
  200. end
  201. CommonDB.SetCommerceActInfo(tDBData)
  202. end
  203. -- 创建缓存数据
  204. local function CommerceAct_CreateCacheInfo(nOpen, bLater)
  205. local tCommerceInfo = CommonDB.GetCommerceActInfo()
  206. if not tCommerceInfo or nil == _G.next(tCommerceInfo) then
  207. print("[CommerceAct_CreateCacheInfo] 为什么会不存在数据\n")
  208. return
  209. end
  210. tCommerceActInfo = {}
  211. tCommerceActInfo.nStartTime = tCommerceInfo.nBeginTime
  212. tCommerceActInfo.nEendTime = tCommerceInfo.nEndTime
  213. tCommerceActInfo.isRun = bLater == true and false or true
  214. tCommerceActInfo.nOpen = nOpen
  215. tCommerceActInfo.nBatchID = tCommerceInfo.nBatchID
  216. print("[CommerceAct_CreateCacheInfo] 创建缓存数据 nStartTime = "..tCommerceActInfo.nStartTime.." nEendTime = "..tCommerceActInfo.nEendTime
  217. .." nOpen = "..tCommerceActInfo.nOpen.." nBatchID = "
  218. ..tCommerceActInfo.nBatchID.." isRun = "..(tCommerceActInfo.isRun == true and 1 or 0))
  219. if bLater == true then
  220. Timer.addLater(ServerCommerceActDefine.COMMERCEACT_BEGINDELATTIME, CommerceAct_LaterBeginAct)
  221. end
  222. end
  223. -- 清理排行榜数据
  224. local function CommerceAct_ClearRankInfo()
  225. ServerCommerceMiddle.CommerceMiddle_ClearRank()
  226. end
  227. -- 起服初始化数据
  228. local function CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate)
  229. if ServerCommerceActDefine.COMMERCEACT_NOOPEN ~= nOpen then
  230. local tCommerceInfo = CommonDB.GetCommerceActInfo()
  231. -- 不存在数据
  232. if not tCommerceInfo or nil == _G.next(tCommerceInfo) then
  233. print("[CommerceAct_ActCheckOpen_InitServer] 不存在commonDB数据开始创建")
  234. CommerceAct_CreateCommonDB()
  235. else
  236. -- 存在数据检查是否结束
  237. if true == CommerceAct_CheckActIsOverTime() then
  238. local bOpenNext = CommerceAct_CanOpenNext()
  239. if true == bOpenNext then
  240. -- 开启下一轮
  241. if tCommerceInfo.nBatch < ServerCommerceActDefine.COMMERCEACT_ENDBATCH then
  242. CommerveManager_WriteLog("[CommerveManager-onZeroAll] 当前条件满足开启下一轮活动")
  243. CommerceAct_CreateCommonDB()
  244. -- 清理排行榜数据
  245. CommerceAct_ClearRankInfo()
  246. end
  247. end
  248. end
  249. end
  250. local bLater = nOperate == ServerCommerceActDefine.COMMERCEACT_ZERO and true or false
  251. CommerceAct_CreateCacheInfo(nOpen, bLater)
  252. ServerCommerceActRank.CommercerActRank_InitServerQueryRank()
  253. end
  254. end
  255. -- 获取玩家当前点数
  256. local function CommerceAct_GetHumanPoint(human)
  257. return human.db.ServerCommerce.nPoint
  258. end
  259. -- 设置玩家当前点数
  260. local function CommerceAct_SetHumanPoint(human, nValue)
  261. human.db.ServerCommerce.nPoint = nValue
  262. end
  263. ----------------------------------------- 外部调用开始 -------------------------------------
  264. function onZeroAll(funcID)
  265. local nNowTime = os.time()
  266. local tDate = os.date("*t",nNowTime)
  267. -- 活动未开启
  268. if tDate.hour == ServerCommerceActDefine.COMMERCEACT_BEGINTIME then
  269. -- 获取DB数据
  270. local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
  271. if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
  272. CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_ZERO)
  273. return
  274. end
  275. -- 存在DB数据说明已经满足条件了
  276. if true == CommerceAct_CheckActIsOverTime() then
  277. local bOpenNext = CommerceAct_CanOpenNext()
  278. if false == bOpenNext then
  279. return
  280. end
  281. -- 开启下一轮
  282. if tCommerceDBInfo.nBatch < ServerCommerceActDefine.COMMERCEACT_ENDBATCH then
  283. CommerveManager_WriteLog("[CommerveManager-onZeroAll] 当前条件满足开启下一轮活动")
  284. CommerceAct_CreateCommonDB()
  285. -- 存在数据说明时间是满足的
  286. CommerceAct_CreateCacheInfo(ServerCommerceActDefine.COMMERCEACT_OPEN, true)
  287. -- 清理排行榜数据
  288. CommerceAct_ClearRankInfo()
  289. end
  290. end
  291. elseif tDate.hour == ServerCommerceActDefine.COMMERCEACT_ENDTIME then
  292. if not tCommerceActInfo or tCommerceActInfo.nOpen == ServerCommerceActDefine.COMMERCEACT_NOOPEN then
  293. return
  294. end
  295. local tCommerceDBInfo = CommonDB.GetCommerceActInfo()
  296. if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then
  297. print("[CommerceAct_onZeroAll] 存在缓存数据, 但是获取不到DB数据")
  298. return
  299. end
  300. local nEndTime = tCommerceDBInfo.nEndTime
  301. if nEndTime <= nNowTime then
  302. -- 活动结束处理
  303. CommerceAct_End()
  304. tCommerceActInfo.nSendRankMail = true
  305. end
  306. end
  307. end
  308. -- 请求活动打开信息
  309. function CommerceAct_GetActOpen(nOperate)
  310. print("[CommerceAct_GetActOpen] 开始向中心服请求活动数据")
  311. ServerCommerceMiddle.CommerceMiddle_IsActOpen(nOperate)
  312. end
  313. -- 中心服回复活动信息
  314. function CommerceAct_ActCheckOpen(nOpen, nOperate)
  315. print("[CommerceAct_ActCheckOpen] 收到活动信息开始处理 nOpen = "..nOpen.." nOperate ="..nOperate)
  316. CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate)
  317. end
  318. -- 起服初始化
  319. function CommerceAct_Init()
  320. if _G.is_middle == true then
  321. -- 中心服起服获取信息
  322. ServerCommerceMiddle.CommerceMiddle_InitServer()
  323. return
  324. end
  325. local nNowTime = os.time()
  326. for nID, v in pairs(ServerCommerceCof.CommerceAct) do
  327. if v.moduleFn then
  328. local moduleFn = load("return require(\"" .. v.moduleFn .. "\")")()
  329. if moduleFn then
  330. tCommerceActModuel[nID] = moduleFn
  331. if moduleFn.Init then
  332. local bRet = moduleFn.Init()
  333. if false == bRet then
  334. print("[CommerceAct_Init] 初始化模块数据失败 nID ".. nID)
  335. end
  336. end
  337. print("[CommerceAct_Init] 加载模块成功 name = "..v.name)
  338. else
  339. print("[CommerceAct_Init] 加载模块失败 nID ".. nID)
  340. end
  341. else
  342. print("[CommerceAct_Init] 未配置 name = "..v.name)
  343. end
  344. end
  345. print("[CommerceAct_Init] 延迟进行请求数据操作")
  346. -- 延迟向中心服请求数据
  347. CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_SERVEROPEN)
  348. end
  349. -- 是否还在活动期间
  350. function CommerceAct_IsRun()
  351. if not tCommerceActInfo then
  352. return false
  353. end
  354. return tCommerceActInfo.isRun
  355. end
  356. -- 玩家登录
  357. function onLogin(human, funcID)
  358. if not human then
  359. return
  360. end
  361. -- 下发活动基础数据
  362. if true == CommerceAct_IsRun() then
  363. local tCommonDBData = CommonDB.GetCommerceActInfo()
  364. if not tCommonDBData or nil == _G.next(tCommonDBData) then
  365. print("[onLogin] 玩家登录不存在对应的数据通用DB数据!!!!")
  366. return
  367. end
  368. -- 不存在数据直接创建
  369. if not human.db.ServerCommerce then
  370. CommerceAct_CreateHumanDB(human)
  371. else
  372. -- 用记在玩家身上的批次 和 CommonDB 中的数据比较
  373. local nOldBatchID = human.db.ServerCommerce.nBatchID
  374. local nNowBatchID = tCommonDBData.nBatchID
  375. if nNowBatchID ~= nOldBatchID then
  376. CommerceAct_CreateHumanDB(human)
  377. end
  378. end
  379. CommerceAct_SendActInfo(human)
  380. for _, module in pairs(tCommerceActModuel) do
  381. if module and module.onLogin then
  382. module.onLogin(human)
  383. end
  384. end
  385. else
  386. -- 不在活动中
  387. local tCommonDBData = CommonDB.GetCommerceActInfo()
  388. if not tCommonDBData or nil == _G.next(tCommonDBData) then
  389. print("[onLogin] 玩家登录不存在对应的数据通用DB数据!!!!")
  390. return
  391. end
  392. -- 不存在对应DB数据
  393. if not human.db.ServerCommerce then
  394. return
  395. end
  396. -- 未发送个人邮件都是未上榜的
  397. local bSendPlayerMail = CommerveManager_GetHumanSendPlayerMail(human)
  398. if false == bSendPlayerMail then
  399. CommerveManager_SetHumanSendPlayerMail(human, true)
  400. ServerCommerceActRank.CommercerActRank_SendHumanMailHumanLogin(human)
  401. end
  402. -- 全服邮件
  403. local bSendServerMail = CommerveManager_GetHumanSendServerMail(human)
  404. if false == bSendServerMail and true == tCommonDBData.bSendServerMail then
  405. CommerveManager_SetHumanSendServerMail(human, true)
  406. ServerCommerceActRank.CommercerActRank_SendServerMailHumanLogin(human)
  407. end
  408. end
  409. end
  410. -- 是否开启
  411. function isOpen(human, YYInfo, funcConfig)
  412. local bRet = CommerceAct_IsRun()
  413. local nRet = bRet and 1 or 0
  414. print("CommerceAct_isOpen 结束 nRet = "..nRet)
  415. return bRet
  416. end
  417. --
  418. function isActive(human, YYInfo, funcConfig)
  419. return not isOpen(human, YYInfo, funcConfig)
  420. end
  421. function isRed(human, YYInfo, funcConfig)
  422. local bRet = false
  423. for _, module in pairs(tCommerceActModuel) do
  424. if module and module.isRed then
  425. bRet = module.isRed(human)
  426. if true == bRet then
  427. break
  428. end
  429. end
  430. end
  431. return bRet
  432. end
  433. function onCharge(human, price, funcID, buyID)
  434. if not human then
  435. return
  436. end
  437. if false == CommerceAct_IsRun() then
  438. return
  439. end
  440. print("[onCharge] 跨服商业活动 玩家充值回调开始 name = "..human.db.name.." price = "..price)
  441. for _, module in pairs(tCommerceActModuel) do
  442. if module and module.onCharge then
  443. module.onCharge(human, price, funcID, buyID)
  444. end
  445. end
  446. end
  447. -- 增加整个服务器的点数
  448. function CommerveManager_AddServerPoint(nAddPoint)
  449. local tCommonDBData = CommonDB.GetCommerceActInfo()
  450. local nNewPoint = tCommonDBData.nPoint + nAddPoint
  451. CommonDB.SetCommerceActInfo_Point(nNewPoint)
  452. for _, module in pairs(tCommerceActModuel) do
  453. if module and module.onAllPointChange then
  454. module.onAllPointChange()
  455. end
  456. end
  457. end
  458. -- 增加个人积分
  459. function CommerveManager_AddHumanPint(human, nPoint)
  460. local nNowPoint = CommerceAct_GetHumanPoint(human)
  461. local nNewPoint = nNowPoint + nPoint
  462. CommerceAct_SetHumanPoint(human ,nNewPoint)
  463. ServerCommerceActRank.CommercerActRank_HumanPointChange(human, nNewPoint)
  464. end
  465. -- 写日志
  466. function CommerveManager_WriteLog(szLogText, human)
  467. if human then
  468. szLogText = szLogText.." name = "..human.db.name.." id = "..human.db._id
  469. end
  470. Log.write(Log.LOGID_OSS_COMMON_ACT, szLogText)
  471. end
  472. -- 设置全服发送邮件奖励
  473. function CommerveManager_SetCommDBSendMail()
  474. local bSendMail = CommonDB.GetCommerceActInfo_SendServerMail()
  475. if nil == bSendMail then
  476. return
  477. end
  478. CommonDB.SetCommerceActInfo_SendServerMail(true)
  479. end
  480. -- 获取全服发送邮件标识
  481. function CommerveManager_GetCommDBSendMail()
  482. return CommonDB.GetCommerceActInfo_SendServerMail()
  483. end
  484. -- 设置玩家已经获取了全服奖励邮件
  485. function CommerveManager_SetHumanSendServerMail(human, nValue)
  486. human.db.ServerCommerce.bSendServerMail = nValue
  487. end
  488. -- 获取玩家全服奖励邮件状态
  489. function CommerveManager_GetHumanSendServerMail(human)
  490. return human.db.ServerCommerce.bSendServerMail
  491. end
  492. -- 设置玩家已经获取了个人奖励邮件
  493. function CommerveManager_SetHumanSendPlayerMail(human, nValue)
  494. human.db.ServerCommerce.bSendPlayerMail = nValue
  495. end
  496. -- 获取玩家全服奖励邮件状态
  497. function CommerveManager_GetHumanSendPlayerMail(human)
  498. return human.db.ServerCommerce.bSendPlayerMail
  499. end