113 lines
3.3 KiB
Go
113 lines
3.3 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
|
|
maxbuy int32
|
|
need []*cfg.Gameatn
|
|
atno []*pb.UserAtno
|
|
pricekey int32
|
|
)
|
|
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
|
|
}
|
|
pricekey = this.module.ModuleTools.GetGlobalConf().PsBuyGroup
|
|
if maxbuy, err = this.module.ModuleTools.GetPriceGroupLen(pricekey); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_CacheReadError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
need = make([]*cfg.Gameatn, 0)
|
|
for i := int32(0); i < int32(req.Amount); i++ {
|
|
if user.Physicalbuynum+i+1 > maxbuy {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ArenaTicketBuyUp,
|
|
Title: pb.ErrorCode_ArenaTicketBuyUp.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if res, err := this.module.ModuleTools.GetPriceGroupCost(pricekey, user.Physicalbuynum+i+1); len(res) > 0 {
|
|
need = append(need, res...)
|
|
} else {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
if errdata = this.module.ConsumeRes(session, need, true); errdata != nil {
|
|
return
|
|
}
|
|
|
|
sale := []*cfg.Gameatn{{A: psitem.A, T: psitem.T, N: psitem.N * int32(req.Amount)}}
|
|
|
|
if errdata, atno = this.module.DispenseAtno(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: atno})
|
|
return
|
|
}
|