go_dreamfactory/modules/library/module.go
2023-04-20 18:32:04 +08:00

340 lines
8.7 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) {
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.modelFetterstory = this.RegisterComp(new(modelFetterstory)).(*modelFetterstory)
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) 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) {
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 _d := this.CheckHeroFetter(uid, hid); _d != nil { // check DBHeroFetter
tmp.Herofetter[hid] = _d.Id
fetter = append(fetter, _d)
}
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 _d := this.CheckHeroFetter(uid, hid); _d == nil {
list.Herofetter[hid] = _d.Id
fetter = append(fetter, _d)
}
mapData := make(map[string]interface{}, 0)
mapData["herofetter"] = list.Herofetter
this.modelLibrary.modifyLibraryDataByObjId(uid, list.Id, mapData) // 更新新的羁绊信息
dbLibrary = append(dbLibrary, list)
}
}
}
return
}
//通过羁绊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,
Herofetter: map[string]string{},
Prize: map[int32]int32{},
}
conf := this.configure.GetFriendData(fid, 1)
if conf != nil {
if err := this.modelLibrary.createLibrary(uid, obj); err != nil {
code = pb.ErrorCode_DBError
}
obj.Herofetter[heroConfId] = obj.Id
}
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) 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 {
obj = 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,
Stroyprize: make([]int32, 0),
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.RPCGeneralReqA2{Param1: uid, Param2: heroConfId},
nil); err != nil {
this.Errorln(err)
}
return
}
return
}
// 创建一条羁绊信息
func (this *Library) AddHeroFetterData(uid, heroConfId string) (code pb.ErrorCode) {
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.RPCGeneralReqA2, reply *pb.EmptyResp) (err error) {
this.Debug("Rpc_ModuleFetter", log.Field{Key: "args", Value: args.String()})
if args.Param1 == "" || args.Param2 == "" {
err = errors.New("请求参数错误")
return
}
var (
session comm.IUserSession
ok bool
)
if session, ok = this.GetUserSession(args.Param1); ok {
this.AddHeroFetterData(session.GetUserId(), args.Param2)
session.Push()
} else {
this.Debugf("Rpc_ModuleFetter 目标用户:%s 不在线", args.Param1)
}
this.PutUserSession(session)
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
}
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
}