This commit is contained in:
殷勇 2023-09-04 19:10:46 +08:00
parent e4b1bd4038
commit 8a07fcdd8c
6 changed files with 227 additions and 185 deletions

View File

@ -3771,13 +3771,13 @@ type MFGuild struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
AutoId *int64 `protobuf:"varint,1,opt,name=auto_id,json=autoId" json:"auto_id,omitempty"`
GuildId *int64 `protobuf:"varint,2,opt,name=guild_id,json=guildId" json:"guild_id,omitempty"`
Name *string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"`
LeaderId *string `protobuf:"bytes,4,opt,name=leader_id,json=leaderId" json:"leader_id,omitempty"`
Members *int32 `protobuf:"varint,5,opt,name=members" json:"members,omitempty"`
MaxMembers *int32 `protobuf:"varint,6,opt,name=max_members,json=maxMembers" json:"max_members,omitempty"`
Notice *string `protobuf:"bytes,7,opt,name=notice" json:"notice,omitempty"`
AutoId *int64 `protobuf:"varint,1,opt,name=auto_id,json=autoId" json:"auto_id,omitempty"`
GuildId *int64 `protobuf:"varint,2,opt,name=guild_id,json=guildId" json:"guild_id,omitempty"`
Name *string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"`
LeaderId *string `protobuf:"bytes,4,opt,name=leader_id,json=leaderId" json:"leader_id,omitempty"`
MembersCount *int32 `protobuf:"varint,5,opt,name=membersCount" json:"membersCount,omitempty"`
MaxMembers *int32 `protobuf:"varint,6,opt,name=max_members,json=maxMembers" json:"max_members,omitempty"`
Notice *string `protobuf:"bytes,7,opt,name=notice" json:"notice,omitempty"`
}
func (x *MFGuild) Reset() {
@ -3840,9 +3840,9 @@ func (x *MFGuild) GetLeaderId() string {
return ""
}
func (x *MFGuild) GetMembers() int32 {
if x != nil && x.Members != nil {
return *x.Members
func (x *MFGuild) GetMembersCount() int32 {
if x != nil && x.MembersCount != nil {
return *x.MembersCount
}
return 0
}
@ -4452,44 +4452,44 @@ var file_cs_proto_proto_rawDesc = []byte{
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x65, 0x72, 0x72, 0x4d, 0x73, 0x67, 0x12, 0x23,
0x0a, 0x06, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b,
0x2e, 0x63, 0x73, 0x2e, 0x4d, 0x46, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x52, 0x06, 0x67, 0x75, 0x69,
0x6c, 0x64, 0x73, 0x22, 0xc1, 0x01, 0x0a, 0x07, 0x4d, 0x46, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x12,
0x6c, 0x64, 0x73, 0x22, 0xcb, 0x01, 0x0a, 0x07, 0x4d, 0x46, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x12,
0x17, 0x0a, 0x07, 0x61, 0x75, 0x74, 0x6f, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03,
0x52, 0x06, 0x61, 0x75, 0x74, 0x6f, 0x49, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x67, 0x75, 0x69, 0x6c,
0x64, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x67, 0x75, 0x69, 0x6c,
0x64, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28,
0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x6c, 0x65, 0x61, 0x64, 0x65,
0x72, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x65, 0x61, 0x64,
0x65, 0x72, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x18,
0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x12, 0x1f,
0x0a, 0x0b, 0x6d, 0x61, 0x78, 0x5f, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x18, 0x06, 0x20,
0x01, 0x28, 0x05, 0x52, 0x0a, 0x6d, 0x61, 0x78, 0x4d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x12,
0x16, 0x0a, 0x06, 0x6e, 0x6f, 0x74, 0x69, 0x63, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52,
0x06, 0x6e, 0x6f, 0x74, 0x69, 0x63, 0x65, 0x22, 0x0d, 0x0a, 0x0b, 0x43, 0x4d, 0x47, 0x75, 0x69,
0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73, 0x22, 0x6f, 0x0a, 0x0b, 0x53, 0x4d, 0x47, 0x75, 0x69, 0x6c,
0x64, 0x4c, 0x6f, 0x67, 0x73, 0x12, 0x19, 0x0a, 0x08, 0x65, 0x72, 0x72, 0x5f, 0x63, 0x6f, 0x64,
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x65, 0x72, 0x72, 0x43, 0x6f, 0x64, 0x65,
0x12, 0x17, 0x0a, 0x07, 0x65, 0x72, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x06, 0x65, 0x72, 0x72, 0x4d, 0x73, 0x67, 0x12, 0x2c, 0x0a, 0x09, 0x67, 0x75, 0x69,
0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63,
0x73, 0x2e, 0x4d, 0x46, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x52, 0x09, 0x67, 0x75,
0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73, 0x22, 0x7b, 0x0a, 0x0a, 0x4d, 0x46, 0x47, 0x75, 0x69,
0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x12, 0x19, 0x0a, 0x08, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x5f, 0x69,
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x49, 0x64,
0x12, 0x1d, 0x0a, 0x0a, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, 0x12,
0x19, 0x0a, 0x08, 0x6c, 0x6f, 0x67, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28,
0x05, 0x52, 0x07, 0x6c, 0x6f, 0x67, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f,
0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e,
0x74, 0x65, 0x6e, 0x74, 0x22, 0x24, 0x0a, 0x08, 0x43, 0x4d, 0x4e, 0x6f, 0x74, 0x69, 0x63, 0x65,
0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x3e, 0x0a, 0x08, 0x53, 0x4d,
0x4e, 0x6f, 0x74, 0x69, 0x63, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x65, 0x72, 0x72, 0x5f, 0x63, 0x6f,
0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x65, 0x72, 0x72, 0x43, 0x6f, 0x64,
0x65, 0x12, 0x17, 0x0a, 0x07, 0x65, 0x72, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01,
0x28, 0x09, 0x52, 0x06, 0x65, 0x72, 0x72, 0x4d, 0x73, 0x67, 0x2a, 0x22, 0x0a, 0x0a, 0x43, 0x6f,
0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x5f, 0x65, 0x12, 0x14, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x74,
0x6f, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x10, 0xd1, 0xa2, 0xd5, 0xc4, 0x07, 0x42, 0x06,
0x5a, 0x04, 0x2e, 0x3b, 0x63, 0x73,
0x65, 0x72, 0x49, 0x64, 0x12, 0x22, 0x0a, 0x0c, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x43,
0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0c, 0x6d, 0x65, 0x6d, 0x62,
0x65, 0x72, 0x73, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1f, 0x0a, 0x0b, 0x6d, 0x61, 0x78, 0x5f,
0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x6d,
0x61, 0x78, 0x4d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x6f, 0x74,
0x69, 0x63, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6e, 0x6f, 0x74, 0x69, 0x63,
0x65, 0x22, 0x0d, 0x0a, 0x0b, 0x43, 0x4d, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73,
0x22, 0x6f, 0x0a, 0x0b, 0x53, 0x4d, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73, 0x12,
0x19, 0x0a, 0x08, 0x65, 0x72, 0x72, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x05, 0x52, 0x07, 0x65, 0x72, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x17, 0x0a, 0x07, 0x65, 0x72,
0x72, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x65, 0x72, 0x72,
0x4d, 0x73, 0x67, 0x12, 0x2c, 0x0a, 0x09, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x73,
0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x73, 0x2e, 0x4d, 0x46, 0x47, 0x75,
0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x52, 0x09, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67,
0x73, 0x22, 0x7b, 0x0a, 0x0a, 0x4d, 0x46, 0x47, 0x75, 0x69, 0x6c, 0x64, 0x4c, 0x6f, 0x67, 0x12,
0x19, 0x0a, 0x08, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x03, 0x52, 0x07, 0x67, 0x75, 0x69, 0x6c, 0x64, 0x49, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x61, 0x63,
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x6c, 0x6f, 0x67,
0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x6c, 0x6f, 0x67,
0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18,
0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x24,
0x0a, 0x08, 0x43, 0x4d, 0x4e, 0x6f, 0x74, 0x69, 0x63, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f,
0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e,
0x74, 0x65, 0x6e, 0x74, 0x22, 0x3e, 0x0a, 0x08, 0x53, 0x4d, 0x4e, 0x6f, 0x74, 0x69, 0x63, 0x65,
0x12, 0x19, 0x0a, 0x08, 0x65, 0x72, 0x72, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01,
0x28, 0x05, 0x52, 0x07, 0x65, 0x72, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x17, 0x0a, 0x07, 0x65,
0x72, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x65, 0x72,
0x72, 0x4d, 0x73, 0x67, 0x2a, 0x22, 0x0a, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74,
0x5f, 0x65, 0x12, 0x14, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x56, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x10, 0xd1, 0xa2, 0xd5, 0xc4, 0x07, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x63, 0x73,
}
var (

View File

@ -4,8 +4,10 @@ import "q5"
// User 用户实体
type User struct {
AccountId string
Username string
AccountId string
Username string
FriendRequest map[string]*FriendRequest // AccountId -> FriendRequest
FriendBlackList map[string]*FriendBlackList // AccountId -> FriendBlackList
}
// Friendship user1, user2 构成一个好友关系
@ -15,18 +17,92 @@ type Friendship struct {
}
type FriendRequest struct {
RequestID int
SenderID int
ReceiverID int
AccountId string
IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
}
// BlockedUser 直接存列表, 黑名单上限就50个
type BlockedUser struct {
AccountId string
BlockedAccountId string
// FriendBlackList 直接存列表, 黑名单上限就50个
type FriendBlackList struct {
AccountId string
isRemoved int32 // default: 0, isRemoved
}
// RandomUsername generates a random username
func RandomUsername() string {
return q5.RandomString(6)
}
func (u *User) AddFriendRequest(account2Id string) {
if u.FriendRequest == nil {
u.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest := &FriendRequest{
AccountId: account2Id,
IsFriendship: 0,
}
u.FriendRequest[account2Id] = friendRequest
}
func (u *User) GetFriendRequest(account2Id string) *FriendRequest {
if friendRequest, exists := u.FriendRequest[account2Id]; exists {
if exists {
return friendRequest
}
}
return nil
}
func (u *User) GetAllFriendRequestAccountIds() []string {
var accountIds []string
for accountId, _ := range u.FriendRequest {
accountIds = append(accountIds, accountId)
}
return accountIds
}
func (u *User) RemoveFriendRequest(account2Id string) {
delete(u.FriendRequest, account2Id)
}
func (u *User) IsInReq(targetAccountId string) bool {
if u.FriendRequest == nil {
u.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest, exists := u.FriendRequest[targetAccountId]
return exists && friendRequest.IsFriendship != 1
}
// AddBlacklist 加入黑名单
func (u *User) AddBlacklist(b *FriendBlackList) {
if u.FriendBlackList == nil {
u.FriendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
}
u.FriendBlackList[b.AccountId] = b
}
// GetBlacklist 获取黑名单
func (u *User) GetBlacklist(account2Id string) *FriendBlackList {
if friendBlackList, exists := u.FriendBlackList[account2Id]; exists {
if exists {
return friendBlackList
}
}
return nil
}
// IsInBlacklist 在黑名单中
func (u *User) IsInBlacklist(account2Id string) bool {
if friendBlackList, exists := u.FriendBlackList[account2Id]; exists {
return exists && friendBlackList.isRemoved == 0
}
return false
}
func (u *User) RemoveBlacklist(account2Id string) {
delete(u.FriendBlackList, account2Id)
}
// GetBlacklistCount 获取黑名单
func (u *User) GetBlacklistCount() int {
return len(u.FriendBlackList)
}

View File

@ -129,7 +129,6 @@ func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
fmt.Printf("loadUsersResult err:%v \n", err)
return
}
fm.users = make(map[string]*User, 100)
for rows.Next() {
user := &User{
@ -192,12 +191,20 @@ func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
fmt.Printf("loadPendingRequestsResult err:%v \n", err)
return
}
fm.pendingReqs = make(map[string]map[string]bool)
for rows.Next() {
senderAccountId := q5.ToString(*rows.GetByIndex(0))
receiverAccountId := q5.ToString(*rows.GetByIndex(1))
fm.pendingReqs[receiverAccountId] = make(map[string]bool)
fm.pendingReqs[receiverAccountId][senderAccountId] = false
user := fm.users[receiverAccountId]
if user != nil {
if user.FriendRequest == nil {
user.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest := &FriendRequest{
AccountId: senderAccountId,
IsFriendship: 0,
}
user.FriendRequest[receiverAccountId] = friendRequest
}
}
}
@ -220,11 +227,20 @@ func (fm *FriendsMgr) loadBlacklistResult(err error, rows *f5.DataSet) {
fmt.Printf("loadBlacklistResult err:%v \n", err)
return
}
fm.blackList = make(map[string]map[string]bool, 100)
for rows.Next() {
account1Id := q5.ToString(*rows.GetByIndex(0))
account2Id := q5.ToString(*rows.GetByIndex(1))
fm.blackList[account1Id] = make(map[string]bool, MaxBlockedMembers)
fm.blackList[account1Id][account2Id] = false
friendBlackList := &FriendBlackList{
AccountId: account2Id,
isRemoved: 0,
}
user := fm.users[account1Id]
if user != nil {
if user.FriendBlackList == nil {
user.FriendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
}
user.FriendBlackList[account1Id] = friendBlackList
}
}
}

View File

@ -12,12 +12,10 @@ import (
type FriendsMgr struct {
cs.MsgHandlerImpl
mu sync.RWMutex
users map[string]*User // AccountId -> 用户
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
pendingReqs map[string]map[string]bool // AccountId -> 等待请求列表 map[account2Id]false default false, isFriendShip
blackList map[string]map[string]bool // AccountId -> 黑名单列表 map[account2Id]false default: false, isRemoved
userCount int // 用户总数
users map[string]*User // AccountId -> 用户
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
userCount int // 用户总数
}
type SearchCache struct {
@ -93,7 +91,7 @@ func (fm *FriendsMgr) searchUsers(keyword string) []*User {
return listFriend
}
// addFriendRequest 添加好友请求
// addFriendRequest 添加好友请求 By default, account1Id is me, account2Id is them
func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) error {
user1 := fm.getUser(account1Id)
if user1 == nil {
@ -104,15 +102,17 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
return fmt.Errorf("user not found, accountId:%s", account2Id)
}
err := fm.checkInBlackList(account1Id, account2Id)
if err != nil {
return err
found1 := user1.IsInBlacklist(account2Id)
found2 := user2.IsInBlacklist(account1Id)
if found1 == true || found2 == true {
return fmt.Errorf("user:%s in user:%s blocked", account1Id, account2Id)
}
// 已发送请求
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
if _, ok := user1.FriendRequest[account2Id]; ok {
return nil
}
// 自己好友已满
if fm.getFriendCount(account1Id) >= MaxFriendMembers {
return fmt.Errorf("player:%s, friends are full", account1Id)
@ -126,18 +126,8 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
return fmt.Errorf("player:%s, friends are full", account2Id)
}
if fm.pendingReqs == nil {
fm.pendingReqs = make(map[string]map[string]bool, MaxPendingFriendReqs*10)
}
if fm.pendingReqs[account1Id] == nil {
fm.pendingReqs[account1Id] = make(map[string]bool, MaxPendingFriendReqs)
}
fm.pendingReqs[account1Id][account2Id] = false
if fm.pendingReqs[account2Id] == nil {
fm.pendingReqs[account2Id] = make(map[string]bool, MaxPendingFriendReqs)
}
fm.pendingReqs[account2Id][account1Id] = false
user1.AddFriendRequest(account2Id)
user2.AddFriendRequest(account1Id)
// persist to db
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDefault))
@ -147,7 +137,8 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
// acceptFriendRequest 接受好友请求
func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) error {
if _, ok := fm.pendingReqs[account1Id][account2Id]; !ok {
user := fm.getUser(account1Id)
if !user.IsInReq(account2Id) {
return fmt.Errorf("no pending friend request from account1Id to account2Id")
}
@ -170,26 +161,28 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
}
fm.friendships[account1Id] = append(fm.friendships[account1Id], friendship)
fm.friendships[account2Id] = append(fm.friendships[account2Id], friendship)
delete(fm.pendingReqs[account1Id], account2Id)
delete(fm.pendingReqs[account2Id], account1Id)
user.RemoveFriendRequest(account2Id)
// remove
user2 := fm.getUser(account2Id)
user2.RemoveFriendRequest(account1Id)
return nil
}
// rejectFriendRequest 拒绝好友请求
func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error {
if fm.pendingReqs[account1Id] == nil {
return fmt.Errorf("no pending friend request to reject")
}
if _, ok := fm.pendingReqs[account1Id][account2Id]; !ok {
return fmt.Errorf("no pending friend request from user1 to user2")
}
user := fm.getUser(account1Id)
//friendRequest := user.GetFriendRequest(account2Id)
// 申请表,申请者,目标者,
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusReject))
user.RemoveFriendRequest(account2Id)
delete(fm.pendingReqs[account1Id], account2Id)
delete(fm.pendingReqs[account2Id], account1Id)
// remove
user2 := fm.getUser(account2Id)
user2.RemoveFriendRequest(account1Id)
return nil
}
@ -249,8 +242,9 @@ func (fm *FriendsMgr) getFriendCount(accountId string) int {
// getFriendRequestCount 等待请求数量
func (fm *FriendsMgr) getFriendRequestCount(accountId string) int {
if _, ok := fm.pendingReqs[accountId]; ok {
return len(fm.pendingReqs[accountId])
user := fm.getUser(accountId)
if user != nil {
return len(user.FriendRequest)
}
return 0
}
@ -279,25 +273,26 @@ func (fm *FriendsMgr) listFriends(accountId string) []*User {
// addBlacklist 加入黑名单
func (fm *FriendsMgr) addBlacklist(account1Id string, account2Id string) error {
user1 := fm.getUser(account1Id)
user2 := fm.getUser(account2Id)
if user2 == nil {
return fmt.Errorf("user not found, accountId:%s", account2Id)
}
if fm.blackList[account1Id] == nil {
fm.blackList[account1Id] = make(map[string]bool, MaxBlockedMembers)
}
isRemoved, exists := fm.blackList[account1Id][account2Id]
if exists && !isRemoved {
user1FriendBlackList := user1.GetBlacklist(account2Id)
if user1FriendBlackList != nil && user1FriendBlackList.isRemoved != 0 {
return nil
}
if len(fm.blackList[account1Id]) >= MaxBlockedMembers {
if user1.GetBlacklistCount() >= MaxBlockedMembers {
return fmt.Errorf("your blacklist has reached the limit")
}
fm.blackList[account1Id][account2Id] = false
user1.AddBlacklist(&FriendBlackList{
AccountId: account2Id,
isRemoved: 0,
})
fm.upsertBlacklist(account1Id, account2Id, BlacklistStatusDefault)
return nil
@ -305,20 +300,14 @@ func (fm *FriendsMgr) addBlacklist(account1Id string, account2Id string) error {
// removeBlacklist 移除黑名单
func (fm *FriendsMgr) removeBlacklist(account1Id string, account2Id string) error {
user1 := fm.getUser(account1Id)
user2 := fm.getUser(account2Id)
if user2 == nil {
return fmt.Errorf("user not found, accountId:%s", account2Id)
}
if fm.blackList[account1Id] == nil {
return fmt.Errorf("your blacklist is emtpy")
}
if isRemoved, exists := fm.blackList[account1Id][account2Id]; !exists || isRemoved {
return fmt.Errorf("your blacklist not exists target account id")
}
delete(fm.blackList[account1Id], account2Id)
user1.RemoveBlacklist(account2Id)
fm.upsertBlacklist(account1Id, account2Id, BlacklistStatusIsRemoved)
return nil
@ -391,24 +380,6 @@ func (fm *FriendsMgr) findFriendShipIndex(Account1Id, Account2Id string) int {
return -1
}
func (fm *FriendsMgr) checkInBlackList(account1Id, account2Id string) error {
if fm.blackList[account1Id] == nil {
fm.blackList[account1Id] = make(map[string]bool, MaxBlockedMembers)
}
if isRemoved, exists := fm.blackList[account1Id][account2Id]; isRemoved && exists {
return fmt.Errorf("user:%s in user:%s blocked", account2Id, account1Id)
}
if fm.blackList[account2Id] == nil {
fm.blackList[account2Id] = make(map[string]bool, MaxBlockedMembers)
}
if isRemoved, exists := fm.blackList[account2Id][account1Id]; isRemoved && exists {
return fmt.Errorf("user:%s in user:%s blocked", account1Id, account2Id)
}
return nil
}
func swapAccountIds(account1Id, account2Id string) (string, string) {
if strings.Compare(account1Id, account2Id) > 0 {
account1Id, account2Id = account2Id, account1Id
@ -429,17 +400,3 @@ func (fm *FriendsMgr) getFriendships(accountId string) []*Friendship {
}
return nil
}
func (fm *FriendsMgr) getPendingReqs(accountId string) map[string]bool {
if reqs, ok := fm.pendingReqs[accountId]; ok {
return reqs
}
return nil
}
func (fm *FriendsMgr) getBlacklist(accountId string) map[string]bool {
if b, ok := fm.blackList[accountId]; ok {
return b
}
return nil
}

View File

@ -118,12 +118,8 @@ func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendReq
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
rspMsg := &cs.SMListPendingFriendRequest{}
accountId := p.accountId
resAccountIds := make([]string, MaxPendingFriendReqs)
for accountId, _ := range friendMgr.pendingReqs[accountId] {
resAccountIds = append(resAccountIds, accountId)
}
rspMsg.AccountIds = resAccountIds
user := friendMgr.getUser(accountId)
rspMsg.AccountIds = user.GetAllFriendRequestAccountIds()
f5.GetSysLog().Info("CMListPendingFriendRequest requests count:%d\n", len(rspMsg.GetAccountIds()))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
@ -211,26 +207,23 @@ func (p *Player) CMRemoveBlacklist(hdr *f5.MsgHdr, msg *cs.CMRemoveBlacklist) {
func (p *Player) CMFriendInfo(hdr *f5.MsgHdr, msg *cs.CMFriendInfo) {
rspMsg := new(cs.SMFriendInfo)
accountId := p.accountId
user := friendMgr.getUser(accountId)
// friendships
users := friendMgr.listFriends(accountId)
for _, user := range users {
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, user.AccountId)
friends := friendMgr.listFriends(accountId)
for _, friend := range friends {
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, friend.AccountId)
}
// pending reqs
reqs := friendMgr.getPendingReqs(accountId)
for reqAccountId, _ := range reqs {
rspMsg.PendingReqsAccountIds = append(rspMsg.PendingReqsAccountIds, reqAccountId)
}
accountIds := user.GetAllFriendRequestAccountIds()
rspMsg.PendingReqsAccountIds = accountIds
// blacklist
blacklist := friendMgr.getBlacklist(accountId)
for blockedAccountId, isRemoved := range blacklist {
if isRemoved == false {
rspMsg.BlacklistAccountIds = append(rspMsg.BlacklistAccountIds, blockedAccountId)
}
var blacklistAccountIds []string
for bAccountId := range user.FriendBlackList {
blacklistAccountIds = append(blacklistAccountIds, bAccountId)
}
rspMsg.BlacklistAccountIds = blacklistAccountIds
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
@ -293,26 +286,27 @@ func IsValidChatChannel(chatChannel int32) bool {
// CMGuildInfo 我的公会信息
func (p *Player) CMGuildInfo(hdr *f5.MsgHdr, msg *cs.CMGuildInfo) {
rspMsg := new(cs.SMGuildInfo)
g := guildMgr.GetGuildByAccountId(p.accountId)
if g == nil {
guild := guildMgr.GetGuildByAccountId(p.accountId)
if guild == nil {
rspMsg.Guild = nil
rspMsg.RandomGuilds = p.FillGuild(guildMgr.RandomGuilds())
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
return
}
members := q5.ToInt32(len(g.Members))
maxMembers := q5.ToInt32(g.MaxMembers)
currGuild := &cs.MFGuild{
AutoId: &g.AutoId,
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
Members: &members,
MaxMembers: &maxMembers,
Notice: &g.Notice,
membersCount := q5.ToInt32(len(guild.Members))
maxMembers := q5.ToInt32(guild.MaxMembers)
rspGuild := &cs.MFGuild{
AutoId: &guild.AutoId,
GuildId: &guild.GuildId,
Name: &guild.Name,
LeaderId: &guild.LeaderId,
MembersCount: &membersCount,
MaxMembers: &maxMembers,
Notice: &guild.Notice,
}
rspMsg.Guild = currGuild
rspMsg.Guild = rspGuild
rspMsg.RandomGuilds = nil
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
@ -516,16 +510,16 @@ func (p *Player) CMGuildLogs(hdr *f5.MsgHdr, msg *cs.CMGuildLogs) {
func (p *Player) FillGuild(guilds []*Guild) []*cs.MFGuild {
var resGuilds []*cs.MFGuild
for _, g := range guilds {
members := q5.ToInt32(len(g.Members))
membersCount := q5.ToInt32(len(g.Members))
maxMembers := q5.ToInt32(g.MaxMembers)
guild := &cs.MFGuild{
AutoId: &g.AutoId,
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
Members: &members,
MaxMembers: &maxMembers,
Notice: &g.Notice,
AutoId: &g.AutoId,
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
MembersCount: &membersCount,
MaxMembers: &maxMembers,
Notice: &g.Notice,
}
resGuilds = append(resGuilds, guild)
}

View File

@ -355,7 +355,6 @@ message SMGuildInfo
message CMCreateGuild
{
optional string name = 1;
}
//
@ -501,7 +500,7 @@ message MFGuild {
optional int64 guild_id = 2;
optional string name = 3;
optional string leader_id = 4;
optional int32 members = 5;
optional int32 members_count = 5;
optional int32 max_members = 6;
optional string notice = 7;
}