244 lines
6.7 KiB
Go
244 lines
6.7 KiB
Go
package shop
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/utils"
|
|
"math"
|
|
)
|
|
|
|
// 参数校验
|
|
func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.ShopBuyReq) (errdata *pb.ErrorData) {
|
|
if req.ShopType == 0 || req.Gid == 0 || req.BuyNum <= 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// /获取用户商品列表
|
|
func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
conf *cfg.GameShopitemData
|
|
condiIds []int32
|
|
condis []*pb.ConIProgress
|
|
condisMap map[int32]*pb.ConIProgress
|
|
addbuynum int32
|
|
condi *pb.ConIProgress
|
|
shopData *pb.DBShop
|
|
good *pb.UserShopGood
|
|
record *pb.UserShopData
|
|
need []*cfg.Gameatn
|
|
give []*cfg.Gameatn
|
|
award []*pb.UserAtno
|
|
tasks []*pb.BuriedParam = make([]*pb.BuriedParam, 0)
|
|
ok bool
|
|
)
|
|
if errdata = this.BuyCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
|
|
if shopData, err = this.module.modelShop.QueryUserShopData(session.GetUserId()); err != nil { //没有购买记录
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
record = shopData.Shops[int32(req.ShopType)]
|
|
if record == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: "no found shop !",
|
|
}
|
|
return
|
|
}
|
|
|
|
for _, v := range record.Items {
|
|
if v.Id == req.Gid {
|
|
good = v
|
|
}
|
|
}
|
|
|
|
if good == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
Message: fmt.Sprintf("no found good:%d", req.Gid),
|
|
}
|
|
return
|
|
}
|
|
|
|
if conf, err = this.module.configure.GetShopItemsConfigure(good.Gid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
condiIds = append(condiIds, conf.Buymaxcond...)
|
|
condiIds = append(condiIds, conf.Unlock...)
|
|
if _, condis, err = this.module.ModuleBuried.CheckCondition(session, condiIds...); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_SystemError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
condisMap = make(map[int32]*pb.ConIProgress)
|
|
for _, v := range condis {
|
|
condisMap[v.Conid] = v
|
|
}
|
|
ok = true
|
|
for _, v := range conf.Buymaxcond {
|
|
if condi, ok = condisMap[v]; !ok || condi.State == pb.BuriedItemFinishState_buried_unfinish {
|
|
ok = false
|
|
break
|
|
}
|
|
}
|
|
if ok {
|
|
addbuynum = conf.Addminnum
|
|
}
|
|
for _, v := range conf.Unlock {
|
|
if condi, ok = condisMap[v]; !ok || condi.State == pb.BuriedItemFinishState_buried_unfinish {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Message: fmt.Sprintf("%d is lock!", v),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
need = make([]*cfg.Gameatn, len(conf.Need))
|
|
for i, v := range conf.Need {
|
|
need[i] = &cfg.Gameatn{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: int32(math.Ceil(float64(v.N)*float64(conf.Sale)/float64(1000))) * req.BuyNum,
|
|
}
|
|
}
|
|
|
|
give = make([]*cfg.Gameatn, len(conf.Iteminfo))
|
|
for i, v := range conf.Iteminfo {
|
|
give[i] = &cfg.Gameatn{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: v.N * req.BuyNum,
|
|
}
|
|
}
|
|
|
|
if conf.Buyminnum+addbuynum-good.Buy < req.BuyNum {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ShopGoodsIsSoldOut,
|
|
Title: pb.ErrorCode_ShopGoodsIsSoldOut.ToString(),
|
|
}
|
|
return
|
|
}
|
|
good.Buy += req.BuyNum
|
|
if errdata = this.module.ConsumeRes(session, need, true); errdata != nil {
|
|
return
|
|
}
|
|
|
|
this.module.modelShop.Change(session.GetUserId(), map[string]interface{}{"shops": shopData.Shops})
|
|
if !conf.Preview {
|
|
if errdata, award = this.module.DispenseAtno(session, give, true); errdata != nil {
|
|
return
|
|
}
|
|
} else {
|
|
if errdata = this.module.equip.AddEquipment(session, record.Preview[req.Gid].Equipment); errdata != nil {
|
|
return
|
|
}
|
|
award = []*pb.UserAtno{{
|
|
A: comm.EquipmentType,
|
|
T: record.Preview[req.Gid].Equipment.CId,
|
|
N: 1,
|
|
O: record.Preview[req.Gid].Equipment.Id,
|
|
}}
|
|
}
|
|
|
|
//随机任务
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype64, 1))
|
|
for _, v := range give {
|
|
if v.A == comm.ItemType {
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype65, v.N, utils.ToInt32(v.T)))
|
|
}
|
|
}
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype66, 1, int32(req.ShopType)))
|
|
for _, v := range need {
|
|
if v.A == comm.AttrType {
|
|
switch v.T {
|
|
case comm.ResGold:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 1))
|
|
break
|
|
case comm.ResExp:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 3))
|
|
break
|
|
case comm.ResDiamond:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.VipExp:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.ResTaskActive:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.ResFriend:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 4))
|
|
break
|
|
case comm.StarCoin:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 9))
|
|
break
|
|
case comm.SociatyCoin:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 8))
|
|
break
|
|
case comm.ArenaCoin:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 11))
|
|
break
|
|
case comm.ResPs:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 12))
|
|
break
|
|
case comm.Moongold:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 14))
|
|
break
|
|
case comm.Talent1:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.Talent2:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.Talent3:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.Talent4:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 2))
|
|
break
|
|
case comm.Merchantmoney:
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 15))
|
|
break
|
|
case comm.Integral:
|
|
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 15))
|
|
break
|
|
|
|
case comm.Consumemoney:
|
|
break
|
|
}
|
|
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype67, v.N, utils.ToInt32(v.T))
|
|
}
|
|
}
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype241, 1))
|
|
session.SendMsg(string(this.module.GetType()), "buy", &pb.ShopBuyResp{Award: award})
|
|
if len(tasks) > 0 {
|
|
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
|
|
this.module.ModuleBuried.TriggerBuried(session, tasks...)
|
|
this.module.WriteUserLog(session.GetUserId(), req, comm.GMResAddType, "ShopBuyResq", award)
|
|
})
|
|
}
|
|
return
|
|
}
|