94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package items
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"time"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) BuyPhysicalCheck(session comm.IUserSession, req *pb.ItemsBuyPhysicalReq) (errdata *pb.ErrorData) {
|
|
if req.Amount <= 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//购买
|
|
func (this *apiComp) BuyPhysical(session comm.IUserSession, req *pb.ItemsBuyPhysicalReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
user *pb.DBUserExpand
|
|
psitem *cfg.Gameatn
|
|
needs []*cfg.Gameatn
|
|
haveneeds []*cfg.Gameatn
|
|
sale []*cfg.Gameatn
|
|
)
|
|
if errdata = this.BuyPhysicalCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
if user, err = this.module.ModuleUser.GetUserExpand(session.GetUserId()); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
Message: req.String(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if needs = this.module.ModuleTools.GetGlobalConf().PsBuy; needs == nil && len(needs) == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: comm.NewNotFoundConfErr(modelName, "global.json", "PsBuy").Error(),
|
|
}
|
|
return
|
|
}
|
|
if psitem = this.module.ModuleTools.GetGlobalConf().PsItem; psitem == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: comm.NewNotFoundConfErr(modelName, "global.json", "PsItem").Error(),
|
|
}
|
|
return
|
|
}
|
|
if time.Unix(user.PhysicalbuyLasttime, 0).Day() != time.Now().Day() { //不是同一天 重置购买次数
|
|
user.Physicalbuynum = 0
|
|
}
|
|
|
|
if req.Amount+uint32(user.Physicalbuynum) > uint32(len(needs)) {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ItemsBuyPsUpperLimit,
|
|
Title: pb.ErrorCode_ItemsBuyPsUpperLimit.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
haveneeds = make([]*cfg.Gameatn, 0)
|
|
for i := uint32(0); i < req.Amount; i++ {
|
|
haveneeds = append(haveneeds, needs[int(int32(i)+user.Physicalbuynum)])
|
|
}
|
|
|
|
if errdata = this.module.ConsumeRes(session, haveneeds, true); errdata != nil {
|
|
return
|
|
}
|
|
|
|
sale = []*cfg.Gameatn{{A: psitem.A, T: psitem.T, N: int32(req.Amount)}}
|
|
|
|
if errdata = this.module.DispenseRes(session, sale, true); errdata != nil {
|
|
return
|
|
}
|
|
user.Physicalbuynum += int32(req.Amount)
|
|
user.PhysicalbuyLasttime = time.Now().Unix()
|
|
this.module.ModuleUser.ChangeUserExpand(session.GetUserId(), map[string]interface{}{
|
|
"physicalbuynum": user.Physicalbuynum,
|
|
"physicalbuyLasttime": user.PhysicalbuyLasttime,
|
|
})
|
|
session.SendMsg(string(this.module.GetType()), "buyphysical", &pb.ItemsBuyPhysicalResp{Issucc: true, PhysicalBuyNum: user.Physicalbuynum, Asets: []*pb.UserAssets{{A: psitem.A, T: psitem.T, N: int32(req.Amount)}}})
|
|
return
|
|
}
|