user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package handler
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "bet24.com/log"
  8. "bet24.com/redis"
  9. "bet24.com/servers/common"
  10. pb "bet24.com/servers/micros/dotservice/proto"
  11. )
  12. // 用户信息
  13. type userInfo struct {
  14. userId string // 用户id
  15. pb.DotScope
  16. dot_list []*pb.DotInfo // 打点数据
  17. timeStamp int // 时间戳
  18. }
  19. func newUserInfo(userId string) *userInfo {
  20. u := new(userInfo)
  21. u.userId = userId
  22. u.timeStamp = common.GetTimeStamp() + 300
  23. go u.loadRedisData()
  24. return u
  25. }
  26. // redis key
  27. func (this *userInfo) getRedisKey() string {
  28. return fmt.Sprintf("%s:%s", "dotservice", this.userId)
  29. }
  30. // 获取redis数据
  31. func (this *userInfo) loadRedisData() {
  32. key := this.getRedisKey()
  33. value, ok := redis.String_Get(key)
  34. if !ok || value == "" {
  35. return
  36. }
  37. err := json.Unmarshal([]byte(value), &this.DotScope)
  38. if err != nil {
  39. log.Error("user.loadRedisData json unmarshal userId=%s value ==> %v", this.userId, value)
  40. }
  41. return
  42. }
  43. // 处理过期
  44. func (this *userInfo) dealExpire() bool {
  45. if this.timeStamp >= common.GetTimeStamp() {
  46. return false
  47. }
  48. // 保存并清理数据
  49. this.saveAndClearData()
  50. return true
  51. }
  52. // 保存并清理数据
  53. func (this *userInfo) saveAndClearData() {
  54. for _, v := range this.dot_list {
  55. // TODO:存入数据库
  56. go trans_insert(this.userId, *v)
  57. }
  58. // 保存临时数据到redis
  59. buf, _ := json.Marshal(this.DotScope)
  60. redis.String_SetEx(this.getRedisKey(), string(buf), 3600)
  61. return
  62. }
  63. // 添加打点
  64. func (this *userInfo) add(scope pb.DotScope) {
  65. // 判断游戏是否有效
  66. if !this.isGameValid(scope) {
  67. return
  68. }
  69. // 热更启动场景处理
  70. if scope.Scene == pb.Scene_HotUpdate {
  71. scope = this.hotUpdate(scope)
  72. }
  73. // 今天日期
  74. dateFlag := time.Now().Format("2006-01-02")
  75. for _, v := range mgr.getConfigList() {
  76. if v.Scene != scope.Scene {
  77. continue
  78. }
  79. // 启动场景
  80. if scope.Scene == pb.Scene_HotUpdate {
  81. nowSeconds, _ := strconv.Atoi(scope.Extra)
  82. cfgSeconds, _ := strconv.Atoi(v.Extra)
  83. // 超过一定时间为点击
  84. if nowSeconds >= cfgSeconds {
  85. scope.Action = pb.Action_Click
  86. } else {
  87. scope.Action = pb.Action_Complete
  88. }
  89. scope.Extra = strconv.Itoa(common.GetTimeStamp())
  90. this.DotScope = scope
  91. } else { // 其他场景
  92. if v.Extra != "" && v.Extra != scope.Extra && "BL_"+v.Extra != scope.Extra {
  93. continue
  94. }
  95. }
  96. // 获取打点信息
  97. info := this.getDotInfo(dateFlag, v.Scene, v.Event, scope.Action)
  98. if info != nil {
  99. info.Times++
  100. continue
  101. }
  102. info = &pb.DotInfo{
  103. DateFlag: dateFlag,
  104. Scene: scope.Scene,
  105. Event: v.Event,
  106. Action: scope.Action,
  107. Times: 1,
  108. }
  109. this.dot_list = append(this.dot_list, info)
  110. }
  111. return
  112. }
  113. // 游戏是否有效
  114. func (this *userInfo) isGameValid(scope pb.DotScope) bool {
  115. // 游戏、赛事场景才需要判断
  116. if scope.Scene != pb.Scene_Game && scope.Scene != pb.Scene_Match {
  117. return true
  118. }
  119. // 点击事件,直接返回
  120. if scope.Action == pb.Action_Click {
  121. this.DotScope = scope
  122. return true
  123. }
  124. // 完成事件,同场景且之前是点击事件
  125. if this.DotScope.Scene == scope.Scene && this.DotScope.Action == pb.Action_Click && scope.Action == pb.Action_Complete {
  126. this.DotScope = scope
  127. return true
  128. }
  129. this.DotScope = scope
  130. return false
  131. }
  132. // 热更处理
  133. func (this *userInfo) hotUpdate(scope pb.DotScope) pb.DotScope {
  134. // 缓存里没有热更场景
  135. if this.DotScope.Scene != scope.Scene {
  136. this.DotScope = scope
  137. return scope
  138. }
  139. // 有缓存处理
  140. cache, _ := strconv.Atoi(this.DotScope.Extra)
  141. now, _ := strconv.Atoi(scope.Extra)
  142. scope.Extra = strconv.Itoa(now - cache)
  143. return scope
  144. }
  145. // 获取今天打点信息
  146. func (this *userInfo) getDotInfo(dateFlag, scene, event string, action int) *pb.DotInfo {
  147. for _, v := range this.dot_list {
  148. if v.DateFlag == dateFlag && v.Scene == scene && v.Event == event && v.Action == action {
  149. return v
  150. }
  151. }
  152. return nil
  153. }