This commit is contained in:
123
modules/player/service/cdk_reward.go
Normal file
123
modules/player/service/cdk_reward.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/common/data"
|
||||
baseservice "blazing/modules/base/service"
|
||||
configservice "blazing/modules/config/service"
|
||||
"blazing/modules/player/model"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CdkRewardPet struct {
|
||||
PetID uint32 `json:"pet_id"`
|
||||
CatchTime uint32 `json:"catch_time"`
|
||||
}
|
||||
|
||||
type CdkRewardResult struct {
|
||||
CdkID uint32 `json:"cdk_id"`
|
||||
Items []data.ItemInfo `json:"items,omitempty"`
|
||||
Pets []CdkRewardPet `json:"pets,omitempty"`
|
||||
TitleIDs []uint32 `json:"title_ids,omitempty"`
|
||||
Coins int64 `json:"coins,omitempty"`
|
||||
Gold int64 `json:"gold,omitempty"`
|
||||
FreeGold int64 `json:"free_gold,omitempty"`
|
||||
ExpPool int64 `json:"exp_pool,omitempty"`
|
||||
EVPool int64 `json:"ev_pool,omitempty"`
|
||||
}
|
||||
|
||||
// GrantConfigReward 按 cdk 配置 ID 发放奖励,不处理兑换码次数和领取资格校验。
|
||||
func (s *CdkService) GrantConfigReward(cdkID uint32) (*CdkRewardResult, error) {
|
||||
cfg := configservice.NewCdkService().GetByID(cdkID)
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("绑定的CDK不存在")
|
||||
}
|
||||
if cfg.BindUserId != 0 && cfg.BindUserId != s.userid {
|
||||
return nil, fmt.Errorf("CDK已绑定其他用户")
|
||||
}
|
||||
if !cfg.ValidEndTime.IsZero() && cfg.ValidEndTime.Before(time.Now()) {
|
||||
return nil, fmt.Errorf("绑定的CDK已过期")
|
||||
}
|
||||
|
||||
result := &CdkRewardResult{CdkID: cdkID}
|
||||
infoService := NewInfoService(s.userid)
|
||||
playerInfo := infoService.GetLogin()
|
||||
if playerInfo == nil {
|
||||
return nil, fmt.Errorf("玩家角色不存在")
|
||||
}
|
||||
|
||||
var (
|
||||
infoDirty bool
|
||||
bagItems []data.ItemInfo
|
||||
)
|
||||
|
||||
appendRewardItem := func(itemID uint32, count int64) {
|
||||
if itemID == 0 || count <= 0 {
|
||||
return
|
||||
}
|
||||
switch itemID {
|
||||
case 1:
|
||||
result.Coins += count
|
||||
playerInfo.Coins += count
|
||||
infoDirty = true
|
||||
case 3:
|
||||
result.ExpPool += count
|
||||
playerInfo.ExpPool += count
|
||||
infoDirty = true
|
||||
case 5:
|
||||
result.Gold += count
|
||||
case 9:
|
||||
result.EVPool += count
|
||||
playerInfo.EVPool += count
|
||||
infoDirty = true
|
||||
default:
|
||||
bagItems = append(bagItems, data.ItemInfo{ItemId: int64(itemID), ItemCnt: count})
|
||||
}
|
||||
}
|
||||
|
||||
for _, rewardID := range cfg.ItemRewardIds {
|
||||
itemInfo := configservice.NewItemService().GetItemCount(rewardID)
|
||||
appendRewardItem(uint32(itemInfo.ItemId), itemInfo.ItemCnt)
|
||||
}
|
||||
|
||||
if result.Gold != 0 {
|
||||
baseservice.NewBaseSysUserService().UpdateGold(s.userid, result.Gold*100)
|
||||
}
|
||||
if result.FreeGold != 0 {
|
||||
baseservice.NewBaseSysUserService().UpdateFreeGold(s.userid, result.FreeGold*100)
|
||||
}
|
||||
if len(bagItems) > 0 {
|
||||
items, err := NewItemService(s.userid).AddItems(bagItems)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Items = items
|
||||
}
|
||||
|
||||
for _, rewardID := range cfg.ElfRewardIds {
|
||||
pet := configservice.NewPetRewardService().Get(rewardID)
|
||||
if pet == nil {
|
||||
continue
|
||||
}
|
||||
petInfo := model.GenPetInfo(int(pet.MonID), int(pet.DV), int(pet.Nature), int(pet.Effect), int(pet.Lv), nil, 0)
|
||||
catchTime, err := NewPetService(s.userid).PetAdd(petInfo, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Pets = append(result.Pets, CdkRewardPet{
|
||||
PetID: uint32(pet.MonID),
|
||||
CatchTime: catchTime,
|
||||
})
|
||||
}
|
||||
|
||||
if cfg.TitleRewardIds != 0 {
|
||||
NewTitleService(s.userid).Give(cfg.TitleRewardIds)
|
||||
result.TitleIDs = append(result.TitleIDs, cfg.TitleRewardIds)
|
||||
}
|
||||
|
||||
if infoDirty {
|
||||
infoService.Save(*playerInfo)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user