package smithy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/utils" "time" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) type modelSmithy struct { modules.MCompModel module *Smithy } func (this *modelSmithy) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = string(comm.TableSmithy) 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 *modelSmithy) getSmithyList(uid string) (result *pb.DBSmithy, err error) { result = &pb.DBSmithy{} if err = this.Get(uid, result); err != nil { if redis.RedisNil != err { // 没有数据直接创建新的数据 result.Id = primitive.NewObjectID().Hex() result.Uid = uid result.Skill = make(map[int32]int32, 0) result.StoveLv = 1 // storv 等级默认1级 result.DeskFloor = make(map[int32]int32, 0) mapType := this.module.configure.GetSmithyTypeConfigData() // 找类型 for key := range mapType { result.Skill[key] = 1 result.DeskFloor[key] = 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 *modelSmithy) modifySmithyDataByObjId(uid string, data map[string]interface{}) error { return this.Change(uid, data) } func (this *modelSmithy) CalculationSmithy(uid string, smithy *pb.DBSmithy) { var ( bCooking bool costTime int32 curTime int32 szTime map[int32]int32 // k 台子类型 v订单耗时 zeroTime int64 // 订单开始的时间对应第二天0点时间 nextDay bool // 是否跨天了 nextDayTime int32 // 跨天累计时间 ) mapData := make(map[string]interface{}, 0) szTime = make(map[int32]int32, 0) defer this.module.ModifySmithyData(uid, mapData) // 记录每个食材耗时 for k, v := range smithy.Skill { // 计算出需要的时间 _skillCfg := this.module.configure.GetSmithyStoveConfigData(v) // 技能配置表 szTime[k] = _skillCfg.Time } if smithy.Clang != nil && smithy.Clang.ETime > 0 { zeroTime = utils.GetZeroTime(smithy.Clang.STime) // 获取订单开始时间当天的0点 costTime = int32(time.Now().Unix() - smithy.Clang.ETime) // 当前过去的时间 if costTime < 0 { // 没有完成 不做处理 return } } for _, order := range smithy.Orders { if order.Count == 0 { continue } desktype := order.DeskType skillLv := smithy.Skill[desktype] // 获取技能等级 // 计算出需要的时间 _smithycfg := this.module.configure.GetSmithyConfigData(desktype, skillLv) // 美食家配置表 iCount := int(order.Count) for i := 0; i < iCount; i++ { curTime += szTime[order.DeskType] order.Count-- if order.Count == 0 { order.NeedTime = 0 } order.NeedTime = order.Count * szTime[order.DeskType] if smithy.Clang == nil { if zeroTime == 0 { zeroTime = utils.GetZeroTime(time.Now().Unix()) } smithy.Clang = &pb.Clang{} smithy.Clang.STime = time.Now().Unix() smithy.Clang.ETime = time.Now().Unix() + int64(szTime[order.DeskType]) } else { smithy.Clang.STime += int64(szTime[order.DeskType]) smithy.Clang.ETime += int64(szTime[order.DeskType]) } smithy.Clang.DeskType = order.DeskType // 判断订单是否跨天 if smithy.Clang.ETime >= zeroTime && !nextDay { smithy.Ctime = zeroTime // 设置 smithy.OrderCostTime = 0 nextDay = true } if nextDay { nextDayTime += szTime[order.DeskType] } if curTime > costTime { // 转时间戳 smithy.Clang.DeskType = order.DeskType smithy.Clang.ETime = time.Now().Unix() + int64(curTime-costTime) smithy.Clang.STime = smithy.Clang.ETime - int64(szTime[order.DeskType]) bCooking = true // 记录下订单时间 smithy.Ctime = time.Now().Unix() break } smithy.Items = this.module.configure.GetMultipleDropReward(_smithycfg.Using, _smithycfg.Drop, smithy.Items) // 获取掉落奖励 } if bCooking { // 分配了正在製作的食物 break } } if nextDay { smithy.OrderCostTime += nextDayTime for _, order := range smithy.Orders { if order.Count == 0 { continue } smithy.OrderCostTime += szTime[order.DeskType] * order.Count } } if utils.GetZeroTime(smithy.Ctime) <= time.Now().Unix() { smithy.OrderCostTime = 0 } if smithy.Clang != nil && smithy.Clang.ETime <= time.Now().Unix() { // 当前时间超过正在做的时间 desktype := smithy.Clang.DeskType skillLv := smithy.Skill[desktype] // 获取技能等级 _smithycfg := this.module.configure.GetSmithyConfigData(desktype, skillLv) smithy.Items = this.module.configure.GetMultipleDropReward(_smithycfg.Using, _smithycfg.Drop, smithy.Items) smithy.Clang = nil } // 保存信息 mapData["items"] = smithy.Items mapData["orders"] = smithy.Orders mapData["orderCostTime"] = smithy.OrderCostTime mapData["clang"] = smithy.Clang // 正在做的 mapData["ctime"] = smithy.Ctime } func (this *modelSmithy) CalculationDeskSkillLv(uid string, Smithy *pb.DBSmithy) { mapData := make(map[string]interface{}, 0) mapData["skill"] = Smithy.Skill mapData["deskFloor"] = Smithy.DeskFloor this.module.ModifySmithyData(uid, mapData) } func (this *modelSmithy) CalculationStoveSkillLv(uid string, Smithy *pb.DBSmithy, stoveSkillLv int32) { mapData := make(map[string]interface{}, 0) for _, v := range Smithy.Orders { if v.Count > 0 { _skillCfg := this.module.configure.GetSmithyStoveConfigData(stoveSkillLv) if _skillCfg != nil { v.NeedTime += _skillCfg.Time * v.Count if v.NeedTime < 0 { // 担心配置错误 为负数情况 所以这里做下判断 v.NeedTime = 0 } mapData["orders"] = Smithy.Orders } } } mapData["stoveLv"] = Smithy.StoveLv mapData["deskFloor"] = Smithy.DeskFloor this.module.ModifySmithyData(uid, mapData) }