feat: 新增CDK兑换冠名接口

This commit is contained in:
xinian
2026-04-08 18:12:02 +08:00
parent 1ca0ff344e
commit 28b6386963
4 changed files with 120 additions and 57 deletions

View File

@@ -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"`
}

View File

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

View File

@@ -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"`

View File

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