refactor(controller): 优化战斗和道具购买控制器的代码结构 - 为函数添加详细的参数和返回值注释说明 - 将参数名从通用的 'c' 重命名为更具描述性的 'player' - 重命名局部变量以提高代码可读性,如 mo -> monster, moinfo -> monsterInfo - 修复变量命名不一致问题,如 taskid -> taskID, cancpet -> canCapture - 统一变量命名规范,使用驼峰命名法 - 为 processMonID 函数添加功能说明注释 - 重命名 handleNPCFightSpecial 函数参数 petid -> petID ```
125 lines
3.5 KiB
Go
125 lines
3.5 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 购买单个道具
|
|
// data: 包含购买道具信息的输入数据
|
|
// player: 当前玩家对象
|
|
// 返回: 购买结果和错误码
|
|
func (h Controller) BuyItem(data *item.BuyInboundInfo, player *player.Player) (result *item.BuyOutboundInfo, err errorcode.ErrorCode) {
|
|
itemInfo, exists := xmlres.ItemsMAP[int(data.ItemId)]
|
|
if !exists {
|
|
return &item.BuyOutboundInfo{Coins: player.Info.Coins}, 0
|
|
}
|
|
|
|
// 免费道具直接添加
|
|
if itemInfo.Price == 0 {
|
|
if player.ItemAdd(data.ItemId, data.Count) {
|
|
return &item.BuyOutboundInfo{
|
|
ItemId: data.ItemId,
|
|
Level: 1,
|
|
Count: data.Count,
|
|
Coins: player.Info.Coins,
|
|
}, 0
|
|
}
|
|
return &item.BuyOutboundInfo{Coins: player.Info.Coins}, 0
|
|
}
|
|
|
|
// 需要付费的道具
|
|
totalCost := data.Count * uint32(itemInfo.Price)
|
|
if !player.UseCoins(totalCost) {
|
|
return &item.BuyOutboundInfo{Coins: player.Info.Coins}, errorcode.ErrorCodes.ErrSunDouInsufficient10016
|
|
}
|
|
|
|
if player.ItemAdd(data.ItemId, data.Count) {
|
|
player.Info.Coins -= totalCost
|
|
return &item.BuyOutboundInfo{
|
|
ItemId: data.ItemId,
|
|
Level: 1,
|
|
Count: data.Count,
|
|
Coins: player.Info.Coins,
|
|
}, 0
|
|
}
|
|
|
|
// 购买失败,返还赛尔豆
|
|
player.Info.Coins += totalCost
|
|
return &item.BuyOutboundInfo{Coins: player.Info.Coins}, 0
|
|
}
|
|
|
|
// BuyMultipleItems 批量购买道具
|
|
// data: 包含批量购买道具信息的输入数据
|
|
// player: 当前玩家对象
|
|
// 返回: 批量购买结果和错误码
|
|
func (h Controller) BuyMultipleItems(data *item.BuyMultiInboundInfo, player *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 {
|
|
player.ItemAdd(itemID, 1)
|
|
continue
|
|
}
|
|
|
|
// 需要付费的道具
|
|
if !player.UseCoins(uint32(itemInfo.Price)) {
|
|
break
|
|
}
|
|
|
|
if player.ItemAdd(itemID, 1) {
|
|
player.Info.Coins -= uint32(itemInfo.Price)
|
|
}
|
|
}
|
|
|
|
return &item.BuyMultiOutboundInfo{
|
|
Coins: player.Info.Coins,
|
|
}, 0
|
|
}
|
|
|
|
// BuyGoldItem 使用金豆购买商品
|
|
// data: 包含金豆购买商品信息的输入数据
|
|
// player: 当前玩家对象
|
|
// 返回: 金豆购买结果和错误码
|
|
func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, player *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 !player.UseGold(useGold) {
|
|
return &item.S2C_GoldBuyProductInfo{
|
|
Gold: player.User.GetGold(uint(player.Info.UserID)),
|
|
PayGold: 0,
|
|
Reserved: 0,
|
|
}, errorcode.ErrorCodes.ErrXinDouInsufficient
|
|
}
|
|
|
|
addSuccess := player.ItemAdd(uint32(gconv.Uint32(product.ItemID)), uint32(data.Count))
|
|
if addSuccess {
|
|
player.User.UpdateGold(player.Info.UserID, -int64(useGold))
|
|
return &item.S2C_GoldBuyProductInfo{
|
|
Gold: player.User.GetGold(uint(player.Info.UserID)),
|
|
PayGold: useGold,
|
|
Reserved: 0,
|
|
}, 0
|
|
}
|
|
|
|
return &item.S2C_GoldBuyProductInfo{
|
|
Gold: player.User.GetGold(uint(player.Info.UserID)),
|
|
PayGold: 0,
|
|
Reserved: 0,
|
|
}, errorcode.ErrorCodes.ErrSystemError
|
|
} |