233 lines
7.5 KiB
Go
233 lines
7.5 KiB
Go
package gourmet
|
|
|
|
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 modelGourmet struct {
|
|
modules.MCompModel
|
|
module *Gourmet
|
|
}
|
|
|
|
func (this *modelGourmet) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = string(comm.TableGourmet)
|
|
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 *modelGourmet) getGourmetList(uid string) (result *pb.DBGourmet, err error) {
|
|
result = &pb.DBGourmet{}
|
|
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)
|
|
szSkill := this.module.configure.GetGourmetSkillConfigBySkillType(1) // 查表获取初始技能
|
|
for _, v := range szSkill {
|
|
result.Skill[v] = 1
|
|
}
|
|
|
|
szSpecailSkill := this.module.configure.GetGourmetSkillConfigBySkillType(2) // 高效制作 等通用技能
|
|
result.SpecialSkill = make(map[int32]int32, 0)
|
|
for _, v := range szSpecailSkill {
|
|
result.SpecialSkill[v] = 1
|
|
}
|
|
result.SpecialSkill[1005] = 1 // 通用技能
|
|
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 *modelGourmet) modifyGourmetDataByObjId(uid string, data map[string]interface{}) error {
|
|
return this.Change(uid, data)
|
|
}
|
|
|
|
// 计算订单信息
|
|
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 // 跨天累计时间
|
|
)
|
|
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
|
|
// 高效制作技能
|
|
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(time.Now().Unix() - gourmet.CookingFood.ETime) // 当前过去的时间
|
|
if costTime < 0 { // 没有完成 不做处理
|
|
return
|
|
}
|
|
}
|
|
|
|
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(time.Now().Unix())
|
|
}
|
|
gourmet.CookingFood = &pb.Cooking{}
|
|
gourmet.CookingFood.STime = time.Now().Unix()
|
|
gourmet.CookingFood.ETime = time.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]
|
|
}
|
|
if curTime > costTime {
|
|
// 转时间戳
|
|
gourmet.CookingFood.FoodType = order.FoodType
|
|
gourmet.CookingFood.ETime = time.Now().Unix() + int64(curTime-costTime)
|
|
gourmet.CookingFood.STime = gourmet.CookingFood.ETime - int64(szTime[order.FoodType])
|
|
bCooking = true
|
|
|
|
// 记录下订单时间
|
|
gourmet.Ctime = time.Now().Unix()
|
|
mapData["ctime"] = gourmet.Ctime
|
|
break
|
|
}
|
|
gourmet.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) // 获取掉落奖励
|
|
}
|
|
|
|
if bCooking { // 分配了正在製作的食物
|
|
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) <= time.Now().Unix() {
|
|
gourmet.OrderCostTime = 0
|
|
}
|
|
|
|
if gourmet.CookingFood != nil && gourmet.CookingFood.ETime <= time.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 // 正在做的
|
|
}
|
|
|
|
// 技能等级提高了 重新计算订单时间(只对订单中数据有影响)
|
|
func (this *modelGourmet) CalculationGourmetbySkiiLv(uid string, gourmet *pb.DBGourmet, skillType int32, skilllv int32) {
|
|
mapData := make(map[string]interface{}, 0)
|
|
var totalTime int32
|
|
for _, v := range gourmet.Foods {
|
|
if v.FoodCount > 0 && v.FoodType == skillType {
|
|
_skillCfg := this.module.configure.GetGourmetConfigData(skillType, skilllv)
|
|
if _skillCfg != nil {
|
|
totalTime += _skillCfg.Needtime * v.FoodCount
|
|
v.CookTime += totalTime
|
|
mapData["foods"] = gourmet.Foods
|
|
}
|
|
break
|
|
}
|
|
}
|
|
// 总时间也减少了
|
|
gourmet.OrderCostTime += totalTime
|
|
mapData["orderCostTime"] = gourmet.OrderCostTime
|
|
mapData["skill"] = gourmet.Skill
|
|
this.module.ModifyGourmetData(uid, mapData)
|
|
}
|
|
|
|
// 升级高效制作技能重计算时间消耗
|
|
func (this *modelGourmet) CalculationSpecialSkillLv(uid string, gourmet *pb.DBGourmet, skillType int32, skilllv int32) {
|
|
mapData := make(map[string]interface{}, 0)
|
|
var totalTime int32
|
|
for _, v := range gourmet.Foods {
|
|
if v.FoodCount > 0 {
|
|
_skillCfg := this.module.configure.GetGourmetConfigData(skillType, skilllv)
|
|
if _skillCfg != nil {
|
|
totalTime += _skillCfg.Needtime * v.FoodCount
|
|
v.CookTime += totalTime
|
|
if v.CookTime < 0 { // 担心配置错误 为负数情况 所以这里做下判断
|
|
v.CookTime = 0
|
|
}
|
|
mapData["foods"] = gourmet.Foods
|
|
}
|
|
}
|
|
}
|
|
// 总时间也减少了
|
|
gourmet.OrderCostTime += totalTime
|
|
mapData["orderCostTime"] = gourmet.OrderCostTime
|
|
mapData["specialSkill"] = gourmet.SpecialSkill
|
|
this.module.ModifyGourmetData(uid, mapData)
|
|
}
|