refactor(controller): 重构控制器代码结构并优化战斗状态检查 - 添加包级注释说明controller包的功能和架构设计 - 重命名Controller结构体注释,使其更清晰明了 - 添加ParseCmd函数的
117 lines
2.9 KiB
Go
117 lines
2.9 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
|
|
}
|
|
|
|
// BuyMItem 批量购买道具
|
|
func (h Controller) BuyMItem(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
|
|
}
|