147 lines
3.8 KiB
Go
147 lines
3.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"blazing/common/data/xmlres"
|
|
"blazing/common/socket/errorcode"
|
|
"blazing/modules/config/service"
|
|
|
|
"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.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
|
|
}
|
|
|
|
// 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.GetCoins(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)]
|
|
pro := service.NewShopService().Get(data.ProductID)
|
|
if pro == nil {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
var usegold uint64
|
|
var addSuccess bool
|
|
switch data.Type {
|
|
case 0:
|
|
if pro.SeerdouPrice == 0 {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
|
|
if !player.GetCoins(data.Count * uint32(pro.SeerdouPrice)) {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
usegold = uint64(data.Count) * uint64(pro.SeerdouPrice)
|
|
|
|
case 1:
|
|
if pro.JindouPrice == 0 {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
|
|
if !player.UseGold(int64(data.Count) * int64(pro.JindouPrice) * 100) {
|
|
return nil, errorcode.ErrorCodes.ErrSystemError
|
|
}
|
|
usegold = uint64(data.Count) * uint64(pro.JindouPrice*100)
|
|
|
|
}
|
|
|
|
addSuccess = player.ItemAdd(uint32(gconv.Uint32(pro.ProductID)), uint32(data.Count))
|
|
if addSuccess {
|
|
|
|
switch data.Type {
|
|
case 0:
|
|
player.Info.Coins -= uint32(usegold)
|
|
|
|
case 1:
|
|
player.User.UpdateGold(player.Info.UserID, -int64(usegold))
|
|
|
|
}
|
|
|
|
player.SendPackCmd(1105, item.GoldOnlineRemainOutboundInfo{
|
|
Coin: player.Info.Coins,
|
|
GoldNumber: uint32(player.User.GetGold(uint(player.Info.UserID))),
|
|
})
|
|
}
|
|
|
|
return nil, -1
|
|
}
|