go_dreamfactory/modules/modulebase.go
2022-07-01 12:11:25 +08:00

205 lines
6.1 KiB
Go

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
//常用的一些通用模块 在底层注册好
ModuleUser comm.IUser //用户模块
ModuleItems comm.IItems //道具背包模块
ModuleHero comm.IHero //英雄模块
ModuleEquipment comm.IEquipment //装备模块
}
//模块初始化接口
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) Start() (err error) {
err = this.ModuleBase.Start()
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleUser); err != nil {
return
}
this.ModuleUser = module.(comm.IUser)
if module, err = this.service.GetModule(comm.ModuleItems); err != nil {
return
}
this.ModuleItems = module.(comm.IItems)
if module, err = this.service.GetModule(comm.ModuleHero); err != nil {
return
}
this.ModuleHero = module.(comm.IHero)
if module, err = this.service.GetModule(comm.ModuleEquipment); err == nil {
return
}
this.ModuleEquipment = module.(comm.IEquipment)
return
}
//向指定用户发送消息
func (this *ModuleBase) SendMsgToUser(mainType, subType string, msg proto.Message, user *pb.CacheUser) (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.CacheUser) (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 (
err error
resID int
amount int32
)
source := &comm.ModuleCallSource{
Module: string(this.module.GetType()),
FuncName: "CheckConsumeRes",
Describe: "消耗资源",
}
//校验消费资源是否充足
for _, v := range res {
if v.A == comm.AttrType { //用户属性资源
if amount = this.ModuleUser.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(this.ModuleItems.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(this.ModuleHero.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 { //用户属性资源
this.ModuleUser.AddAttributeValue(uid, v.T, -1*v.N)
} else if v.A == comm.ItemType { //道具资源
resID, _ = strconv.Atoi(v.T)
this.ModuleItems.AddItem(source, uid, int32(resID), -1*v.N)
} else if v.A == comm.CardType { //卡片资源
resID, _ = strconv.Atoi(v.T)
this.ModuleHero.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
}
//发放资源
func (this *ModuleBase) DispenseRes(uid string, res []*cfg.Game_atn) (code pb.ErrorCode) {
var (
resID int
)
source := &comm.ModuleCallSource{
Module: string(this.module.GetType()),
FuncName: "DispenseRes",
Describe: "发放资源",
}
for _, v := range res {
if v.A == comm.AttrType { //用户属性资源
this.ModuleUser.AddAttributeValue(uid, v.T, v.N)
} else if v.A == comm.ItemType { //道具资源
resID, _ = strconv.Atoi(v.T)
this.ModuleItems.AddItem(source, uid, int32(resID), v.N)
} else if v.A == comm.CardType { //卡片资源
resID, _ = strconv.Atoi(v.T)
this.ModuleHero.ChangeCard(uid, int32(resID), v.N)
} else if v.A == comm.EquipmentType {
resID, _ = strconv.Atoi(v.T)
this.ModuleEquipment.AddNewEquipments(source, uid, map[int32]uint32{int32(resID): uint32(v.N)})
}
}
return
}