go_dreamfactory/modules/moonfantasy/api_buy.go

60 lines
1.7 KiB
Go

package moonfantasy
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.MoonfantasyBuyReq) (code pb.ErrorCode) {
if req.BuyNum == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
///询问怪物是否可以挑战
func (this *apiComp) Buy(session comm.IUserSession, req *pb.MoonfantasyBuyReq) (code pb.ErrorCode, data proto.Message) {
var (
umfantasy *pb.DBUserMFantasy
challengeD *cfg.GameDreamlandChallengeData
need []*cfg.Gameatn
err error
)
if code = this.BuyCheck(session, req); code != pb.ErrorCode_Success {
return
}
if umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil && err != mgo.MongodbNil {
code = pb.ErrorCode_CacheReadError
return
}
need = make([]*cfg.Gameatn, 0)
for i := int32(0); i < req.BuyNum; i++ {
if challengeD, err = this.module.configure.GetchallengeData(umfantasy.BuyNum + i + 1); err != nil {
code = pb.ErrorCode_ConfigNoFound
return
}
if challengeD == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
need = append(need, challengeD.Need...)
}
if code = this.module.ConsumeRes(session, need, true); code != pb.ErrorCode_Success {
return
}
umfantasy.BuyNum += req.BuyNum
umfantasy.BattleNum += req.BuyNum
this.module.modelUserMF.Change(session.GetUserId(), map[string]interface{}{
"buyNum": umfantasy.BuyNum,
"battleNum": umfantasy.BattleNum,
})
session.SendMsg(string(this.module.GetType()), "buy", &pb.MoonfantasyBuyResp{Issucc: true, BattleNum: umfantasy.BattleNum})
return
}