172 lines
4.6 KiB
Go
172 lines
4.6 KiB
Go
package friend
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/utils"
|
|
)
|
|
|
|
//单个/批量同意
|
|
|
|
func (this *apiComp) AgreeCheck(session comm.IUserSession, req *pb.FriendAgreeReq) (errdata *pb.ErrorData) {
|
|
if len(req.FriendIds) == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
this.module.Error("好友审批同意参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (errdata *pb.ErrorData) {
|
|
if errdata = this.AgreeCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
var (
|
|
err error
|
|
self *pb.DBFriend
|
|
target *pb.DBFriend
|
|
optNum int32
|
|
)
|
|
uid := session.GetUserId()
|
|
//获取玩家自己好友数据
|
|
self = this.module.modelFriend.GetFriend(uid)
|
|
if self == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_FriendSelfNoData,
|
|
Title: pb.ErrorCode_FriendSelfNoData.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
//同意的好友
|
|
agreeIds := []string{}
|
|
for _, friendId := range req.FriendIds {
|
|
// 验证friendId是否有效
|
|
user := this.module.ModuleUser.GetUser(friendId)
|
|
if user == nil {
|
|
continue
|
|
}
|
|
if _, ok := utils.Find(self.FriendIds, friendId); !ok {
|
|
//不在好友列表中就加入
|
|
agreeIds = append(agreeIds, friendId)
|
|
}
|
|
}
|
|
|
|
pushAssistHero := func(uid, friendId, heroObjId string) {
|
|
//把设置的助战英雄推送给好友
|
|
push := &pb.FriendAssistHeroUpdatePush{
|
|
Friend: &pb.FriendBase{
|
|
UserId: uid,
|
|
HeroObjId: heroObjId,
|
|
},
|
|
}
|
|
|
|
this.module.Debug("设置的助战英雄推送给好友",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "heroObjId", Value: heroObjId},
|
|
)
|
|
if err := this.module.SendMsgToUsers(string(this.module.GetType()), "assistheroupdate", push, friendId); err != nil {
|
|
this.module.Error("推送助战英雄列表",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "err", Value: err.Error()},
|
|
)
|
|
}
|
|
}
|
|
|
|
//将目标加入到自己的好友列表中
|
|
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{}
|
|
}
|
|
self.FriendIds = append(self.FriendIds, userId)
|
|
}
|
|
|
|
//双向添加:将自己加入到申请人的好友列表中
|
|
target = this.module.modelFriend.GetFriend(userId)
|
|
if target == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_FriendTargetNoData,
|
|
Title: pb.ErrorCode_FriendTargetNoData.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
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{}
|
|
}
|
|
target.FriendIds = append(target.FriendIds, self.Uid)
|
|
}
|
|
if err = this.module.modelFriend.Change(target.Uid, map[string]interface{}{
|
|
"friendIds": target.FriendIds,
|
|
}); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
this.module.Error("好友审批同意",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "params", Value: req.FriendIds},
|
|
log.Field{Key: "err", Value: err.Error()},
|
|
)
|
|
return
|
|
}
|
|
|
|
//将目标的助战英雄推给自己
|
|
if target.AssistHeroId != "" {
|
|
pushAssistHero(target.Uid, self.Uid, target.AssistHeroId)
|
|
}
|
|
|
|
// 将自己的助战英雄推个新加入的好友
|
|
if self.AssistHeroId != "" {
|
|
pushAssistHero(self.Uid, target.Uid, self.AssistHeroId)
|
|
}
|
|
|
|
optNum++
|
|
}
|
|
|
|
if optNum > 0 {
|
|
//更新
|
|
err = this.module.modelFriend.Change(self.Uid, map[string]interface{}{
|
|
"applyIds": self.ApplyIds,
|
|
"friendIds": self.FriendIds,
|
|
})
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
event.TriggerEvent(comm.EventFriendChange, uid, len(self.FriendIds))
|
|
}
|
|
|
|
// 拥有xx个好友
|
|
// this.moduleFriend.ModuleRtask.SendToRtask(session, comm.Rtype10, int32(len(agreeIds)))
|
|
var sz []*pb.BuriedParam
|
|
for _, v := range req.FriendIds {
|
|
sz = append(sz, comm.GetBuriedParam2(comm.Rtype10, v))
|
|
}
|
|
|
|
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), sz...)
|
|
resp := &pb.FriendAgreeResp{
|
|
Num: optNum,
|
|
}
|
|
session.SendMsg(string(this.module.GetType()), FriendSubTypeAgree, resp)
|
|
return
|
|
}
|