From 1783ca780b34c8bf5222a9f7eca1d571f87862d2 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Tue, 14 Feb 2023 10:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=93=81=E5=8C=A0=E9=93=BA?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/friend/api_cross_stop.go | 6 +- modules/smithyv2/api.go | 35 +++++ modules/smithyv2/api_createorder.go | 117 +++++++++++++++++ modules/smithyv2/api_deskskilllv.go | 85 ++++++++++++ modules/smithyv2/api_getReward.go | 49 +++++++ modules/smithyv2/api_getlist.go | 34 +++++ modules/smithyv2/api_getranduser.go | 96 ++++++++++++++ modules/smithyv2/api_stoveskilllv.go | 63 +++++++++ modules/smithyv2/comp_configure.go | 93 +++++++++++++ modules/smithyv2/model_smithy.go | 188 +++++++++++++++++++++++++++ modules/smithyv2/module.go | 63 +++++++++ 11 files changed, 827 insertions(+), 2 deletions(-) create mode 100644 modules/smithyv2/api.go create mode 100644 modules/smithyv2/api_createorder.go create mode 100644 modules/smithyv2/api_deskskilllv.go create mode 100644 modules/smithyv2/api_getReward.go create mode 100644 modules/smithyv2/api_getlist.go create mode 100644 modules/smithyv2/api_getranduser.go create mode 100644 modules/smithyv2/api_stoveskilllv.go create mode 100644 modules/smithyv2/comp_configure.go create mode 100644 modules/smithyv2/model_smithy.go create mode 100644 modules/smithyv2/module.go diff --git a/modules/friend/api_cross_stop.go b/modules/friend/api_cross_stop.go index 6a5190074..f61e3d0d4 100644 --- a/modules/friend/api_cross_stop.go +++ b/modules/friend/api_cross_stop.go @@ -3,6 +3,8 @@ package friend import ( "go_dreamfactory/comm" "go_dreamfactory/pb" + + "google.golang.org/protobuf/proto" ) // 切磋终止 @@ -10,14 +12,14 @@ func (this *apiComp) StopCheck(session comm.IUserSession, req *pb.FriendStopReq) return } -func (this *apiComp) Stop(session comm.IUserSession, req *pb.FriendStopReq) (code pb.ErrorCode) { +func (this *apiComp) Stop(session comm.IUserSession, req *pb.FriendStopReq) (code pb.ErrorCode, data proto.Message) { if code = this.StopCheck(session, req); code != pb.ErrorCode_Success { return } //清楚切磋请求记录 this.moduleFriend.ModelFriendQiecuo.DelByUId(req.Uid) - + resp := &pb.FriendStopResp{ IsSucc: true, } diff --git a/modules/smithyv2/api.go b/modules/smithyv2/api.go new file mode 100644 index 000000000..ac92b2ac9 --- /dev/null +++ b/modules/smithyv2/api.go @@ -0,0 +1,35 @@ +package smithy + +import ( + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" +) + +const ( + SmithyGetListResp = "getlist" + SmithyCreateOrderResp = "createorder" + SmithyDeskSkillLvResp = "deskskilllv" + SmithyStoveSkillLvResp = "stoveskilllv" + SmithyGetRewardResp = "getreward" + SmithyGetRandUserResp = "getranduser" +) + +type apiComp struct { + modules.MCompGate + service core.IService + module *Smithy +} + +//组件初始化接口 +func (this *apiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompGate.Init(service, module, comp, options) + this.module = module.(*Smithy) + this.service = service + return +} + +func (this *apiComp) Start() (err error) { + err = this.MCompGate.Start() + + return +} diff --git a/modules/smithyv2/api_createorder.go b/modules/smithyv2/api_createorder.go new file mode 100644 index 000000000..c315ba6b8 --- /dev/null +++ b/modules/smithyv2/api_createorder.go @@ -0,0 +1,117 @@ +package smithy + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/pb" + "go_dreamfactory/sys/configure" + cfg "go_dreamfactory/sys/configure/structs" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) CreateOrderCheck(session comm.IUserSession, req *pb.SmithyCreateOrderReq) (code pb.ErrorCode) { + if len(req.Order) == 0 { + code = pb.ErrorCode_ReqParameterError + return + } + return +} + +func (this *apiComp) CreateOrder(session comm.IUserSession, req *pb.SmithyCreateOrderReq) (code pb.ErrorCode, data proto.Message) { + var ( + res []*cfg.Gameatn + costTime int32 + privilegeAddItme int32 // 特权额外增加的时间 + ) + + code = this.CreateOrderCheck(session, req) + if code != pb.ErrorCode_Success { + return // 参数校验失败直接返回 + } + _smithy, err := this.module.modelSmithy.getSmithyList(session.GetUserId()) + if err != nil { + code = pb.ErrorCode_DBError + return + } + _skillCfg := this.module.configure.GetSmithyStoveConfigData(_smithy.StoveLv) + needTime := _skillCfg.Time // 订单需要的时间 + for _, order := range req.Order { + if order.Count == 0 { + continue + } + costTime += needTime * order.Count + } + if _smithy.Ctime == 0 { + _smithy.Ctime = configure.Now().Unix() + } + // if !utils.IsToday(_smithy.Ctime) { + // _smithy.Ctime = configure.Now().Unix() + // _smithy.OrderCostTime = 0 + // } + _smithy.Orders = append(_smithy.Orders, req.Order...) // 直接追加订单数据 + if _smithy.Clang == nil || (_smithy.Clang != nil && _smithy.Clang.ETime == 0) { + for _, v := range _smithy.Orders { + if v.Count > 0 { + v.Count-- + // 获取生产时间 + _smithy.Clang = &pb.Clang{ + DeskType: v.DeskType, + ETime: configure.Now().Unix() + int64(needTime), + STime: configure.Now().Unix(), + } + break + } + } + } + // 計算耗時 + for _, v := range _smithy.Orders { + if v.Count > 0 { + v.NeedTime = needTime * v.Count + } + if skillLv, ok := _smithy.Skill[v.DeskType]; ok { + conf := this.module.configure.GetSmithyConfigData(v.DeskType, skillLv) + res = append(res, conf.Orderneed...) // 订单消耗 + } + } + if _smithy.Clang != nil && _smithy.Clang.ETime == 0 { + _smithy.Clang = nil + } + // 获取总的下单时长 + cfgCom := this.module.configure.GetGlobalConf() + if cfgCom == nil { + return + } + _smithy.OrderCostTime += costTime + privilegeAddItme = this.module.ModulePrivilege.GetCountByPrivilegeId(session.GetUserId(), comm.PrivilegeType10) + if cfgCom.SmithyMaxtime+privilegeAddItme < _smithy.OrderCostTime { // 大于总时长是不允许的 + code = pb.ErrorCode_GourmetMoreOrderTime + return + } + if code = this.module.ConsumeRes(session, res, true); code != pb.ErrorCode_Success { // 消耗校验 + return + } + + // 校验通过 写数据 + mapData := make(map[string]interface{}, 0) + sz := make([]*pb.OrderClang, 0) + for _, v := range _smithy.Orders { + if v.Count != 0 { + sz = append(sz, v) + } + } + _smithy.Orders = sz + mapData["orders"] = _smithy.Orders + mapData["orderCostTime"] = _smithy.OrderCostTime + mapData["clang"] = _smithy.Clang // 正在做的 + mapData["ctime"] = _smithy.Ctime + code = this.module.ModifySmithyData(session.GetUserId(), mapData) + iTotal := 0 + for _, v := range req.Order { + iTotal += int(v.Count) + } + this.module.ModuleRtask.SendToRtask(session, comm.Rtype148, int32(iTotal)) + + session.SendMsg(string(this.module.GetType()), SmithyCreateOrderResp, &pb.SmithyCreateOrderResp{Data: _smithy}) + return +} diff --git a/modules/smithyv2/api_deskskilllv.go b/modules/smithyv2/api_deskskilllv.go new file mode 100644 index 000000000..4e185f2ba --- /dev/null +++ b/modules/smithyv2/api_deskskilllv.go @@ -0,0 +1,85 @@ +package smithy + +import ( + "crypto/rand" + "go_dreamfactory/comm" + "go_dreamfactory/pb" + cfg "go_dreamfactory/sys/configure/structs" + "math/big" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) DeskSkillLvCheck(session comm.IUserSession, req *pb.SmithyDeskSkillLvReq) (code pb.ErrorCode) { + if req.DeskType == 0 { + code = pb.ErrorCode_ReqParameterError + } + return +} + +func (this *apiComp) DeskSkillLv(session comm.IUserSession, req *pb.SmithyDeskSkillLvReq) (code pb.ErrorCode, dat proto.Message) { + var ( + bFindSkill bool + curSkillCfg *cfg.GameSmithyData + ) + code = this.DeskSkillLvCheck(session, req) + if code != pb.ErrorCode_Success { + return // 参数校验失败直接返回 + } + _smithy, err := this.module.modelSmithy.getSmithyList(session.GetUserId()) + if err != nil { + code = pb.ErrorCode_DBError + return + } + + for k, v := range _smithy.Skill { + if k == req.DeskType { + bFindSkill = true + // 查询配置文件 + curSkillCfg = this.module.configure.GetSmithyConfigData(k, v) + if curSkillCfg != nil && curSkillCfg.Starupneed != nil { + //获取下一级 + NextSkillCfg := this.module.configure.GetSmithyConfigData(k, v+1) + if NextSkillCfg == nil { + code = pb.ErrorCode_GourmetSkillMaxLv + return + } + // 升级 + code = this.module.ConsumeRes(session, curSkillCfg.Starupneed, true) // 消耗检测 + if code != pb.ErrorCode_Success { + return + } + // 概率升级 + n, _ := rand.Int(rand.Reader, big.NewInt(100)) + + if n.Int64() < int64(curSkillCfg.Probability) { // 可以升级 + // 技能升级成功 + _smithy.Skill[req.DeskType] += 1 + _smithy.DeskFloor[req.DeskType] = 0 + this.module.modelSmithy.CalculationDeskSkillLv(session.GetUserId(), _smithy) + + } else { + _smithy.DeskFloor[req.DeskType] += 1 + if _smithy.DeskFloor[req.DeskType] >= curSkillCfg.Floors { // 触发保底 + _smithy.Skill[req.DeskType] += 1 + _smithy.DeskFloor[req.DeskType] = 0 + this.module.modelSmithy.CalculationDeskSkillLv(session.GetUserId(), _smithy) + + } + } + } else { + code = pb.ErrorCode_GourmetSkillMaxLv + return + } + break + } + } + if !bFindSkill { + code = pb.ErrorCode_ReqParameterError + return + } + + session.SendMsg(string(this.module.GetType()), SmithyDeskSkillLvResp, &pb.SmithyDeskSkillLvResp{Data: _smithy}) + return +} diff --git a/modules/smithyv2/api_getReward.go b/modules/smithyv2/api_getReward.go new file mode 100644 index 000000000..474ce7f9a --- /dev/null +++ b/modules/smithyv2/api_getReward.go @@ -0,0 +1,49 @@ +package smithy + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/pb" + cfg "go_dreamfactory/sys/configure/structs" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) GetRewardCheck(session comm.IUserSession, req *pb.SmithyGetRewardReq) (code pb.ErrorCode) { + + return +} + +///美食城领取奖励 +func (this *apiComp) GetReward(session comm.IUserSession, req *pb.SmithyGetRewardReq) (code pb.ErrorCode, data proto.Message) { + code = this.GetRewardCheck(session, req) + _gourmet, err := this.module.modelSmithy.getSmithyList(session.GetUserId()) + if err != nil { + code = pb.ErrorCode_DBError + return + } + if len(_gourmet.Items) > 0 { + res := make([]*cfg.Gameatn, 0) + for _, v := range _gourmet.Items { + res = append(res, &cfg.Gameatn{ + A: v.A, + T: v.T, + N: v.N, + }) + } + code = this.module.DispenseRes(session, res, true) + if code != pb.ErrorCode_Success { + return + } + // 随机任务 + this.module.SendRdTask(session, _gourmet.Items) + + _gourmet.Items = nil + mapData := make(map[string]interface{}, 0) + mapData["items"] = nil + code = this.module.ModifySmithyData(session.GetUserId(), mapData) + } + session.SendMsg(string(this.module.GetType()), SmithyGetRewardResp, &pb.SmithyGetRewardResp{Data: _gourmet}) + + return +} diff --git a/modules/smithyv2/api_getlist.go b/modules/smithyv2/api_getlist.go new file mode 100644 index 000000000..f95832ef1 --- /dev/null +++ b/modules/smithyv2/api_getlist.go @@ -0,0 +1,34 @@ +package smithy + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/pb" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.SmithyGetListReq) (code pb.ErrorCode) { + + return +} + +///获取美食城基本信息 +func (this *apiComp) GetList(session comm.IUserSession, req *pb.SmithyGetListReq) (code pb.ErrorCode, data proto.Message) { + + code = this.GetListCheck(session, req) + if code != pb.ErrorCode_Success { + return // 参数校验失败直接返回 + } + _smithy, err := this.module.modelSmithy.getSmithyList(session.GetUserId()) + if err != nil { + code = pb.ErrorCode_DBError + return + } + // 计算订单信息 + this.module.modelSmithy.CalculationSmithy(session.GetUserId(), _smithy) + session.SendMsg(string(this.module.GetType()), SmithyGetListResp, &pb.SmithyGetListResp{Data: _smithy}) + + this.module.ModuleRtask.SendToRtask(session, comm.Rtype137, _smithy.TotalTime) + return +} diff --git a/modules/smithyv2/api_getranduser.go b/modules/smithyv2/api_getranduser.go new file mode 100644 index 000000000..8389d55d7 --- /dev/null +++ b/modules/smithyv2/api_getranduser.go @@ -0,0 +1,96 @@ +package smithy + +import ( + "context" + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/pb" + "go_dreamfactory/sys/db" + "go_dreamfactory/utils" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo/options" + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) GetRandUserCheck(session comm.IUserSession, req *pb.SmithyGetRandUserReq) (code pb.ErrorCode) { + if req.People <= 0 { + code = pb.ErrorCode_ReqParameterError + return + } + return +} + +/// 获取一些玩家数据 +func (this *apiComp) GetRandUser(session comm.IUserSession, req *pb.SmithyGetRandUserReq) (code pb.ErrorCode, data proto.Message) { + var ( + szDbUser []*pb.DBUser + mapUser map[string]struct{} + ) + mapUser = make(map[string]struct{}, 0) + code = this.GetRandUserCheck(session, req) + if code != pb.ErrorCode_Success { + return // 参数校验失败直接返回 + } + + // 获取在线玩家信息 + onlineList, err := this.module.ModuleUser.UserOnlineList() + if err != nil { + code = pb.ErrorCode_DBError + return + } + + var szUid []string + for _, v := range onlineList { + if v.Uid == session.GetUserId() || v.Uid == "" { // 过滤自己信息 + continue + } + szUid = append(szUid, v.Uid) + } + // 随机在线玩家信息 + if len(szUid) > int(req.People) { + randArr := utils.RandomNumbers(0, len(szUid), int(req.People)) + for _, v := range randArr { + if szUid[v] != "" { + mapUser[szUid[v]] = struct{}{} + } + } + } else { // 数量不足 则有多少给多少 + for _, v := range szUid { + mapUser[v] = struct{}{} + } + left := int(req.People) - len(mapUser) + if left > 0 { // 一个人也没有 那就从db 中随机取 + tag, _, b := utils.UIdSplit(session.GetUserId()) + if b { + if conn, err := db.ServerDBConn(tag); err == nil { + dbModel := db.NewDBModel(comm.TableHero, time.Hour, conn) + if _data, err1 := dbModel.DB.Find(core.SqlTable(comm.TableUser), bson.M{}, options.Find().SetSort(bson.M{"lv": -1}).SetLimit(int64(req.People))); err1 == nil { + for _data.Next(context.TODO()) { + temp := &pb.DBUser{} + if err = _data.Decode(temp); err == nil { + if len(mapUser)+len(szDbUser) >= int(req.People) { + break + } + if _, ok := mapUser[temp.Uid]; !ok { + szDbUser = append(szDbUser, temp) + } + } + } + } + } + } + } + } + for k := range mapUser { + if user := this.module.ModuleUser.GetUser(k); user != nil { + szDbUser = append(szDbUser, user) // 转成user对象 + } else { + this.module.Errorf("%v", err) + } + } + session.SendMsg(string(this.module.GetType()), SmithyGetRandUserResp, &pb.SmithyGetRandUserResp{User: szDbUser}) + return +} diff --git a/modules/smithyv2/api_stoveskilllv.go b/modules/smithyv2/api_stoveskilllv.go new file mode 100644 index 000000000..90254df9f --- /dev/null +++ b/modules/smithyv2/api_stoveskilllv.go @@ -0,0 +1,63 @@ +package smithy + +import ( + "crypto/rand" + "go_dreamfactory/comm" + "go_dreamfactory/pb" + "math/big" + + "google.golang.org/protobuf/proto" +) + +//参数校验 +func (this *apiComp) StoveSkillLvCheck(session comm.IUserSession, req *pb.SmithyStoveSkillLvReq) (code pb.ErrorCode) { + + return +} + +func (this *apiComp) StoveSkillLv(session comm.IUserSession, req *pb.SmithyStoveSkillLvReq) (code pb.ErrorCode, dat proto.Message) { + var bLevelUp bool + code = this.StoveSkillLvCheck(session, req) + if code != pb.ErrorCode_Success { + return // 参数校验失败直接返回 + } + _smithy, err := this.module.modelSmithy.getSmithyList(session.GetUserId()) + if err != nil { + code = pb.ErrorCode_DBError + return + } + curLvData := this.module.configure.GetSmithyStoveConfigData(_smithy.StoveLv) + if curLvData == nil { + code = pb.ErrorCode_GourmetSkillMaxLv + return + } + nextLvData := this.module.configure.GetSmithyStoveConfigData(_smithy.StoveLv + 1) + if nextLvData == nil { + code = pb.ErrorCode_GourmetSkillMaxLv + return + } + // 升级 + code = this.module.ConsumeRes(session, curLvData.Starupneed, true) // 消耗检测 + if code != pb.ErrorCode_Success { + return + } + + // 概率升级 + n, _ := rand.Int(rand.Reader, big.NewInt(100)) + + if n.Int64() < int64(curLvData.Probability) { // 可以升级 + bLevelUp = true + } else { // 升级失败了 记录 + _smithy.StoveFloor += 1 + if curLvData.Floors >= _smithy.StoveFloor { // 触发保底 + bLevelUp = true + } + } + if bLevelUp { + _smithy.StoveFloor = 0 // 清理保底数据 + _smithy.StoveLv += 1 + this.module.modelSmithy.CalculationStoveSkillLv(session.GetUserId(), _smithy, _smithy.StoveLv) + } + session.SendMsg(string(this.module.GetType()), SmithyStoveSkillLvResp, &pb.SmithyStoveSkillLvResp{Data: _smithy}) + return +} diff --git a/modules/smithyv2/comp_configure.go b/modules/smithyv2/comp_configure.go new file mode 100644 index 000000000..3487bcfb7 --- /dev/null +++ b/modules/smithyv2/comp_configure.go @@ -0,0 +1,93 @@ +package smithy + +import ( + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" + "go_dreamfactory/modules" + "go_dreamfactory/sys/configure" + cfg "go_dreamfactory/sys/configure/structs" + "sync" +) + +const ( + game_smithy = "game_smithy.json" + game_smithystove = "game_smithystove.json" +) + +///配置管理基础组件 +type configureComp struct { + modules.MCompConfigure + module *Smithy + hlock sync.RWMutex + _smithyMap map[int64]*cfg.GameSmithyData +} + +//组件初始化接口 +func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompConfigure.Init(service, module, comp, options) + this._smithyMap = make(map[int64]*cfg.GameSmithyData, 0) + this.module = module.(*Smithy) + configure.RegisterConfigure(game_smithy, cfg.NewGameSmithy, func() { + if v, err := this.GetConfigure(game_smithy); err == nil { + if configure, ok := v.(*cfg.GameSmithy); ok { + this.hlock.Lock() + defer this.hlock.Unlock() + for _, value := range configure.GetDataList() { + this._smithyMap[int64(value.Type<<16)+int64(value.Star)] = value + } + return + } + } + log.Errorf("get game_pagoda conf err:%v", err) + return + }) + err = this.LoadConfigure(game_smithystove, cfg.NewGameSmithyStove) + return +} + +func (this *configureComp) GetSmithyConfigData(smithyType int32, level int32) (data *cfg.GameSmithyData) { + + return this._smithyMap[int64(smithyType<<16)+int64(level)] +} +func (this *configureComp) GetSmithyTypeConfigData() (mapType map[int32]struct{}) { + mapType = make(map[int32]struct{}, 0) + if v, err := this.GetConfigure(game_smithy); err == nil { + if configure, ok := v.(*cfg.GameSmithy); ok { + for _, v1 := range configure.GetDataList() { + if _, ok := mapType[v1.Type]; !ok { + mapType[v1.Type] = struct{}{} + } + } + return + } + } + return +} + +// 获取炉子配置数据 +func (this *configureComp) GetSmithyStoveConfigData(level int32) (data *cfg.GameSmithyStoveData) { + if v, err := this.GetConfigure(game_smithystove); err == nil { + if configure, ok := v.(*cfg.GameSmithyStove); ok { + data = configure.Get(int32(level)) + return + } + } + return +} + +//加载多个配置文件 +func (this *configureComp) LoadMultiConfigure(confs map[string]interface{}) (err error) { + for k, v := range confs { + err = configure.RegisterConfigure(k, v, nil) + if err != nil { + log.Errorf("配置文件:%s解析失败!", k) + break + } + } + return +} + +//读取配置数据 +func (this *configureComp) GetConfigure(name string) (v interface{}, err error) { + return configure.GetConfigure(name) +} diff --git a/modules/smithyv2/model_smithy.go b/modules/smithyv2/model_smithy.go new file mode 100644 index 000000000..728863f21 --- /dev/null +++ b/modules/smithyv2/model_smithy.go @@ -0,0 +1,188 @@ +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()) + + if (smithy.Clang != nil && smithy.Clang.ETime >= configure.Now().Unix()) || smithy.Clang == nil { + for _, order := range smithy.Orders { + _gourmetcfg := this.module.configure.GetSmithyConfigData(order.DeskType, smithy.Skill[order.DeskType]) // 美食家配置表 + + if order.Count > 0 { + if smithy.Clang != nil && 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]) + if smithy.Clang.STime < zeroTime && zeroTime <= smithy.Clang.ETime { // 跨天清空订单耗时 + smithy.OrderCostTime = 0 + for _, order := range smithy.Orders { // 重新计算订单时常 + smithy.OrderCostTime += order.Count * 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 + smithy.TotalTime += szTime[order.DeskType] + } + } + + if smithy.Clang != nil && 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 + } + } + + // 清除数量为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 // 正在做的 + mapData["ctime"] = smithy.Ctime + mapData["totalTime"] = smithy.TotalTime + 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) +} diff --git a/modules/smithyv2/module.go b/modules/smithyv2/module.go new file mode 100644 index 000000000..1bcebeaaf --- /dev/null +++ b/modules/smithyv2/module.go @@ -0,0 +1,63 @@ +/* +模块名:Smithy +描述:美食家模块 +开发:梅雄风 +*/ +package smithy + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" + "go_dreamfactory/pb" +) + +type Smithy struct { + modules.ModuleBase + modelSmithy *modelSmithy + api *apiComp + configure *configureComp +} + +func NewModule() core.IModule { + return &Smithy{} +} + +func (this *Smithy) GetType() core.M_Modules { + return comm.ModuleSmithy +} + +func (this *Smithy) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { + err = this.ModuleBase.Init(service, module, options) + + return +} + +func (this *Smithy) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.api = this.RegisterComp(new(apiComp)).(*apiComp) + this.modelSmithy = this.RegisterComp(new(modelSmithy)).(*modelSmithy) + this.configure = this.RegisterComp(new(configureComp)).(*configureComp) +} + +// 接口信息 +func (this *Smithy) ModifySmithyData(uid string, data map[string]interface{}) (code pb.ErrorCode) { + err := this.modelSmithy.modifySmithyDataByObjId(uid, data) + if err != nil { + code = pb.ErrorCode_DBError + } + return +} + +func (this *Smithy) SendRdTask(session comm.IUserSession, Items []*pb.UserAssets) { + var equip map[int32]int32 // key xingji value 数量 + equip = make(map[int32]int32, 0) + for _, v := range Items { + if cfg := this.configure.GetEquipmentConfigureById(v.T); cfg == nil { + equip[cfg.Star]++ + } + } + for k, v := range equip { + this.ModuleRtask.SendToRtask(session, comm.Rtype51, v, k) + } +}