report.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * @Descripttion:实名认证
  3. * @version:
  4. * @Author: Neo,Huang
  5. * @Date: 2021-04-17 11:04:16
  6. * @LastEditors: Please set LastEditors
  7. * @LastEditTime: 2021-05-10 16:12:02
  8. */
  9. package nppa
  10. import (
  11. "encoding/json"
  12. "fmt"
  13. "log"
  14. "math/rand"
  15. "sync"
  16. "time"
  17. "box-3rdServer/utils"
  18. "github.com/bitly/go-simplejson"
  19. )
  20. type Collection struct {
  21. No int `json:"no,omitempty"` //条目编号
  22. Si string `json:"si,omitempty"` //游戏内部会话标识
  23. Bt int `json:"bt"` //用户行为类型
  24. Ot int `json:"ot,omitempty"` //行为发生时间
  25. Ct int `json:"ct"` //上报类型
  26. Di string `json:"di,omitempty"` //设备标识
  27. Pi string `json:"pi,omitempty"` //用户唯一标识
  28. }
  29. type nppaCollections struct {
  30. aid int
  31. collection chan *Collection
  32. timer *time.Timer
  33. close chan int
  34. }
  35. var mapCollections sync.Map
  36. var aidKey = "nppa:aid:%d"
  37. var interval = time.Duration(120) //秒
  38. func sendCollections(aid int, key string) {
  39. infos := utils.Zrange(key, 0, 127)
  40. collections := []*Collection{}
  41. for k, v := range infos {
  42. collection := Collection{No: k + 1}
  43. json.Unmarshal([]byte(v), &collection)
  44. collections = append(collections, &collection)
  45. }
  46. NppaReportCollections(aid, collections)
  47. utils.Zremrangebyrank(key, 0, len(infos)-1)
  48. }
  49. func getRandomInterval() time.Duration {
  50. rand.Seed(time.Now().UnixNano())
  51. return time.Duration(rand.Intn(21) - 10)
  52. }
  53. func ReportCollections() {
  54. mapCollections.Range(func(k, v interface{}) bool {
  55. _, ok := mapAidToConf.Load(k)
  56. if !ok {
  57. nppaCollections := v.(*nppaCollections)
  58. nppaCollections.close <- 1
  59. mapCollections.Delete(k)
  60. }
  61. return true
  62. })
  63. mapAidToConf.Range(func(k, v interface{}) bool {
  64. _, ok := mapCollections.Load(k)
  65. if !ok {
  66. nppaCollections := nppaCollections{
  67. aid: k.(int),
  68. collection: make(chan *Collection),
  69. timer: time.NewTimer(interval * time.Second),
  70. close: make(chan int),
  71. }
  72. key := fmt.Sprintf(aidKey, nppaCollections.aid)
  73. go func() {
  74. for {
  75. select {
  76. case collection := <-nppaCollections.collection:
  77. info, _ := json.Marshal(collection)
  78. utils.Zadd(key, collection.Ot, string(info))
  79. if utils.Zcard(key) >= 128 {
  80. sendCollections(nppaCollections.aid, key)
  81. if len(nppaCollections.timer.C) > 0 {
  82. <-nppaCollections.timer.C
  83. }
  84. nppaCollections.timer.Reset((interval + getRandomInterval()) * time.Second)
  85. }
  86. case <-nppaCollections.timer.C:
  87. if utils.Zcard(key) > 0 {
  88. sendCollections(nppaCollections.aid, key)
  89. }
  90. nppaCollections.timer.Reset((interval + getRandomInterval()) * time.Second)
  91. case <-nppaCollections.close:
  92. return
  93. }
  94. }
  95. }()
  96. mapCollections.Store(k, &nppaCollections)
  97. }
  98. return true
  99. })
  100. }
  101. // 玩家行为数据上报
  102. func NppaReportCollections(aid int, collections []*Collection) {
  103. mapHead := ConfGetInfoByAid(aid)
  104. if mapHead == nil {
  105. return
  106. }
  107. mapBody := make(map[string]interface{})
  108. mapBody["collections"] = collections
  109. body := MakeRequestBody(mapBody, mapHead["secretKey"])
  110. sign := MakeRequestSign(mapHead, nil, body)
  111. mapHead["sign"] = sign
  112. buff, errReq := Send2Nppa("POST", utils.GetKeyConf("nppa", "host_loginout"), mapHead, nil, body)
  113. j, err := simplejson.NewJson(buff)
  114. if err != nil {
  115. log.Printf("NppaReportCollections err[%v] errReq[%v]", err, errReq)
  116. return
  117. }
  118. code, _ := j.Get("errcode").Int()
  119. msg, _ := j.Get("errmsg").String()
  120. if code != 0 {
  121. log.Println(fmt.Sprintf("NppaReportCollections err code[%d] msg[%s]", code, msg))
  122. for _, v := range collections {
  123. log.Println(fmt.Sprintf("collection %v", v))
  124. }
  125. return
  126. }
  127. for _, v := range j.Get("data").Get("results").MustArray() {
  128. result, _ := v.(map[string]interface{})
  129. no := utils.JsonNumberToInt(result["no"])
  130. errcode := utils.JsonNumberToInt(result["errcode"])
  131. errmsg := result["errmsg"].(string)
  132. if errcode != 0 {
  133. log.Println(fmt.Sprintf("NppaReportCollections err collection[%v] ot[%d] code[%d] msg[%s]", collections[no-1], collections[no-1].Ot, errcode, errmsg))
  134. }
  135. }
  136. }