user_room_task.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package user
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. "bet24.com/servers/micros/audioroom/handler/config"
  6. pb "bet24.com/servers/micros/audioroom/proto"
  7. "bet24.com/servers/micros/audioroom/transaction/database"
  8. "math"
  9. "sort"
  10. )
  11. // 任务列表
  12. func (this *UserRoom) GetTaskList(roomId int) []*pb.UserRoomTask {
  13. // 判断是否是成员
  14. if this.UserRoomInfo(roomId) == nil {
  15. return nil
  16. }
  17. list, ok := this.GetUserTask(roomId)
  18. if ok {
  19. for k := range list {
  20. if !common.IsSameDay(list[k].TimeStamp, common.GetTimeStamp()) {
  21. list[k].Schedule = 0
  22. list[k].CurrNum = 0
  23. list[k].Status = 0
  24. }
  25. }
  26. return list
  27. }
  28. // TODO: 从数据库获取
  29. list = database.GetUserTaskList(this.userId, roomId)
  30. // 初始化任务
  31. for _, v := range config.Mgr.GetSysTask(false) {
  32. isExist := false
  33. for _, t := range list {
  34. if v.Id != t.TaskId {
  35. continue
  36. }
  37. isExist = true
  38. break
  39. }
  40. if isExist {
  41. continue
  42. }
  43. info := &pb.UserRoomTask{
  44. RoomTask: pb.RoomTask{
  45. TaskId: v.Id,
  46. Schedule: 0,
  47. CurrNum: 0,
  48. TimeStamp: common.GetTimeStamp(),
  49. },
  50. Status: 0,
  51. }
  52. list = append(list, info)
  53. // TODO:更新数据库
  54. go database.UpdateUserTask(this.userId, roomId, info)
  55. }
  56. sort.SliceStable(list, func(i, j int) bool {
  57. return list[i].TaskId < list[j].TaskId
  58. })
  59. this.SetUserTask(roomId, list)
  60. return list
  61. }
  62. // 获取用户任务
  63. func (this *UserRoom) GetUserTask(roomId int) ([]*pb.UserRoomTask, bool) {
  64. this.lock.RLock()
  65. defer this.lock.RUnlock()
  66. list, ok := this.taskList[roomId]
  67. return list, ok
  68. }
  69. // 设置用户任务
  70. func (this *UserRoom) SetUserTask(roomId int, list []*pb.UserRoomTask) {
  71. this.lock.Lock()
  72. defer this.lock.Unlock()
  73. this.taskList[roomId] = list
  74. }
  75. // 触发房间任务
  76. func (this *UserRoom) DoTaskAction(roomId, action, num, ext int) {
  77. //log.Debug("user_roomtask.doTaskAction userId=%d roomId=%d action=%d num=%d ext=%d",
  78. // this.userId, roomId, action, num, ext)
  79. r := this.UserRoomInfo(roomId)
  80. if r == nil {
  81. log.Release("user_roomtask.doTaskAction userId=%d roomId=%d action=%d num=%d is nil",
  82. this.userId, roomId, action, num)
  83. return
  84. }
  85. // 遍历房间任务
  86. for _, v := range this.GetTaskList(roomId) {
  87. // 获取任务配置
  88. cfg := config.Mgr.GetTaskConfig(v.TaskId, false)
  89. if cfg == nil {
  90. log.Debug("user_roomtask.doTaskAction userId=%d roomId=%d taskId %d not found", this.userId, roomId, v.TaskId)
  91. continue
  92. }
  93. // 判断触发动作
  94. if cfg.Action != action {
  95. continue
  96. }
  97. // 判断是否过期
  98. if !common.IsSameDay(v.TimeStamp, common.GetTimeStamp()) {
  99. v.Schedule = 0
  100. v.CurrNum = 0
  101. v.Status = 0
  102. }
  103. // 判断任务是否完成
  104. if cfg.Target > 0 && v.Schedule >= cfg.Target {
  105. continue
  106. }
  107. // 当前数值
  108. v.CurrNum += num
  109. if len(cfg.Exps) <= 0 || ext > len(cfg.Exps) {
  110. log.Debug("user_roomtask.doTaskAction userId=%d roomId=%d taskId exps=%+v is invalid", this.userId, roomId, v.TaskId, cfg.Exps)
  111. return
  112. }
  113. // 计算经验值
  114. exps := int(math.Floor(float64(v.CurrNum/cfg.NeedNum)*cfg.Exps[ext] + 0.5))
  115. if cfg.Target > 0 && v.Schedule+exps > cfg.Target {
  116. exps = cfg.Target - v.Schedule
  117. }
  118. // 经验值不够,累计数值积累
  119. if exps <= 0 {
  120. // TODO:更新数据库
  121. go database.UpdateUserTask(this.userId, r.RoomId, v)
  122. continue
  123. }
  124. // 扣掉升级的所需数值
  125. v.CurrNum = v.CurrNum % cfg.NeedNum
  126. // 任务进度
  127. v.Schedule += exps
  128. // 更新时间戳
  129. v.TimeStamp = common.GetTimeStamp()
  130. // 达到统计阈值
  131. if cfg.StatValue > 0 && v.Schedule >= cfg.StatValue {
  132. v.Status = 1
  133. }
  134. // 添加房间经验
  135. r.Exps += exps
  136. r.DayExps += exps
  137. // 判断是否升级
  138. if newLevel := config.Mgr.GetLevel(r.Exps, false); newLevel > r.Level {
  139. r.Level = newLevel
  140. // 发送通知
  141. go this.levelNotify(r.RoomId)
  142. }
  143. // 同步房间成员等级经验
  144. go this.roomMgr.UpdateMemberLevel(this.userId, r.RoomId, r.Level, r.Exps)
  145. // TODO:更新数据库
  146. go database.UpdateUserTask(this.userId, r.RoomId, v)
  147. go database.UpdateUserExps(this.userId, r.RoomId, r.Level, r.Exps)
  148. }
  149. return
  150. }
  151. // 清理任务
  152. func (this *UserRoom) DelTask(roomId int) {
  153. this.lock.Lock()
  154. defer this.lock.Unlock()
  155. delete(this.taskList, roomId)
  156. }