go_dreamfactory/modules/library/module.go

371 lines
9.8 KiB
Go

package library
import (
"context"
"errors"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Library struct {
modules.ModuleBase
service base.IRPCXService
modelLibrary *modelLibrary
modelFetter *modelFetter
modelFetterstory *modelFetterstory
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) {
if err = this.ModuleBase.Init(service, module, options); err != nil {
return
}
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.modelFetterstory = this.RegisterComp(new(modelFetterstory)).(*modelFetterstory)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
func (this *Library) Start() (err error) {
if err = this.ModuleBase.Start(); err != nil {
return
}
this.service.RegisterFunctionName(string(comm.Rpc_ModuleFetter), this.Rpc_ModuleFetter)
return
}
// 接口信息 更新藏书馆数据
func (this *Library) ModifyLibraryData(uid string, obj string, data map[string]interface{}) (errdata *pb.ErrorData) {
err := this.modelLibrary.modifyLibraryDataByObjId(uid, obj, data)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
}
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) GetLibraryListByHid(uid string, hid string) *pb.DBLibrary {
list := this.modelLibrary.getLibraryList(uid)
for _, v := range list {
if _, ok := v.Herofetter[hid]; ok {
return v
}
}
return nil
}
//
func (this *Library) CheckFetter(uid string, hid string) (dbLibrary []*pb.DBLibrary, fetter []*pb.DBHeroFetter) {
var (
oid *pb.DBHeroFetter
)
oid = this.CheckHeroFetter(uid, hid) // 校验羁绊数据
szFid := this.configure.GetHeroFetterID(hid)
for fid := range szFid {
if list := this.GetLibraryListByFid(uid, fid); list == nil { // 没有这条羁绊数据
tmp := &pb.DBLibrary{ // 创建一条羁绊数据
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Fid: fid,
Herofetter: map[string]string{},
Prize: map[int32]int32{},
}
if oid != nil { // check DBHeroFetter
tmp.Herofetter[hid] = oid.Id
fetter = append(fetter, oid)
}
if err := this.modelLibrary.createLibrary(uid, tmp); err != nil {
this.modelFetter.module.Errorf("createLibrary error: %v,obj:%v", err, tmp)
continue
}
dbLibrary = append(dbLibrary, tmp)
} else { // 有这条羁绊数据
if _, ok := list.Herofetter[hid]; !ok {
if oid != nil {
list.Herofetter[hid] = oid.Id
fetter = append(fetter, oid)
}
mapData := make(map[string]interface{}, 0)
mapData["herofetter"] = list.Herofetter
this.modelLibrary.modifyLibraryDataByObjId(uid, list.Id, mapData) // 更新新的羁绊信息
dbLibrary = append(dbLibrary, list)
}
}
}
return
}
func (this *Library) ModifyHeroFetterData(uid string, obj string, data map[string]interface{}) (errdata *pb.ErrorData) {
err := this.modelFetter.modifyHeroFetterDataByObjId(uid, obj, data)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
}
return
}
func (this *Library) GetHeroFetterList(uid string) []*pb.DBHeroFetter {
return this.modelFetter.getHeroFetterList(uid)
}
// 校验羁绊数据 如果没有 则创建
func (this *Library) CheckHeroFetter(uid string, hid string) (obj *pb.DBHeroFetter) {
var (
sz []*pb.DBHeroFetter
bFound bool
err error
)
sz = this.modelFetter.getHeroFetterList(uid)
for _, v := range sz {
if v.Heroid == hid {
bFound = true
break
}
}
if !bFound { // 创建一条英雄数据
if obj, err = this.createHeroFetter(uid, hid); err != nil {
return
}
}
return
}
// 创建一条英雄羁绊信息
func (this *Library) createHeroFetter(uid string, heroConfId string) (obj *pb.DBHeroFetter, err error) {
obj = &pb.DBHeroFetter{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Heroid: heroConfId,
History: make([]int32, 0),
Favorlv: 1,
Lvprize: make(map[int32]int32, 0),
}
err = this.modelFetter.createHeroFetter(uid, obj)
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(uid string, heroConfId []string, serverTag string) (err error) {
if this.IsCross() {
if _, err = this.service.AcrossClusterRpcGo( // 给区服发送rpc消息
context.Background(),
serverTag,
comm.Service_Worker,
string(comm.Rpc_ModuleFetter),
pb.RPCGeneralReqB1{Param1: uid, Param2: heroConfId},
nil); err != nil {
this.Errorln(err)
}
return
}
return
}
// 创建一条羁绊信息
func (this *Library) AddHeroFetterData(uid, heroConfId string) (errdata *pb.ErrorData) {
rsp := &pb.LibraryChangePush{}
rsp.Data, rsp.Fetter = this.CheckFetter(uid, heroConfId)
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.RPCGeneralReqB1, reply *pb.EmptyResp) (err error) {
this.Debug("Rpc_ModuleFetter", log.Field{Key: "args", Value: args.String()})
if args.Param1 == "" || len(args.Param2) > 0 {
err = errors.New("请求参数错误")
return
}
var (
session comm.IUserSession
ok bool
)
if session, ok = this.GetUserSession(args.Param1); ok {
this.AddHerosFetterData(session.GetUserId(), args.Param2)
session.Push()
} else {
this.Debugf("Rpc_ModuleFetter 目标用户:%s 不在线", args.Param1)
}
this.PutUserSession(session)
return
}
func (this *Library) TaskFinishNotify(uid string, taskId, fetterId int32) error {
ft := this.modelFetterstory.getFetterTasks(uid, fetterId)
if ft == nil {
return comm.NewCustomError(pb.ErrorCode_LibraryFetterTaskNoFound)
}
if !this.modelFetterstory.isPreFinished(ft.Tasks, taskId) {
return comm.NewCustomError(pb.ErrorCode_LibraryPreTaskNoFinished)
}
var isUpdate bool
if _, ok := utils.Findx(ft.Tasks, taskId); !ok {
ft.Tasks = append(ft.Tasks, taskId)
isUpdate = true
}
if isUpdate {
update := map[string]interface{}{
"feeterTask": ft.Tasks,
}
return this.modelFetterstory.Change(uid, update)
}
return nil
}
// 获取阵营的好感度
func (this *Library) QueryFavorabilityByRace(uid string, race int32) int32 {
if rst, err := this.ModuleUser.GetUserExpand(uid); err == nil { // 统计主线进度
return rst.Race[race]
}
return 0
}
// 红点
func (this *Library) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]*pb.ReddotItem) {
reddot = make(map[comm.ReddotType]*pb.ReddotItem)
for _, v := range rid {
switch v {
case comm.Reddot19105:
reddot[comm.Reddot19105] = &pb.ReddotItem{
Rid: int32(comm.Reddot19105),
Activated: this.modelLibrary.checkReddot19105(session.GetUserId()),
}
break
case comm.Reddot19103:
reddot[comm.Reddot19103] = &pb.ReddotItem{
Rid: int32(comm.Reddot19103),
Activated: this.modelFetter.checkReddot19103(session.GetUserId()),
}
break
case comm.Reddot19109:
reddot[comm.Reddot19109] = &pb.ReddotItem{
Rid: int32(comm.Reddot19109),
Activated: this.modelLibrary.checkReddot19109(session.GetUserId()),
}
break
case comm.Reddot19110:
reddot[comm.Reddot19110] = &pb.ReddotItem{
Rid: int32(comm.Reddot19110),
Activated: this.modelLibrary.checkReddot19110(session.GetUserId()),
}
break
}
}
return
}
// 创建一条羁绊信息
func (this *Library) AddHerosFetterData(uid string, heroConfIds []string) (errdata *pb.ErrorData) {
rsp := &pb.LibraryChangePush{}
for _, heroConfId := range heroConfIds {
library, fetter := this.CheckFetter(uid, heroConfId)
rsp.Data = append(rsp.Data, library...)
rsp.Fetter = append(rsp.Fetter, fetter...)
}
if len(rsp.Data) != 0 || len(rsp.Fetter) != 0 {
this.SendMsgToUser(string(this.GetType()), LibraryChangePush, rsp, uid)
}
return
}
func (this *Library) GMCreateFavorability(uid string) {
fetter := this.GetHeroFetterList(uid)
update := map[string]interface{}{}
for _, v := range fetter {
if conf, er := this.configure.GetFavorabilityExp(v.Heroid); er == nil {
// 达到最大等级不让继续升级
v.Favorlv = int32(len(conf))
v.Favorexp = 0
update["favorlv"] = v.Favorlv
update["favorexp"] = v.Favorexp
if err := this.modelFetter.ChangeList(uid, v.Id, update); err != nil {
this.Errorf("modelFetter ChangeList error: %v", err)
}
}
}
return
}