diff --git a/logic/controller/user_cdk.go b/logic/controller/user_cdk.go index 1d2bd781..09c684a2 100644 --- a/logic/controller/user_cdk.go +++ b/logic/controller/user_cdk.go @@ -35,7 +35,12 @@ func (h Controller) CDK(data *C2S_GET_GIFT_COMPLETE, player *logicplayer.Player) return nil, errorcode.ErrorCodes.ErrMolecularCodeGiftsGone } - reward, grantErr := playerservice.NewCdkService(data.Head.UserID).GrantConfigReward(uint32(r.ID)) + reward, grantErr := playerservice.NewCdkService(data.Head.UserID).GrantConfigReward( + uint32(r.ID), + func(itemID uint32, count int64) bool { + return player.ItemAdd(int64(itemID), count) + }, + ) if grantErr != nil { return nil, errorcode.ErrorCodes.ErrSystemError } diff --git a/modules/player/service/cdk_reward.go b/modules/player/service/cdk_reward.go index 9cba2976..ed380bbc 100644 --- a/modules/player/service/cdk_reward.go +++ b/modules/player/service/cdk_reward.go @@ -26,8 +26,11 @@ type CdkRewardResult struct { EVPool int64 `json:"ev_pool,omitempty"` } +type CdkSpecialRewardAdder func(itemID uint32, count int64) bool + // GrantConfigReward 按 cdk 配置 ID 发放奖励,不处理兑换码次数和领取资格校验。 -func (s *CdkService) GrantConfigReward(cdkID uint32) (*CdkRewardResult, error) { +// 当传入 specialAdder 时,赛尔豆/累计经验/金豆/学习力会优先走在线玩家加成逻辑。 +func (s *CdkService) GrantConfigReward(cdkID uint32, specialAdders ...CdkSpecialRewardAdder) (*CdkRewardResult, error) { cfg := configservice.NewCdkService().GetByID(cdkID) if cfg == nil { return nil, fmt.Errorf("绑定的CDK不存在") @@ -45,42 +48,72 @@ func (s *CdkService) GrantConfigReward(cdkID uint32) (*CdkRewardResult, error) { if playerInfo == nil { return nil, fmt.Errorf("玩家角色不存在") } + var specialAdder CdkSpecialRewardAdder + if len(specialAdders) > 0 { + specialAdder = specialAdders[0] + } var ( infoDirty bool bagItems []data.ItemInfo ) - appendRewardItem := func(itemID uint32, count int64) { + appendRewardItem := func(itemID uint32, count int64) error { if itemID == 0 || count <= 0 { - return + return nil } switch itemID { case 1: result.Coins += count - playerInfo.Coins += count - infoDirty = true + if specialAdder != nil { + if !specialAdder(itemID, count) { + return fmt.Errorf("在线发放赛尔豆失败") + } + } else { + playerInfo.Coins += count + infoDirty = true + } case 3: result.ExpPool += count - playerInfo.ExpPool += count - infoDirty = true + if specialAdder != nil { + if !specialAdder(itemID, count) { + return fmt.Errorf("在线发放经验池失败") + } + } else { + playerInfo.ExpPool += count + infoDirty = true + } case 5: result.Gold += count + if specialAdder != nil { + if !specialAdder(itemID, count) { + return fmt.Errorf("在线发放金豆失败") + } + } case 9: result.EVPool += count - playerInfo.EVPool += count - infoDirty = true + if specialAdder != nil { + if !specialAdder(itemID, count) { + return fmt.Errorf("在线发放学习力失败") + } + } else { + playerInfo.EVPool += count + infoDirty = true + } default: bagItems = append(bagItems, data.ItemInfo{ItemId: int64(itemID), ItemCnt: count}) } + return nil } for _, rewardID := range cfg.ItemRewardIds { itemInfo := configservice.NewItemService().GetItemCount(rewardID) - appendRewardItem(uint32(itemInfo.ItemId), itemInfo.ItemCnt) + if err := appendRewardItem(uint32(itemInfo.ItemId), itemInfo.ItemCnt); err != nil { + return nil, err + } } - if result.Gold != 0 { + if result.Gold != 0 && specialAdder == nil { baseservice.NewBaseSysUserService().UpdateGold(s.userid, result.Gold*100) } if result.FreeGold != 0 {