同步商店代码
This commit is contained in:
parent
319ba5a96d
commit
0a17fb1517
@ -1,6 +1,7 @@
|
||||
package shop
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
cfg "go_dreamfactory/sys/configure/structs"
|
||||
@ -10,7 +11,7 @@ import (
|
||||
|
||||
// 参数校验
|
||||
func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.ShopBuyReq) (errdata *pb.ErrorData) {
|
||||
if req.ShopType == 0 || req.GoodsId == 0 || req.BuyNum <= 0 {
|
||||
if req.ShopType == 0 || req.Gid == 0 || req.BuyNum <= 0 {
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_ReqParameterError,
|
||||
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
||||
@ -25,6 +26,7 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata
|
||||
err error
|
||||
conf *cfg.GameShopitemData
|
||||
shopData *pb.DBShop
|
||||
good *pb.UserShopGood
|
||||
filed string
|
||||
record *pb.UserShopData
|
||||
need []*cfg.Gameatn
|
||||
@ -35,14 +37,6 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata
|
||||
return
|
||||
}
|
||||
|
||||
if conf, err = this.module.configure.GetShopItemsConfigure(req.GoodsId); err != nil {
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_SystemError,
|
||||
Title: pb.ErrorCode_SystemError.ToString(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return
|
||||
}
|
||||
if shopData, err = this.module.modelShop.QueryUserShopData(session.GetUserId()); err != nil { //没有购买记录
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_DBError,
|
||||
@ -51,23 +45,6 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata
|
||||
}
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
switch req.ShopType {
|
||||
case pb.ShopType_GoldShop:
|
||||
@ -106,18 +83,64 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata
|
||||
return
|
||||
}
|
||||
if record == nil {
|
||||
record = &pb.UserShopData{
|
||||
Buy: map[int32]int32{},
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_DBError,
|
||||
Title: pb.ErrorCode_DBError.ToString(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range record.Items {
|
||||
if v.Id == req.Gid {
|
||||
good = v
|
||||
}
|
||||
}
|
||||
if conf.Buyminnum-record.Buy[req.GoodsId] < req.BuyNum {
|
||||
|
||||
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_SystemError,
|
||||
Title: pb.ErrorCode_SystemError.ToString(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
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-good.Buy < req.BuyNum {
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_ShopGoodsIsSoldOut,
|
||||
Title: pb.ErrorCode_ShopGoodsIsSoldOut.ToString(),
|
||||
}
|
||||
return
|
||||
}
|
||||
record.Buy[req.GoodsId] += req.BuyNum
|
||||
good.Buy += req.BuyNum
|
||||
if errdata = this.module.ConsumeRes(session, need, true); errdata != nil {
|
||||
return
|
||||
}
|
||||
@ -127,25 +150,21 @@ func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (errdata
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if errdata = this.module.equip.AddEquipment(session, record.Preview[req.GoodsId]); errdata != nil {
|
||||
if errdata = this.module.equip.AddEquipment(session, record.Preview[req.Gid]); errdata != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//随机任务
|
||||
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype64, 1))
|
||||
// this.module.ModuleRtask.SendToRtask(session, 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)))
|
||||
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype65, v.N, utils.ToInt32(v.T))
|
||||
}
|
||||
}
|
||||
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype66, 1, int32(req.ShopType)))
|
||||
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype66, 1, int32(req.ShopType))
|
||||
for _, v := range need {
|
||||
if v.A == comm.AttrType {
|
||||
// tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, utils.ToInt32(v.T)))
|
||||
switch v.T {
|
||||
case comm.ResGold:
|
||||
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype67, v.N, 1))
|
||||
|
@ -25,6 +25,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
var (
|
||||
err error
|
||||
filed string
|
||||
refresh int32
|
||||
shopconf *cfg.GameShopData
|
||||
shopData *pb.DBShop
|
||||
udata *pb.DBUser
|
||||
@ -66,6 +67,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
case pb.ShopType_GoldShop:
|
||||
sdata = shopData.GoldShop
|
||||
filed = "goldShop"
|
||||
refresh = this.module.privilege.GetCountByPrivilegeId(session.GetUserId(), comm.PrivilegeType2)
|
||||
break
|
||||
case pb.ShopType_DiamondShop:
|
||||
sdata = shopData.DiamondShop
|
||||
@ -100,7 +102,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
}
|
||||
if sdata == nil {
|
||||
sdata = &pb.UserShopData{
|
||||
Buy: map[int32]int32{},
|
||||
Items: make([]*pb.UserShopGood, 0),
|
||||
}
|
||||
}
|
||||
if shopconf.Rnum > 0 {
|
||||
@ -127,14 +129,15 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
|
||||
if req.IsManualRefresh && shopconf.Rnum > 0 { //可以手动刷新
|
||||
isrefresh := false
|
||||
refresh := int(this.module.privilege.GetCountByPrivilegeId(session.GetUserId(), comm.PrivilegeType2))
|
||||
if refresh > 0 { //
|
||||
if time.Unix(shopData.RefreshtimegoldShop, 0).Day() < configure.Now().Day() {
|
||||
shopData.RefreshtimegoldShop = configure.Now().Unix()
|
||||
shopData.RefreshnumgoldShop = 0
|
||||
}
|
||||
if int(shopData.RefreshnumgoldShop) < refresh {
|
||||
if shopData.RefreshnumgoldShop < refresh {
|
||||
isrefresh = true
|
||||
shopData.RefreshnumgoldShop++
|
||||
|
||||
}
|
||||
}
|
||||
if !isrefresh {
|
||||
@ -166,15 +169,20 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
}
|
||||
items = append(items, randomGoods(_items))
|
||||
}
|
||||
sdata.Buy = make(map[int32]int32)
|
||||
sdata.LastRefreshTime = configure.Now().Unix()
|
||||
sdata.ManualRefreshNum++
|
||||
sdata.Items = make([]int32, len(items))
|
||||
sdata.Leftfreerefreshnum = refresh - shopData.RefreshnumgoldShop
|
||||
sdata.Items = make([]*pb.UserShopGood, len(items))
|
||||
sdata.Preview = make(map[int32]*pb.DB_Equipment)
|
||||
for i, v := range items {
|
||||
sdata.Items[i] = v.Key
|
||||
id := v.Key*100 + int32(i)
|
||||
sdata.Items[i] = &pb.UserShopGood{
|
||||
Id: id,
|
||||
Gid: v.Key,
|
||||
Buy: 0,
|
||||
}
|
||||
if v.Preview { //是否预览
|
||||
if errdata, sdata.Preview[v.Key] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil {
|
||||
if errdata, sdata.Preview[id] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -213,14 +221,18 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
}
|
||||
items = append(items, randomGoods(_items))
|
||||
}
|
||||
sdata.Buy = make(map[int32]int32)
|
||||
sdata.LastRefreshTime = configure.Now().Unix()
|
||||
sdata.Items = make([]int32, len(items))
|
||||
sdata.Items = make([]*pb.UserShopGood, len(items))
|
||||
sdata.Preview = make(map[int32]*pb.DB_Equipment)
|
||||
for i, v := range items {
|
||||
sdata.Items[i] = v.Key
|
||||
id := v.Key*100 + int32(i)
|
||||
sdata.Items[i] = &pb.UserShopGood{
|
||||
Id: id,
|
||||
Gid: v.Key,
|
||||
Buy: 0,
|
||||
}
|
||||
if v.Preview { //是否预览
|
||||
if errdata, sdata.Preview[v.Key] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil {
|
||||
if errdata, sdata.Preview[id] = this.module.equip.NewEquipment(session.GetUserId(), v.Iteminfo[0].T); errdata != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -230,7 +242,11 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype105, 1)
|
||||
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype105, 1))
|
||||
} else { //返回以前的商品列表
|
||||
if items, err = this.module.configure.GetShopItemsConfigureByIds(sdata.Items...); err != nil {
|
||||
keys := make([]int32, len(sdata.Items))
|
||||
for i, v := range sdata.Items {
|
||||
keys[i] = v.Gid
|
||||
}
|
||||
if items, err = this.module.configure.GetShopItemsConfigureByIds(keys...); err != nil {
|
||||
errdata = &pb.ErrorData{
|
||||
Code: pb.ErrorCode_SystemError,
|
||||
Title: pb.ErrorCode_SystemError.ToString(),
|
||||
@ -247,6 +263,6 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ShopGetListReq)
|
||||
}
|
||||
return
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), "getlist", &pb.ShopGetListResp{SType: req.SType, IsManualRefresh: req.IsManualRefresh, Goods: goods, SurplusRefreshNum: leftrefnum, Lastrefreshtime: sdata.LastRefreshTime})
|
||||
session.SendMsg(string(this.module.GetType()), "getlist", &pb.ShopGetListResp{SType: req.SType, IsManualRefresh: req.IsManualRefresh, Goods: goods, SurplusRefreshNum: leftrefnum, Lastrefreshtime: sdata.LastRefreshTime, Leftfreerefreshnum: sdata.Leftfreerefreshnum})
|
||||
return
|
||||
}
|
||||
|
@ -111,6 +111,8 @@ func (this *configureComp) GetShopItemsConfigureByIds(keys ...int32) (result []*
|
||||
result = append(result, item)
|
||||
} else {
|
||||
this.module.Errorf("no found GetShopItemsConfigureByIds:%d", v)
|
||||
err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_shopitem, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,17 +33,13 @@ func randomGoods(goods []*cfg.GameShopitemData) (result *cfg.GameShopitemData) {
|
||||
// 转换商品对象
|
||||
func transGoods(goods []*cfg.GameShopitemData, sdata *pb.UserShopData) (result []*pb.ShopItem) {
|
||||
result = make([]*pb.ShopItem, len(goods))
|
||||
// ok := false
|
||||
// uitem := &pb.DBShopItem{}
|
||||
for i, v := range goods {
|
||||
// if uitem, ok = ushoputem[v.Key]; !ok {
|
||||
// uitem = &pb.DBShopItem{}
|
||||
// }
|
||||
result[i] = &pb.ShopItem{
|
||||
Gid: sdata.Items[i].Id,
|
||||
GoodsId: v.Key,
|
||||
Sale: int32(v.Sale),
|
||||
}
|
||||
result[i].LeftBuyNum = v.Buyminnum - sdata.Buy[v.Key]
|
||||
result[i].LeftBuyNum = v.Buyminnum - sdata.Items[i].Buy
|
||||
result[i].Items = make([]*pb.UserAssets, len(v.Iteminfo))
|
||||
for i1, v1 := range v.Iteminfo {
|
||||
result[i].Items[i1] = &pb.UserAssets{
|
||||
|
@ -83,11 +83,3 @@ func Test_Module_APIGetList(t *testing.T) {
|
||||
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62b16dda909b2f8faeff788d", MainType: "shop", SubType: "getlist", Message: data}, reply)
|
||||
log.Debugf("reply:%v", reply)
|
||||
}
|
||||
|
||||
//测试api_buy
|
||||
func Test_Module_APIBuy(t *testing.T) {
|
||||
data, _ := ptypes.MarshalAny(&pb.ShopBuyReq{GoodsId: 7})
|
||||
reply := &pb.RPCMessageReply{}
|
||||
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62b16dda909b2f8faeff788d", MainType: "shop", SubType: "buy", Message: data}, reply)
|
||||
log.Debugf("reply:%v", reply)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user