From 5270f706d34a32fe7f87a9436950c09862faae69 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Wed, 14 Dec 2022 19:24:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=BD=E5=8F=8B=E5=AE=A1=E6=A0=B8=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/const.go | 2 +- modules/friend/api_cross_agree.go | 24 ++++++++++------- modules/friend/api_cross_apply.go | 44 ++++++++++++++++--------------- modules/friend/model_friend.go | 30 ++++++++++++++++----- modules/friend/module.go | 20 ++++++++++++++ 5 files changed, 81 insertions(+), 39 deletions(-) diff --git a/comm/const.go b/comm/const.go index a16c17b18..6a3fc3acc 100644 --- a/comm/const.go +++ b/comm/const.go @@ -228,7 +228,7 @@ const ( //Rpc Rpc_ModuleRtaskSendTask core.Rpc_Key = "Rpc_ModuleRtaskSendTask" //随机任务触发 // friend Rpc_ModuleFriendUseAssitHero core.Rpc_Key = "Rpc_ModuleFriendUseAssitHero" //使用助战英雄 - + Rpc_ModuleFriendDB core.Rpc_Key = "Rpc_ModuleFriendDB" // arena Rpc_ModuleArenaRaceSettlement core.Rpc_Key = "Rpc_ModuleArenaRaceSettlement" //竞技场比赛结算信息 Rpc_ModuleArenaModifyIntegral core.Rpc_Key = "Rpc_ModuleArenaModifyIntegral" //竞技场修改积分 diff --git a/modules/friend/api_cross_agree.go b/modules/friend/api_cross_agree.go index 8f7346271..180336ee2 100644 --- a/modules/friend/api_cross_agree.go +++ b/modules/friend/api_cross_agree.go @@ -69,6 +69,9 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c //将目标加入到自己的好友列表中 for _, userId := range agreeIds { + //将目标从申请列表中删除 + self.ApplyIds = utils.DeleteString(self.ApplyIds, userId) + if _, ok := utils.Find(self.FriendIds, userId); !ok { if self.FriendIds == nil { self.FriendIds = []string{} @@ -82,6 +85,7 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c code = pb.ErrorCode_FriendTargetNoData return } + target.ApplyIds = utils.DeleteString(target.ApplyIds, self.Uid) if _, ok := utils.Find(target.FriendIds, self.Uid); !ok { if target.FriendIds == nil { target.FriendIds = []string{} @@ -106,19 +110,19 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c pushAssistHero(self.Uid, target.Uid, self.AssistHeroId) } - //将目标从申请列表中删除 - self.ApplyIds = utils.DeleteString(self.ApplyIds, userId) optNum++ } - //更新 - err = this.moduleFriend.modelFriend.Change(self.Uid, map[string]interface{}{ - "applyIds": self.ApplyIds, - "friendIds": self.FriendIds, - }) - if err != nil { - code = pb.ErrorCode_DBError - return + if optNum > 0 { + //更新 + err = this.moduleFriend.modelFriend.Change(self.Uid, map[string]interface{}{ + "applyIds": self.ApplyIds, + "friendIds": self.FriendIds, + }) + if err != nil { + code = pb.ErrorCode_DBError + return + } } // 拥有xx个好友 diff --git a/modules/friend/api_cross_apply.go b/modules/friend/api_cross_apply.go index f78905e19..94bb267d0 100644 --- a/modules/friend/api_cross_apply.go +++ b/modules/friend/api_cross_apply.go @@ -31,6 +31,13 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c ) uid := session.GetUserId() + + //判断是否是自己 + if req.FriendId == uid { + code = pb.ErrorCode_FriendNotSelf + return + } + //获取玩家自己好友数据 self = this.moduleFriend.modelFriend.GetFriend(uid) if self == nil { @@ -38,6 +45,19 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c return } + globalConf := this.moduleFriend.globalConf + //判断是否超过最大好友数量 + if len(self.FriendIds) >= int(globalConf.FriendMaxnum) { + code = pb.ErrorCode_FriendSelfMax + return + } + + //判断是否是好友 + if _, ok := utils.Find(self.FriendIds, req.FriendId); ok { + code = pb.ErrorCode_FriendYet + return + } + //获取好友数据 target = this.moduleFriend.modelFriend.GetFriend(req.FriendId) if target == nil { @@ -45,15 +65,9 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c return } - //判断是否是自己 - if req.FriendId == uid { - code = pb.ErrorCode_FriendNotSelf - return - } - globalConf := this.moduleFriend.globalConf - //判断是否超过最大好友数量 - if len(self.FriendIds) >= int(globalConf.FriendMaxnum) { - code = pb.ErrorCode_FriendSelfMax + //判断自己是否在目标用户的申请列表中 + if _, ok := utils.Find(target.ApplyIds, self.Uid); ok { + code = pb.ErrorCode_FriendApplyYet return } @@ -63,18 +77,6 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c return } - //判断是否是好友 - if _, ok := utils.Find(self.FriendIds, req.FriendId); ok { - code = pb.ErrorCode_FriendYet - return - } - - //判断自己是否在目标用户的申请列表中 - if _, ok := utils.Find(target.ApplyIds, self.Uid); ok { - code = pb.ErrorCode_FriendApplyYet - return - } - //判断目标用户是否在黑名单中 if _, ok := utils.Find(self.BlackIds, req.FriendId); ok { code = pb.ErrorCode_FriendSelfBlackYet diff --git a/modules/friend/model_friend.go b/modules/friend/model_friend.go index 157e5b3b1..df80b30a2 100644 --- a/modules/friend/model_friend.go +++ b/modules/friend/model_friend.go @@ -1,6 +1,7 @@ package friend import ( + "context" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" @@ -14,12 +15,13 @@ import ( type ModelFriend struct { modules.MCompModel + moduleFriend *Friend } func (this *ModelFriend) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = comm.TableFriend err = this.MCompModel.Init(service, module, comp, options) - + this.moduleFriend = module.(*Friend) return } @@ -38,14 +40,28 @@ func (this *ModelFriend) Frined_FindCond(nickName string) *pb.DBUser { //查询好友 func (this *ModelFriend) GetFriend(uid string) *pb.DBFriend { friend := &pb.DBFriend{Uid: uid} - err := this.Get(uid, friend) - if err != nil { - if err == redis.RedisNil || err == mongo.ErrNoDocuments { - if err := this.Add(uid, friend); err != nil { - return nil + if this.moduleFriend.IsCross() { + err := this.Get(uid, friend) + if err != nil { + if err == redis.RedisNil || err == mongo.ErrNoDocuments { + if err := this.Add(uid, friend); err != nil { + return nil + } } + log.Error("未获得好友数据", log.Fields{"uid": uid}) + } + } else { + friend := &pb.DBFriend{} + if err := this.moduleFriend.service.AcrossClusterRpcCall( + context.Background(), + this.moduleFriend.GetCrossTag(), + comm.Service_Worker, + string(comm.Rpc_ModuleFriendDB), + pb.RPCGeneralReqA1{Param1: uid}, + friend); err != nil { + this.moduleFriend.Errorln(err) } - log.Error("未获得好友数据", log.Fields{"uid": uid}) } + return friend } diff --git a/modules/friend/module.go b/modules/friend/module.go index 204dc0009..c5323cdc2 100644 --- a/modules/friend/module.go +++ b/modules/friend/module.go @@ -15,10 +15,12 @@ import ( "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/sys/redis" cfg "go_dreamfactory/sys/configure/structs" "github.com/spf13/cast" + "go.mongodb.org/mongo-driver/mongo" ) var _ comm.IFriend = (*Friend)(nil) @@ -56,6 +58,7 @@ func (this *Friend) OnInstallComp() { func (this *Friend) Start() (err error) { err = this.ModuleBase.Start() this.service.RegisterFunctionName(string(comm.Rpc_ModuleFriendUseAssitHero), this.RpcUseAssisHero) + this.service.RegisterFunctionName(string(comm.Rpc_ModuleFriendDB), this.RpcFriendDB) this.globalConf = this.configure.GetGlobalConf() if this.globalConf == nil { err = errors.New("global config not found") @@ -109,6 +112,23 @@ func (this *Friend) RpcUseAssisHero(ctx context.Context, req *pb.RPCGeneralReqA2 return nil } +func (this *Friend) RpcFriendDB(ctx context.Context, req *pb.RPCGeneralReqA1, reply *pb.DBFriend) error { + this.Debug("Rpc_ModuleFriendDB", log.Fields{"req": req}) + friend := &pb.DBFriend{Uid: req.Param1} + err := this.modelFriend.Get(req.Param1, friend) + if err != nil { + if err == redis.RedisNil || err == mongo.ErrNoDocuments { + if err := this.modelFriend.Add(req.Param1, friend); err != nil { + return nil + } + } else { + log.Error("未获得好友数据", log.Fields{"uid": req.Param1, "err": err}) + } + } + *reply = *friend + return nil +} + // 使用好友助战英雄 // friendId 好友Id func (this *Friend) UseAssistHero(uid, friendId string) (*pb.DBHero, error) {