go_dreamfactory/modules/items/api_sellItem.go
2023-10-17 13:54:53 +08:00

77 lines
2.2 KiB
Go

package items
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
//参数校验
func (this *apiComp) SellItemCheck(session comm.IUserSession, req *pb.ItemsSellItemReq) (errdata *pb.ErrorData) {
if req.GridId == "" || req.Amount <= 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
//分解道具
func (this *apiComp) SellItem(session comm.IUserSession, req *pb.ItemsSellItemReq) (errdata *pb.ErrorData) {
var (
err error
item *pb.DB_UserItemData
itemcf *cfg.GameItemData
sale []*cfg.Gameatn
atno []*pb.UserAtno
)
if errdata = this.SellItemCheck(session, req); errdata != nil {
return
}
if item, err = this.module.modelItems.QueryUserPackByGridId(session.GetUserId(), req.GridId); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
if itemcf, err = this.module.configure.GetItemConfigure(item.ItemId); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigurationException,
Title: pb.ErrorCode_ConfigurationException.ToString(),
}
return
}
if itemcf.Sale == nil || len(itemcf.Sale) == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ItemsUseNoCanSell,
Title: pb.ErrorCode_ItemsUseNoCanSell.ToString(),
}
return
}
if req.Amount > item.Amount {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
this.module.Errorf("SellItemCheck over all amount:[%d:%d]", req.Amount, item.Amount)
return
}
sale = make([]*cfg.Gameatn, len(itemcf.Sale))
for i, v := range itemcf.Sale {
temp := *v
sale[i] = &temp
sale[i].N = v.N * int32(req.Amount)
}
if errdata, atno = this.module.DispenseAtno(session, sale, true); errdata != nil {
return
}
item.Amount = item.Amount - req.Amount
if errdata = this.module.AddItemforGrid(session, req.GridId, -1*int32(req.Amount), true); errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), "sellitem", &pb.ItemsSellItemResp{GridId: req.GridId, Amount: req.Amount, Issucc: true, Asets: atno})
return
}