From 022fcf092d33d31e4d2579f54e402b82cb9df935 Mon Sep 17 00:00:00 2001 From: "dian.chen" Date: Thu, 30 May 2024 22:06:56 +0800 Subject: [PATCH] fix --- ....run.xml => go build app_cmd_main.run.xml} | 8 +- cmd/robot/main.go | 49 ------------ cmd/robot/robot_test.go | 74 ++++++++++++++++++ service/main/redis/redis.go | 75 +++++++++++++++---- service/main/user/user.go | 16 +--- service/robot/user/user.go | 3 +- 6 files changed, 145 insertions(+), 80 deletions(-) rename .run/{go build main.go.run.xml => go build app_cmd_main.run.xml} (55%) delete mode 100644 cmd/robot/main.go create mode 100644 cmd/robot/robot_test.go diff --git a/.run/go build main.go.run.xml b/.run/go build app_cmd_main.run.xml similarity index 55% rename from .run/go build main.go.run.xml rename to .run/go build app_cmd_main.run.xml index 1ad82a2..b59a186 100644 --- a/.run/go build main.go.run.xml +++ b/.run/go build app_cmd_main.run.xml @@ -1,13 +1,13 @@ - - + + - + - + diff --git a/cmd/robot/main.go b/cmd/robot/main.go deleted file mode 100644 index 1dbc8f6..0000000 --- a/cmd/robot/main.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "app/network" - "app/service/main/message/pb" - "app/service/main/msg_util" - "app/service/robot/user" - "sync" -) - -func main() { - client := network.NewTcpClient( - ":8888", - newParser, - newHandler) - err := client.Connect(false) - if err != nil { - return - } - if err != nil { - panic(err) - } - err = client.SendMsg(&pb.Report{ - Info: []*pb.ReportInfo{ - { - OpenId: "1", - Score: 123, - }, - { - OpenId: "2", - Score: 456, - }, - }, - }) - if err != nil { - panic(err) - } - wg := &sync.WaitGroup{} - wg.Add(1) - wg.Wait() -} - -func newParser() network.ICodec { - return msg_util.NewProtoCodec(msg_util.NewProtoParser("message", "MSG_TYPE"), 1024*64, false) -} - -func newHandler() network.INetHandler { - return user.NewUser() -} diff --git a/cmd/robot/robot_test.go b/cmd/robot/robot_test.go new file mode 100644 index 0000000..01a145f --- /dev/null +++ b/cmd/robot/robot_test.go @@ -0,0 +1,74 @@ +package main + +import ( + "app/network" + "app/service/main/message/pb" + "app/service/main/msg_util" + "app/service/robot/user" + "math/rand" + "sync" + "testing" +) + +func TestName(t *testing.T) { + client := network.NewTcpClient( + "129.211.8.84:8899", + newParser, + newHandler) + err := client.Connect(false) + if err != nil { + panic(err) + } + //登陆 + login := &pb.Login{ + AppId: "debug", + IsDebug: true, + } + err = client.SendMsg(login) + if err != nil { + panic(err) + } + + //上传分数 + openIdList := []string{ + "7374686837987841059", + "7374698021348676648", + "7372845558429537319", + "7374677100777002003", + "7374698021348660264", + "7374673415892489242", + "7374683012379776011", + "7374686060594746368", + "7374687164753499177", + "7372850431053173801", + } + report := &pb.Report{} + for _, openId := range openIdList { + report.Info = append(report.Info, &pb.ReportInfo{ + OpenId: openId, + Score: rand.Int31n(100), + IsWin: rand.Float32() > 0.5, + }) + } + err = client.SendMsg(report) + if err != nil { + panic(err) + } + //拉排行榜 + rank := &pb.GetRank{ + TopCount: 5, + } + err = client.SendMsg(rank) + + wg := &sync.WaitGroup{} + wg.Add(1) + wg.Wait() +} + +func newParser() network.ICodec { + return msg_util.NewProtoCodec(msg_util.NewProtoParser("message", "MSG_TYPE"), 1024*64, false) +} + +func newHandler() network.INetHandler { + return user.NewUser() +} diff --git a/service/main/redis/redis.go b/service/main/redis/redis.go index a266371..5f1318f 100644 --- a/service/main/redis/redis.go +++ b/service/main/redis/redis.go @@ -4,6 +4,7 @@ import ( "app/service/main/message/pb" "context" "encoding/json" + "fmt" "github.com/redis/go-redis/v9" "os" ) @@ -46,16 +47,23 @@ func GetRank(appId string, topCount int32) ([]*pb.Audience, error) { if err != nil { return nil, err } - pip := client.Pipeline() + var openIdList []string for _, info := range result { - key := ThisWeekScoreKey(appId) - - ret = append(ret, &RankInfo{ - OpenId: info.Member.(string), - Score: int32(info.Score), + openIdList = append(openIdList, info.Member.(string)) + } + audienceBasicList := GetAudienceBasicList(appId, openIdList) + audienceInfoList := GetAudienceInfoList(appId, openIdList) + if len(openIdList) != len(audienceBasicList) || len(openIdList) != len(audienceInfoList) { + return nil, fmt.Errorf("GetRank: len(openIdList) != len(audienceBasicList) || len(openIdList) != len(audienceInfoList)") + } + var ret []*pb.Audience + for i := range openIdList { + ret = append(ret, &pb.Audience{ + AudienceBasic: audienceBasicList[i], + AudienceInfo: audienceInfoList[i], }) } - return rankList, nil + return ret, nil } func SetAudienceBasic(appId string, data *pb.AudienceBasic) { @@ -68,7 +76,6 @@ func SetAudienceBasic(appId string, data *pb.AudienceBasic) { if exist == 1 { return } - jsonData, err := json.Marshal(data) if err != nil { return @@ -106,18 +113,16 @@ func GetAudienceInfo(appId string, openId string) *pb.AudienceInfo { pip := client.Pipeline() keyScore := ThisWeekScoreKey(appId) keyScoreLast := LastWeekScoreKey(appId) + keyWiningStreak := WinningStreakKey(appId) - var err error cmdScore := pip.ZScore(ctx, keyScore, openId) cmdRank := pip.ZRank(ctx, keyScore, openId) cmdRankLast := pip.ZRank(ctx, keyScoreLast, openId) - result, err := pip.Exec(ctx) + cmdWiningStreak := pip.ZScore(ctx, keyWiningStreak, openId) + _, err := pip.Exec(ctx) if err != nil { return ret } - if len(result) != 3 { - return ret - } if result, err := cmdScore.Result(); err == nil { ret.Score = int32(result) } @@ -127,6 +132,9 @@ func GetAudienceInfo(appId string, openId string) *pb.AudienceInfo { if result, err := cmdRankLast.Result(); err == nil { ret.Score = int32(result) } + if result, err := cmdWiningStreak.Result(); err == nil { + ret.WinningStreak = int32(result) + } return ret } @@ -154,7 +162,48 @@ func GetAudienceBasicList(appId string, openIdList []string) (ret []*pb.Audience func GetAudienceInfoList(appId string, openIdList []string) (ret []*pb.AudienceInfo) { ctx := context.Background() + pip := client.Pipeline() + keyScore := ThisWeekScoreKey(appId) + keyScoreLast := LastWeekScoreKey(appId) + keyWiningStreak := WinningStreakKey(appId) + var cmdScoreList []*redis.FloatCmd + var cmdRankList []*redis.IntCmd + var cmdRankLastList []*redis.IntCmd + var cmdWiningStreakList []*redis.FloatCmd + for _, openId := range openIdList { + cmdScoreList = append(cmdScoreList, pip.ZScore(ctx, keyScore, openId)) + cmdRankList = append(cmdRankList, pip.ZRank(ctx, keyScore, openId)) + cmdRankLastList = append(cmdRankLastList, pip.ZRank(ctx, keyScoreLast, openId)) + cmdWiningStreakList = append(cmdWiningStreakList, pip.ZScore(ctx, keyWiningStreak, openId)) + } + _, err := pip.Exec(ctx) + if err != nil { + return ret + } + for i, openId := range openIdList { + cmdScore := cmdScoreList[i] + cmdRank := cmdRankList[i] + cmdRankLast := cmdRankLastList[i] + cmdWiningStreak := cmdWiningStreakList[i] + info := &pb.AudienceInfo{ + OpenId: openId, + } + if result, err := cmdScore.Result(); err == nil { + info.Score = int32(result) + } + if result, err := cmdRank.Result(); err == nil { + info.Rank = int32(result) + } + if result, err := cmdRankLast.Result(); err == nil { + info.Score = int32(result) + } + if result, err := cmdWiningStreak.Result(); err == nil { + info.WinningStreak = int32(result) + } + ret = append(ret, info) + } + return ret } func Subscribe(appId string, roomId string, pushFunc func(string), pushCloseChan chan struct{}) { diff --git a/service/main/user/user.go b/service/main/user/user.go index 049ed9c..15d457f 100644 --- a/service/main/user/user.go +++ b/service/main/user/user.go @@ -161,11 +161,7 @@ func (s *User) OnPlayEnd(msg *pb.PlayEnd) { s.SendUserMsg(&pb.PlayEndResult{Result: pb.ERROR_CODE_SUCCESS}) } func (s *User) OnReport(msg *pb.Report) { - scoreMap := map[string]int32{} - for _, info := range msg.Info { - scoreMap[info.OpenId] = info.Score - } - err := redis.UpdateReport(s.appId, scoreMap) + err := redis.UpdateReport(s.appId, msg) if err != nil { s.SendUserMsg(&pb.ReportResult{Result: pb.ERROR_CODE_FAIL}) return @@ -175,17 +171,11 @@ func (s *User) OnReport(msg *pb.Report) { func (s *User) OnGetRank(msg *pb.GetRank) { rankList, err := redis.GetRank(s.appId, msg.TopCount) if err != nil { + s.Log("OnGetRank err", err) s.SendUserMsg(&pb.GetRankResult{Result: pb.ERROR_CODE_FAIL}) return } - var ret []*pb.ReportInfo - for _, info := range rankList { - ret = append(ret, &pb.ReportInfo{ - OpenId: info.OpenId, - Score: info.Score, - }) - } - s.SendUserMsg(&pb.GetRankResult{Result: pb.ERROR_CODE_SUCCESS, Info: ret}) + s.SendUserMsg(&pb.GetRankResult{Result: pb.ERROR_CODE_SUCCESS, Info: rankList}) } func (s *User) setPushActive(isActive bool) { diff --git a/service/robot/user/user.go b/service/robot/user/user.go index f9c4061..b9f2235 100644 --- a/service/robot/user/user.go +++ b/service/robot/user/user.go @@ -2,6 +2,7 @@ package user import ( "app/network" + "fmt" ) func NewUser() *User { @@ -19,5 +20,5 @@ func (s *User) OnSessionClosed() { } func (s *User) OnRecv(msgId int32, data interface{}) { - + fmt.Println(data) }