diff --git a/comm/const.go b/comm/const.go index c71e4fae4..5811313ea 100644 --- a/comm/const.go +++ b/comm/const.go @@ -239,6 +239,9 @@ const ( TablePandaAtlas = "pandaatlas" //武馆切磋 TablePandataQiecuo = "qiecuo" + + // 美食馆 图鉴 + TableGourmetAtlas = "gourmetatlas" ) // RPC服务接口定义处 diff --git a/modules/gourmet/api_createfood.go b/modules/gourmet/api_createfood.go new file mode 100644 index 000000000..f03be4a29 --- /dev/null +++ b/modules/gourmet/api_createfood.go @@ -0,0 +1,86 @@ +package gourmet + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/pb" + cfg "go_dreamfactory/sys/configure/structs" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +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 proto.Message) { + 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, + }) + + return +} diff --git a/modules/gourmet/comp_configure.go b/modules/gourmet/comp_configure.go index 56a4fd781..df2e20612 100644 --- a/modules/gourmet/comp_configure.go +++ b/modules/gourmet/comp_configure.go @@ -11,6 +11,7 @@ import ( const ( game_gourmet = "game_gourmet.json" + game_food = "game_breakingbad.json" ) ///配置管理基础组件 @@ -18,12 +19,17 @@ type configureComp struct { hlock sync.RWMutex modules.MCompConfigure _gourmetMap map[int64]*cfg.GameGourmetData + module *Gourmet + normal string } //组件初始化接口 func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.MCompConfigure.Init(service, module, comp, options) - + this.module = module.(*Gourmet) + err = this.LoadMultiConfigure(map[string]interface{}{ + game_food: cfg.NewGameBreakingbad, + }) this._gourmetMap = make(map[int64]*cfg.GameGourmetData, 0) configure.RegisterConfigure(game_gourmet, cfg.NewGameGourmet, func() { if v, err := this.GetConfigure(game_gourmet); err == nil { @@ -39,7 +45,7 @@ func (this *configureComp) Init(service core.IService, module core.IModule, comp log.Errorf("get game_pagoda conf err:%v", err) return }) - + this.SetGrormetCookBookConf() return } @@ -81,3 +87,36 @@ func (this *configureComp) GetGourmetSkillConfigBySkillType(skillType int32) (da } return } + +func (this *configureComp) SetGrormetCookBookConf() { + + if v, err := this.GetConfigure(game_food); err == nil { + if conf, ok := v.(*cfg.GameBreakingbad); ok { + for _, v1 := range conf.GetDataList() { + if v1.Type == 1 { + this.normal = v1.Delicacies + return + } + } + } + } + + return +} +func (this *configureComp) GetGrormetCookBookConf(id string) (configure *cfg.GameBreakingbadData) { + + if v, err := this.GetConfigure(game_food); err == nil { + if conf, ok := v.(*cfg.GameBreakingbad); ok { + return conf.Get(id) + } + } + this.module.Errorf("can't found GameBreakingbadData:%d", id) + return +} + +// 获取类型为1 的菜谱 + +func (this *configureComp) GetNormalGourmetFood() string { + + return this.normal +} diff --git a/modules/gourmet/model_food.go b/modules/gourmet/model_food.go new file mode 100644 index 000000000..26a4963f1 --- /dev/null +++ b/modules/gourmet/model_food.go @@ -0,0 +1,47 @@ +package gourmet + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/redis" + "go_dreamfactory/modules" + "go_dreamfactory/pb" + + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/x/bsonx" +) + +type modelAtlas struct { + modules.MCompModel + module *Gourmet +} + +func (this *modelAtlas) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.TableName = string(comm.TableGourmetAtlas) + err = this.MCompModel.Init(service, module, comp, options) + this.module = module.(*Gourmet) + // uid 创建索引 + this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ + Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, + }) + return +} + +func (this *modelAtlas) getGourmetAtlasList(uid string) (result *pb.DBGourmetAtlas, err error) { + result = &pb.DBGourmetAtlas{} + if err = this.Get(uid, result); err != nil { + if redis.RedisNil != err { // 没有数据直接创建新的数据 + + result.Id = primitive.NewObjectID().Hex() + result.Uid = uid + result.Atlas = make(map[string]int32) + } + return + } + err = nil + return result, err +} +func (this *modelAtlas) modifyGourmetAtlasByObjId(uid string, data map[string]interface{}) error { + return this.Change(uid, data) +} diff --git a/modules/gourmet/module.go b/modules/gourmet/module.go index 96459e2da..219ae351c 100644 --- a/modules/gourmet/module.go +++ b/modules/gourmet/module.go @@ -6,10 +6,13 @@ package gourmet import ( + "crypto/rand" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/pb" + cfg "go_dreamfactory/sys/configure/structs" + "math/big" ) type Gourmet struct { @@ -17,6 +20,7 @@ type Gourmet struct { modelGourmet *modelGourmet api *apiComp configure *configureComp + modelAtlas *modelAtlas } func NewModule() core.IModule { @@ -38,6 +42,7 @@ func (this *Gourmet) OnInstallComp() { this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelGourmet = this.RegisterComp(new(modelGourmet)).(*modelGourmet) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) + this.modelAtlas = this.RegisterComp(new(modelAtlas)).(*modelAtlas) } // 接口信息 @@ -144,3 +149,25 @@ func (this *Gourmet) CheckPoint22(uid string) bool { } return false } + +// 获取做菜成功率 返回菜单的ID +func (this *Gourmet) GetSuccessRate(m map[string]int32, conf *cfg.GameBreakingbadData) (cid string) { + if conf == nil { + return + } + var ( + rate int32 + ) + for _, v := range conf.Recipe { + if v1, ok := m[v.A]; ok { + rate += v.N * v1 + } + } + n, _ := rand.Int(rand.Reader, big.NewInt(100)) + if n.Int64() < int64(rate) { + return conf.Delicacies + } else { + cid = this.configure.GetNormalGourmetFood() + } + return +}