usertask.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package coupontask
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "sync"
  6. "bet24.com/log"
  7. "bet24.com/servers/common"
  8. inventory "bet24.com/servers/micros/item_inventory/proto"
  9. item "bet24.com/servers/micros/item_inventory/proto"
  10. notification "bet24.com/servers/micros/notification/proto"
  11. )
  12. type user_task struct {
  13. lock *sync.RWMutex
  14. userId int // 用户ID
  15. Info *BaseInfo // 基础信息
  16. List []*UserTask // 任务列表
  17. }
  18. func newUserTask(userId int) *user_task {
  19. ret := new(user_task)
  20. ret.lock = &sync.RWMutex{}
  21. ret.userId = userId
  22. ret.loadInfo()
  23. ret.loadList()
  24. return ret
  25. }
  26. func (this *user_task) loadInfo() {
  27. info := getUserInfo(this.userId)
  28. info.MaxAddTimes = mgr.getVideoMaxTimes()
  29. this.lock.Lock()
  30. defer this.lock.Unlock()
  31. this.Info = info
  32. }
  33. func (this *user_task) isFacebook() bool {
  34. this.lock.RLock()
  35. defer this.lock.RUnlock()
  36. return this.Info.isFacebook == 1
  37. }
  38. func (this *user_task) loadList() {
  39. list := getUserTaskList(this.userId)
  40. this.lock.Lock()
  41. defer this.lock.Unlock()
  42. this.List = list
  43. }
  44. // 检查是否跨天
  45. func (this *user_task) checkCrossDay() {
  46. // 检查是否同一天
  47. if common.IsSameDay(this.Info.updateTime, common.GetTimeStamp()) {
  48. return
  49. }
  50. this.lock.Lock()
  51. this.Info.TodayCount = 0
  52. this.Info.TmpAdd = 0
  53. this.Info.PlayTimes = 0
  54. this.Info.updateTime = common.GetTimeStamp()
  55. this.lock.Unlock()
  56. // 重置
  57. go reset(this.userId, this.Info)
  58. }
  59. // 信息及完成列表
  60. func (this *user_task) getUserTask() (*BaseInfo, []*UserTask) {
  61. var (
  62. count int // 计数器
  63. list []*UserTask
  64. )
  65. this.lock.RLock()
  66. defer this.lock.RUnlock()
  67. for i := len(this.List) - 1; i >= 0; i-- {
  68. if this.List[i].GiftStatus == status_active {
  69. continue
  70. }
  71. // 返还最近20条
  72. if count >= 20 {
  73. break
  74. }
  75. list = append(list, this.List[i])
  76. count++
  77. }
  78. sort.SliceStable(list, func(i, j int) bool {
  79. return list[i].UserTaskId > list[j].UserTaskId
  80. })
  81. return this.Info, list
  82. }
  83. // 修改信息
  84. func (this *user_task) updateInfo(coupons int) {
  85. this.lock.Lock()
  86. this.Info.TodayCount += coupons
  87. this.lock.Unlock()
  88. // 更新到数据库
  89. go updateInfo(this.userId, this.Info.TodayCount, this.Info.updateTime)
  90. }
  91. // 修改基础上限
  92. func (this *user_task) updateBaseLimit(baseLimit int) {
  93. this.lock.Lock()
  94. this.Info.BaseLimit += baseLimit
  95. this.lock.Unlock()
  96. // 更新到数据库
  97. go updateLimit(this.userId, this.Info.BaseLimit, this.Info.PlayTimes, this.Info.TmpAdd)
  98. }
  99. // 修改临时上限
  100. func (this *user_task) updateTmpLimit() int {
  101. // 播放广告,获取临时加成
  102. cfg := mgr.getVideoConfigs(this.Info.PlayTimes)
  103. if cfg.CouponLimit <= 0 {
  104. return 0
  105. }
  106. this.lock.Lock()
  107. this.Info.PlayTimes++
  108. this.Info.TmpAdd += cfg.CouponLimit
  109. this.lock.Unlock()
  110. // 更新到数据库
  111. go updateLimit(this.userId, this.Info.BaseLimit, this.Info.PlayTimes, this.Info.TmpAdd)
  112. return cfg.CouponLimit
  113. }
  114. // 触发任务
  115. func (this *user_task) trigger(gameId, baseScore, players, doubleType int) {
  116. // 是否Facebook账户
  117. if !this.isFacebook() {
  118. return
  119. }
  120. sysTask := mgr.getTask(gameId, baseScore, players, doubleType)
  121. if sysTask == nil {
  122. log.Debug("coupontask.usertask trigger userId=%d gameId=%d baseScore=%d players=%d doubleType=%d is nil",
  123. this.userId, gameId, baseScore, players, doubleType)
  124. return
  125. }
  126. var myTask *UserTask
  127. for _, v := range this.List {
  128. if v.GameId == sysTask.GameID && v.BaseScore == sysTask.BaseScore && v.GiftStatus == status_active {
  129. myTask = v
  130. break
  131. }
  132. }
  133. // 超过上限
  134. if this.Info.TodayCount > this.Info.BaseLimit+this.Info.TmpAdd {
  135. return
  136. }
  137. // 当前缓存没有
  138. if myTask == nil {
  139. myTask = &UserTask{
  140. UserTaskId: 0,
  141. GameId: gameId,
  142. BaseScore: baseScore,
  143. GameCount: 1,
  144. Coupons: sysTask.Coupons,
  145. GiftStatus: status_active,
  146. Crdate: common.GetNowTime().Format(common.Layout),
  147. }
  148. userTaskId := insertTask(this.userId, myTask)
  149. if userTaskId <= 0 {
  150. log.Error("coupontask.usertask trigger insert userId=%d userTaskId=%d myTask=%+v",
  151. this.userId, userTaskId, myTask)
  152. return
  153. }
  154. myTask.UserTaskId = userTaskId
  155. this.List = append(this.List, myTask)
  156. // 超过50条
  157. max := 50
  158. if count := len(this.List); count > max {
  159. this.List = append(this.List[:count-max], this.List[count-max+1:]...)
  160. }
  161. } else { // 更新数据
  162. myTask.Coupons += sysTask.Coupons
  163. myTask.GameCount++
  164. if myTask.GameCount >= 3 {
  165. myTask.GiftStatus = status_complete
  166. // 直接给奖励
  167. retCode := awardTask(&award_req{
  168. UserTaskId: myTask.UserTaskId,
  169. UserId: this.userId,
  170. Status: myTask.GiftStatus,
  171. })
  172. if retCode == 1 {
  173. // 更新个人信息
  174. this.updateInfo(myTask.Coupons)
  175. var items []item.ItemPack
  176. items = append(items, item.ItemPack{
  177. ItemId: ItemID,
  178. Count: myTask.Coupons,
  179. })
  180. go inventory.AddItems(this.userId, items, "红包券任务多倍", common.LOGTYPE_TASK_COUPON_MULTIPLE)
  181. }
  182. }
  183. go updateTask(this.userId, myTask)
  184. }
  185. // 道具, 通知客户端
  186. d, _ := json.Marshal(notification.NotificationCouponTask{
  187. Action: 2,
  188. TodayCount: this.Info.TodayCount,
  189. Data: myTask,
  190. })
  191. go notification.AddNotification(this.userId, notification.Notification_CouponTask, string(d))
  192. return
  193. }
  194. // 领取多倍奖励
  195. func (this *user_task) award(userTaskId int) *award_resp {
  196. var myTask *UserTask
  197. for _, v := range this.List {
  198. if v.UserTaskId == userTaskId {
  199. myTask = v
  200. break
  201. }
  202. }
  203. resp := &award_resp{}
  204. // 没有找到任务
  205. if myTask == nil {
  206. resp.RetCode = 11
  207. return resp
  208. }
  209. // 已经领取过
  210. if myTask.GiftStatus == status_awardedMultiple {
  211. resp.RetCode = 12
  212. return resp
  213. }
  214. // 已经领取过
  215. if myTask.GiftStatus != status_complete {
  216. resp.RetCode = 13
  217. return resp
  218. }
  219. myTask.GiftStatus = status_awardedMultiple
  220. retCode := awardTask(&award_req{
  221. UserTaskId: myTask.UserTaskId,
  222. UserId: this.userId,
  223. Status: myTask.GiftStatus,
  224. })
  225. if retCode == 1 {
  226. resp.RetCode = 1
  227. resp.Coupons += myTask.Coupons * PlayMultiple
  228. // 更新个人信息
  229. this.updateInfo(resp.Coupons)
  230. resp.TodayCount = this.Info.TodayCount
  231. var items []item.ItemPack
  232. items = append(items, item.ItemPack{
  233. ItemId: ItemID,
  234. Count: resp.Coupons,
  235. })
  236. inventory.AddItems(this.userId, items, "红包券任务", common.LOGTYPE_TASK_COUPON)
  237. }
  238. return resp
  239. }