package modules import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "strconv" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" ) /* 基础业务模块实现 封装一些通用的接口提供给业务模块使用 */ type ModuleBase struct { cbase.ModuleBase module core.IModule service base.IRPCXService } //模块初始化接口 func (this *ModuleBase) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) this.module = module return } //向指定用户发送消息 func (this *ModuleBase) SendMsgToUser(mainType, subType string, msg proto.Message, user *pb.Cache_UserData) (err error) { reply := &pb.RPCMessageReply{} data, _ := ptypes.MarshalAny(msg) if _, err = this.service.RpcGo(context.Background(), fmt.Sprintf("%s/%s", comm.Service_Gateway, user.GatewayServiceId), string(comm.Rpc_GatewayAgentSendMsg), &pb.AgentSendMessageReq{ UserSessionId: user.SessionId, MainType: mainType, SubType: subType, Data: data, }, reply); err != nil { log.Errorf("SendMsgToUser%d:%s [%s.%s] err:%v", user.Uid, user.SessionId, mainType, subType, err) } return } //向多个用户发送消息 func (this *ModuleBase) SendMsgToUsers(mainType, subType string, msg proto.Message, user ...*pb.Cache_UserData) (err error) { var ( gateways map[string][]string = make(map[string][]string) gateway []string ok bool ) for _, v := range user { if gateway, ok = gateways[v.GatewayServiceId]; !ok { gateway = make([]string, 0) gateways[v.GatewayServiceId] = gateway } gateway = append(gateway, v.SessionId) } reply := &pb.RPCMessageReply{} data, _ := ptypes.MarshalAny(msg) for k, v := range gateways { if _, err = this.service.RpcGo(context.Background(), fmt.Sprintf("%s/%s", comm.Service_Gateway, k), string(comm.Rpc_GatewayAgentSendMsg), &pb.BatchMessageReq{ UserSessionIds: v, MainType: mainType, SubType: subType, Data: data, }, reply); err != nil { log.Errorf("SendMsgToUsers:%s->%s.%s err:%v", k, mainType, subType, err) } } return } //校验消耗资源 func (this *ModuleBase) CheckConsumeRes(uid string, res []*cfg.Game_atn) (code pb.ErrorCode) { var ( module core.IModule err error resID int amount int32 user comm.IUser //用户模块 items comm.IItems //道具背包模块 hero comm.IHero //英雄模块 equipment comm.IEquipment //装备模块 ) if module, err = this.service.GetModule(comm.SM_UserModule); err == nil { return } user = module.(comm.IUser) if module, err = this.service.GetModule(comm.SM_UserModule); err == nil { return } items = module.(comm.IItems) if module, err = this.service.GetModule(comm.SM_UserModule); err == nil { return } hero = module.(comm.IHero) if module, err = this.service.GetModule(comm.SM_UserModule); err == nil { return } source := &comm.ModuleCallSource{ Module: string(this.module.GetType()), FuncName: "CheckConsumeRes", Describe: "消耗资源", } equipment = module.(comm.IEquipment) //校验消费资源是否充足 for _, v := range res { if v.A == comm.AttrType { //用户属性资源 if amount = user.QueryAttributeValue(uid, v.T); amount < v.N { code = pb.ErrorCode_ResNoEnough return } } else if v.A == comm.ItemType { //道具资源 if resID, err = strconv.Atoi(v.T); err != nil { code = pb.ErrorCode_ConfigurationException return } if amount = int32(items.QueryItemAmount(source, uid, int32(resID))); amount < v.N { code = pb.ErrorCode_ResNoEnough return } } else if v.A == comm.CardType { //卡片资源 if resID, err = strconv.Atoi(v.T); err != nil { code = pb.ErrorCode_ConfigurationException return } if amount = int32(hero.QueryHeroAmount(uid, int32(resID))); amount < v.N { code = pb.ErrorCode_ResNoEnough return } } else if v.A == comm.EquipmentType { if resID, err = strconv.Atoi(v.T); err != nil { code = pb.ErrorCode_ConfigurationException return } if amount = int32(equipment.QueryEquipmentAmount(source, uid, int32(resID))); amount < v.N { code = pb.ErrorCode_ResNoEnough return } } } for _, v := range res { if v.A == comm.AttrType { //用户属性资源 user.AddAttributeValue(uid, v.T, -1*v.N) } else if v.A == comm.ItemType { //道具资源 resID, _ = strconv.Atoi(v.T) items.AddItem(source, uid, int32(resID), -1*v.N) } else if v.A == comm.CardType { //卡片资源 resID, _ = strconv.Atoi(v.T) hero.ChangeCard(uid, int32(resID), -1*v.N) } else if v.A == comm.EquipmentType { resID, _ = strconv.Atoi(v.T) equipment.AddNewEquipments(source, uid, resID, -1*v.N) } } return }