diff --git a/.gitignore b/.gitignore index 5e5da64..94a9fc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -data/ \ No newline at end of file +data/ +game_open_api/logs/ \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 147e888..3cffdb4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,7 +19,7 @@ services: - ./data/mysql/data:/var/lib/mysql - ./data/mysql/logs:/var/log/mysql ##初始化的脚本,初始化我们存放的init.sql文件 - - ./sql:/docker-entrypoint-initdb.d/ + - ./data/sql:/docker-entrypoint-initdb.d/ - ./data/mysql/conf:/etc/mysql/conf.d ports: - "3306:3306" diff --git a/game_open_api/app_user.api b/game_open_api/app_user.api index cc554b3..ff11d57 100644 --- a/game_open_api/app_user.api +++ b/game_open_api/app_user.api @@ -4,6 +4,7 @@ import ( "game.api" "douyin.api" "wechat.api" + "common.api" ) type UserId { @@ -24,6 +25,6 @@ type SetAppUserRequest { ) service game_open_api-api { @handler appUserSetUser - post /set_app_account (SetAppUserRequest) returns (UserId) + post /set_app_account (SetAppUserRequest) returns (Base) } diff --git a/game_open_api/auth.api b/game_open_api/auth.api deleted file mode 100644 index d39595e..0000000 --- a/game_open_api/auth.api +++ /dev/null @@ -1,6 +0,0 @@ -syntax = "v1" - - -type Auth { - Token string `json:"token"` -} \ No newline at end of file diff --git a/game_open_api/common.api b/game_open_api/common.api new file mode 100644 index 0000000..600f59a --- /dev/null +++ b/game_open_api/common.api @@ -0,0 +1,12 @@ +syntax = "v1" + + +type Auth { + Base + Token string `json:"token"` +} + +type Base { + Code int `json:"code"` + Message string `json:"message,omitempty"` +} \ No newline at end of file diff --git a/game_open_api/douyin.api b/game_open_api/douyin.api index ffe1583..bb8322c 100644 --- a/game_open_api/douyin.api +++ b/game_open_api/douyin.api @@ -1,6 +1,6 @@ syntax = "v1" -import "auth.api" +import "common.api" type DouyinCode2UserIdRequest { AppId string `form:"appId"` diff --git a/game_open_api/etc/gameopenapi-api.yaml b/game_open_api/etc/gameopenapi-api.yaml index 288f8c4..9068432 100644 --- a/game_open_api/etc/gameopenapi-api.yaml +++ b/game_open_api/etc/gameopenapi-api.yaml @@ -9,4 +9,10 @@ Cache: Auth: AccessSecret: youtu123! - AccessExpire: 3600 + AccessExpire: 86400 + +Log: + Level: debug +# Mode: file + Path: ./logs + MaxSize: 100 \ No newline at end of file diff --git a/game_open_api/game.api b/game_open_api/game.api index dcce9f5..187d95d 100644 --- a/game_open_api/game.api +++ b/game_open_api/game.api @@ -1,5 +1,7 @@ syntax = "v1" +import "common.api" + type SetUserGameScoreRequest { Score uint64 `json:"score"` } @@ -27,6 +29,6 @@ service game_open_api-api { get /ranking/list returns ([]RankingData) @handler rankingSetScore - post /ranking/set_score (SetUserGameScoreRequest) + post /ranking/set_score (SetUserGameScoreRequest) returns (Base) } diff --git a/game_open_api/gameOpenApi-Api.go b/game_open_api/gameOpenApi-Api.go index e881f97..8025718 100644 --- a/game_open_api/gameOpenApi-Api.go +++ b/game_open_api/gameOpenApi-Api.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "youtu_server/game_open_api/internal/config" "youtu_server/game_open_api/internal/handler" "youtu_server/game_open_api/internal/svc" diff --git a/game_open_api/internal/handler/app_user/appUserSetUserHandler.go b/game_open_api/internal/handler/app_user/appUserSetUserHandler.go index 7822402..1b9bdfa 100644 --- a/game_open_api/internal/handler/app_user/appUserSetUserHandler.go +++ b/game_open_api/internal/handler/app_user/appUserSetUserHandler.go @@ -20,6 +20,7 @@ func AppUserSetUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := app_user.NewAppUserSetUserLogic(r.Context(), svcCtx) resp, err := l.AppUserSetUser(&req) if err != nil { + resp.Code = -1 httpx.ErrorCtx(r.Context(), w, err) } else { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go b/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go index 32318a2..4f45729 100644 --- a/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go +++ b/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go @@ -20,6 +20,7 @@ func DouyinCode2UserIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := douyin.NewDouyinCode2UserIdLogic(r.Context(), svcCtx) resp, err := l.DouyinCode2UserId(&req) if err != nil { + resp.Code = -1 httpx.ErrorCtx(r.Context(), w, err) } else { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/game_open_api/internal/handler/game/rankingSetScoreHandler.go b/game_open_api/internal/handler/game/rankingSetScoreHandler.go index 9739d3b..d76ab73 100644 --- a/game_open_api/internal/handler/game/rankingSetScoreHandler.go +++ b/game_open_api/internal/handler/game/rankingSetScoreHandler.go @@ -18,11 +18,12 @@ func RankingSetScoreHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } l := game.NewRankingSetScoreLogic(r.Context(), svcCtx) - err := l.RankingSetScore(&req) + resp, err := l.RankingSetScore(&req) if err != nil { + resp.Code = -1 httpx.ErrorCtx(r.Context(), w, err) } else { - httpx.Ok(w) + httpx.OkJsonCtx(r.Context(), w, resp) } } } diff --git a/game_open_api/internal/handler/wechat/wechatCode2UserIdHandler.go b/game_open_api/internal/handler/wechat/wechatCode2UserIdHandler.go index a13a605..027a889 100644 --- a/game_open_api/internal/handler/wechat/wechatCode2UserIdHandler.go +++ b/game_open_api/internal/handler/wechat/wechatCode2UserIdHandler.go @@ -20,6 +20,7 @@ func WechatCode2UserIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := wechat.NewWechatCode2UserIdLogic(r.Context(), svcCtx) resp, err := l.WechatCode2UserId(&req) if err != nil { + resp.Code = -1 httpx.ErrorCtx(r.Context(), w, err) } else { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/game_open_api/internal/logic/app_user/appUserSetUserLogic.go b/game_open_api/internal/logic/app_user/appUserSetUserLogic.go index 1a0f80a..9bdec9b 100644 --- a/game_open_api/internal/logic/app_user/appUserSetUserLogic.go +++ b/game_open_api/internal/logic/app_user/appUserSetUserLogic.go @@ -24,7 +24,8 @@ func NewAppUserSetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ap } } -func (l *AppUserSetUserLogic) AppUserSetUser(req *types.SetAppUserRequest) (resp *types.UserId, err error) { +func (l *AppUserSetUserLogic) AppUserSetUser(req *types.SetAppUserRequest) (resp *types.Base, err error) { + resp = &types.Base{} at, err := svc.GetCtxToken(l.ctx) if err != nil { return diff --git a/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go b/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go index b9d3b93..5029241 100644 --- a/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go +++ b/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go @@ -2,7 +2,6 @@ package douyin import ( "context" - "errors" "github.com/golang-jwt/jwt/v4" "time" "youtu_server/game_open_api/internal/app_api_helper" @@ -43,7 +42,7 @@ func (l *DouyinCode2UserIdLogic) DouyinCode2UserId(req *types.DouyinCode2UserIdR } if res.Errcode != 0 { - err = errors.New(res.Errmsg) + resp.Message = res.Errmsg return } diff --git a/game_open_api/internal/logic/game/rankingSetScoreLogic.go b/game_open_api/internal/logic/game/rankingSetScoreLogic.go index 3cfb455..73e20f5 100644 --- a/game_open_api/internal/logic/game/rankingSetScoreLogic.go +++ b/game_open_api/internal/logic/game/rankingSetScoreLogic.go @@ -2,6 +2,8 @@ package game import ( "context" + "errors" + "github.com/zeromicro/go-zero/core/stores/sqlc" "youtu_server/game_open_api/model" "youtu_server/game_open_api/internal/svc" @@ -24,16 +26,31 @@ func NewRankingSetScoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *R } } -func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreRequest) error { +func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreRequest) (resp *types.Base, err error) { + resp = &types.Base{} at, err := svc.GetCtxToken(l.ctx) if err != nil { - return err + return } - _, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{ - AppUserId: at.UserId, - AppAccount: at.AppId, - Score: req.Score, - }) - return err + oldScore, err := l.svcCtx.GameScore.FindUserScore(l.ctx, at.AppId, at.UserId) + if err != nil && !errors.Is(err, sqlc.ErrNotFound) { + return + } + + if oldScore != nil && req.Score <= oldScore.Score { + return + } + + if oldScore != nil { + err = l.svcCtx.GameScore.UpdateScore(l.ctx, at.AppId, at.UserId, req.Score) + } else { + _, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{ + AppUserId: at.UserId, + AppAccount: at.AppId, + Score: req.Score, + }) + } + + return } diff --git a/game_open_api/internal/types/types.go b/game_open_api/internal/types/types.go index 338677f..31d6c5b 100644 --- a/game_open_api/internal/types/types.go +++ b/game_open_api/internal/types/types.go @@ -4,9 +4,15 @@ package types type Auth struct { + Base Token string `json:"token"` } +type Base struct { + Code int `json:"code"` + Message string `json:"message,omitempty"` +} + type DouyinCode2UserIdRequest struct { AppId string `form:"appId"` Code string `form:"code,optional"` diff --git a/game_open_api/model/gamescoremodel.go b/game_open_api/model/gamescoremodel.go index c878a4e..27a9481 100644 --- a/game_open_api/model/gamescoremodel.go +++ b/game_open_api/model/gamescoremodel.go @@ -2,6 +2,7 @@ package model import ( "context" + "database/sql" "errors" "fmt" "github.com/zeromicro/go-zero/core/stores/cache" @@ -13,8 +14,9 @@ import ( var _ GameScoreModel = (*customGameScoreModel)(nil) const ( - cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:" - cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:" + cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:" + cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:" + cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:" ) type ( @@ -24,6 +26,8 @@ type ( gameScoreModel GetRankList(ctx context.Context, appId uint64, userId uint64) ([]types.RankingData, error) GetUserRank(ctx context.Context, appId uint64, userId uint64) (*types.RankingData, error) + FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) + UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error } customGameScoreModel struct { @@ -92,3 +96,32 @@ WHERE return nil, err } } + +func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) { + ecpmGameScoreAppUserIdKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId) + var ( + resp GameScore + err error + ) + err = m.QueryRowCtx(ctx, &resp, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := fmt.Sprintf("select %s from %s where `app_user_id` = ? and app_account = ? limit 1", gameScoreRows, m.table) + return conn.QueryRowCtx(ctx, v, query, userId, appId) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error { + ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId) + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("update %s set `score` = ? where `app_account` = ? and `app_user_id` = ?", m.table) + return conn.ExecCtx(ctx, query, score, appId, userId) + }, ecpmGameScoreAppUserIdAppAccountKey) + return err +} diff --git a/game_open_api/model/gamescoremodel_gen.go b/game_open_api/model/gamescoremodel_gen.go index 87ac083..5daa11d 100644 --- a/game_open_api/model/gamescoremodel_gen.go +++ b/game_open_api/model/gamescoremodel_gen.go @@ -23,8 +23,7 @@ var ( gameScoreRowsExpectAutoSet = strings.Join(stringx.Remove(gameScoreFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") gameScoreRowsWithPlaceHolder = strings.Join(stringx.Remove(gameScoreFieldNames, "`app_user_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" - cacheEcpmGameScoreAppUserIdPrefix = "cache:ecpm:gameScore:appUserId:" - cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:appUserId:appAccount:" + cacheEcpmGameScoreAppUserIdPrefix = "cache:ecpm:gameScore:appUserId:" ) type ( diff --git a/game_open_api/wechat.api b/game_open_api/wechat.api index 1a033d2..8d59a84 100644 --- a/game_open_api/wechat.api +++ b/game_open_api/wechat.api @@ -1,6 +1,6 @@ syntax = "v1" -import "auth.api" +import "common.api" type WechatCode2UserIdRequest {