From 30ef92bd953a6c0460635b91fccff3aa50fe4cdb Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Wed, 16 Nov 2022 18:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E9=A3=9F=E9=A6=86=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/gourmet/api_createorder.go | 13 +-- modules/gourmet/model_gourmet.go | 149 +++++++++++------------------ pb/gourmet_db.pb.go | 2 +- utils/time.go | 8 ++ 4 files changed, 73 insertions(+), 99 deletions(-) diff --git a/modules/gourmet/api_createorder.go b/modules/gourmet/api_createorder.go index c12bd0247..5ab87fd0f 100644 --- a/modules/gourmet/api_createorder.go +++ b/modules/gourmet/api_createorder.go @@ -39,13 +39,14 @@ func (this *apiComp) CreateOrder(session comm.IUserSession, req *pb.GourmetCreat // 记录每个食材耗时 for k, v := range _gourmet.Skill { _skillCfg := this.module.configure.GetGourmetConfigData(k, v) - szTime[k] = _skillCfg.Needtime - } - // 高效制作技能 - for k, v := range _gourmet.SpecialSkill { - specalSkill := this.module.configure.GetGourmetConfigData(k, v) - szTime[k] = specalSkill.Needtime + szTime[k] += _skillCfg.Needtime + // 高效制作技能 + for k1, v1 := range _gourmet.SpecialSkill { + specalSkill := this.module.configure.GetGourmetConfigData(k1, v1) + szTime[k] += specalSkill.Needtime + } } + if !utils.IsToday(_gourmet.Ctime) { // 跨天了 _gourmet.Ctime = configure.Now().Unix() _gourmet.OrderCostTime = 0 diff --git a/modules/gourmet/model_gourmet.go b/modules/gourmet/model_gourmet.go index b82470c66..8c5d4ed60 100644 --- a/modules/gourmet/model_gourmet.go +++ b/modules/gourmet/model_gourmet.go @@ -67,123 +67,88 @@ func (this *modelGourmet) modifyGourmetDataByObjId(uid string, data map[string]i // 计算订单信息 func (this *modelGourmet) CalculationGourmet(uid string, gourmet *pb.DBGourmet) { var ( - bCooking bool - costTime int32 - curTime int32 - szTime map[int32]int32 - zeroTime int64 // 订单开始的时间对应第二天0点时间 - nextDay bool // 是否跨天了 - nextDayTime int32 // 跨天累计时间 + szTime map[int32]int32 + zeroTime int64 // 当前时间对应的0点时间戳,用来判断是否跨天了 ) mapData := make(map[string]interface{}, 0) szTime = make(map[int32]int32, 0) - defer this.module.ModifyGourmetData(uid, mapData) // 记录每个食材耗时 for k, v := range gourmet.Skill { - var _time int32 // 计算出需要的时间 _skillCfg := this.module.configure.GetGourmetConfigData(k, v) - _time += _skillCfg.Needtime + szTime[k] += _skillCfg.Needtime // 高效制作技能 - for k, v := range gourmet.SpecialSkill { - specalSkill := this.module.configure.GetGourmetConfigData(k, v) - _time += specalSkill.Needtime - } - szTime[k] = _time - } - if gourmet.CookingFood != nil && gourmet.CookingFood.ETime > 0 { - zeroTime = utils.GetZeroTime(gourmet.CookingFood.STime) // 获取订单开始时间当天的0点 - costTime = int32(configure.Now().Unix() - gourmet.CookingFood.ETime) // 当前过去的时间 - if costTime < 0 { // 没有完成 不做处理 - return + for k1, v1 := range gourmet.SpecialSkill { + specalSkill := this.module.configure.GetGourmetConfigData(k1, v1) + szTime[k] += specalSkill.Needtime } } - for _, order := range gourmet.Foods { - if order.FoodCount == 0 { - continue - } - foodtype := order.FoodType - skillLv := gourmet.Skill[foodtype] // 获取技能等级 - // 计算出需要的时间 - _gourmetcfg := this.module.configure.GetGourmetConfigData(foodtype, skillLv) // 美食家配置表 - iCount := int(order.FoodCount) - for i := 0; i < iCount; i++ { - curTime += szTime[order.FoodType] - order.FoodCount-- - if order.FoodCount == 0 { - order.CookTime = 0 - } - order.CookTime = order.FoodCount * szTime[order.FoodType] - if gourmet.CookingFood == nil { - if zeroTime == 0 { - zeroTime = utils.GetZeroTime(configure.Now().Unix()) - } - gourmet.CookingFood = &pb.Cooking{} - gourmet.CookingFood.STime = configure.Now().Unix() - gourmet.CookingFood.ETime = configure.Now().Unix() + int64(szTime[order.FoodType]) - } else { - gourmet.CookingFood.STime += int64(szTime[order.FoodType]) - gourmet.CookingFood.ETime += int64(szTime[order.FoodType]) - } - gourmet.CookingFood.FoodType = order.FoodType - - // 判断订单是否跨天 - if gourmet.CookingFood.ETime >= zeroTime && !nextDay { - gourmet.Ctime = zeroTime // 设置 - gourmet.OrderCostTime = 0 - nextDay = true - } - if nextDay { - nextDayTime += szTime[order.FoodType] - } - gourmet.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) // 获取掉落奖励 - - if curTime > costTime { - // 转时间戳 - gourmet.CookingFood.FoodType = order.FoodType - gourmet.CookingFood.ETime = configure.Now().Unix() + int64(curTime-costTime) - gourmet.CookingFood.STime = gourmet.CookingFood.ETime - int64(szTime[order.FoodType]) - bCooking = true - - // 记录下订单时间 - gourmet.Ctime = gourmet.CookingFood.ETime - mapData["ctime"] = gourmet.Ctime + // 有订单在做 + zeroTime = utils.GetTodayZeroTime(configure.Now().Unix()) + for { + if gourmet.CookingFood != nil { + if configure.Now().Unix() < gourmet.CookingFood.ETime { break } } + bRet := false + for _, order := range gourmet.Foods { + _gourmetcfg := this.module.configure.GetGourmetConfigData(order.FoodType, gourmet.Skill[order.FoodType]) // 美食家配置表 + if order.FoodCount > 0 { + order.FoodCount-- + if order.FoodCount == 0 { + order.CookTime = 0 + } + order.CookTime = order.FoodCount * szTime[order.FoodType] + if gourmet.CookingFood == nil { + gourmet.CookingFood = &pb.Cooking{} + gourmet.CookingFood.STime = configure.Now().Unix() + gourmet.CookingFood.ETime = configure.Now().Unix() + int64(szTime[order.FoodType]) - if bCooking { // 分配了正在製作的食物 + } else { + gourmet.CookingFood.STime += int64(szTime[order.FoodType]) + oldTime := gourmet.CookingFood.ETime + gourmet.CookingFood.ETime += int64(szTime[order.FoodType]) + // 如果此时跨天了 清除订单时常 + if oldTime < zeroTime && zeroTime <= gourmet.CookingFood.ETime { // 跨天清空订单耗时 + gourmet.OrderCostTime = 0 + // 重新计算订单时常 + for _, order := range gourmet.Foods { + gourmet.OrderCostTime += order.FoodCount * szTime[order.FoodType] + } + } + } + gourmet.CookingFood.FoodType = order.FoodType + // 设置掉落组 + gourmet.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) // 获取掉落奖励 + // 记录下订单时间 + gourmet.Ctime = gourmet.CookingFood.ETime + mapData["ctime"] = gourmet.Ctime + bRet = true + break + } + } + if !bRet { // 没有订单可以做 + if gourmet.CookingFood != nil { + if gourmet.CookingFood.ETime <= configure.Now().Unix() { + _gourmetcfg := this.module.configure.GetGourmetConfigData(gourmet.CookingFood.FoodType, gourmet.Skill[gourmet.CookingFood.FoodType]) + gourmet.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) + gourmet.CookingFood = nil + } + } break } } - if nextDay { - gourmet.OrderCostTime += nextDayTime - for _, order := range gourmet.Foods { - if order.FoodCount == 0 { - continue - } - gourmet.OrderCostTime += szTime[order.FoodType] * order.FoodCount - } - } - if utils.GetZeroTime(gourmet.Ctime) <= configure.Now().Unix() { - gourmet.OrderCostTime = 0 - } - - if gourmet.CookingFood != nil && gourmet.CookingFood.ETime <= configure.Now().Unix() { // 当前时间超过正在做的时间 - foodtype := gourmet.CookingFood.FoodType - skillLv := gourmet.Skill[foodtype] // 获取技能等级 - _gourmetcfg := this.module.configure.GetGourmetConfigData(foodtype, skillLv) // 美食家配置表 - gourmet.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) - gourmet.CookingFood = nil - } // 保存信息 mapData["items"] = gourmet.Items mapData["foods"] = gourmet.Foods mapData["orderCostTime"] = gourmet.OrderCostTime mapData["cookingFood"] = gourmet.CookingFood // 正在做的 + + this.module.ModifyGourmetData(uid, mapData) // 同步数据 } // 技能等级提高了 重新计算订单时间(只对订单中数据有影响) diff --git a/pb/gourmet_db.pb.go b/pb/gourmet_db.pb.go index 141de57d0..4f1047216 100644 --- a/pb/gourmet_db.pb.go +++ b/pb/gourmet_db.pb.go @@ -155,7 +155,7 @@ type DBGourmet struct { Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid" bson:"uid"` //用户ID - CookingFood *Cooking `protobuf:"bytes,3,opt,name=cookingFood,proto3" json:"cookingFood" bson:"cooking"` //正在烹饪的食品 + CookingFood *Cooking `protobuf:"bytes,3,opt,name=cookingFood,proto3" json:"cookingFood" bson:"cookingFood"` //正在烹饪的食品 Foods []*OrderCook `protobuf:"bytes,4,rep,name=foods,proto3" json:"foods" bson:"foods"` //等待烹饪的食品 Items []*UserAssets `protobuf:"bytes,5,rep,name=items,proto3" json:"items" bson:"items"` //已经做好的食品 Skill map[int32]int32 `protobuf:"bytes,6,rep,name=skill,proto3" json:"skill" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3" bson:"skill"` //技能ID diff --git a/utils/time.go b/utils/time.go index 86591ef65..f47b3a90e 100644 --- a/utils/time.go +++ b/utils/time.go @@ -108,3 +108,11 @@ func GetMonthStartEnd() (int64, int64) { fmt.Printf("%d,%d", _d1, _d2) return _d1, _d2 } + +// 获取今天零点时间戳 +func GetTodayZeroTime(curTime int64) int64 { + currentTime := time.Unix(curTime, 0) + startTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location()) + + return startTime.Unix() +}