From 28b6386963162cd31ed01fd57e8a2f4be57e85ba Mon Sep 17 00:00:00 2001 From: xinian Date: Wed, 8 Apr 2026 18:12:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9ECDK=E5=85=91=E6=8D=A2?= =?UTF-8?q?=E5=86=A0=E5=90=8D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/controller/inbound_user.go | 11 +-- logic/controller/user_cdk.go | 36 +-------- logic/service/user/cdk.go | 15 +--- modules/player/controller/app/cdk.go | 115 +++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 modules/player/controller/app/cdk.go diff --git a/logic/controller/inbound_user.go b/logic/controller/inbound_user.go index 9f136667f..365b8bfd3 100644 --- a/logic/controller/inbound_user.go +++ b/logic/controller/inbound_user.go @@ -70,13 +70,6 @@ type ChangeTitleInboundInfo struct { // C2S_GET_GIFT_COMPLETE 定义请求或响应数据结构。 type C2S_GET_GIFT_COMPLETE struct { - Head common.TomeeHeader `cmd:"2801" struc:"skip"` - PassText string `struc:"[16]byte"` - Type uint32 - ServerID uint32 - ServerName string `struc:"[16]byte"` -} - -type C2S_GET_DONATION_SERVER_IDS struct { - Head common.TomeeHeader `cmd:"2802" struc:"skip"` + Head common.TomeeHeader `cmd:"2801" struc:"skip"` + PassText string `struc:"[16]byte"` } diff --git a/logic/controller/user_cdk.go b/logic/controller/user_cdk.go index b5750b25e..1d2bd7816 100644 --- a/logic/controller/user_cdk.go +++ b/logic/controller/user_cdk.go @@ -2,7 +2,6 @@ package controller import ( "blazing/common/socket/errorcode" - "blazing/cool" logicplayer "blazing/logic/service/player" "blazing/logic/service/user" configservice "blazing/modules/config/service" @@ -11,16 +10,9 @@ import ( "time" ) -// DonationServerIDs 返回当前可用于捐赠冠名的服务器ID列表。 -func (h Controller) DonationServerIDs(data *C2S_GET_DONATION_SERVER_IDS, player *logicplayer.Player) (result *user.S2C_GET_DONATION_SERVER_IDS, err errorcode.ErrorCode) { - return &user.S2C_GET_DONATION_SERVER_IDS{ - ServerIDs: configservice.NewServerService().GetDonationAvailableServerIDs(), - }, 0 -} - // CDK 处理控制器请求。 func (h Controller) CDK(data *C2S_GET_GIFT_COMPLETE, player *logicplayer.Player) (result *user.S2C_GET_GIFT_COMPLETE, err errorcode.ErrorCode) { - result = &user.S2C_GET_GIFT_COMPLETE{Type: data.Type} + result = &user.S2C_GET_GIFT_COMPLETE{} cdkCode := strings.Trim(data.PassText, "\x00") cdkService := configservice.NewCdkService() @@ -39,32 +31,6 @@ func (h Controller) CDK(data *C2S_GET_GIFT_COMPLETE, player *logicplayer.Player) if !player.Service.Cdk.CanGet(uint32(r.ID)) { return } - - if r.CDKType == configservice.CDKTypeServerNaming { - if data.Type != configservice.CDKTypeServerNaming { - return nil, errorcode.ErrorCodes.ErrSystemError - } - - serverName := cool.Filter.Replace(strings.Trim(data.ServerName, "\x00"), '*') - if data.ServerID == 0 || serverName == "" { - return nil, errorcode.ErrorCodes.ErrSystemError - } - - serverInfo, useErr := cdkService.UseServerNamingCDK(nil, cdkCode, data.Head.UserID, data.ServerID, serverName) - if useErr != nil { - return nil, errorcode.ErrorCodes.ErrSystemError - } - - result.Flag = 1 - result.ServerID = serverInfo.OnlineID - result.ServerName = serverInfo.Name - player.Service.Cdk.Log(uint32(r.ID)) - return result, 0 - } - - if data.Type == configservice.CDKTypeServerNaming { - return nil, errorcode.ErrorCodes.ErrSystemError - } if !cdkService.Set(cdkCode) { return nil, errorcode.ErrorCodes.ErrMolecularCodeGiftsGone } diff --git a/logic/service/user/cdk.go b/logic/service/user/cdk.go index dfcacdaf3..57f6e3321 100644 --- a/logic/service/user/cdk.go +++ b/logic/service/user/cdk.go @@ -5,32 +5,21 @@ import "blazing/logic/service/common" // C2S_GET_GIFT_COMPLETE 礼品兑换完成协议 // 前端到后端 type C2S_GET_GIFT_COMPLETE struct { - Head common.TomeeHeader `cmd:"2801" struc:"skip"` - PassText string `struc:"[16]byte"` - Type uint32 - ServerID uint32 - ServerName string `struc:"[16]byte"` + Head common.TomeeHeader `cmd:"2801" struc:"skip"` + PassText string `struc:"[16]byte"` } // S2C_GET_GIFT_COMPLETE 礼品兑换完成协议 // 后端到前端 type S2C_GET_GIFT_COMPLETE struct { Flag uint32 `json:"flag"` - Type uint32 `json:"type"` Tile uint32 `json:"tile"` - ServerID uint32 `json:"server_id"` - ServerName string `struc:"[16]byte" json:"server_name"` GiftListLen uint32 `struc:"sizeof=GiftList"` GiftList []GiftInfo `json:"giftList"` PetGiftLen uint32 `struc:"sizeof=PetGift"` PetGift []PetGiftInfo `json:"petGiftList"` } -type S2C_GET_DONATION_SERVER_IDS struct { - ServerIDsLen uint32 `struc:"sizeof=ServerIDs" json:"server_ids_len"` - ServerIDs []uint32 `json:"server_ids"` -} - // GiftInfo 礼品信息 type GiftInfo struct { GiftID int64 `struc:"uint32"` diff --git a/modules/player/controller/app/cdk.go b/modules/player/controller/app/cdk.go new file mode 100644 index 000000000..e46bb90e9 --- /dev/null +++ b/modules/player/controller/app/cdk.go @@ -0,0 +1,115 @@ +package app + +import ( + "blazing/cool" + configservice "blazing/modules/config/service" + playerservice "blazing/modules/player/service" + "context" + "strings" + "time" + + "github.com/gogf/gf/v2/frame/g" +) + +type CdkController struct { + *cool.Controller +} + +func init() { + controller := &CdkController{ + &cool.Controller{ + Prefix: "/seer/game/cdk", + Api: []string{}, + Service: configservice.NewCdkService(), + }, + } + cool.RegisterController(controller) +} + +type DonationServerListReq struct { + g.Meta `path:"/donation/serverIds" method:"GET"` + UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"` + Session string `json:"session" v:"required#session不能为空"` +} + +type DonationRedeemReq struct { + g.Meta `path:"/donation/redeem" method:"POST"` + UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"` + Session string `json:"session" v:"required#session不能为空"` + CDKCode string `json:"cdk_code" v:"required#CDK不能为空"` + ServerID uint32 `json:"server_id" v:"required|min:1#服务器ID不能为空|服务器ID非法"` + ServerName string `json:"server_name" v:"required#服务器名称不能为空"` +} + +type DonationRedeemRes struct { + ServerID uint32 `json:"server_id"` + ServerName string `json:"server_name"` + OwnerID uint32 `json:"owner_id"` + ExpireTime time.Time `json:"expire_time"` +} + +func (c *CdkController) DonationServerIDs(ctx context.Context, req *DonationServerListReq) (res *cool.BaseRes, err error) { + if err = g.Validator().Data(req).Run(ctx); err != nil { + return cool.Fail(err.Error()), nil + } + if err = validateGameSession(req.UserID, req.Session); err != nil { + return cool.Fail(err.Error()), nil + } + + return cool.Ok(g.Map{ + "server_ids": configservice.NewServerService().GetDonationAvailableServerIDs(), + }), nil +} + +func (c *CdkController) DonationRedeem(ctx context.Context, req *DonationRedeemReq) (res *cool.BaseRes, err error) { + if err = g.Validator().Data(req).Run(ctx); err != nil { + return cool.Fail(err.Error()), nil + } + if err = validateGameSession(req.UserID, req.Session); err != nil { + return cool.Fail(err.Error()), nil + } + + cdkCode := strings.TrimSpace(req.CDKCode) + if cdkCode == "" { + return cool.Fail("CDK不能为空"), nil + } + + serverName := strings.TrimSpace(req.ServerName) + serverName = strings.Trim(cool.Filter.Replace(serverName, '*'), "*") + if serverName == "" { + return cool.Fail("服务器名称不能为空"), nil + } + + cdkService := configservice.NewCdkService() + cdkInfo := cdkService.Get(cdkCode) + if cdkInfo == nil { + return cool.Fail("CDK不存在或已被使用"), nil + } + if cdkInfo.CDKType != configservice.CDKTypeServerNaming { + return cool.Fail("CDK类型不匹配"), nil + } + if cdkInfo.BindUserId != 0 && cdkInfo.BindUserId != req.UserID { + return cool.Fail("CDK已绑定其他用户"), nil + } + if !cdkInfo.ValidEndTime.IsZero() && cdkInfo.ValidEndTime.Before(time.Now()) { + return cool.Fail("CDK已过期"), nil + } + + playerCdkService := playerservice.NewCdkService(req.UserID) + if !playerCdkService.CanGet(uint32(cdkInfo.ID)) { + return cool.Fail("CDK已领取"), nil + } + + serverInfo, useErr := cdkService.UseServerNamingCDK(ctx, cdkCode, req.UserID, req.ServerID, serverName) + if useErr != nil { + return cool.Fail(useErr.Error()), nil + } + + playerCdkService.Log(uint32(cdkInfo.ID)) + return cool.Ok(&DonationRedeemRes{ + ServerID: serverInfo.OnlineID, + ServerName: serverInfo.Name, + OwnerID: serverInfo.Owner, + ExpireTime: serverInfo.ExpireTime, + }), nil +}