fix获取排行榜初始化数据问题,sql执行问题
This commit is contained in:
parent
7f7a881d46
commit
9cc97b5c06
@ -19,4 +19,5 @@ Log:
|
|||||||
|
|
||||||
DWCache:
|
DWCache:
|
||||||
Host: redis:6379
|
Host: redis:6379
|
||||||
|
Password:
|
||||||
IdleTimeout: 60
|
IdleTimeout: 60
|
@ -19,5 +19,6 @@ Log:
|
|||||||
|
|
||||||
DWCache:
|
DWCache:
|
||||||
Host: 127.0.0.1:6379
|
Host: 127.0.0.1:6379
|
||||||
|
Password:
|
||||||
IdleTimeout: 60
|
IdleTimeout: 60
|
||||||
EcpmLogPath: ./logs/ecpm.log
|
EcpmLogPath: ./logs/ecpm.log
|
@ -5,6 +5,7 @@ import (
|
|||||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/logic/rankings"
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/logic/rankings"
|
||||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc"
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc"
|
||||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types"
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
@ -40,13 +41,16 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
|||||||
|
|
||||||
var flag bool
|
var flag bool
|
||||||
|
|
||||||
resp.RankingData = make([]types.RankingData, len(cacheData))
|
resp.RankingData = make([]types.RankingData, 0, len(cacheData))
|
||||||
var userRank types.RankingData
|
var userRank types.RankingData
|
||||||
|
|
||||||
for i, datum := range cacheData {
|
for i, datum := range cacheData {
|
||||||
if id, ok := datum.Member.(uint64); ok {
|
userId, err := strconv.Atoi(datum.Member.(string))
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
//查询用户数据,FindOne带缓存
|
//查询用户数据,FindOne带缓存
|
||||||
user, err := l.svcCtx.AppUser.FindOne(l.ctx, id)
|
user, err := l.svcCtx.AppUser.FindOne(l.ctx, uint64(userId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -54,7 +58,7 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
|||||||
Nickname: user.Nickname,
|
Nickname: user.Nickname,
|
||||||
Avatar: user.Avatar,
|
Avatar: user.Avatar,
|
||||||
Score: uint32(datum.Score),
|
Score: uint32(datum.Score),
|
||||||
Rank: uint32(i), //todo
|
Rank: uint32(i) + 1,
|
||||||
Self: user.Id == at.UserId,
|
Self: user.Id == at.UserId,
|
||||||
}
|
}
|
||||||
if user.Id == at.UserId {
|
if user.Id == at.UserId {
|
||||||
@ -63,7 +67,6 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
|||||||
}
|
}
|
||||||
resp.RankingData = append(resp.RankingData, data)
|
resp.RankingData = append(resp.RankingData, data)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !flag {
|
if !flag {
|
||||||
userRank, err = l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId, req.Type)
|
userRank, err = l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId, req.Type)
|
||||||
@ -71,11 +74,11 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
|||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
l.Logger.Debugf("userRank: %+v", userRank)
|
|
||||||
userRank.Self = true
|
userRank.Self = true
|
||||||
resp.RankingData = append(resp.RankingData, userRank)
|
|
||||||
}
|
}
|
||||||
l.Logger.Debugf("resp: %+v", resp)
|
resp.RankingData = append(resp.RankingData, userRank)
|
||||||
|
|
||||||
|
l.Logger.Debugf("userRank: %+v", userRank)
|
||||||
|
l.Logger.Debugf("resp: %+v", resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -26,20 +26,20 @@ func GetRankingsCacheKey(appId, t uint64) string {
|
|||||||
|
|
||||||
// SetList 向排行榜中添加成员及其分数
|
// SetList 向排行榜中添加成员及其分数
|
||||||
func (r *Ranking) SetList(ctx context.Context, key string, data ...redis.Z) {
|
func (r *Ranking) SetList(ctx context.Context, key string, data ...redis.Z) {
|
||||||
r.c.ZAdd(ctx, EcpmRankingsListPrefix+key, data...)
|
r.c.ZAdd(ctx, key, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetList 获取排行榜,按照分数从高到低排序
|
// GetList 获取排行榜,按照分数从高到低排序
|
||||||
func (r *Ranking) GetList(ctx context.Context, key string, start, stop int64) (data []redis.Z, err error) {
|
func (r *Ranking) GetList(ctx context.Context, key string, start, stop int64) (data []redis.Z, err error) {
|
||||||
return r.c.ZRevRangeWithScores(ctx, EcpmRankingsListPrefix+key, start, stop).Result()
|
return r.c.ZRevRangeWithScores(ctx, key, start, stop).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRank 获取指定成员在排行榜中的排名(排名从 0 开始,分数越高排名越靠前)
|
// GetRank 获取指定成员在排行榜中的排名(排名从 0 开始,分数越高排名越靠前)
|
||||||
func (r *Ranking) GetRank(ctx context.Context, key, member string) (rank int64, err error) {
|
func (r *Ranking) GetRank(ctx context.Context, key, member string) (rank int64, err error) {
|
||||||
return r.c.ZRevRank(ctx, EcpmRankingsListPrefix+key, member).Result()
|
return r.c.ZRevRank(ctx, key, member).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetScore 获取指定成员在排行榜中的分数
|
// GetScore 获取指定成员在排行榜中的分数
|
||||||
func (r *Ranking) GetScore(ctx context.Context, key, member string) (score float64, err error) {
|
func (r *Ranking) GetScore(ctx context.Context, key, member string) (score float64, err error) {
|
||||||
return r.c.ZScore(ctx, EcpmRankingsListPrefix+key, member).Result()
|
return r.c.ZScore(ctx, key, member).Result()
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
|||||||
|
|
||||||
//添加排行榜数据
|
//添加排行榜数据
|
||||||
for s, scores := range rankList {
|
for s, scores := range rankList {
|
||||||
data := make([]redis.Z, len(scores))
|
data := make([]redis.Z, 0, len(scores))
|
||||||
for _, score := range scores {
|
for _, score := range scores {
|
||||||
data = append(data, redis.Z{
|
data = append(data, redis.Z{
|
||||||
Score: float64(score.Score),
|
Score: float64(score.Score),
|
||||||
|
@ -73,7 +73,7 @@ func (m *customGameScoreModel) userRankCacheKey(userId, appId, t uint64) string
|
|||||||
func (m *customGameScoreModel) FindAllRankList(ctx context.Context) (resp map[string][]GameScore, err error) {
|
func (m *customGameScoreModel) FindAllRankList(ctx context.Context) (resp map[string][]GameScore, err error) {
|
||||||
list := make([]GameScore, 0)
|
list := make([]GameScore, 0)
|
||||||
resp = make(map[string][]GameScore)
|
resp = make(map[string][]GameScore)
|
||||||
err = m.QueryRowNoCacheCtx(ctx, &list, "select DISTINCT app_account,t from game_score")
|
err = m.QueryRowsPartialNoCacheCtx(ctx, &list, "select DISTINCT app_account,t from game_score")
|
||||||
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ func (m *customGameScoreModel) FindAllRankList(ctx context.Context) (resp map[st
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *customGameScoreModel) FindAllScore(ctx context.Context, appId uint64, t uint64) (resp []GameScore, err error) {
|
func (m *customGameScoreModel) FindAllScore(ctx context.Context, appId uint64, t uint64) (resp []GameScore, err error) {
|
||||||
err = m.QueryRowNoCacheCtx(ctx, &resp, "select * from game_score where app_account = ? and t = ?", appId, t)
|
err = m.QueryRowsPartialNoCacheCtx(ctx, &resp, "select * from game_score where app_account = ? and t = ? order by score desc", appId, t)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user