115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package maincity
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"time"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
)
|
|
|
|
type MainCity struct {
|
|
modules.ModuleBase
|
|
service comm.IService
|
|
api *apiComp
|
|
model *modelComp
|
|
friend comm.IFriend
|
|
sociaty comm.ISociaty
|
|
}
|
|
|
|
func NewModule() core.IModule {
|
|
return &MainCity{}
|
|
}
|
|
|
|
func (this *MainCity) GetType() core.M_Modules {
|
|
return comm.ModuleMaincity
|
|
}
|
|
|
|
func (this *MainCity) 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 *MainCity) Start() (err error) {
|
|
if err = this.ModuleBase.Start(); err != nil {
|
|
return
|
|
}
|
|
var module core.IModule
|
|
if module, err = this.service.GetModule(comm.ModuleFriend); err != nil {
|
|
return
|
|
}
|
|
this.friend = module.(comm.IFriend)
|
|
if module, err = this.service.GetModule(comm.ModuleSociaty); err != nil {
|
|
return
|
|
}
|
|
this.sociaty = module.(comm.ISociaty)
|
|
event.Register(comm.EventUserLogin, this.EventUserLogin)
|
|
event.Register(comm.EventUserOffline, this.EventUserOffline)
|
|
return
|
|
}
|
|
|
|
func (this *MainCity) OnInstallComp() {
|
|
this.ModuleBase.OnInstallComp()
|
|
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.model = this.RegisterComp(new(modelComp)).(*modelComp)
|
|
}
|
|
|
|
// 用户登录
|
|
func (this *MainCity) EventUserLogin(session comm.IUserSession) {
|
|
var (
|
|
uids []string
|
|
sociaty *pb.DBSociaty
|
|
)
|
|
uids = this.friend.GetFriendList(session.GetUserId())
|
|
if sociaty = this.sociaty.GetSociaty(session.GetUserId()); sociaty != nil {
|
|
for _, v := range sociaty.Members {
|
|
if v.Uid != session.GetUserId() {
|
|
uids = append(uids, v.Uid)
|
|
}
|
|
}
|
|
}
|
|
this.model.setplayerPos(session.GetUserId(), uids)
|
|
}
|
|
|
|
// 用户登录
|
|
func (this *MainCity) EventUserOffline(uid string, sessionid string) {
|
|
this.model.removelayerPos(uid)
|
|
}
|
|
|
|
func (this *MainCity) AddMainCityFriends(uid string, users []string) {
|
|
this.model.setplayerPos(uid, users)
|
|
for _, user := range users {
|
|
this.model.addplayerPos(user, uid)
|
|
}
|
|
}
|
|
|
|
// 向多个用户发送消息
|
|
func (this *MainCity) SendMsgToSession(mainType, subType string, msg proto.Message, users map[string][]string) (err error) {
|
|
data, _ := anypb.New(msg)
|
|
for k, users := range users {
|
|
uids := []string{}
|
|
for _, v := range users {
|
|
uids = append(uids, v)
|
|
}
|
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
|
|
if _, err = this.service.AcrossClusterRpcGo(ctx, k, comm.Service_Gateway, string(comm.Rpc_GatewaySendBatchMsgByUid), &pb.BatchUsersMessageReq{
|
|
Uids: uids,
|
|
MainType: mainType,
|
|
SubType: subType,
|
|
Data: data,
|
|
}, nil); err != nil {
|
|
log.Errorf("SendMsgToUsers:%s->%s.%s err:%v", k, mainType, subType, err)
|
|
}
|
|
}
|
|
return
|
|
}
|