203 lines
6.1 KiB
Go
203 lines
6.1 KiB
Go
package gourmet
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/utils"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
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)
|
|
|
|
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)
|
|
}
|
|
|
|
// todo 调用drop 表 获取掉落信息
|
|
func (this *modelGourmet) GetDropReward(count, dropId int32, Items []*pb.UserAssets) {
|
|
res := make([]*cfg.Gameatn, 0)
|
|
for i := 0; i < int(count); i++ {
|
|
data := this.module.configure.GetDropData(dropId)
|
|
szW := make([]int32, 0)
|
|
for _, value := range data {
|
|
szW = append(szW, value.P)
|
|
}
|
|
index := comm.GetRandW(szW)
|
|
res = append(res, data[index].Prize...)
|
|
}
|
|
for _, v := range res {
|
|
bFind := false
|
|
for _, v1 := range Items {
|
|
if v.A == v1.A && v.T == v1.T {
|
|
v1.N += v.N
|
|
bFind = true
|
|
}
|
|
}
|
|
if !bFind {
|
|
Items = append(Items, &pb.UserAssets{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: v.N,
|
|
})
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 计算订单信息
|
|
func (this *modelGourmet) CalculationGourmet(uid string, gourmet *pb.DBGourmet) {
|
|
var (
|
|
bCooking bool
|
|
costTime int32
|
|
curTime int32
|
|
)
|
|
mapData := make(map[string]interface{}, 0)
|
|
if gourmet.CookingFood != nil && gourmet.CookingFood.ETime > 0 {
|
|
costTime = int32(time.Now().Unix() - gourmet.CookingFood.ETime) // 当前过去的时间
|
|
if costTime < 0 { // 没有完成 不做处理
|
|
return
|
|
}
|
|
}
|
|
if gourmet.CookingFood == nil {
|
|
gourmet.CookingFood = &pb.Cooking{}
|
|
}
|
|
for _, order := range gourmet.Foods {
|
|
if order.FoodCount == 0 {
|
|
continue
|
|
}
|
|
foodtype := order.FoodType
|
|
// 获取技能等级
|
|
skillLv := gourmet.Skill[foodtype]
|
|
|
|
// 计算出需要的时间
|
|
_skillCfg := this.module.configure.GetGourmetSkillConfigData(foodtype, skillLv) // 技能配置表
|
|
_gourmetcfg := this.module.configure.GetGourmetConfigData(foodtype, skillLv) // 美食家配置表
|
|
for i := 0; i < int(order.FoodCount); i++ {
|
|
curTime += _skillCfg.Needtime
|
|
// 判断是不是第二天
|
|
if gourmet.CookingFood.ETime == 0 {
|
|
gourmet.CookingFood.ETime = time.Now().Unix()
|
|
}
|
|
if !utils.IsToday(gourmet.CookingFood.ETime + int64(curTime)) { // 判断是不是今天
|
|
// 跨天了
|
|
gourmet.CookingFood.ETime = gourmet.CookingFood.ETime + int64(curTime) // 设置下单的时间
|
|
gourmet.Ctime = gourmet.CookingFood.ETime // 设置创建订单时间
|
|
gourmet.OrderCostTime = 0 // 清空当天的订单时长
|
|
}
|
|
order.FoodCount--
|
|
this.GetDropReward(_gourmetcfg.Using, _gourmetcfg.Propsgroup, gourmet.Items) // 获取掉落奖励
|
|
|
|
if curTime > costTime {
|
|
gourmet.OrderCostTime += int32(curTime - costTime)
|
|
// 转时间戳
|
|
eTimd := time.Now().Unix() + int64(curTime-costTime)
|
|
gourmet.CookingFood.FoodType = order.FoodType
|
|
gourmet.CookingFood.ETime = eTimd
|
|
bCooking = true
|
|
|
|
// 记录下订单时间
|
|
gourmet.Ctime = time.Now().Unix()
|
|
mapData["ctime"] = gourmet.Ctime
|
|
break
|
|
}
|
|
gourmet.OrderCostTime += curTime
|
|
}
|
|
if bCooking { // 分配了正在製作的食物
|
|
break
|
|
}
|
|
}
|
|
if !bCooking { // 经过计算没有烹饪食物的时候
|
|
gourmet.CookingFood = nil
|
|
}
|
|
|
|
// 保存信息
|
|
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)
|
|
for _, v := range gourmet.Foods {
|
|
if v.FoodCount > 0 && v.FoodType == skillType {
|
|
_skillCfg := this.module.configure.GetGourmetSkillConfigData(skillType, skilllv)
|
|
if _skillCfg != nil {
|
|
v.CookTime = _skillCfg.Needtime * v.FoodCount
|
|
mapData["foods"] = gourmet.Foods
|
|
}
|
|
break
|
|
}
|
|
}
|
|
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)
|
|
for _, v := range gourmet.Foods {
|
|
if v.FoodCount > 0 {
|
|
_skillCfg := this.module.configure.GetGourmetSkillConfigData(skillType, skilllv)
|
|
if _skillCfg != nil {
|
|
v.CookTime += _skillCfg.Needtime * v.FoodCount
|
|
if v.CookTime < 0 { // 担心配置错误 为负数情况 所以这里做下判断
|
|
v.CookTime = 0
|
|
}
|
|
mapData["foods"] = gourmet.Foods
|
|
}
|
|
}
|
|
}
|
|
mapData["specialSkill"] = gourmet.SpecialSkill
|
|
this.module.ModifyGourmetData(uid, mapData)
|
|
}
|