package entertainment import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/event" "go_dreamfactory/modules" "go_dreamfactory/pb" ) func NewModule() core.IModule { m := new(Entertainment) return m } type Entertainment struct { modules.ModuleBase service comm.IService api *apiComp configure *configureComp model *modelComp gameMgr *gameMgrComp //room *Room match *matchComp } // 模块名 func (this *Entertainment) GetType() core.M_Modules { return comm.ModuleEntertainment } // 模块初始化接口 注册用户创建角色事件 func (this *Entertainment) 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.(comm.IService) return } // 装备组件 func (this *Entertainment) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.model = this.RegisterComp(new(modelComp)).(*modelComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.gameMgr = this.RegisterComp(new(gameMgrComp)).(*gameMgrComp) //this.room = this.RegisterComp(new(Room)).(*Room) this.match = this.RegisterComp(new(matchComp)).(*matchComp) } func (this *Entertainment) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } event.RegisterGO(comm.EventUserOffline, this.EventUserOffline) this.service.RegisterFunctionName(string(comm.RPC_XXLOffLine), this.useroffline) return } // 分发资源 func (this *Entertainment) AddXxlCard(session comm.IUserSession, cards map[string]int32, bPush bool) (errdata *pb.ErrorData) { var ( result *pb.DBXXLData err error ) for k := range cards { if _, err := this.model.module.configure.GetGameConsumeHero(k); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), Message: err.Error(), } return } } if result, err = this.model.getEntertainmList(session.GetUserId()); err != nil { return } for k, v := range cards { result.Card[k] += v } this.model.modifyEntertainmList(session.GetUserId(), map[string]interface{}{ "card": result.Card, }) if bPush { session.SendMsg(string(this.GetType()), "change", &pb.EntertainChangePush{ Card: result.Card, }) } return } // 消耗一张卡 func (this *Entertainment) ConsumXxlCard(session comm.IUserSession, card string) (errdata *pb.ErrorData) { var ( result *pb.DBXXLData err error ) if result, err = this.model.getEntertainmList(session.GetUserId()); err != nil { return } if _, ok := result.Card[card]; ok { if result.Card[card] <= 0 { return } result.Card[card] -= 1 this.model.modifyEntertainmList(session.GetUserId(), map[string]interface{}{ "card": result.Card, }) } session.SendMsg(string(this.GetType()), "titlelist", &pb.EntertainChangePush{ Card: result.Card, }) return } // 用户离线处理 func (this *Entertainment) EventUserOffline(uid, sessionid string) { this.Debugf("user offline: %s", uid) if list, err := this.model.getEntertainmList(uid); err == nil { if list.Roomid == "" { // 不在房间不处理 return } serverPath := fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()) if serverPath != list.ServicePath { // RPC 通知房间所在的服务器 _, err = this.service.RpcGo( context.Background(), list.ServicePath, string(comm.RPC_XXLOffLine), &pb.RPCGeneralReqA2{ Param1: list.Roomid, Param2: list.Uid, }, nil) if err != nil { this.Errorln(err) return } } else { this.useroffline(context.Background(), &pb.RPCGeneralReqA2{Param1: list.Roomid, Param2: list.Uid}, nil) } return } } func (this *Entertainment) useroffline(ctx context.Context, req *pb.RPCGeneralReqA2, resp *pb.RPCGeneralReqA2) (err error) { this.Debugf("user offline :%s,%s", req.Param1, req.Param2) // 房间不在直接返回 if _, err = this.gameMgr.GetRoomInfo(req.Param1); err != nil { return } this.gameMgr.RoomDistribute(req.Param1, nil, "offline", req) return }