210 lines
6.9 KiB
Go
210 lines
6.9 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/sys/configure"
|
|
"go_dreamfactory/utils"
|
|
|
|
"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 (
|
|
szTime map[int32]int32
|
|
zeroTime int64 // 当前时间对应的0点时间戳,用来判断是否跨天了
|
|
)
|
|
mapData := make(map[string]interface{}, 0)
|
|
szTime = make(map[int32]int32, 0)
|
|
|
|
// 记录每个食材耗时
|
|
for k, v := range gourmet.Skill {
|
|
// 计算出需要的时间
|
|
_skillCfg := this.module.configure.GetGourmetConfigData(k, v)
|
|
szTime[k] += _skillCfg.Needtime
|
|
// 高效制作技能
|
|
for k1, v1 := range gourmet.SpecialSkill {
|
|
specalSkill := this.module.configure.GetGourmetConfigData(k1, v1)
|
|
szTime[k] += specalSkill.Needtime
|
|
}
|
|
}
|
|
|
|
// 有订单在做
|
|
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 {
|
|
if gourmet.CookingFood.ETime > configure.Now().Unix() {
|
|
break
|
|
}
|
|
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])
|
|
|
|
} 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
|
|
gourmet.TotalTime += szTime[order.FoodType]
|
|
mapData["totalTime"] = gourmet.TotalTime
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|
|
|
|
// 保存信息
|
|
mapData["items"] = gourmet.Items
|
|
mapData["foods"] = gourmet.Foods
|
|
mapData["orderCostTime"] = gourmet.OrderCostTime
|
|
mapData["cookingFood"] = gourmet.CookingFood // 正在做的
|
|
|
|
this.module.ModifyGourmetData(uid, mapData) // 同步数据
|
|
|
|
}
|
|
|
|
// 技能等级提高了 重新计算订单时间(只对订单中数据有影响)
|
|
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 {
|
|
preScaleTime := 0
|
|
preSkillConf := this.module.configure.GetGourmetConfigData(skillType, skilllv-1)
|
|
if preSkillConf != nil {
|
|
preScaleTime = int(preSkillConf.Needtime)
|
|
}
|
|
_skillCfg := this.module.configure.GetGourmetConfigData(skillType, skilllv)
|
|
if _skillCfg != nil {
|
|
scaleTime := (_skillCfg.Needtime - int32(preScaleTime)) * v.FoodCount
|
|
totalTime += scaleTime
|
|
v.CookTime += scaleTime
|
|
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)
|
|
}
|