go_dreamfactory/modules/moonfantasy/module.go
2023-06-02 11:49:44 +08:00

223 lines
5.9 KiB
Go

package moonfantasy
import (
"context"
"crypto/rand"
"errors"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"math/big"
)
const modelName = "月之秘境"
/*
模块名:月子秘境
描述:随机世界副本
开发:李伟
*/
func NewModule() core.IModule {
m := new(Moonfantasy)
return m
}
type Moonfantasy struct {
modules.ModuleBase
service base.IRPCXService
chat comm.IChat
friend comm.IFriend
battle comm.IBattle
privilege comm.IPrivilege
api_comp *apiComp
configure *configureComp
modelDream *modelDreamComp
modelUserMF *modelUserMF
}
//模块名
func (this *Moonfantasy) GetType() core.M_Modules {
return comm.ModuleMoonfantasy
}
//模块初始化接口 注册用户创建角色事件
func (this *Moonfantasy) 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 *Moonfantasy) Start() (err error) {
err = this.ModuleBase.Start()
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleChat); err != nil {
return
}
this.chat = module.(comm.IChat)
if module, err = this.service.GetModule(comm.ModuleFriend); err != nil {
return
}
this.friend = module.(comm.IFriend)
if module, err = this.service.GetModule(comm.ModuleBattle); err != nil {
return
}
this.battle = module.(comm.IBattle)
if module, err = this.service.GetModule(comm.ModulePrivilege); err != nil {
return
}
this.privilege = module.(comm.IPrivilege)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleMoonfantasyTrigger), this.Rpc_ModuleMoonfantasyTrigger)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleMoonfantasyTriggerMF), this.Rpc_ModuleMoonfantasyTriggerMF)
event.RegisterGO(comm.EventOpenCond, this.EventOpenCond)
return
}
//装备组件
func (this *Moonfantasy) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelDream = this.RegisterComp(new(modelDreamComp)).(*modelDreamComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
this.modelUserMF = this.RegisterComp(new(modelUserMF)).(*modelUserMF)
}
//触发月之秘境
func (this *Moonfantasy) Trigger(session comm.IUserSession, source *pb.BattleReport) {
var (
triggerData *cfg.GameDreamlandTriggerData
err error
)
if source == nil || source.Info == nil {
this.Error("Trigger", log.Field{Key: "err", Value: "source is nil"})
return
}
this.Debug("Trigger", log.Field{Key: "session", Value: session.ToString()}, log.Field{Key: "ptype", Value: source.Info.Ptype})
if triggerData, err = this.configure.GettriggerData(int32(source.Info.Ptype)); err == nil && triggerData.Open {
n, _ := rand.Int(rand.Reader, big.NewInt(1000))
if int32(n.Int64()) < triggerData.DreamlandPro {
if this.IsCross() {
go func(uid string) {
if ss, ok := this.GetUserSession(uid); ok {
this.modelDream.trigger(ss)
if err = ss.Push(); err != nil {
this.Errorln(err)
}
this.PutUserSession(ss)
} else {
this.PutUserSession(ss)
}
}(session.GetUserId())
} else {
if _, err = this.service.AcrossClusterRpcGo(context.Background(),
this.GetCrossTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleMoonfantasyTrigger),
pb.RPCGeneralReqA1{Param1: session.GetUserId()},
nil,
); err != nil {
this.Errorln(err)
}
}
}
}
}
//接收区服worker发起的秘境事件
func (this *Moonfantasy) Rpc_ModuleMoonfantasyTrigger(ctx context.Context, args *pb.RPCGeneralReqA1, reply *pb.EmptyResp) (err error) {
this.Debug("Rpc_ModuleMoonfantasyTrigger", log.Field{Key: "args", Value: args.String()})
if args.Param1 == "" {
err = errors.New("参数异常!")
return
}
var (
session comm.IUserSession
ok bool
)
defer func() {
this.PutUserSession(session)
}()
if session, ok = this.GetUserSession(args.Param1); !ok {
err = fmt.Errorf("未查询到用户:%s在线信息!", args.Param1)
return
} else {
this.modelDream.trigger(session)
session.Push()
}
return
}
//触发月之秘境
func (this *Moonfantasy) Rpc_ModuleMoonfantasyTriggerMF(ctx context.Context, args *pb.RPCTargetMFReq, reply *pb.EmptyResp) (err error) {
var (
session comm.IUserSession
ok bool
)
defer func() {
this.PutUserSession(session)
}()
if session, ok = this.GetUserSession(args.Uid); !ok {
err = fmt.Errorf("no found user%s", args.Uid)
this.Errorln(err)
return
}
if err = this.modelDream.triggerbyid(session, args.Boosid); err != nil {
this.Errorln(err)
}
return
}
//触发月之秘境
func (this *Moonfantasy) TriggerMF(session comm.IUserSession, Boosid string) (err error) {
if this.IsCross() {
err = this.modelDream.triggerbyid(session, Boosid)
} else {
if _, err = this.service.AcrossClusterRpcGo(context.Background(),
this.GetCrossTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleMoonfantasyTriggerMF),
pb.RPCTargetMFReq{Uid: session.GetUserId(), Boosid: Boosid},
nil,
); err != nil {
this.Errorln(err)
}
}
return
}
//功能开启事件触发
func (this *Moonfantasy) EventOpenCond(uid string, funcIds []string) {
for _, v := range funcIds {
if v == "Mystery" { //月子秘境
if this.IsCross() {
if ss, ok := this.GetUserSession(uid); ok {
this.modelDream.trigger(ss)
if err := ss.Push(); err != nil {
this.Errorln(err)
}
this.PutUserSession(ss)
} else {
this.PutUserSession(ss)
}
} else {
if _, err := this.service.AcrossClusterRpcGo(context.Background(),
this.GetCrossTag(),
comm.Service_Worker,
string(comm.Rpc_ModuleMoonfantasyTrigger),
pb.RPCGeneralReqA1{Param1: uid},
nil,
); err != nil {
this.Errorln(err)
}
}
break
}
}
}