130 lines
3.8 KiB
Go
130 lines
3.8 KiB
Go
package friend
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
//好友列表
|
|
|
|
func (this *apiComp) GetAssistHeroCheck(session comm.IUserSession, req *pb.FriendGetAssistHeroReq) (errdata *pb.ErrorData) {
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) GetAssistHero(session comm.IUserSession, req *pb.FriendGetAssistHeroReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
list *pb.DBAssistHero
|
|
uids []string
|
|
friends []*pb.DBFriend
|
|
heros []*pb.DBHero
|
|
myFriend *pb.DBFriend // 我的好友信息
|
|
leftCount int32
|
|
)
|
|
|
|
list, err = this.module.modelAssist.getAssist(session.GetUserId())
|
|
if err == mgo.MongodbNil {
|
|
list = &pb.DBAssistHero{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: session.GetUserId(),
|
|
Data: map[string]string{},
|
|
RefreshNum: 0,
|
|
}
|
|
if myFriend, err = this.module.modelFriend.GetFriend(session.GetUserId()); err == nil {
|
|
if friends, err = this.module.modelFriend.GetFriends(myFriend.FriendIds); err != nil {
|
|
for _, v := range friends {
|
|
if v.AssistHeroId != "" {
|
|
list.Data[v.Uid] = v.Info.Name
|
|
heros = append(heros, v.Hero)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
leftCount = int32(comm.AssistHeroCount - len(list.Data))
|
|
//localNum, _ := this.module.modelFriend.DB.CountDocuments(core.SqlTable(this.module.modelFriend.TableName), bson.M{})
|
|
//randomIndex := comm.GetRandNum(0, int32(localNum))
|
|
cur, err := this.module.modelFriend.DB.Find(core.SqlTable(this.module.modelFriend.TableName), bson.M{"assistHeroId": bson.M{"$ne": ""}}, options.Find().SetSkip(int64(0)).SetLimit(int64(leftCount))) //.skip(1).limit(1)
|
|
for cur.Next(context.TODO()) {
|
|
tmp := &pb.DBFriend{}
|
|
if err = cur.Decode(tmp); err == nil {
|
|
if session.GetUserId() != tmp.Uid && tmp.Info != nil {
|
|
list.Data[tmp.Uid] = tmp.Info.Name
|
|
heros = append(heros, tmp.Hero)
|
|
}
|
|
}
|
|
}
|
|
err = this.module.modelAssist.Add(session.GetUserId(), list)
|
|
|
|
session.SendMsg(string(this.module.GetType()), "getassisthero", &pb.FriendGetAssistHeroResp{
|
|
Data: list,
|
|
Hero: heros,
|
|
})
|
|
return
|
|
}
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if len(list.Data) == 0 { // 异常处理
|
|
leftCount = int32(comm.AssistHeroCount - len(list.Data))
|
|
// localNum, _ := this.module.modelFriend.DB.CountDocuments(core.SqlTable(this.module.modelFriend.TableName), bson.M{})
|
|
// randomIndex := comm.GetRandNum(0, int32(localNum))
|
|
cur, err := this.module.modelFriend.DB.Find(core.SqlTable(this.module.modelFriend.TableName), bson.M{"assistHeroId": bson.M{"$ne": ""}}, options.Find().SetSkip(int64(0)).SetLimit(int64(leftCount))) //.skip(1).limit(1)
|
|
for cur.Next(context.TODO()) {
|
|
tmp := &pb.DBFriend{}
|
|
if err = cur.Decode(tmp); err == nil {
|
|
if session.GetUserId() != tmp.Uid && tmp.Info != nil {
|
|
if tmp.Info != nil {
|
|
list.Data[tmp.Uid] = tmp.Info.Name
|
|
heros = append(heros, tmp.Hero)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
err = this.module.modelAssist.modifyAssistData(session.GetUserId(), map[string]interface{}{
|
|
"data": list.Data,
|
|
})
|
|
} else {
|
|
for k := range list.Data {
|
|
if session.GetUserId() != k {
|
|
uids = append(uids, k)
|
|
}
|
|
}
|
|
// 批量查
|
|
friends, err = this.module.modelFriend.GetFriends(uids)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_FriendSelfNoData,
|
|
Title: pb.ErrorCode_FriendSelfNoData.ToString(),
|
|
}
|
|
return
|
|
}
|
|
for _, v := range friends {
|
|
if v.Hero != nil {
|
|
heros = append(heros, v.Hero)
|
|
}
|
|
}
|
|
}
|
|
|
|
session.SendMsg(string(this.module.GetType()), "getassisthero", &pb.FriendGetAssistHeroResp{
|
|
Data: list,
|
|
Hero: heros,
|
|
})
|
|
|
|
return
|
|
}
|