package smithy import ( "crypto/rand" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "math/big" "strconv" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) type modelStove struct { modules.MCompModel module *Smithy } func (this *modelStove) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = string(comm.TableStove) err = this.MCompModel.Init(service, module, comp, options) this.module = module.(*Smithy) // uid 创建索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } // 获取铁匠铺信息 func (this *modelStove) getSmithyStoveList(uid string) (result *pb.DBStove, err error) { result = &pb.DBStove{} if err = this.Get(uid, result); err != nil { if redis.RedisNil != err { // 没有数据直接创建新的数据 result.Id = primitive.NewObjectID().Hex() result.Uid = uid result.Data = make(map[int32]*pb.Mastery, 0) result.Skill = make(map[int32]int32, 0) result.Forge = make(map[int32]int32, 0) result.Lv = 1 result.Temperature = 20000 // 配置 result.RecoveTime = 0 if err = this.Add(uid, result); err != nil { this.module.Errorf("err:%v", err) err = nil return } } return } err = nil return result, err } func (this *modelStove) updateSmithyStove(uid string, update map[string]interface{}) (err error) { err = this.Change(uid, update) return err } // 图纸属性 炉温消耗减少 func (this *modelStove) CheckTemperature(reelId int32, lv int32) (t int32) { var index int32 for index = 1; index <= lv; index++ { if cfgData := this.module.configure.GetSmithyProficileData(reelId, index); cfgData != nil { if cfgData.Type == comm.SmithyReelType1 { t += cfgData.Value1 } } } return } // 检查额外概率制造两件装备 func (this *modelStove) CheckForgetwoEquip(reelId int32, lv int32) (b bool) { var ( index int32 value int32 ) for index = 1; index <= lv; index++ { if cfgData := this.module.configure.GetSmithyProficileData(reelId, index); cfgData != nil { if cfgData.Type == comm.SmithyReelType5 { value += cfgData.Value1 } } } if value > 0 { n, _ := rand.Int(rand.Reader, big.NewInt(100)) if value < int32(n.Int64()) { return true } } return false } // 检查消耗减少 func (this *modelStove) CheckForgeConsume(reelId int32, lv int32) (atn []*cfg.Gameatn) { var index int32 for index = 1; index <= lv; index++ { if cfgData := this.module.configure.GetSmithyProficileData(reelId, index); cfgData != nil { if cfgData.Type == comm.SmithyReelType2 { atn = append(atn, &cfg.Gameatn{ A: "item", T: strconv.Itoa(int(cfgData.Value1)), N: cfgData.Value2, }) } } } return } // 计算恢复进度 func (this *modelStove) calculationRecoveryT(uid string, stove *pb.DBStove) { conf := this.module.configure.GetSmithyStoveConf(stove.Lv) if conf == nil { return } if stove.Temperature < conf.MaxTemperature { update := make(map[string]interface{}) // 小于最高温度就开始恢复 addT := (configure.Now().Unix() - stove.RecoveTime) / int64(conf.TemperatureRecovery) stove.Temperature += int32(addT) if stove.Temperature > conf.MaxTemperature { stove.Temperature = conf.MaxTemperature stove.RecoveTime = 0 } else { stove.RecoveTime += addT * int64(conf.TemperatureRecovery) } update["temperature"] = stove.Temperature update["recoveTime"] = stove.RecoveTime this.module.modelStove.updateSmithyStove(uid, update) } } // 通过技能获取额外的提升 func (this *modelStove) StoveToolsQualityProbability(stove *pb.DBStove) int32 { if v, ok := stove.Skill[comm.SmithyToolsSkill1]; ok { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, v); conf != nil { return conf.Value } else { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, 0); conf != nil { return conf.Value } } } return 0 } // 所有装备售价提升{0}% func (this *modelStove) StoveToolsSellUp(uid string) int32 { if stove, err := this.module.modelStove.getSmithyStoveList(uid); err == nil { if v, ok := stove.Skill[comm.SmithyToolsSkill2]; ok { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, v); conf != nil { return conf.Value } else { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, 0); conf != nil { return conf.Value } } } } return 0 } // 所有图纸炉温消耗减少 func (this *modelStove) StoveToolsTemperature(stove *pb.DBStove) int32 { if v, ok := stove.Skill[comm.SmithyToolsSkill3]; ok { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, v); conf != nil { return conf.Value } else { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, 0); conf != nil { return conf.Value } } } return 0 } //每日顾客数量提升至{0}人 func (this *modelStove) StoveSkillAddCustomer(uid string) int32 { if stove, err := this.module.modelStove.getSmithyStoveList(uid); err == nil { if v, ok := stove.Skill[comm.SmithyToolsSkill4]; ok { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, v); conf != nil { return conf.Value } } else { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, 0); conf != nil { return conf.Value } } } return 0 } //顾客购买装备数量上限提高至{0}件 func (this *modelStove) StoveSkillBuyEquip(uid string) int32 { if stove, err := this.module.modelStove.getSmithyStoveList(uid); err == nil { if v, ok := stove.Skill[comm.SmithyToolsSkill5]; ok { if conf := this.module.configure.GetSmithySkill(comm.SmithyToolsSkill1, v); conf != nil { return conf.Value } } } return 0 } func (this *modelStove) GetRandEquipLv(sz []int32) int32 { if len(sz) > 0 { var _totalW int64 // 总权重 var _tmpW int64 // 临时权重 for _, v := range sz { _totalW += int64(v) } // 随机权重 n, _ := rand.Int(rand.Reader, big.NewInt(_totalW)) for i, v := range sz { _tmpW += int64(v) if n.Int64() < _tmpW { return int32(i) } } } return 0 }