优化
This commit is contained in:
parent
3f1efa3ee4
commit
8165b22cb1
@ -23,12 +23,12 @@ UNIQUE KEY `friendship` (`account1_id`,`account2_id`)
|
|||||||
COMMENT "已经成为好友"
|
COMMENT "已经成为好友"
|
||||||
;
|
;
|
||||||
|
|
||||||
drop table if exists `t_friend_pending_requests`;
|
drop table if exists `t_friend_pending_request`;
|
||||||
CREATE TABLE t_friend_pending_requests (
|
CREATE TABLE t_friend_pending_request (
|
||||||
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
|
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
|
||||||
sender_account_id varchar(60) NOT NULL DEFAULT '',
|
sender_account_id varchar(60) NOT NULL DEFAULT '',
|
||||||
receiver_account_id varchar(60) NOT NULL DEFAULT '',
|
receiver_account_id varchar(60) NOT NULL DEFAULT '',
|
||||||
flag tinyint DEFAULT '0' COMMENT '0 pending, 1 ok',
|
is_friendship tinyint DEFAULT '0' COMMENT '已经是好友关系, 0 pending, 1 ok, 2 no',
|
||||||
`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
|
`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
|
||||||
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
|
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
|
||||||
PRIMARY KEY (`idx`),
|
PRIMARY KEY (`idx`),
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -6,22 +6,29 @@ import (
|
|||||||
"q5"
|
"q5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (fm *FriendsMgr) insertFriendRequest(account1Id string, account2Id string) {
|
func (fm *FriendsMgr) insertFriendRequest(account1Id string, account2Id string, is_friendship string) {
|
||||||
fields := [][]string{
|
fields := [][]string{
|
||||||
{"sender_account_id", account1Id},
|
{"sender_account_id", account1Id},
|
||||||
{"receiver_account_id", account2Id},
|
{"receiver_account_id", account2Id},
|
||||||
|
{"is_friendship", is_friendship},
|
||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Insert(
|
f5.GetJsStyleDb().Replace(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_pending_requests",
|
"t_friend_pending_request",
|
||||||
fields,
|
fields,
|
||||||
func(error, int64, int64) {},
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error:%v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("lastInsertId:%d\n", lastInsertId)
|
||||||
|
fmt.Printf("rowsAffected:%d\n", rowsAffected)
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) updateFriendRequest(account1Id string, account2Id string, flag int) {
|
func (fm *FriendsMgr) updateFriendRequest(account1Id string, account2Id string, fields [][]string) {
|
||||||
fields := [][]string{
|
if len(fields) <= 0 {
|
||||||
{"flag", q5.ToString(flag)},
|
return
|
||||||
}
|
}
|
||||||
where := [][]string{
|
where := [][]string{
|
||||||
{"sender_account_id", account1Id},
|
{"sender_account_id", account1Id},
|
||||||
@ -29,29 +36,40 @@ func (fm *FriendsMgr) updateFriendRequest(account1Id string, account2Id string,
|
|||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Update(
|
f5.GetJsStyleDb().Update(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_pending_requests",
|
"t_friend_pending_request",
|
||||||
fields,
|
fields,
|
||||||
where,
|
where,
|
||||||
func(error, int64, int64) {},
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error:%v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("lastInsertId:%d\n", lastInsertId)
|
||||||
|
fmt.Printf("rowsAffected:%d\n", rowsAffected)
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) insertFriendShip(account1Id string, account2Id string) {
|
func (fm *FriendsMgr) insertFriendShip(account1Id string, account2Id string) {
|
||||||
fields := [][]string{
|
fields := [][]string{
|
||||||
{"user1_id", account1Id},
|
{"account1_id", account1Id},
|
||||||
{"user2_id", account2Id},
|
{"account2_id", account2Id},
|
||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Insert(
|
f5.GetJsStyleDb().Insert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_ships",
|
"t_friend_ships",
|
||||||
fields,
|
fields,
|
||||||
func(error, int64, int64) {},
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error:%v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("lastInsertId:%d\n", lastInsertId)
|
||||||
|
fmt.Printf("rowsAffected:%d\n", rowsAffected)
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadUsers 加载所有用户
|
||||||
func (fm *FriendsMgr) loadUsers() {
|
func (fm *FriendsMgr) loadUsers() {
|
||||||
fmt.Printf("loadUsers from db\n")
|
|
||||||
// Load DB users to struct FriendsMgr.user
|
|
||||||
fields := []string{"account_id", "name"}
|
fields := []string{"account_id", "name"}
|
||||||
f5.GetJsStyleDb().Select(
|
f5.GetJsStyleDb().Select(
|
||||||
GAME_DB,
|
GAME_DB,
|
||||||
@ -64,11 +82,11 @@ func (fm *FriendsMgr) loadUsers() {
|
|||||||
|
|
||||||
func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
|
func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("loadUsersFromDBResult err:%v \n", err)
|
fmt.Printf("loadUsersResult err:%v \n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fm.users = make(map[string]*User)
|
fm.users = make(map[string]*User, 100)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
user := &User{
|
user := &User{
|
||||||
AccountId: q5.ToString(*rows.GetByIndex(0)),
|
AccountId: q5.ToString(*rows.GetByIndex(0)),
|
||||||
@ -76,11 +94,11 @@ func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
|
|||||||
}
|
}
|
||||||
fm.users[user.AccountId] = user
|
fm.users[user.AccountId] = user
|
||||||
}
|
}
|
||||||
|
fm.userCount = len(fm.users)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadFriendships 加载好友关系表
|
||||||
func (fm *FriendsMgr) loadFriendships() {
|
func (fm *FriendsMgr) loadFriendships() {
|
||||||
fmt.Printf("loadFriendships from db\n")
|
|
||||||
|
|
||||||
fields := []string{"account1_id", "account2_id"}
|
fields := []string{"account1_id", "account2_id"}
|
||||||
f5.GetJsStyleDb().Select(
|
f5.GetJsStyleDb().Select(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
@ -93,7 +111,7 @@ func (fm *FriendsMgr) loadFriendships() {
|
|||||||
|
|
||||||
func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
|
func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("loadFriendshipsFromDBResult err:%v \n", err)
|
fmt.Printf("loadFriendshipsResult err:%v \n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fm.friendships = make(map[string][]*Friendship)
|
fm.friendships = make(map[string][]*Friendship)
|
||||||
@ -103,7 +121,6 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
|
|||||||
if user1 == nil || user2 == nil {
|
if user1 == nil || user2 == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
friendship := &Friendship{}
|
friendship := &Friendship{}
|
||||||
friendship.User1 = user1
|
friendship.User1 = user1
|
||||||
friendship.User2 = user2
|
friendship.User2 = user2
|
||||||
@ -112,16 +129,15 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadPendingRequests 加载等待验证好友请求
|
||||||
func (fm *FriendsMgr) loadPendingRequests() {
|
func (fm *FriendsMgr) loadPendingRequests() {
|
||||||
fmt.Printf("loadPendingRequests from db\n")
|
|
||||||
|
|
||||||
fields := []string{"sender_account_id", "receiver_account_id"}
|
fields := []string{"sender_account_id", "receiver_account_id"}
|
||||||
f5.GetJsStyleDb().Select(
|
f5.GetJsStyleDb().Select(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_pending_requests",
|
"t_friend_pending_request",
|
||||||
fields,
|
fields,
|
||||||
[][]string{
|
[][]string{
|
||||||
{"flag", "0"},
|
{"is_friendship", "0"},
|
||||||
},
|
},
|
||||||
fm.loadPendingRequestsResult,
|
fm.loadPendingRequestsResult,
|
||||||
)
|
)
|
||||||
@ -129,13 +145,14 @@ func (fm *FriendsMgr) loadPendingRequests() {
|
|||||||
|
|
||||||
func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
|
func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("loadFriendshipsFromDBResult err:%v \n", err)
|
fmt.Printf("loadPendingRequestsResult err:%v \n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fm.pendingReqs = make(map[string]map[string]bool, MaxPendingFriendReqs)
|
fm.pendingReqs = make(map[string]map[string]bool)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
senderAccountId := q5.ToString(*rows.GetByIndex(0))
|
senderAccountId := q5.ToString(*rows.GetByIndex(0))
|
||||||
receiverAccountId := q5.ToString(*rows.GetByIndex(1))
|
receiverAccountId := q5.ToString(*rows.GetByIndex(1))
|
||||||
|
fm.pendingReqs[receiverAccountId] = make(map[string]bool)
|
||||||
fm.pendingReqs[receiverAccountId][senderAccountId] = true
|
fm.pendingReqs[receiverAccountId][senderAccountId] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@ type FriendsMgr struct {
|
|||||||
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
|
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
|
||||||
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
|
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
|
||||||
pendingReqs map[string]map[string]bool // AccountId -> 等待请求列表 map[account2Id]true
|
pendingReqs map[string]map[string]bool // AccountId -> 等待请求列表 map[account2Id]true
|
||||||
blockedUser map[string][]string // AccountId -> 黑名单列表 []AccountIds
|
blackList map[string][]string // AccountId -> 黑名单列表 []AccountIds
|
||||||
|
userCount int // 用户总数
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchCache struct {
|
type SearchCache struct {
|
||||||
@ -24,17 +25,8 @@ type SearchCache struct {
|
|||||||
LastModified time.Time
|
LastModified time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
//func NewFriendsMgr() *FriendsMgr {
|
|
||||||
// return &FriendsMgr{
|
|
||||||
// users: make(map[string]*User),
|
|
||||||
// searchCaches: make(map[string]SearchCache),
|
|
||||||
// friendships: make(map[string][]*Friendship),
|
|
||||||
// pendingReqs: make(map[string]map[string]bool),
|
|
||||||
// blockedUser: make(map[string][]string),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (fm *FriendsMgr) init() {
|
func (fm *FriendsMgr) init() {
|
||||||
|
fm.userCount = 0
|
||||||
// 加载所有用户
|
// 加载所有用户
|
||||||
fm.loadUsers()
|
fm.loadUsers()
|
||||||
// 加载好友关系表 列表
|
// 加载好友关系表 列表
|
||||||
@ -49,6 +41,7 @@ func (fm *FriendsMgr) unInit() {
|
|||||||
// 2. Struct Data Persist to DB
|
// 2. Struct Data Persist to DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 搜索指定用户
|
||||||
func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
|
func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
|
||||||
if user, ok := fm.users[accountId]; ok {
|
if user, ok := fm.users[accountId]; ok {
|
||||||
return user
|
return user
|
||||||
@ -56,6 +49,7 @@ func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 搜索用户
|
||||||
func (fm *FriendsMgr) searchUsers(keyword string) []*User {
|
func (fm *FriendsMgr) searchUsers(keyword string) []*User {
|
||||||
if len(keyword) > SearchWord {
|
if len(keyword) > SearchWord {
|
||||||
return nil
|
return nil
|
||||||
@ -97,40 +91,6 @@ func (fm *FriendsMgr) searchUsers(keyword string) []*User {
|
|||||||
return listFriend
|
return listFriend
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) addBlocked(account1Id string, account2Id string) error {
|
|
||||||
if fm.blockedUser[account1Id] == nil {
|
|
||||||
fm.blockedUser[account1Id] = []string{}
|
|
||||||
}
|
|
||||||
if len(fm.blockedUser[account1Id]) >= 50 {
|
|
||||||
return fmt.Errorf("your blacklist has reached the limit")
|
|
||||||
}
|
|
||||||
|
|
||||||
index := fm.findBlockedUserIndex(account1Id, account2Id)
|
|
||||||
if index < 0 {
|
|
||||||
fm.blockedUser[account1Id] = append(fm.blockedUser[account1Id], account2Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FriendsMgr) removeBlocked(account1Id string, account2Id string) error {
|
|
||||||
if fm.blockedUser[account1Id] == nil {
|
|
||||||
return fmt.Errorf("your blacklist is emtpy")
|
|
||||||
}
|
|
||||||
|
|
||||||
index := fm.findBlockedUserIndex(account1Id, account2Id)
|
|
||||||
if index < 0 {
|
|
||||||
return fmt.Errorf("your blacklist not exists target account id")
|
|
||||||
}
|
|
||||||
|
|
||||||
blockedUsers := fm.blockedUser[account1Id]
|
|
||||||
blockedUsers = append(blockedUsers[:index], blockedUsers[index+1:]...)
|
|
||||||
|
|
||||||
fm.blockedUser[account1Id] = blockedUsers
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// addFriendRequest 添加好友请求
|
// addFriendRequest 添加好友请求
|
||||||
func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) error {
|
func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) error {
|
||||||
_, exists1 := fm.users[account1Id]
|
_, exists1 := fm.users[account1Id]
|
||||||
@ -139,17 +99,11 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
|
|||||||
return errors.New("users not exist")
|
return errors.New("users not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
// step1. check in blocked
|
err := fm.checkInBlackList(account1Id, account2Id)
|
||||||
for _, blockedAccountId := range fm.blockedUser[account1Id] {
|
if err != nil {
|
||||||
if blockedAccountId == account2Id {
|
return err
|
||||||
return errors.New("users in blocked")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, blockedAccountId := range fm.blockedUser[account2Id] {
|
|
||||||
if blockedAccountId == account2Id {
|
|
||||||
return errors.New("users in blocked")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 已发送请求
|
// 已发送请求
|
||||||
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
|
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
|
||||||
return nil
|
return nil
|
||||||
@ -167,22 +121,28 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
|
|||||||
return fmt.Errorf("player:%s, friends are full", account2Id)
|
return fmt.Errorf("player:%s, friends are full", account2Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 为 account2Id 创建等待验证好友请求 记录,由 account1Id 申请的
|
if fm.pendingReqs == nil {
|
||||||
if fm.pendingReqs[account2Id] == nil {
|
fm.pendingReqs = make(map[string]map[string]bool, 200)
|
||||||
fm.pendingReqs[account2Id] = make(map[string]bool, 20)
|
}
|
||||||
|
if fm.pendingReqs[account1Id] == nil {
|
||||||
|
fm.pendingReqs[account1Id] = make(map[string]bool, MaxPendingFriendReqs)
|
||||||
}
|
}
|
||||||
fm.pendingReqs[account1Id][account2Id] = true
|
fm.pendingReqs[account1Id][account2Id] = true
|
||||||
|
|
||||||
|
if fm.pendingReqs[account2Id] == nil {
|
||||||
|
fm.pendingReqs[account2Id] = make(map[string]bool, MaxPendingFriendReqs)
|
||||||
|
}
|
||||||
fm.pendingReqs[account2Id][account1Id] = true
|
fm.pendingReqs[account2Id][account1Id] = true
|
||||||
|
|
||||||
// persist to db
|
// persist to db
|
||||||
fm.insertFriendRequest(account1Id, account2Id)
|
fm.insertFriendRequest(account1Id, account2Id, "0")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// acceptFriendRequest 接受好友请求
|
// acceptFriendRequest 接受好友请求
|
||||||
func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) error {
|
func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) error {
|
||||||
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
|
if _, ok := fm.pendingReqs[account1Id][account2Id]; !ok {
|
||||||
return errors.New("no pending friend request from account1Id to account2Id")
|
return errors.New("no pending friend request from account1Id to account2Id")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,10 +153,9 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
|
|||||||
return fmt.Errorf("player:%s, friends are full", account2Id)
|
return fmt.Errorf("player:%s, friends are full", account2Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// step1. update friend reqs
|
// step1. update reqs
|
||||||
flag := 1
|
fm.insertFriendRequest(account2Id, account1Id, "1")
|
||||||
fm.updateFriendRequest(account1Id, account2Id, flag)
|
// step2. insert friendship
|
||||||
// step2. add t_friend_ships
|
|
||||||
compareResult := strings.Compare(account1Id, account2Id)
|
compareResult := strings.Compare(account1Id, account2Id)
|
||||||
if compareResult > 0 {
|
if compareResult > 0 {
|
||||||
temp := account1Id
|
temp := account1Id
|
||||||
@ -220,15 +179,18 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
|
|||||||
|
|
||||||
// rejectFriendRequest 拒绝好友请求
|
// rejectFriendRequest 拒绝好友请求
|
||||||
func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error {
|
func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error {
|
||||||
|
|
||||||
if fm.pendingReqs[account1Id] == nil {
|
if fm.pendingReqs[account1Id] == nil {
|
||||||
return errors.New("no pending friend request to reject")
|
return errors.New("no pending friend request to reject")
|
||||||
}
|
}
|
||||||
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
|
if _, ok := fm.pendingReqs[account1Id][account2Id]; !ok {
|
||||||
return errors.New("no pending friend request from user1 to user2")
|
return errors.New("no pending friend request from user1 to user2")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag := 2
|
//fields := [][]string{{"is_friendship", q5.ToString(2)}}
|
||||||
fm.updateFriendRequest(account1Id, account2Id, flag)
|
//fm.updateFriendRequest(account1Id, account2Id, fields)
|
||||||
|
// 申请表,申请者,目标者,
|
||||||
|
fm.insertFriendRequest(account2Id, account1Id, "2")
|
||||||
|
|
||||||
delete(fm.pendingReqs[account1Id], account2Id)
|
delete(fm.pendingReqs[account1Id], account2Id)
|
||||||
delete(fm.pendingReqs[account2Id], account1Id)
|
delete(fm.pendingReqs[account2Id], account1Id)
|
||||||
@ -305,6 +267,40 @@ func (fm *FriendsMgr) listFriend(accountId string) []*User {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) addBlocked(account1Id string, account2Id string) error {
|
||||||
|
if fm.blackList[account1Id] == nil {
|
||||||
|
fm.blackList[account1Id] = []string{}
|
||||||
|
}
|
||||||
|
if len(fm.blackList[account1Id]) >= 50 {
|
||||||
|
return fmt.Errorf("your blacklist has reached the limit")
|
||||||
|
}
|
||||||
|
|
||||||
|
index := fm.findBlockedUserIndex(account1Id, account2Id)
|
||||||
|
if index < 0 {
|
||||||
|
fm.blackList[account1Id] = append(fm.blackList[account1Id], account2Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) removeBlocked(account1Id string, account2Id string) error {
|
||||||
|
if fm.blackList[account1Id] == nil {
|
||||||
|
return fmt.Errorf("your blacklist is emtpy")
|
||||||
|
}
|
||||||
|
|
||||||
|
index := fm.findBlockedUserIndex(account1Id, account2Id)
|
||||||
|
if index < 0 {
|
||||||
|
return fmt.Errorf("your blacklist not exists target account id")
|
||||||
|
}
|
||||||
|
|
||||||
|
blockedUsers := fm.blackList[account1Id]
|
||||||
|
blockedUsers = append(blockedUsers[:index], blockedUsers[index+1:]...)
|
||||||
|
|
||||||
|
fm.blackList[account1Id] = blockedUsers
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendship) {
|
func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendship) {
|
||||||
if fm.friendships[accountID] == nil {
|
if fm.friendships[accountID] == nil {
|
||||||
fm.friendships[accountID] = []*Friendship{friendship}
|
fm.friendships[accountID] = []*Friendship{friendship}
|
||||||
@ -337,8 +333,8 @@ func PrintUsers(str string, userList []*User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fm *FriendsMgr) findBlockedUserIndex(Account1Id, Account2Id string) int {
|
func (fm *FriendsMgr) findBlockedUserIndex(Account1Id, Account2Id string) int {
|
||||||
if fm.blockedUser[Account1Id] != nil {
|
if fm.blackList[Account1Id] != nil {
|
||||||
for i, blockedAccountId := range fm.blockedUser[Account1Id] {
|
for i, blockedAccountId := range fm.blackList[Account1Id] {
|
||||||
if blockedAccountId == Account2Id {
|
if blockedAccountId == Account2Id {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@ -367,3 +363,17 @@ func (fm *FriendsMgr) getUser(accountId string) *User {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) checkInBlackList(account1Id, account2Id string) error {
|
||||||
|
for _, blockedAccountId := range fm.blackList[account1Id] {
|
||||||
|
if blockedAccountId == account2Id {
|
||||||
|
return fmt.Errorf("user:%s in user:%s blocked", account2Id, account1Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, blockedAccountId := range fm.blackList[account2Id] {
|
||||||
|
if blockedAccountId == account1Id {
|
||||||
|
return fmt.Errorf("user:%s in user:%s blocked", account1Id, account2Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -34,11 +34,11 @@ func (p *Player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
|
|||||||
// CMSearchUserByAccountId 搜索指定用户
|
// CMSearchUserByAccountId 搜索指定用户
|
||||||
func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
|
func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
|
||||||
rspMsg := new(cs.SMSearchUserByAccountId)
|
rspMsg := new(cs.SMSearchUserByAccountId)
|
||||||
user := friendMgr.searchByAccountId(msg.GetSearchKeyword())
|
user := friendMgr.searchByAccountId(msg.GetAccountId())
|
||||||
if user != nil {
|
if user != nil {
|
||||||
rspMsg.AccountId = &user.AccountId
|
rspMsg.AccountId = &user.AccountId
|
||||||
rspMsg.Username = &user.Username
|
rspMsg.Username = &user.Username
|
||||||
f5.GetSysLog().Info("CMSearchUserByAccountId search result: %s\n", *rspMsg.AccountId)
|
f5.GetSysLog().Info("CMSearchUserByAccountId AccountId: %s\n", *rspMsg.AccountId)
|
||||||
}
|
}
|
||||||
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
}
|
}
|
||||||
@ -91,13 +91,12 @@ func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendReq
|
|||||||
|
|
||||||
user1Id := p.accountId
|
user1Id := p.accountId
|
||||||
user2Id := msg.GetTargetAccountId()
|
user2Id := msg.GetTargetAccountId()
|
||||||
err := friendMgr.acceptFriendRequest(user1Id, user2Id)
|
err := friendMgr.rejectFriendRequest(user1Id, user2Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reason := err.Error()
|
reason := err.Error()
|
||||||
rspMsg.Reason = &reason
|
rspMsg.Reason = &reason
|
||||||
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
|
f5.GetSysLog().Info("CMRejectFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
|
||||||
f5.GetSysLog().Info("CMAcceptFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,14 +109,14 @@ func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendReq
|
|||||||
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
|
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
|
||||||
rspMsg := &cs.SMListPendingFriendRequest{}
|
rspMsg := &cs.SMListPendingFriendRequest{}
|
||||||
accountId := p.accountId
|
accountId := p.accountId
|
||||||
for accountId, flag := range friendMgr.pendingReqs[accountId] {
|
|
||||||
row := &cs.MFPendingFriendRequest{
|
resAccountIds := make([]string, MaxPendingFriendReqs)
|
||||||
AccountId: &accountId,
|
for accountId, _ := range friendMgr.pendingReqs[accountId] {
|
||||||
Flag: &flag,
|
resAccountIds = append(resAccountIds, accountId)
|
||||||
}
|
|
||||||
rspMsg.PendingFriendRequest = append(rspMsg.PendingFriendRequest, row)
|
|
||||||
}
|
}
|
||||||
f5.GetSysLog().Info("CMListPendingFriendRequest requests count:%d\n", len(rspMsg.PendingFriendRequest))
|
rspMsg.AccountIds = resAccountIds
|
||||||
|
|
||||||
|
f5.GetSysLog().Info("CMListPendingFriendRequest requests count:%d\n", len(rspMsg.GetAccountIds()))
|
||||||
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ message SMSearchUser
|
|||||||
// 请求搜索用户 CMSearchUserByAccountId
|
// 请求搜索用户 CMSearchUserByAccountId
|
||||||
message CMSearchUserByAccountId
|
message CMSearchUserByAccountId
|
||||||
{
|
{
|
||||||
optional string search_keyword = 1; //搜索account id
|
optional string account_id = 1; //搜索account id
|
||||||
}
|
}
|
||||||
|
|
||||||
// 回复搜索用户
|
// 回复搜索用户
|
||||||
@ -191,7 +191,7 @@ message CMListPendingFriendRequest
|
|||||||
// 回复等待验证的好友请求
|
// 回复等待验证的好友请求
|
||||||
message SMListPendingFriendRequest
|
message SMListPendingFriendRequest
|
||||||
{
|
{
|
||||||
repeated MFPendingFriendRequest pendingFriendRequest = 1;
|
repeated string accountIds = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求我的好友列表
|
// 请求我的好友列表
|
||||||
@ -223,10 +223,6 @@ message MFUser {
|
|||||||
optional string account_id = 1;
|
optional string account_id = 1;
|
||||||
optional string username = 2;
|
optional string username = 2;
|
||||||
}
|
}
|
||||||
message MFPendingFriendRequest {
|
|
||||||
optional string account_id = 1;
|
|
||||||
optional bool flag = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求公会信息
|
// 请求公会信息
|
||||||
message CMGuildInfo
|
message CMGuildInfo
|
||||||
|
Loading…
x
Reference in New Issue
Block a user