diff --git a/.gitignore b/.gitignore index 35ed6c1..cd4ea1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .idea data/ game_open_api/logs/ -game_open_api game_open_api.exe \ No newline at end of file diff --git a/game_open_api/common.api b/game_open_api/common.api index d5787bf..7c58e99 100644 --- a/game_open_api/common.api +++ b/game_open_api/common.api @@ -12,6 +12,6 @@ type Base { } type PageBase { - Page int `json:"page" form:"page,default=1"` - PageSize int `json:"page_size" form:"page_size,default=20"` + Page int `json:"page,default=1" form:"page,default=1"` + PageSize int `json:"page_size,default=20" form:"page_size,default=20"` } \ No newline at end of file diff --git a/game_open_api/game.api b/game_open_api/game.api index c2295c0..4834f7e 100644 --- a/game_open_api/game.api +++ b/game_open_api/game.api @@ -4,7 +4,7 @@ import "common.api" type SetUserGameScoreRequest { Score uint64 `json:"score"` - Type uint64 `json:"type"` + Type uint64 `json:"type,default=0" form:"type,default=0"` } type RankingData { @@ -22,8 +22,8 @@ type RankingResponse { } type RankingListRequest { - Type uint64 `json:"type" form:"type"` - Page PageBase `json:"page" form:"page"` + Type uint64 `json:"type,default=0" form:"type,default=0"` + PageBase } diff --git a/game_open_api/internal/handler/game/ranking_list_handler.go b/game_open_api/internal/handler/game/ranking_list_handler.go new file mode 100644 index 0000000..db10ee9 --- /dev/null +++ b/game_open_api/internal/handler/game/ranking_list_handler.go @@ -0,0 +1,28 @@ +package game + +import ( + "net/http" + + "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/logic/game" + "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc" + "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func RankingListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.RankingListRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := game.NewRankingListLogic(r.Context(), svcCtx) + resp, err := l.RankingList(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/game_open_api/internal/handler/routes.go b/game_open_api/internal/handler/routes.go index 8a877fc..871bc3e 100644 --- a/game_open_api/internal/handler/routes.go +++ b/game_open_api/internal/handler/routes.go @@ -73,7 +73,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, rest.WithJwt(serverCtx.Config.Auth.AccessSecret), rest.WithPrefix("/v1/game"), - rest.WithTimeout(3000*time.Millisecond), + rest.WithTimeout(30000*time.Millisecond), rest.WithMaxBytes(1048576), ) diff --git a/game_open_api/internal/logic/douyin/get_ecpm_logic.go b/game_open_api/internal/logic/douyin/get_ecpm_logic.go index 0fb4796..471dfce 100644 --- a/game_open_api/internal/logic/douyin/get_ecpm_logic.go +++ b/game_open_api/internal/logic/douyin/get_ecpm_logic.go @@ -29,8 +29,6 @@ func (l *GetEcpmLogic) GetEcpm() (result bool, errNeverNil error) { errNeverNil = err return } - at.AppIdStr = "tt5bc32d7ec4f6ccb907" - at.OpenId = "_0005ITFnsqTvXpTC7wqg6yvQ1_mXhk1TbOv" res, err := l.svcCtx.DouyinCli.GetEcpmData(at.AppIdStr, at.OpenId, time.Now().Format(time.DateOnly)) if err != nil { @@ -38,6 +36,8 @@ func (l *GetEcpmLogic) GetEcpm() (result bool, errNeverNil error) { return } + l.Logger.Infof("ecpm data result: %+v", res) + ecpm, err := l.svcCtx.DouyinCli.GetEcpm(res) if err != nil { l.Logger.Error(err.Error()) diff --git a/game_open_api/internal/logic/game/ranking_list_logic.go b/game_open_api/internal/logic/game/ranking_list_logic.go index ecc8c9b..a585db8 100644 --- a/game_open_api/internal/logic/game/ranking_list_logic.go +++ b/game_open_api/internal/logic/game/ranking_list_logic.go @@ -29,7 +29,7 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ return nil, err } - resp.RankingData, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, req.Type, req.Page) + resp.RankingData, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, req.Type, req.PageBase) if err != nil { return nil, err } diff --git a/game_open_api/internal/types/types.go b/game_open_api/internal/types/types.go index 2a58d9b..ede7998 100644 --- a/game_open_api/internal/types/types.go +++ b/game_open_api/internal/types/types.go @@ -20,8 +20,8 @@ type DouyinCode2TokenRequest struct { } type PageBase struct { - Page int `json:"page" form:"page,default=1"` - PageSize int `json:"page_size" form:"page_size,default=20"` + Page int `json:"page,default=1" form:"page,default=1"` + PageSize int `json:"page_size,default=20" form:"page_size,default=20"` } type RankingData struct { @@ -34,8 +34,8 @@ type RankingData struct { } type RankingListRequest struct { - Type uint64 `json:"type" form:"type"` - Page PageBase `json:"page" form:"page"` + Type uint64 `json:"type,default=0" form:"type,default=0"` + PageBase } type RankingResponse struct { @@ -50,7 +50,7 @@ type SetAppUserRequest struct { type SetUserGameScoreRequest struct { Score uint64 `json:"score"` - Type uint64 `json:"type"` + Type uint64 `json:"type,default=0" form:"type,default=0"` } type UserId struct { diff --git a/game_open_api/model/app_account_model.go b/game_open_api/model/app_account_model.go index 2c2ab55..3db0e49 100644 --- a/game_open_api/model/app_account_model.go +++ b/game_open_api/model/app_account_model.go @@ -48,7 +48,7 @@ type GameAppConfig struct { } func (m *defaultAppAccountModel) FindAll(ctx context.Context) (*[]*GameAppConfig, error) { - ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, "*") + ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, "all") var resp []*GameAppConfig err := m.QueryRowCtx(ctx, &resp, ecpmAppAccountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := "select `app_id`,`secret`,`ecpm_value`,`ecpm_view`,`type` from `app_account` left join douyin_ecpm_config on app_account.id = douyin_ecpm_config.app_account_id" diff --git a/game_open_api/model/game_score_model.go b/game_open_api/model/game_score_model.go index 27e7eb6..9bb380d 100644 --- a/game_open_api/model/game_score_model.go +++ b/game_open_api/model/game_score_model.go @@ -9,6 +9,7 @@ import ( "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" + "sync" ) var _ GameScoreModel = (*customGameScoreModel)(nil) @@ -33,6 +34,8 @@ type ( customGameScoreModel struct { *defaultGameScoreModel + rankListKey []string + mutex sync.Mutex } ) @@ -43,29 +46,28 @@ func NewGameScoreModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Optio } } -func (m *defaultGameScoreModel) pageCacheKey(page types.PageBase) string { +func (m *customGameScoreModel) pageCacheKey(page types.PageBase) string { return fmt.Sprintf(":page:%d:pageSize:%d", page.Page, page.PageSize) } // rankListCacheKey 排行榜缓存key -func (m *defaultGameScoreModel) rankListCacheKey(appId uint64, t uint64) string { +func (m *customGameScoreModel) rankListCacheKey(appId uint64, t uint64) string { return fmt.Sprintf("%s:rankList:appId:%d:t:%d", cacheEcpmGameScorePrefix, appId, t) } // userScoreCacheKey 用户游戏分数缓存key -func (m *defaultGameScoreModel) userScoreCacheKey(userId, appId, t uint64) string { +func (m *customGameScoreModel) userScoreCacheKey(userId, appId, t uint64) string { return fmt.Sprintf("%suserId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t) } // userRankCacheKey 用户游戏排名缓存key -func (m *defaultGameScoreModel) userRankCacheKey(userId, appId, t uint64) string { +func (m *customGameScoreModel) userRankCacheKey(userId, appId, t uint64) string { return fmt.Sprintf("%srank:userId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t) } // GetRankList 获取排行榜列表 -func (m *defaultGameScoreModel) GetRankList(ctx context.Context, appId uint64, t uint64, page types.PageBase) (resp []types.RankingData, err error) { - limit, offset := page.PageSize, (page.Page-1)*page.PageSize - cacheKey := m.rankListCacheKey(appId, t) + m.pageCacheKey(page) +func (m *customGameScoreModel) GetRankList(ctx context.Context, appId uint64, t uint64, page types.PageBase) (resp []types.RankingData, err error) { + cacheKey := m.rankListCacheKey(appId, t) err = m.QueryRowCtx(ctx, &resp, cacheKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := ` SELECT @@ -80,9 +82,8 @@ FROM WHERE game_score.app_account = ? AND game_score.t = ? - LIMIT ? - OFFSET ?` - return conn.QueryRowsPartialCtx(ctx, v, query, appId, t, limit, offset) + LIMIT 20` + return conn.QueryRowsPartialCtx(ctx, v, query, appId, t) }) switch { case err == nil: @@ -95,7 +96,7 @@ WHERE } // GetUserRank 获取用户排名 -func (m *defaultGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64, t uint64) (resp types.RankingData, err error) { +func (m *customGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64, t uint64) (resp types.RankingData, err error) { var res []types.RankingData err = m.QueryRowCtx(ctx, &res, m.userRankCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := ` @@ -137,14 +138,14 @@ WHERE } // FindUserScore 查询用户游戏分数 -func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId, userId, t uint64) (*GameScore, error) { +func (m *customGameScoreModel) FindUserScore(ctx context.Context, appId, userId, t uint64) (*GameScore, error) { var ( resp GameScore err error ) err = m.QueryRowCtx(ctx, &resp, m.userScoreCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where app_account = ? and `app_user_id` = ? and t = ? limit 1", gameScoreRows, m.table) - return conn.QueryRowCtx(ctx, v, query, userId, appId) + return conn.QueryRowCtx(ctx, v, query, appId, userId, t) }) switch err { case nil: @@ -157,7 +158,7 @@ func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId, userId } // UpdateScore 更新游戏分数记录 -func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, data *GameScore) error { +func (m *customGameScoreModel) UpdateScore(ctx context.Context, data *GameScore) error { cacheKeys := []string{ m.rankListCacheKey(data.AppAccount, data.T), // 删除排行榜缓存 m.userRankCacheKey(data.AppUserId, data.AppAccount, data.T), // 删除用户排行缓存 @@ -172,10 +173,15 @@ func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, data *GameScore } // CreateScore 创建游戏分数记录 -func (m *defaultGameScoreModel) CreateScore(ctx context.Context, data *GameScore) error { +func (m *customGameScoreModel) CreateScore(ctx context.Context, data *GameScore) error { + cacheKeys := []string{ + m.rankListCacheKey(data.AppAccount, data.T), // 删除排行榜缓存 + m.userRankCacheKey(data.AppUserId, data.AppAccount, data.T), // 删除用户排行缓存 + m.userScoreCacheKey(data.AppUserId, data.AppAccount, data.T), // 删除用户分数缓存 + } _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, gameScoreRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T) - }, m.rankListCacheKey(data.AppAccount, data.T)) + }, cacheKeys...) return err }