taskmgr.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package coupontask
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "sync"
  6. "time"
  7. notification "bet24.com/servers/micros/notification/proto"
  8. )
  9. type taskmgr struct {
  10. lock *sync.RWMutex
  11. enabled bool // 是否启用
  12. videoConfigs []*videoConfig
  13. sys_tasklist []*task
  14. user_tasklist map[int]*user_task
  15. }
  16. func newTaskMgr() *taskmgr {
  17. ret := new(taskmgr)
  18. ret.lock = &sync.RWMutex{}
  19. ret.user_tasklist = make(map[int]*user_task)
  20. ret.loadVideoConfig()
  21. ret.loadSysTask()
  22. go ret.checkSwitchRefresh()
  23. return ret
  24. }
  25. func (this *taskmgr) loadVideoConfig() {
  26. list := getVideoConfig()
  27. sort.SliceStable(list, func(i, j int) bool {
  28. return list[i].PlayTimes < list[j].PlayTimes
  29. })
  30. this.lock.Lock()
  31. defer this.lock.Unlock()
  32. this.videoConfigs = list
  33. }
  34. func (this *taskmgr) loadSysTask() {
  35. list := getSysList()
  36. this.lock.Lock()
  37. defer this.lock.Unlock()
  38. this.sys_tasklist = list
  39. }
  40. func (this *taskmgr) getVideoConfigs(playTimes int) *videoConfig {
  41. this.lock.RLock()
  42. defer this.lock.RUnlock()
  43. for _, v := range this.videoConfigs {
  44. if playTimes+1 == v.PlayTimes {
  45. return v
  46. }
  47. }
  48. return &videoConfig{}
  49. }
  50. func (this *taskmgr) getVideoMaxTimes() int {
  51. this.lock.RLock()
  52. defer this.lock.RUnlock()
  53. for i := len(this.videoConfigs) - 1; i >= 0; i-- {
  54. return this.videoConfigs[i].PlayTimes
  55. }
  56. return 0
  57. }
  58. func (this *taskmgr) getTask(gameId, baseScore, players, doubleType int) *task {
  59. this.lock.RLock()
  60. defer this.lock.RUnlock()
  61. for _, v := range this.sys_tasklist {
  62. if v.GameID == gameId && v.BaseScore == baseScore && v.Players == players && v.DoubleType == doubleType {
  63. return v
  64. }
  65. }
  66. return nil
  67. }
  68. // 用户任务
  69. func (this *taskmgr) getUser(userId int) *user_task {
  70. // 判断是否开启
  71. if !this.getSwitch() {
  72. return nil
  73. }
  74. this.lock.RLock()
  75. u, ok := this.user_tasklist[userId]
  76. this.lock.RUnlock()
  77. if ok {
  78. u.checkCrossDay()
  79. return u
  80. }
  81. u = newUserTask(userId)
  82. this.lock.Lock()
  83. this.user_tasklist[userId] = u
  84. this.lock.Unlock()
  85. return u
  86. }
  87. // 信息及完成列表
  88. func (this *taskmgr) getUserTask(userId int) (bool, *BaseInfo, []*UserTask) {
  89. u := this.getUser(userId)
  90. if u == nil {
  91. return false, nil, nil
  92. }
  93. // 获取开关
  94. enabled := this.getSwitch()
  95. // 获取用户信息及列表
  96. info, list := u.getUserTask()
  97. return enabled, info, list
  98. }
  99. // 修改基础上限(通过收徒)
  100. func (this *taskmgr) updateBaseLimit(userId, baseLimit int) {
  101. u := this.getUser(userId)
  102. if u == nil {
  103. return
  104. }
  105. u.updateBaseLimit(baseLimit)
  106. }
  107. // 修改临时上限
  108. func (this *taskmgr) updateTmpLimit(userId int) int {
  109. u := this.getUser(userId)
  110. if u == nil {
  111. return 0
  112. }
  113. return u.updateTmpLimit()
  114. }
  115. // 触发任务
  116. func (this *taskmgr) trigger(userId, gameId, baseScore, players, doubleType int) {
  117. u := this.getUser(userId)
  118. if u == nil {
  119. return
  120. }
  121. u.trigger(gameId, baseScore, players, doubleType)
  122. }
  123. // 领取奖励
  124. func (this *taskmgr) award(userId, userTaskId int) *award_resp {
  125. u := this.getUser(userId)
  126. if u == nil {
  127. return &award_resp{}
  128. }
  129. return u.award(userTaskId)
  130. }
  131. // 检查开关
  132. func (this *taskmgr) checkSwitchRefresh() {
  133. this.checkSwitch()
  134. t := time.NewTicker(20 * time.Second)
  135. for {
  136. select {
  137. case <-t.C:
  138. this.checkSwitch()
  139. }
  140. }
  141. }
  142. func (this *taskmgr) checkSwitch() {
  143. enabled := getSwitchInfo()
  144. if this.enabled == enabled {
  145. return
  146. }
  147. this.lock.Lock()
  148. this.enabled = enabled
  149. this.lock.Unlock()
  150. this.lock.RLock()
  151. defer this.lock.RUnlock()
  152. // 组装协议
  153. d, _ := json.Marshal(notification.NotificationCouponTask{
  154. Action: 1,
  155. Data: this.enabled,
  156. })
  157. // 通知客户端
  158. for u := range this.user_tasklist {
  159. go notification.AddNotification(u, notification.Notification_CouponTask, string(d))
  160. }
  161. }
  162. func (this *taskmgr) getSwitch() bool {
  163. this.lock.RLock()
  164. defer this.lock.RUnlock()
  165. return this.enabled
  166. }
  167. func (this *taskmgr) onUserEnter(userId int) {
  168. this.lock.RLock()
  169. _, ok := this.user_tasklist[userId]
  170. this.lock.RUnlock()
  171. if ok {
  172. return
  173. }
  174. u := newUserTask(userId)
  175. this.lock.Lock()
  176. this.user_tasklist[userId] = u
  177. this.lock.Unlock()
  178. }
  179. func (this *taskmgr) onUserExit(userId int) {
  180. this.lock.Lock()
  181. defer this.lock.Unlock()
  182. delete(this.user_tasklist, userId)
  183. }