go_dreamfactory/modules/friend/api_randlist.go
2022-09-15 16:20:29 +08:00

95 lines
2.1 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"google.golang.org/protobuf/proto"
)
func (this *apiComp) RandlistCheck(session comm.IUserSession, req *pb.FriendRandlistReq) (code pb.ErrorCode) {
return
}
func (this *apiComp) Randlist(session comm.IUserSession, req *pb.FriendRandlistReq) (code pb.ErrorCode, data proto.Message) {
var (
self *pb.DBFriend
)
//在线玩家
cuList, err := this.moduleFriend.ModuleUser.UserOnlineList()
if err != nil {
code = pb.ErrorCode_DBError
return
}
// 只随机选择5个在线玩家
var randOnlineUsers []string
if len(cuList) > 5 {
randArr := utils.Numbers(0, len(cuList), 5)
for _, v := range randArr {
if cuList[v] != nil {
randOnlineUsers = append(randOnlineUsers, cuList[v].Uid)
}
}
} else {
for _, v := range cuList {
randOnlineUsers = append(randOnlineUsers, v.Uid)
}
}
// 当前玩家好友数据
self = this.moduleFriend.modelFriend.GetFriend(session.GetUserId())
if self == nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
var userList []*pb.FriendBase
for _, uid := range randOnlineUsers {
// 将自己从在线玩家中过滤掉
if uid == session.GetUserId() {
continue
}
// 将自己的好友从在线玩家中过滤掉
if _, ok := utils.Findx(self.FriendIds, uid); ok {
continue
}
// 判断当前在线好友是否在自己的申请列表中,如果存在也过滤掉
target := this.moduleFriend.modelFriend.GetFriend(uid)
if target == nil {
continue
}
// 获取在线好友的信息
user := this.moduleFriend.ModuleUser.GetUser(uid)
if user == nil {
continue
}
base := this.setDefaultFriendUserBaseInfo(uid)
if base == nil {
continue
}
// 申请过的在线好友,设置申请状态
if _, ok := utils.Findx(target.ApplyIds, self.Uid); ok {
base.IsApplied = true
}
userList = append(userList, base)
}
rsp := &pb.FriendRandlistResp{
List: userList,
}
if err := session.SendMsg(string(this.moduleFriend.GetType()), FriendSubTypeRandList, rsp); err != nil {
code = pb.ErrorCode_SystemError
return
}
return
}