refactor: 优化代码结构和逻辑
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blazing/common/data/xmlres"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/modules/config/service"
|
||||
|
||||
@@ -12,73 +11,34 @@ import (
|
||||
// 防止封包通过领取来获取道具
|
||||
|
||||
// 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
|
||||
result = &item.BuyOutboundInfo{Coins: player.Info.Coins}
|
||||
|
||||
bought, err := buySeerdouBackpackItem(player, data.ItemId, data.Count)
|
||||
if err != 0 {
|
||||
return result, err
|
||||
}
|
||||
if !bought {
|
||||
result.Coins = player.Info.Coins
|
||||
return result, 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 := int64(data.Count) * int64(itemInfo.Price)
|
||||
if !player.GetCoins(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
|
||||
result.ItemId = data.ItemId
|
||||
result.Level = 1
|
||||
result.Count = data.Count
|
||||
result.Coins = player.Info.Coins
|
||||
return result, 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(int64(itemID), 1)
|
||||
continue
|
||||
}
|
||||
|
||||
// 需要付费的道具
|
||||
if !player.GetCoins(int64(itemInfo.Price)) {
|
||||
bought, buyErr := buySeerdouBackpackItem(player, int64(itemID), 1)
|
||||
if buyErr == errorcode.ErrorCodes.ErrSunDouInsufficient10016 {
|
||||
break
|
||||
}
|
||||
|
||||
if player.ItemAdd(int64(itemID), 1) {
|
||||
player.Info.Coins -= int64(itemInfo.Price)
|
||||
if buyErr != 0 || !bought {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,11 +48,7 @@ func (h Controller) BuyMultipleItems(data *item.BuyMultiInboundInfo, player *pla
|
||||
}
|
||||
|
||||
// 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)]
|
||||
pro := service.NewShopService().Get(data.ProductID)
|
||||
if pro == nil {
|
||||
return nil, errorcode.ErrorCodes.ErrTooManyProducts
|
||||
@@ -105,15 +61,6 @@ func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, player *player.
|
||||
}
|
||||
}
|
||||
|
||||
// if pro.QuotaType != 0 {
|
||||
// if data.Count > int64(pro.QuotaLimit) {
|
||||
// return nil, errorcode.ErrorCodes.ErrExceedStock
|
||||
// }
|
||||
// if player.Service.Talk.Cheak(0, int(data.ProductID)) {
|
||||
// return nil, errorcode.ErrorCodes.ErrExceedStock
|
||||
// }
|
||||
// }
|
||||
|
||||
var usegold uint64
|
||||
|
||||
switch data.Type {
|
||||
@@ -136,12 +83,10 @@ func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, player *player.
|
||||
return nil, errorcode.ErrorCodes.ErrSystemError
|
||||
}
|
||||
usegold = uint64(data.Count) * uint64(pro.JindouPrice*100)
|
||||
|
||||
}
|
||||
|
||||
switch data.Type {
|
||||
case 0:
|
||||
|
||||
if player.ItemAdd(pro.ProductID, data.Count) {
|
||||
player.Info.Coins -= int64(usegold)
|
||||
}
|
||||
@@ -157,9 +102,6 @@ func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, player *player.
|
||||
if player.ItemAdd(pro.ProductID, data.Count) {
|
||||
player.User.UpdateGold(player.Info.UserID, -int64(usegold))
|
||||
}
|
||||
|
||||
//购买成功,执行记录
|
||||
|
||||
}
|
||||
|
||||
player.SendPackCmd(1105, item.GoldOnlineRemainOutboundInfo{
|
||||
|
||||
Reference in New Issue
Block a user