CompetitionData.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. local CompetitionData = class("CompetitionData", require("DataBase"))
  2. local ProtocalDataNormal = require("ProtocalDataNormal")
  3. local rankProcessParam, rankProcessParam1 = 0.75, 2000
  4. function CompetitionData:ctor()
  5. self.data = {}
  6. end
  7. function CompetitionData:InitCurCompetitionData(data)
  8. local val = GlobalConfig.Instance:GetConfigStrValue(178)
  9. if val ~= "" and val ~= nil then
  10. local t = CommonUtil.DeserializeGlobalStrToTable(val)
  11. if t and t[1] then
  12. rankProcessParam = t[1][1] and tonumber(t[1][1])*0.0001 or 0.75
  13. rankProcessParam1 = t[1][2] and tonumber(t[1][2]) or 2000
  14. end
  15. end
  16. if data == nil then return end
  17. self.data.curCompetitionData = ProtocalDataNormal.ParseCompetitionData(data.cur_competition)
  18. self.data.rewardCompetitiGetIdxByIdonId = data.reward_competition_id
  19. self.data.rewardCompetitionSubId = data.reward_competition_sub_id
  20. self.data.SeasonItemList = self:GetAllSeasonItem()
  21. --LogError(Inspect(self.data.curCompetitionData))
  22. end
  23. function CompetitionData:GetAllSeasonItem()
  24. local list = {}
  25. local data = ManagerContainer.CfgMgr:GetCompetitionData()
  26. for k, v in pairs(data) do
  27. local itemList = v.HuodongItem
  28. if itemList and #itemList > 0 then
  29. for i = 1, #itemList do
  30. if list[v.CompetitionId] == nil then
  31. list[v.CompetitionId] = {}
  32. end
  33. if list[v.CompetitionId][itemList[i]] == nil then
  34. list[v.CompetitionId][itemList[i]] = itemList[i]
  35. end
  36. end
  37. end
  38. end
  39. return list
  40. end
  41. function CompetitionData:IsCurCompetitionItem(cfgid)
  42. local curid = self:GetCurCompetitionId()
  43. return self.data.SeasonItemList[curid] and self.data.SeasonItemList[curid][cfgid]
  44. end
  45. function CompetitionData:QueryCurCompetitionReq(ignore)
  46. if not ignore then
  47. local canOpen = self:CanOpenSeasonUI()
  48. if not canOpen then
  49. self._refreshSeason = false
  50. return
  51. end
  52. end
  53. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_COMPETITION_REQ, {competition_id = 0})
  54. end
  55. function CompetitionData:RefreshCompetitionData(data)
  56. if data == nil then return end
  57. local unlockState = ManagerContainer.UIFuncUnlockMgr:GetFuncLockStatusById(10)
  58. if not unlockState then return end
  59. local oldRank = self:GetCurSectionRank()
  60. self.data.curCompetitionData = ProtocalDataNormal.ParseCompetitionData(data.competition_data)
  61. self.data.nextCompetitionTime = data.next_competition_time
  62. if data.data then
  63. ManagerContainer.DataMgr.IdolData:InitRewardData(data.data)
  64. end
  65. self:RefreshCurCompetitionStage()
  66. local newRank = self:GetCurSectionRank()
  67. local type = 0
  68. if oldRank > 0 then
  69. if oldRank > newRank then
  70. type = 1
  71. elseif oldRank < newRank then
  72. type = 2
  73. end
  74. end
  75. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_RANK_CHANGED_NTF, type)
  76. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_EXCHANGE_NTF_MAIN)
  77. end
  78. --当前赛季是否结束
  79. function CompetitionData:IsOveerCurSeason()
  80. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  81. local curCompetitionData = self:GetCurCompetitionData()
  82. if curTime >= curCompetitionData.curEndTime then
  83. return true
  84. end
  85. return false
  86. end
  87. function CompetitionData:RefreshCurCompetitionStage()
  88. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  89. local nextTime = self:GetNextSeasonStartTime()
  90. self.nextSmallStageTime = nil
  91. local curCompetitionData = self:GetCurCompetitionData()
  92. if curCompetitionData.competitionId == 0 then
  93. self.stage = 1
  94. self.nextStageTime = nextTime
  95. self.curCompetitionId = 1
  96. elseif curCompetitionData.competitionId > 0 then
  97. if curTime <= curCompetitionData.curEndTime then
  98. self.stage = 2
  99. self.nextSmallStageTime = nil
  100. local idx = self:GetCurPeriodIdx()
  101. if idx then
  102. local periodData = self:GetCurPeriodData(idx)
  103. if periodData then
  104. self.nextSmallStageTime = periodData.rewardEndTme
  105. end
  106. end
  107. if not self.nextSmallStageTime then
  108. self.nextSmallStageTime = curCompetitionData.curEndTime
  109. end
  110. self.nextStageTime = curCompetitionData.curEndTime
  111. self.curCompetitionId = curCompetitionData.competitionId
  112. local itemData = ManagerContainer.CfgMgr:GetCompetitionDatasByType(self.curCompetitionId)
  113. if itemData and itemData[1] then
  114. self.uiJumpId = itemData[1].UIJumpId
  115. end
  116. else
  117. self.stage = 3
  118. self.nextStageTime = nextTime
  119. self.curCompetitionId = curCompetitionData.competitionId
  120. if nextTime > 0 then
  121. local seasons = ManagerContainer.CfgMgr:GetCompetitionDatasByType(curCompetitionData.competitionId + 1)
  122. if #seasons > 0 then
  123. local cfgData = seasons[1]
  124. if cfgData.PrepareTime == -1 then
  125. self.stage = 1
  126. else
  127. local hours = cfgData.PrepareTime * 3600*1000
  128. local previewTime = nextTime - hours
  129. if curTime >= previewTime and curTime < nextTime then
  130. self.stage = 1
  131. self.curCompetitionId = curCompetitionData.competitionId + 1
  132. elseif curTime < previewTime then
  133. self.nextStageTime = previewTime
  134. end
  135. end
  136. end
  137. end
  138. end
  139. end
  140. end
  141. function CompetitionData:GetCurStageAndNextTime(needRefresh)
  142. if needRefresh then
  143. self:RefreshCurCompetitionStage()
  144. end
  145. return self.stage, self.nextStageTime, self.nextSmallStageTime
  146. end
  147. function CompetitionData:GetCurCompetitionId()
  148. --LogError("========curCompetitionId======"..self.curCompetitionId)
  149. return self.curCompetitionId
  150. end
  151. function CompetitionData:GetUIJumpId()
  152. return self.uiJumpId or 0
  153. end
  154. function CompetitionData:GetUIJumpState()
  155. return self.uiJumpState or 0
  156. end
  157. function CompetitionData:SetUIJumpState(state)
  158. self.uiJumpState = state
  159. end
  160. function CompetitionData:GetCurSeasonTitle()
  161. local title = I18N.T("None")
  162. local titleIcon = nil;
  163. local competitionTitleIcon = nil;
  164. --local paramsList = self.data.curCompetitionData.paramList
  165. --title, titleIcon, competitionTitleIcon = CommonUtil.GetCurSeasonTitle(paramsList);
  166. return title, titleIcon, competitionTitleIcon
  167. end
  168. function CompetitionData:GetCurCompetitionData()
  169. return self.data.curCompetitionData
  170. end
  171. function CompetitionData:GetCurMaxSeason()
  172. return self.data.curCompetitionData.competitionId
  173. end
  174. function CompetitionData:GetNextSeasonStartTime()
  175. return self.data.nextCompetitionTime or 0
  176. end
  177. function CompetitionData:GetRankPercent()
  178. local totalRank = self.data.curCompetitionData.totalRank
  179. local selfRank = self.data.curCompetitionData.selfRank
  180. if selfRank == 0 or totalRank == 0 then
  181. return 0
  182. end
  183. if selfRank == 1 then
  184. return 100
  185. end
  186. --1- 实际排名 * 0.75 / MAX(总人数,500)
  187. return (1 - (selfRank*rankProcessParam)/math.max(totalRank, rankProcessParam1)) * 100
  188. end
  189. function CompetitionData:GetRank()
  190. return self.data and self.data.curCompetitionData and self.data.curCompetitionData.selfRank or 0
  191. end
  192. function CompetitionData:GetScore()
  193. return self.data and self.data.curCompetitionData and self.data.curCompetitionData.comParam or 0
  194. end
  195. function CompetitionData:SetRankAndScore(data)
  196. if self.data and self.data.curCompetitionData then
  197. self.data.curCompetitionData.comParam = data.score
  198. self.data.curCompetitionData.selfRank = data.rank
  199. end
  200. end
  201. function CompetitionData:GetCurPeriodIdx()
  202. local curCompetitionData = self.data.curCompetitionData
  203. if not curCompetitionData then return end
  204. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  205. for i = 1, #curCompetitionData.rewardInfoList do
  206. local v = curCompetitionData.rewardInfoList[i]
  207. if v.rewardEndTme > curTime then
  208. return v.rewardIdx
  209. end
  210. end
  211. return nil
  212. end
  213. function CompetitionData:GetCurPeriodData(idx)
  214. local curCompetitionData = self.data.curCompetitionData
  215. if not curCompetitionData then return end
  216. for _,v in pairs(curCompetitionData.rewardInfoList) do
  217. if v.rewardIdx == idx then
  218. return v
  219. end
  220. end
  221. return nil
  222. end
  223. function CompetitionData:GetCurSectionRank()
  224. local idx = self:GetCurPeriodIdx()
  225. local periodData = self:GetCurPeriodData(idx)
  226. return periodData and periodData.paramList[1] or 0
  227. end
  228. function CompetitionData:SendExchangeScoreReq(itemList)
  229. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_COMPETITION_SCORE_REQ, {item_list = itemList})
  230. end
  231. --这个标记说明是不是仅仅数刷新赛季数据,如果是true那么只刷新数据,不打开界面
  232. function CompetitionData:SetRefreshSeasonState(_state)
  233. self._refreshSeason = _state;
  234. end
  235. function CompetitionData:RefreshSecondData(data)
  236. local oldRank = self:GetCurSectionRank()
  237. local curCompetitionData = self.data.curCompetitionData
  238. curCompetitionData.comParam = data.com_score
  239. curCompetitionData.nextComScore = data.next_com_score
  240. curCompetitionData.lastComScore = data.last_com_score
  241. curCompetitionData.selfRank = data.self_rank
  242. curCompetitionData.totalRank = data.total_rank
  243. local idx = self:GetCurPeriodIdx()
  244. local periodData = self:GetCurPeriodData(idx)
  245. if not periodData then
  246. return
  247. end
  248. periodData.paramList[1] = data.section_id
  249. local newRank = data.section_id
  250. local type = 0
  251. if oldRank > newRank then
  252. type = 1
  253. elseif oldRank < newRank then
  254. type = 2
  255. end
  256. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_RANK_CHANGED_NTF, type)
  257. end
  258. function CompetitionData:RegisterNetEvents()
  259. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_COMPETITION_NTF, function(data)
  260. self:RefreshCompetitionData(data)
  261. end)
  262. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_COMPETITION_ACK, function(data)
  263. if data.error == Enum.NetErrorCode.ERROR_OK then
  264. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_OPEN_NTF, data.competition_id, self._refreshSeason)
  265. self._refreshSeason = false; --这个值是事先设置的,要想他生效每次调用的时候都要设置一下,然后这里返回消息之后自动设置未false
  266. end
  267. end)
  268. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_COMPETITION_SCORE_ACK, function(data)
  269. if data.error == Enum.NetErrorCode.ERROR_OK then
  270. local oldScore = self.data.curCompetitionData.comParam
  271. self:RefreshSecondData(data)
  272. local newScore = self.data.curCompetitionData.comParam
  273. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_EXCHANGE_NTF, newScore - oldScore)
  274. end
  275. end)
  276. end
  277. function CompetitionData:CanOpenSeasonUI()
  278. local curSeasonData = self:GetCurCompetitionData()
  279. if curSeasonData == nil then return false end
  280. local endTime = curSeasonData.curEndTime
  281. local curServerTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  282. if endTime > 0 and endTime < curServerTime then
  283. local delta = (curServerTime - endTime)/1000
  284. local minutes = ManagerContainer.LuaTimerMgr:TransSeconds2Minutes(delta)
  285. if minutes < 5 then
  286. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("DscSeasonResult8")
  287. return false
  288. end
  289. end
  290. return true
  291. end
  292. function CompetitionData:UpdateCompetitionTime()
  293. local unlockState = ManagerContainer.UIFuncUnlockMgr:GetFuncLockStatusById(10)
  294. if not unlockState then return end
  295. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  296. local nextTime = self:GetNextSeasonStartTime()
  297. local curCompetitionData = self:GetCurCompetitionData()
  298. if curCompetitionData.competitionId > 0 and curTime > curCompetitionData.curEndTime and nextTime == 0 then
  299. return
  300. end
  301. local stage, nextTime1 = self:GetCurStageAndNextTime()
  302. if stage == nil then
  303. self:SetRefreshSeasonState(true)
  304. self:QueryCurCompetitionReq(true)
  305. return
  306. end
  307. if curTime >= nextTime1 then
  308. self:SetRefreshSeasonState(true)
  309. self:QueryCurCompetitionReq(true)
  310. end
  311. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SEASON_MINUTE_UPDATE)
  312. end
  313. function CompetitionData:Clear()
  314. self.data = {}
  315. end
  316. function CompetitionData:Destroy()
  317. if self.Clear then
  318. self:Clear()
  319. end
  320. self:UnRegisterNetEvents()
  321. end
  322. function CompetitionData:UnRegisterNetEvents()
  323. end
  324. return CompetitionData