Files
bl/logic/controller/item_buy.go
昔念 502d497dce ```
refactor(controller): 重构控制器函数命名和代码注释

- 重命名 EGG 函数为 EggGamePlay,更新宠物生成逻辑
- 重命名 Leiyi 函数为 GetLeiyiTrainStatus
- 重命名 Cacthpet 函数为 CatchPet,添加详细函数注释
- 为 ArenaSetOwner、ArenaFightOwner、ArenaGetInfo、ArenaUpfight、ArenaOwnerAcce
  等擂台相关函数添加注释前缀
- 重命名 PETKing 函数为 PetKing
- 重命名 FRESH_CHOICE_FIGHT_LEVEL 函数为 FreshChoiceFightLevel,添加详细参数说明
- 重命名 BuyMItem 函数为 BuyMultipleItems
- 重命名 ITEM_S
2025-12-24 19:03:11 +08:00

117 lines
3.0 KiB
Go

package controller
import (
"blazing/common/data/xmlres"
"blazing/common/socket/errorcode"
"blazing/logic/service/item"
"blazing/logic/service/player"
"github.com/gogf/gf/v2/util/gconv"
)
// 防止封包通过领取来获取道具
// BuyItem 购买单个道具
func (h Controller) BuyItem(data *item.BuyInboundInfo, c *player.Player) (result *item.BuyOutboundInfo, err errorcode.ErrorCode) {
itemInfo, exists := xmlres.ItemsMAP[int(data.ItemId)]
if !exists {
return &item.BuyOutboundInfo{Coins: c.Info.Coins}, 0
}
// 免费道具直接添加
if itemInfo.Price == 0 {
if c.ItemAdd(data.ItemId, data.Count) {
return &item.BuyOutboundInfo{
ItemId: data.ItemId,
Level: 1,
Count: data.Count,
Coins: c.Info.Coins,
}, 0
}
return &item.BuyOutboundInfo{Coins: c.Info.Coins}, 0
}
// 需要付费的道具
totalCost := data.Count * uint32(itemInfo.Price)
if !c.UseCoins(totalCost) {
return &item.BuyOutboundInfo{Coins: c.Info.Coins}, errorcode.ErrorCodes.ErrSunDouInsufficient10016
}
if c.ItemAdd(data.ItemId, data.Count) {
c.Info.Coins -= totalCost
return &item.BuyOutboundInfo{
ItemId: data.ItemId,
Level: 1,
Count: data.Count,
Coins: c.Info.Coins,
}, 0
}
// 购买失败,返还赛尔豆
c.Info.Coins += totalCost
return &item.BuyOutboundInfo{Coins: c.Info.Coins}, 0
}
// BuyMultipleItems 批量购买道具
func (h Controller) BuyMultipleItems(data *item.BuyMultiInboundInfo, c *player.Player) (result *item.BuyMultiOutboundInfo, err errorcode.ErrorCode) {
for _, itemID := range data.ItemIds {
itemInfo, exists := xmlres.ItemsMAP[int(itemID)]
if !exists {
continue
}
// 免费道具直接添加
if itemInfo.Price == 0 {
c.ItemAdd(itemID, 1)
continue
}
// 需要付费的道具
if !c.UseCoins(uint32(itemInfo.Price)) {
break
}
if c.ItemAdd(itemID, 1) {
c.Info.Coins -= uint32(itemInfo.Price)
}
}
return &item.BuyMultiOutboundInfo{
Coins: c.Info.Coins,
}, 0
}
// BuyGoldItem 使用金豆购买商品
func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, c *player.Player) (result *item.S2C_GoldBuyProductInfo, err errorcode.ErrorCode) {
product, exists := xmlres.GoldProductMap[int(data.ProductID)]
if !exists {
return nil, errorcode.ErrorCodes.ErrSystemError
}
useGold := uint32(data.Count) * uint32(gconv.Float64(product.Price)*100)
if !c.UseGold(useGold) {
return &item.S2C_GoldBuyProductInfo{
Gold: c.User.GetGold(uint(c.Info.UserID)),
PayGold: 0,
Reserved: 0,
}, errorcode.ErrorCodes.ErrXinDouInsufficient
}
addSuccess := c.ItemAdd(uint32(gconv.Uint32(product.ItemID)), uint32(data.Count))
if addSuccess {
c.User.UpdateGold(c.Info.UserID, -int64(useGold))
return &item.S2C_GoldBuyProductInfo{
Gold: c.User.GetGold(uint(c.Info.UserID)),
PayGold: useGold,
Reserved: 0,
}, 0
}
return &item.S2C_GoldBuyProductInfo{
Gold: c.User.GetGold(uint(c.Info.UserID)),
PayGold: 0,
Reserved: 0,
}, errorcode.ErrorCodes.ErrSystemError
}