room_task.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package room
  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. badge "bet24.com/servers/micros/badge/proto"
  9. "math"
  10. "sort"
  11. )
  12. // 加载任务列表
  13. func (this *Room) loadTask() {
  14. list := database.GetTaskList(this.RoomId)
  15. for i, _ := range list {
  16. this.taskList = append(this.taskList, &list[i])
  17. }
  18. // 初始化任务
  19. for _, v := range config.Mgr.GetSysTask(true) {
  20. if t := this.GetTaskInfo(v.Id); t != nil {
  21. continue
  22. }
  23. info := &pb.RoomTask{
  24. TaskId: v.Id,
  25. Schedule: 0,
  26. CurrNum: 0,
  27. TimeStamp: common.GetTimeStamp(),
  28. }
  29. this.taskList = append(this.taskList, info)
  30. // TODO:更新数据库
  31. go database.UpdateTask(this.RoomId, info)
  32. }
  33. sort.SliceStable(this.taskList, func(i, j int) bool {
  34. return this.taskList[i].TaskId < this.taskList[j].TaskId
  35. })
  36. return
  37. }
  38. // 获取任务列表
  39. func (this *Room) GetTaskList() []*pb.RoomTask {
  40. for k, _ := range this.taskList {
  41. // 过期重置
  42. if !common.IsSameDay(this.taskList[k].TimeStamp, common.GetTimeStamp()) {
  43. this.taskList[k].Schedule = 0
  44. this.taskList[k].CurrNum = 0
  45. }
  46. }
  47. return this.taskList
  48. }
  49. // 获取任务信息
  50. func (this *Room) GetTaskInfo(taskId int) *pb.RoomTask {
  51. for _, v := range this.taskList {
  52. if v.TaskId == taskId {
  53. return v
  54. }
  55. }
  56. return nil
  57. }
  58. // 触发房间任务
  59. func (this *Room) DoTaskAction(userId, action, num, stillExps int) (ok bool, exps int) {
  60. //log.Debug("room_task.doTaskAction roomId=%d action=%d num=%d stillExps=%d", this.RoomId, action, num, stillExps)
  61. // 扩展信息
  62. ext := this.GetExtInfo()
  63. // 判断是否达到每天经验上限
  64. if ext.DayExps >= config.Mgr.GetRoomConfig().DayExpLimit {
  65. return
  66. }
  67. // 遍历房间任务
  68. for _, v := range this.taskList {
  69. // 获取系统任务
  70. cfg := config.Mgr.GetTaskConfig(v.TaskId, true)
  71. if cfg == nil {
  72. log.Debug("room_task.doTaskAction roomId=%d taskId %d not found", this.RoomId, v.TaskId)
  73. continue
  74. }
  75. // 判断触发动作
  76. if cfg.Action != action {
  77. continue
  78. }
  79. // 判断是否过期
  80. if !common.IsSameDay(v.TimeStamp, common.GetTimeStamp()) {
  81. v.Schedule = 0
  82. v.CurrNum = 0
  83. }
  84. // 判断任务是否完成
  85. if v.Schedule >= cfg.Target {
  86. continue
  87. }
  88. // 当前数值
  89. v.CurrNum += num
  90. if len(cfg.Exps) <= 0 {
  91. log.Debug("room_task.doTaskAction roomId=%d taskId exps=%+v is invalid", this.RoomId, v.TaskId, cfg.Exps)
  92. return
  93. }
  94. // 计算经验值
  95. exps = int(math.Floor(float64(v.CurrNum/cfg.NeedNum)*cfg.Exps[0] + 0.5))
  96. if cfg.Target > 0 && v.Schedule+exps > cfg.Target {
  97. exps = cfg.Target - v.Schedule
  98. }
  99. // 大于个人上限
  100. if stillExps > 0 && exps > stillExps {
  101. exps = stillExps
  102. }
  103. // 经验值不够,累计数值积累
  104. if exps <= 0 {
  105. // TODO:更新数据库
  106. go database.UpdateTask(this.RoomId, v)
  107. continue
  108. }
  109. // 扣掉升级的所需数值
  110. v.CurrNum = v.CurrNum % cfg.NeedNum
  111. // 任务进度
  112. v.Schedule += exps
  113. // 更新时间戳
  114. v.TimeStamp = common.GetTimeStamp()
  115. // 添加房间经验
  116. this.Exps += exps
  117. this.DayExps += exps
  118. // 判断是否升级
  119. if newLevel := config.Mgr.GetLevel(this.Exps, true); newLevel > this.Level {
  120. this.Level = newLevel
  121. // 徽章进度
  122. go badge.DoAction(this.UserId, badge.Action_AudioRoom_RoomGrade, 1, badge.Scope{})
  123. }
  124. // TODO:更新数据库
  125. go database.UpdateTask(this.RoomId, v)
  126. go database.UpdateExps(this.RoomId, this.Level, this.Exps)
  127. go database.UpdateExtInfo(this.RoomId, &this.RoomExtInfo)
  128. go database.AddRoomExpLog(userId, this.RoomId, exps)
  129. ok = true
  130. return
  131. }
  132. return
  133. }
  134. // 获取用户任务统计
  135. func (this *Room) GetUserRoomTaskStat(userId int) []pb.UserRoomTaskStat {
  136. if userId != this.UserId {
  137. return nil
  138. }
  139. var ret []pb.UserRoomTaskStat
  140. // 获取完成的任务统计列表
  141. list := database.GetUserTaskStat(this.RoomId)
  142. for _, task := range config.Mgr.GetSysTask(false) {
  143. finishNum := 0
  144. for _, v := range list {
  145. if task.Id != v.TaskId {
  146. continue
  147. }
  148. finishNum = v.FinishNum
  149. break
  150. }
  151. ret = append(ret, pb.UserRoomTaskStat{
  152. TaskId: task.Id,
  153. FinishNum: finishNum,
  154. })
  155. }
  156. return ret
  157. }