This commit is contained in:
xiabin 2025-01-22 12:02:12 +08:00
parent f6536f6ad7
commit 6e4c296218
10 changed files with 65 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
.idea .idea
data/ data/
game_open_api/logs/ game_open_api/logs/
game_open_api
game_open_api.exe game_open_api.exe

View File

@ -12,6 +12,6 @@ type Base {
} }
type PageBase { type PageBase {
Page int `json:"page" form:"page,default=1"` Page int `json:"page,default=1" form:"page,default=1"`
PageSize int `json:"page_size" form:"page_size,default=20"` PageSize int `json:"page_size,default=20" form:"page_size,default=20"`
} }

View File

@ -4,7 +4,7 @@ import "common.api"
type SetUserGameScoreRequest { type SetUserGameScoreRequest {
Score uint64 `json:"score"` Score uint64 `json:"score"`
Type uint64 `json:"type"` Type uint64 `json:"type,default=0" form:"type,default=0"`
} }
type RankingData { type RankingData {
@ -22,8 +22,8 @@ type RankingResponse {
} }
type RankingListRequest { type RankingListRequest {
Type uint64 `json:"type" form:"type"` Type uint64 `json:"type,default=0" form:"type,default=0"`
Page PageBase `json:"page" form:"page"` PageBase
} }

View File

@ -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)
}
}
}

View File

@ -73,7 +73,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
}, },
rest.WithJwt(serverCtx.Config.Auth.AccessSecret), rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
rest.WithPrefix("/v1/game"), rest.WithPrefix("/v1/game"),
rest.WithTimeout(3000*time.Millisecond), rest.WithTimeout(30000*time.Millisecond),
rest.WithMaxBytes(1048576), rest.WithMaxBytes(1048576),
) )

View File

@ -29,8 +29,6 @@ func (l *GetEcpmLogic) GetEcpm() (result bool, errNeverNil error) {
errNeverNil = err errNeverNil = err
return return
} }
at.AppIdStr = "tt5bc32d7ec4f6ccb907"
at.OpenId = "_0005ITFnsqTvXpTC7wqg6yvQ1_mXhk1TbOv"
res, err := l.svcCtx.DouyinCli.GetEcpmData(at.AppIdStr, at.OpenId, time.Now().Format(time.DateOnly)) res, err := l.svcCtx.DouyinCli.GetEcpmData(at.AppIdStr, at.OpenId, time.Now().Format(time.DateOnly))
if err != nil { if err != nil {
@ -38,6 +36,8 @@ func (l *GetEcpmLogic) GetEcpm() (result bool, errNeverNil error) {
return return
} }
l.Logger.Infof("ecpm data result: %+v", res)
ecpm, err := l.svcCtx.DouyinCli.GetEcpm(res) ecpm, err := l.svcCtx.DouyinCli.GetEcpm(res)
if err != nil { if err != nil {
l.Logger.Error(err.Error()) l.Logger.Error(err.Error())

View File

@ -29,7 +29,7 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,8 +20,8 @@ type DouyinCode2TokenRequest struct {
} }
type PageBase struct { type PageBase struct {
Page int `json:"page" form:"page,default=1"` Page int `json:"page,default=1" form:"page,default=1"`
PageSize int `json:"page_size" form:"page_size,default=20"` PageSize int `json:"page_size,default=20" form:"page_size,default=20"`
} }
type RankingData struct { type RankingData struct {
@ -34,8 +34,8 @@ type RankingData struct {
} }
type RankingListRequest struct { type RankingListRequest struct {
Type uint64 `json:"type" form:"type"` Type uint64 `json:"type,default=0" form:"type,default=0"`
Page PageBase `json:"page" form:"page"` PageBase
} }
type RankingResponse struct { type RankingResponse struct {
@ -50,7 +50,7 @@ type SetAppUserRequest struct {
type SetUserGameScoreRequest struct { type SetUserGameScoreRequest struct {
Score uint64 `json:"score"` Score uint64 `json:"score"`
Type uint64 `json:"type"` Type uint64 `json:"type,default=0" form:"type,default=0"`
} }
type UserId struct { type UserId struct {

View File

@ -48,7 +48,7 @@ type GameAppConfig struct {
} }
func (m *defaultAppAccountModel) FindAll(ctx context.Context) (*[]*GameAppConfig, error) { 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 var resp []*GameAppConfig
err := m.QueryRowCtx(ctx, &resp, ecpmAppAccountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { 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" 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"

View File

@ -9,6 +9,7 @@ import (
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
"sync"
) )
var _ GameScoreModel = (*customGameScoreModel)(nil) var _ GameScoreModel = (*customGameScoreModel)(nil)
@ -33,6 +34,8 @@ type (
customGameScoreModel struct { customGameScoreModel struct {
*defaultGameScoreModel *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) return fmt.Sprintf(":page:%d:pageSize:%d", page.Page, page.PageSize)
} }
// rankListCacheKey 排行榜缓存key // 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) return fmt.Sprintf("%s:rankList:appId:%d:t:%d", cacheEcpmGameScorePrefix, appId, t)
} }
// userScoreCacheKey 用户游戏分数缓存key // 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) return fmt.Sprintf("%suserId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t)
} }
// userRankCacheKey 用户游戏排名缓存key // 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) return fmt.Sprintf("%srank:userId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t)
} }
// GetRankList 获取排行榜列表 // GetRankList 获取排行榜列表
func (m *defaultGameScoreModel) GetRankList(ctx context.Context, appId uint64, t uint64, page types.PageBase) (resp []types.RankingData, err error) { func (m *customGameScoreModel) 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)
cacheKey := m.rankListCacheKey(appId, t) + m.pageCacheKey(page)
err = m.QueryRowCtx(ctx, &resp, cacheKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { err = m.QueryRowCtx(ctx, &resp, cacheKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := ` query := `
SELECT SELECT
@ -80,9 +82,8 @@ FROM
WHERE WHERE
game_score.app_account = ? game_score.app_account = ?
AND game_score.t = ? AND game_score.t = ?
LIMIT ? LIMIT 20`
OFFSET ?` return conn.QueryRowsPartialCtx(ctx, v, query, appId, t)
return conn.QueryRowsPartialCtx(ctx, v, query, appId, t, limit, offset)
}) })
switch { switch {
case err == nil: case err == nil:
@ -95,7 +96,7 @@ WHERE
} }
// GetUserRank 获取用户排名 // 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 var res []types.RankingData
err = m.QueryRowCtx(ctx, &res, m.userRankCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error { err = m.QueryRowCtx(ctx, &res, m.userRankCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := ` query := `
@ -137,14 +138,14 @@ WHERE
} }
// FindUserScore 查询用户游戏分数 // 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 ( var (
resp GameScore resp GameScore
err error err error
) )
err = m.QueryRowCtx(ctx, &resp, m.userScoreCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) 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) 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 { switch err {
case nil: case nil:
@ -157,7 +158,7 @@ func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId, userId
} }
// UpdateScore 更新游戏分数记录 // UpdateScore 更新游戏分数记录
func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, data *GameScore) error { func (m *customGameScoreModel) UpdateScore(ctx context.Context, data *GameScore) error {
cacheKeys := []string{ cacheKeys := []string{
m.rankListCacheKey(data.AppAccount, data.T), // 删除排行榜缓存 m.rankListCacheKey(data.AppAccount, data.T), // 删除排行榜缓存
m.userRankCacheKey(data.AppUserId, 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 创建游戏分数记录 // 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) { _, 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) query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, gameScoreRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T) return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T)
}, m.rankListCacheKey(data.AppAccount, data.T)) }, cacheKeys...)
return err return err
} }