/* * @Descripttion:实名认证 * @version: * @Author: Neo,Huang * @Date: 2021-04-17 11:04:16 * @LastEditors: Please set LastEditors * @LastEditTime: 2021-05-10 16:12:02 */ package nppa import ( "encoding/json" "fmt" "log" "math/rand" "sync" "time" "box-3rdServer/utils" "github.com/bitly/go-simplejson" ) type Collection struct { No int `json:"no,omitempty"` //条目编号 Si string `json:"si,omitempty"` //游戏内部会话标识 Bt int `json:"bt"` //用户行为类型 Ot int `json:"ot,omitempty"` //行为发生时间 Ct int `json:"ct"` //上报类型 Di string `json:"di,omitempty"` //设备标识 Pi string `json:"pi,omitempty"` //用户唯一标识 } type nppaCollections struct { aid int collection chan *Collection timer *time.Timer close chan int } var mapCollections sync.Map var aidKey = "nppa:aid:%d" var interval = time.Duration(120) //秒 func sendCollections(aid int, key string) { infos := utils.Zrange(key, 0, 127) collections := []*Collection{} for k, v := range infos { collection := Collection{No: k + 1} json.Unmarshal([]byte(v), &collection) collections = append(collections, &collection) } NppaReportCollections(aid, collections) utils.Zremrangebyrank(key, 0, len(infos)-1) } func getRandomInterval() time.Duration { rand.Seed(time.Now().UnixNano()) return time.Duration(rand.Intn(21) - 10) } func ReportCollections() { mapCollections.Range(func(k, v interface{}) bool { _, ok := mapAidToConf.Load(k) if !ok { nppaCollections := v.(*nppaCollections) nppaCollections.close <- 1 mapCollections.Delete(k) } return true }) mapAidToConf.Range(func(k, v interface{}) bool { _, ok := mapCollections.Load(k) if !ok { nppaCollections := nppaCollections{ aid: k.(int), collection: make(chan *Collection), timer: time.NewTimer(interval * time.Second), close: make(chan int), } key := fmt.Sprintf(aidKey, nppaCollections.aid) go func() { for { select { case collection := <-nppaCollections.collection: info, _ := json.Marshal(collection) utils.Zadd(key, collection.Ot, string(info)) if utils.Zcard(key) >= 128 { sendCollections(nppaCollections.aid, key) if len(nppaCollections.timer.C) > 0 { <-nppaCollections.timer.C } nppaCollections.timer.Reset((interval + getRandomInterval()) * time.Second) } case <-nppaCollections.timer.C: if utils.Zcard(key) > 0 { sendCollections(nppaCollections.aid, key) } nppaCollections.timer.Reset((interval + getRandomInterval()) * time.Second) case <-nppaCollections.close: return } } }() mapCollections.Store(k, &nppaCollections) } return true }) } // 玩家行为数据上报 func NppaReportCollections(aid int, collections []*Collection) { mapHead := ConfGetInfoByAid(aid) if mapHead == nil { return } mapBody := make(map[string]interface{}) mapBody["collections"] = collections body := MakeRequestBody(mapBody, mapHead["secretKey"]) sign := MakeRequestSign(mapHead, nil, body) mapHead["sign"] = sign buff, errReq := Send2Nppa("POST", utils.GetKeyConf("nppa", "host_loginout"), mapHead, nil, body) j, err := simplejson.NewJson(buff) if err != nil { log.Printf("NppaReportCollections err[%v] errReq[%v]", err, errReq) return } code, _ := j.Get("errcode").Int() msg, _ := j.Get("errmsg").String() if code != 0 { log.Println(fmt.Sprintf("NppaReportCollections err code[%d] msg[%s]", code, msg)) for _, v := range collections { log.Println(fmt.Sprintf("collection %v", v)) } return } for _, v := range j.Get("data").Get("results").MustArray() { result, _ := v.(map[string]interface{}) no := utils.JsonNumberToInt(result["no"]) errcode := utils.JsonNumberToInt(result["errcode"]) errmsg := result["errmsg"].(string) if errcode != 0 { log.Println(fmt.Sprintf("NppaReportCollections err collection[%v] ot[%d] code[%d] msg[%s]", collections[no-1], collections[no-1].Ot, errcode, errmsg)) } } }