go_dreamfactory/modules/gourmet/api_createfood.go
2023-04-14 12:21:58 +08:00

85 lines
2.1 KiB
Go

package gourmet
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
//参数校验
func (this *apiComp) CreateFoodCheck(session comm.IUserSession, req *pb.GourmetCreateFoodReq) (code pb.ErrorCode) {
if req.Cid == "" || len(req.Material) == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
///获取美食城基本信息
func (this *apiComp) CreateFood(session comm.IUserSession, req *pb.GourmetCreateFoodReq) (code pb.ErrorCode, data *pb.ErrorData) {
var (
res []*cfg.Gameatn
curFood string // 做出来的食物ID
bFirst bool // 是否首次获得
)
code = this.CreateFoodCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
conf := this.configure.GetGrormetCookBookConf(req.Cid)
if conf == nil { // 配置校验
code = pb.ErrorCode_ConfigNoFound
return
}
for k, v := range req.Material {
if v == 0 { // 过滤
continue
}
res = append(res, &cfg.Gameatn{
A: "item",
T: k,
N: v,
})
}
if len(res) == 0 {
code = pb.ErrorCode_ReqParameterError
return
}
// 构建消耗
if code = this.module.CheckRes(session, res); code != pb.ErrorCode_Success {
return
}
curFood = this.module.GetSuccessRate(req.Material, conf)
if curFood == "" {
code = pb.ErrorCode_ConfigNoFound
return
}
if code = this.module.ConsumeRes(session, res, true); code != pb.ErrorCode_Success {
return
}
atn := &cfg.Gameatn{
A: "item",
T: curFood,
N: 1,
}
this.module.DispenseRes(session, []*cfg.Gameatn{atn}, true)
rst, _ := this.module.modelAtlas.getGourmetAtlasList(session.GetUserId()) // 校验是否首次获得
if _, ok := rst.Atlas[curFood]; !ok {
bFirst = true
rst.Atlas[curFood] = -1
if err := this.module.modelAtlas.Change(session.GetUserId(), map[string]interface{}{
"atlas": rst.Atlas,
}); err != nil {
this.module.Errorf("change modelAtlas failed: %v", err)
}
}
session.SendMsg(string(this.module.GetType()), "createfood", &pb.GourmetCreateFoodResp{
Cid: curFood,
FirstGet: bFirst,
})
go this.module.ModuleRtask.TriggerTask(session.GetUserId(), comm.GettaskParam(comm.Rtype150, 1))
return
}