go_dreamfactory/modules/smithy/model_smithy.go
2022-11-28 12:06:29 +08:00

195 lines
5.9 KiB
Go

package smithy
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 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 (
szTime map[int32]int32
zeroTime int64 // 当前时间对应的0点时间戳,用来判断是否跨天了
)
mapData := make(map[string]interface{}, 0)
szTime = make(map[int32]int32, 0)
// 记录每个食材耗时
for k, v := range smithy.Skill {
// 计算出需要的时间
_skillCfg := this.module.configure.GetSmithyStoveConfigData(v)
szTime[k] += _skillCfg.Time
}
// 有订单在做
zeroTime = utils.GetTodayZeroTime(configure.Now().Unix())
for {
if smithy.Clang != nil {
if configure.Now().Unix() < smithy.Clang.ETime {
break
}
}
bRet := false
for _, order := range smithy.Orders {
_gourmetcfg := this.module.configure.GetSmithyConfigData(order.DeskType, smithy.Skill[order.DeskType]) // 美食家配置表
if order.Count > 0 {
if smithy.Clang.ETime > configure.Now().Unix() {
break
}
order.Count--
if order.Count == 0 {
order.NeedTime = 0
}
order.NeedTime = order.Count * szTime[order.DeskType]
if smithy.Clang == nil {
smithy.Clang = &pb.Clang{}
smithy.Clang.STime = configure.Now().Unix()
smithy.Clang.ETime = configure.Now().Unix() + int64(szTime[order.DeskType])
} else {
smithy.Clang.STime += int64(szTime[order.DeskType])
oldTime := smithy.Clang.ETime
smithy.Clang.ETime += int64(szTime[order.DeskType])
// 如果此时跨天了 清除订单时常
if oldTime < zeroTime && zeroTime <= smithy.Clang.ETime { // 跨天清空订单耗时
smithy.OrderCostTime = 0
// 重新计算订单时常
for _, order := range smithy.Orders {
smithy.OrderCostTime += order.Count * szTime[order.DeskType]
}
}
}
smithy.Clang.DeskType = order.DeskType
// 设置掉落组
smithy.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Drop, smithy.Items) // 获取掉落奖励
// 记录下订单时间
smithy.Ctime = smithy.Clang.ETime
mapData["ctime"] = smithy.Ctime
bRet = true
smithy.TotalTime += szTime[order.DeskType]
mapData["totalTime"] = smithy.TotalTime
}
}
if !bRet { // 没有订单可以做
if smithy.Clang != nil {
if smithy.Clang.ETime <= configure.Now().Unix() {
_gourmetcfg := this.module.configure.GetSmithyConfigData(smithy.Clang.DeskType, smithy.Skill[smithy.Clang.DeskType])
smithy.Items = this.module.configure.GetMultipleDropReward(_gourmetcfg.Using, _gourmetcfg.Drop, smithy.Items)
smithy.Clang = nil
}
}
break
}
}
// 清除数量为0 的订单
pos := 0
for _, order := range smithy.Orders {
if order.Count == 0 {
pos++
}
}
smithy.Orders = append(smithy.Orders[:0], smithy.Orders[pos:]...)
// 保存信息
mapData["items"] = smithy.Items
mapData["orders"] = smithy.Orders
mapData["orderCostTime"] = smithy.OrderCostTime
mapData["clang"] = smithy.Clang // 正在做的
this.module.ModifySmithyData(uid, mapData) // 同步数据
}
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)
var totalTime int32
for _, v := range Smithy.Orders {
if v.Count > 0 {
preScaleTime := 0
preSkillConf := this.module.configure.GetSmithyStoveConfigData(stoveSkillLv - 1)
if preSkillConf != nil {
preScaleTime += int(preSkillConf.Time)
}
_skillCfg := this.module.configure.GetSmithyStoveConfigData(stoveSkillLv)
if _skillCfg != nil {
scaleTime := (_skillCfg.Time - int32(preScaleTime)) * v.Count
v.NeedTime += scaleTime
totalTime += scaleTime
if v.NeedTime < 0 { // 担心配置错误 为负数情况 所以这里做下判断
v.NeedTime = 0
}
}
}
}
mapData["orders"] = Smithy.Orders
mapData["stoveLv"] = Smithy.StoveLv
mapData["deskFloor"] = Smithy.DeskFloor
Smithy.OrderCostTime += totalTime
mapData["orderCostTime"] = Smithy.OrderCostTime
this.module.ModifySmithyData(uid, mapData)
}