package library import ( "context" "errors" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "go.mongodb.org/mongo-driver/bson/primitive" ) type Library struct { modules.ModuleBase service base.IRPCXService modelLibrary *modelLibrary modelFetter *modelFetter api *apiComp configure *configureComp } func NewModule() core.IModule { return &Library{} } func (this *Library) GetType() core.M_Modules { return comm.ModuleLibrary } func (this *Library) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) return } func (this *Library) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelLibrary = this.RegisterComp(new(modelLibrary)).(*modelLibrary) this.modelFetter = this.RegisterComp(new(modelFetter)).(*modelFetter) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } func (this *Library) Start() (err error) { err = this.ModuleBase.Start() this.service.RegisterFunctionName(string(comm.Rpc_ModuleFetter), this.Rpc_ModuleFetter) return } // 接口信息 更新藏书馆数据 func (this *Library) ModifyLibraryData(uid string, obj string, data map[string]interface{}) (code pb.ErrorCode) { err := this.modelLibrary.modifyLibraryDataByObjId(uid, obj, data) if err != nil { code = pb.ErrorCode_DBError } return } // 获取藏书馆列表 func (this *Library) GetLibraryList(uid string) []*pb.DBLibrary { return this.modelLibrary.getLibraryList(uid) } // 通过羁绊id 来查询羁绊信息 func (this *Library) GetLibraryListByFid(uid string, fid int32) *pb.DBLibrary { list := this.modelLibrary.getLibraryList(uid) for _, v := range list { if v.Fid == fid { return v } } return nil } //通过羁绊id 创建多个羁绊信息 func (this *Library) CreateLibrary(uid string, fid int32, heroConfId string) (code pb.ErrorCode, obj *pb.DBLibrary) { obj = &pb.DBLibrary{ Id: primitive.NewObjectID().Hex(), Uid: uid, Fid: fid, Hero: map[string]int32{}, Prize: map[int32]int32{}, Fetterlv: 0, } conf := this.configure.GetLibraryFetter(fid, 1) if conf != nil { for _, v := range conf.Hid { obj.Hero[v] = 0 // 默认值 if v == heroConfId { obj.Hero[heroConfId] = 1 // 获得的英雄好感度等级为1级 } } if err := this.modelLibrary.createLibrary(uid, obj); err != nil { code = pb.ErrorCode_DBError } } return } func (this *Library) ModifyHeroFetterData(uid string, obj string, data map[string]interface{}) (code pb.ErrorCode) { err := this.modelFetter.modifyHeroFetterDataByObjId(uid, obj, data) if err != nil { code = pb.ErrorCode_DBError } return } func (this *Library) GetHeroFetterList(uid string) []*pb.DBHeroFetter { return this.modelFetter.getHeroFetterList(uid) } // 创建一条英雄羁绊信息 func (this *Library) createHeroFetter(uid string, heroConfId string) (obj *pb.DBHeroFetter, code pb.ErrorCode) { this.modelFetter.getHeroFetterList(uid) obj = &pb.DBHeroFetter{ Id: primitive.NewObjectID().Hex(), Uid: uid, Heroid: heroConfId, History: make([]int32, 0), Favorlv: 1, Stroyprize: make([]int32, 0), Lvprize: make([]int32, 0), } if err := this.modelFetter.createHeroFetter(uid, obj); err != nil { code = pb.ErrorCode_DBError } return } func (this *Library) QueryHeroFetter(uid string) (data []*pb.DBHeroFetter) { data = this.GetHeroFetterList(uid) return } func (this *Library) QueryOneHeroFetter(uid string, cid string) *pb.DBHeroFetter { _data := this.GetHeroFetterList(uid) for _, v := range _data { if v.Heroid == cid { return v } } return nil } // 发送prc消息到区服处理 func (this *Library) SendRpcAddHero(session comm.IUserSession, heroConfId string) (err error) { if this.IsCross() { if _, err = this.service.AcrossClusterRpcGo( // 给区服发送rpc消息 context.Background(), session.GetServiecTag(), comm.Service_Worker, string(comm.Rpc_ModuleFetter), pb.RPCGeneralReqA2{Param1: session.GetUserId(), Param2: heroConfId}, nil); err != nil { this.Errorln(err) } return } return } // 创建一条羁绊信息 func (this *Library) AddHeroFetterData(uid, heroConfId string) (code pb.ErrorCode) { _conf := this.configure.GetLibraryHero(heroConfId) // 配置表中没有这个英雄数据 直接返回 if _conf == nil { return } var ( objFetter *pb.DBHeroFetter // 详细羁绊信息数据 ) rsp := &pb.LibraryChangePush{} _data := this.QueryOneHeroFetter(uid, heroConfId) if _data == nil { objFetter, code = this.createHeroFetter(uid, heroConfId) if code == pb.ErrorCode_Success { rsp.Fetter = append(rsp.Fetter, objFetter) } else { this.Errorf("createHeroFetter failed:%v,uid:%s,heroid:%s", code, uid, heroConfId) } } for _, fid := range _conf.Fid { // 查询是否存在这个羁绊对象 obj := this.GetLibraryListByFid(uid, fid) if obj == nil { // 没有羁绊信息 code, obj = this.CreateLibrary(uid, fid, heroConfId) if code != pb.ErrorCode_Success { this.Errorf("CreateLibrary failed: %v,uid:%s,fid:%d", code, uid, fid) } rsp.Data = append(rsp.Data, obj) } else { // 羁绊信息中没有这个heroid 也需要加进来 // 同步数据 mapData := make(map[string]interface{}, 0) if obj.Activation { // 重新计算最低等级 var minLv int32 conf := this.configure.GetLibraryFetter(obj.Fid, 1) list := this.GetHeroFetterList(uid) for _, v1 := range conf.Hid { for _, v := range list { if v.Heroid == v1 { if minLv == 0 { minLv = v.Favorlv } if minLv > v.Favorlv { minLv = v.Favorlv } break } } } obj.Fetterlv = minLv mapData["fetterlv"] = obj.Fetterlv } mapData["hero"] = obj.Hero this.modelLibrary.modifyLibraryDataByObjId(uid, obj.Id, mapData) rsp.Data = append(rsp.Data, obj) } } if len(rsp.Data) != 0 || len(rsp.Fetter) != 0 { this.SendMsgToUser(string(this.GetType()), LibraryChangePush, rsp, uid) } return } func (this *Library) getLibraryByObjID(uid, oid string) *pb.DBLibrary { return this.modelLibrary.getOneLibrary(uid, oid) } func (this *Library) Rpc_ModuleFetter(ctx context.Context, args *pb.RPCGeneralReqA2, reply *pb.EmptyResp) (err error) { this.Debug("Rpc_ModuleFetter", log.Fields{"args": args.String()}) if args.Param1 == "" || args.Param2 == "" { err = errors.New("请求参数错误") } if session, ok := this.GetUserSession(args.Param1); !ok { err = fmt.Errorf("目标用户:%s 不在线", args.Param1) return } else { this.AddHeroFetterData(session.GetUserId(), args.Param2) session.Push() } return } //与N个英雄好感度等级达到A func (this *Library) CheckRtype132(uid string, num int32, lv int32) bool { fetter := this.GetHeroFetterList(uid) if len(fetter) == 0 { return false } for _, v := range fetter { if v.Favorlv >= lv { num-- } } if num >= 0 { return true } return false } //与指定英雄好感度等级达到N func (this *Library) CheckRtype133(uid string, heroId string, lv int32) bool { fetter := this.GetHeroFetterList(uid) if len(fetter) == 0 { return false } for _, v := range fetter { if v.Heroid == heroId && v.Favorlv >= lv { return true } } return false }