user.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package battlepass
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. notification "bet24.com/servers/micros/notification/proto"
  6. "fmt"
  7. )
  8. type user struct {
  9. UserId int
  10. Exp int
  11. DayExp int
  12. DayIndex int
  13. Terms []*UserGrowthTerm
  14. isOnline bool
  15. }
  16. func newUser(userId int) *user {
  17. u := new(user)
  18. u.UserId = userId
  19. u.initData()
  20. u.isOnline = true
  21. return u
  22. }
  23. func (u *user) initData() {
  24. u.Exp = 0
  25. u.DayExp = 0
  26. u.DayIndex = 0
  27. u.Terms = []*UserGrowthTerm{}
  28. // 如果有免费礼包,但用户还没有买,则自动购买
  29. added := false
  30. packs := mgr.getGrowthPacks()
  31. for _, v := range packs {
  32. if v.Price == 0 {
  33. u.addPack(&v)
  34. added = true
  35. }
  36. }
  37. if added {
  38. u.addExp(0)
  39. }
  40. }
  41. func (u *user) addPack(pack *GrowthPack) {
  42. if u.isPackExist(pack.Id) {
  43. return
  44. }
  45. for _, v := range pack.Terms {
  46. u.Terms = append(u.Terms, &UserGrowthTerm{
  47. UserId: u.UserId,
  48. GrowthPackId: pack.Id,
  49. TermIndex: v.TermIndex,
  50. Status: GrowthTermStatus_none,
  51. NeedExp: v.NeedExp,
  52. })
  53. }
  54. }
  55. func (u *user) isPackExist(packId int) bool {
  56. for _, v := range u.Terms {
  57. if v.GrowthPackId == packId {
  58. return true
  59. }
  60. }
  61. return false
  62. }
  63. func (u *user) addExp(exp int) {
  64. dayIndex := getDayIndex()
  65. if dayIndex != u.DayIndex {
  66. u.DayExp = 0
  67. u.DayIndex = dayIndex
  68. }
  69. maxExp := mgr.getMaxExp()
  70. maxDayExp := mgr.getMaxDayExp()
  71. if exp+u.DayExp > maxDayExp {
  72. exp = maxDayExp - u.DayExp
  73. }
  74. if exp+u.Exp > maxExp {
  75. exp = maxExp - u.Exp
  76. }
  77. if exp < 0 {
  78. exp = 0
  79. }
  80. u.Exp += exp
  81. redPoint := false
  82. // 如果触发可领取
  83. for _, v := range u.Terms {
  84. if v.Status == GrowthTermStatus_none && v.NeedExp <= u.Exp {
  85. redPoint = true
  86. v.Status = GrowthTermStatus_complete
  87. }
  88. }
  89. // 通知
  90. if redPoint {
  91. notification.AddNotification(u.UserId, notification.Notification_BattlePass, "")
  92. }
  93. }
  94. func (u *user) rewardTerm(packId, termId int) (bool, string) {
  95. bought := false
  96. term := mgr.getGrowthTerm(packId, termId)
  97. if term == nil {
  98. log.Debug("battlepass_manager.user.rewardTerm invalid params %d.%d", packId, termId)
  99. return false, "invalid packId"
  100. }
  101. for _, v := range u.Terms {
  102. if v.GrowthPackId != packId {
  103. continue
  104. }
  105. bought = true
  106. if v.TermIndex != termId {
  107. continue
  108. }
  109. if v.Status != GrowthTermStatus_complete {
  110. return false, "not complete"
  111. } else {
  112. inventory_add(u.UserId, term.Items, fmt.Sprintf("battlepass[%d]", packId), common.LOGTYPE_PACK_GROWTH)
  113. v.Status = GrowthTermStatus_awarded
  114. //m.updateUserTerm(v)
  115. return true, "success"
  116. }
  117. }
  118. if bought {
  119. return false, "failed"
  120. } else {
  121. return false, "not bought"
  122. }
  123. }
  124. func (u *user) addTerm(term *UserGrowthTerm) {
  125. u.Terms = append(u.Terms, term)
  126. }