This commit is contained in:
殷勇 2023-09-13 19:39:56 +08:00
parent 340309bb26
commit ed0f17dba2
8 changed files with 206 additions and 266 deletions

View File

@ -15,7 +15,7 @@ CREATE TABLE `t_friend_ships` (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id', `idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
`account1_id` varchar(60) NOT NULL DEFAULT '', `account1_id` varchar(60) NOT NULL DEFAULT '',
`account2_id` varchar(60) NOT NULL DEFAULT '', `account2_id` varchar(60) NOT NULL DEFAULT '',
`is_delete_friendship` tinyint DEFAULT '0' COMMENT '是否已删除好友关系, 0 no, 1 deleted', `is_friendship` tinyint DEFAULT '0' COMMENT '是否好友关系, 0 pending, 1 ok, 2 reject, 3 disband',
`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`),
@ -23,18 +23,6 @@ UNIQUE KEY `friendship` (`account1_id`,`account2_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin
COMMENT "已经成为好友关系"; COMMENT "已经成为好友关系";
drop table if exists `t_friend_pending_request`;
CREATE TABLE t_friend_pending_request (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
sender_account_id varchar(60) NOT NULL DEFAULT '',
receiver_account_id varchar(60) NOT NULL DEFAULT '',
is_friendship tinyint DEFAULT '0' COMMENT '已经是好友关系, 0 pending, 1 ok, 2 reject, 3 deleted',
`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),
UNIQUE KEY `friend_req` (`sender_account_id`,`receiver_account_id`)
) COMMENT "等待验证的好友请求";
drop table if exists t_friend_blacklist; drop table if exists t_friend_blacklist;
CREATE TABLE `t_friend_blacklist` ( CREATE TABLE `t_friend_blacklist` (
`idx` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增id', `idx` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增id',

View File

@ -18,19 +18,22 @@ const (
// im server friend // im server friend
const ( const (
MaxFriendMembers = 200 MaxFriendMembers = 200
MaxPendingFriendReqs = 20 MaxPendingFriendReqs = 20
MaxBlockedMembers = 50 MaxBlockedMembers = 50
SearchWord = 42 // 搜索关键字 SearchWord = 42 // 搜索关键字
MaxSearchResults = 20 // 搜索结果20条 MaxSearchResults = 20 // 搜索结果20条
FriendReqsStatusDefault = 0 // 好友请求状态, 等待中
FriendReqsStatusOk = 1 // 好友请求状态, 接受 FriendshipStatusPending = 0 // 好友关系状态 等待中 pending
FriendReqsStatusReject = 2 // 好友请求状态, 拒绝 FriendshipStatusOK = 1 // 好友关系状态 接受 ok
FriendReqsStatusDeleted = 3 // 好友请求状态, 已删除 FriendshipStatusReject = 2 // 好友关系状态 拒绝 reject
FriendshipStatusOk = 0 // 好友关系状态 正常 FriendshipStatusDeleted = 3 // 好友关系状态 已删除 disband
FriendshipStatusDeleted = 1 // 好友关系状态 已删除0
BlacklistStatusDefault = 0 // 好友黑名单状态 是否已移除黑名单 默认0添加进来 未移除 BlacklistStatusDefault = 0 // 好友黑名单状态 是否已移除黑名单 默认0添加进来 未移除
BlacklistStatusIsRemoved = 1 // 好友黑名单状态 已移除黑名单 BlacklistStatusIsRemoved = 1 // 好友黑名单状态 已移除黑名单
OnlineStatusOff = 0 // 在线状态 离线
OnlineStatus = 1 // 在线状态 在线
) )
const ( const (

View File

@ -2,30 +2,17 @@ package main
type User struct { type User struct {
AccountId string AccountId string
FriendRequest map[string]*FriendRequest // targetAccountId -> FriendRequest FriendBlackList map[string]*FriendBlackList // BlockedAccountId -> FriendBlackList
FriendBlackList map[string]*FriendBlackList // targetAccountId -> FriendBlackList Friendships map[string]*Friendship // FriendAccountId -> Friendship
Friendships []*Friendship
} }
type Friendship2 struct { type Friendship struct {
FriendAccountId string // friend's account id FriendAccountId string // friend's account id
IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
RequestTime int32 // send time RequestTime int64 // send time
} }
// Friendship user1, user2 构成一个好友关系 // FriendBlackList 直接存列表, 黑名单上限50个
type Friendship struct {
User1 *User
User2 *User
}
type FriendRequest struct {
AccountId string // target account id
IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
RequestTime int32 // send time
}
// FriendBlackList 直接存列表, 黑名单上限就50个
type FriendBlackList struct { type FriendBlackList struct {
AccountId string AccountId string
IsRemoved int32 // default: 0, isRemoved IsRemoved int32 // default: 0, isRemoved
@ -33,49 +20,34 @@ type FriendBlackList struct {
func NewUser(accountId string) *User { func NewUser(accountId string) *User {
user := &User{ user := &User{
AccountId: accountId, AccountId: accountId,
FriendRequest: make(map[string]*FriendRequest), //FriendRequest: make(map[string]*FriendRequest),
FriendBlackList: make(map[string]*FriendBlackList), FriendBlackList: make(map[string]*FriendBlackList),
Friendships: make([]*Friendship, 0, MaxFriendMembers), Friendships: make(map[string]*Friendship, MaxFriendMembers),
} }
return user return user
} }
func (u *User) AddFriendRequest(account2Id string) { func (u *User) AddFriendRequest(account2Id string, IsFriendship int32, requestTime int64) {
friendRequest := &FriendRequest{ friendShip := &Friendship{
AccountId: account2Id, FriendAccountId: account2Id,
IsFriendship: 0, IsFriendship: IsFriendship,
RequestTime: requestTime,
} }
u.FriendRequest[account2Id] = friendRequest u.Friendships[account2Id] = friendShip
}
func (u *User) GetFriendRequest(account2Id string) *FriendRequest {
if friendRequest, exists := u.FriendRequest[account2Id]; 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) SetFriendRequest(account2Id string) {
friendRequest := u.FriendRequest[account2Id]
friendRequest.IsFriendship = 0
} }
func (u *User) RemoveFriendRequest(account2Id string) { func (u *User) RemoveFriendRequest(account2Id string) {
delete(u.FriendRequest, account2Id) delete(u.Friendships, account2Id)
} }
func (u *User) IsInReq(targetAccountId string) bool { func (u *User) RemoveFriendShip(account2Id string) {
friendRequest, exists := u.FriendRequest[targetAccountId] delete(u.Friendships, account2Id)
return exists && friendRequest.IsFriendship != 1 }
func (u *User) IsInReq(FriendAccountId string) bool {
friendRequest, exists := u.Friendships[FriendAccountId]
return exists && friendRequest.IsFriendship == 0
} }
// AddBlacklist 加入黑名单 // AddBlacklist 加入黑名单

View File

@ -4,6 +4,7 @@ import (
"f5" "f5"
"fmt" "fmt"
"q5" "q5"
"strings"
) )
func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string, isFriendship string, cb func(error)) { func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string, isFriendship string, cb func(error)) {
@ -31,7 +32,9 @@ func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string,
) )
} }
func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isDeleteFriendship int, cb func(error)) { func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isFriendship int, requestTime int64, cb func(error)) {
account1Id, account2Id = SwapAccountIds(account1Id, account2Id)
where := [][]string{ where := [][]string{
{"account1_id", account1Id}, {"account1_id", account1Id},
{"account2_id", account2Id}, {"account2_id", account2Id},
@ -39,10 +42,13 @@ func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isD
insertKv := [][]string{ insertKv := [][]string{
{"account1_id", account1Id}, {"account1_id", account1Id},
{"account2_id", account2Id}, {"account2_id", account2Id},
{"is_delete_friendship", q5.ToString(isDeleteFriendship)}, {"is_friendship", q5.ToString(isFriendship)},
{"createtime", q5.ToString(requestTime)},
{"modifytime", q5.ToString(requestTime)},
} }
updateKv := [][]string{ updateKv := [][]string{
{"is_delete_friendship", q5.ToString(isDeleteFriendship)}, {"is_friendship", q5.ToString(isFriendship)},
{"modifytime", q5.ToString(requestTime)},
} }
f5.GetJsStyleDb().Upsert( f5.GetJsStyleDb().Upsert(
FRIEND_DB, FRIEND_DB,
@ -189,8 +195,9 @@ func (fm *FriendsMgr) findUsersByUsername(username string, sinceId int64, cb fun
} }
// loadFriendships 加载好友关系表 // loadFriendships 加载好友关系表
func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) { func (fm *FriendsMgr) loadFriendships() {
fields := []string{"account1_id", "account2_id"} fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"}
var where [][]string
f5.GetJsStyleDb().Select( f5.GetJsStyleDb().Select(
FRIEND_DB, FRIEND_DB,
"t_friend_ships", "t_friend_ships",
@ -203,40 +210,39 @@ func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) {
for rows.Next() { for rows.Next() {
account1Id := q5.ToString(*rows.GetByIndex(0)) account1Id := q5.ToString(*rows.GetByIndex(0))
account2Id := q5.ToString(*rows.GetByIndex(1)) account2Id := q5.ToString(*rows.GetByIndex(1))
if user.AccountId == account1Id { isFriendship := q5.ToInt32(*rows.GetByIndex(2))
user2 := NewUser(account2Id) requestTime := q5.ToInt64(*rows.GetByIndex(3))
friendMgr.AddUser(account2Id, user2)
friendship1 := &Friendship{} user1 := NewUser(account1Id)
friendship1.User1 = user friendMgr.AddUser(account1Id, user1)
friendship1.User2 = user2 friendship1 := &Friendship{
fm.AddFriendshipToMap(user.AccountId, friendship1) FriendAccountId: account2Id,
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) IsFriendship: isFriendship,
RequestTime: requestTime,
} }
if user.AccountId == account2Id { user1.Friendships[account2Id] = friendship1
user2 := NewUser(account1Id)
//friendMgr.AddUser(account1Id, user2) user2 := NewUser(account2Id)
friendship := &Friendship{} friendMgr.AddUser(account2Id, user2)
friendship.User1 = user friendship2 := &Friendship{
friendship.User2 = user2 FriendAccountId: account1Id,
fm.AddFriendshipToMap(account2Id, friendship) IsFriendship: isFriendship,
cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) RequestTime: requestTime,
} }
user2.Friendships[account1Id] = friendship2
cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {})
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
} }
}, },
) )
} }
// loadPendingRequests 加载等待验证好友请求 func (fm *FriendsMgr) loadUserFriendships(user *User, where [][]string) {
func (fm *FriendsMgr) loadPendingRequests(user *User) { fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"}
fields := []string{"sender_account_id", "receiver_account_id", "createtime"}
// 还未成功好友关系的 请求列表
where := [][]string{
{"receiver_account_id", user.AccountId},
{"is_friendship", "0"},
}
f5.GetJsStyleDb().Select( f5.GetJsStyleDb().Select(
FRIEND_DB, FRIEND_DB,
"t_friend_pending_request", "t_friend_ships",
fields, fields,
where, where,
func(err error, rows *f5.DataSet) { func(err error, rows *f5.DataSet) {
@ -244,32 +250,51 @@ func (fm *FriendsMgr) loadPendingRequests(user *User) {
return return
} }
for rows.Next() { for rows.Next() {
senderAccountId := q5.ToString(*rows.GetByIndex(0)) account1Id := q5.ToString(*rows.GetByIndex(0))
//receiverAccountId := q5.ToString(*rows.GetByIndex(1)) account2Id := q5.ToString(*rows.GetByIndex(1))
requestTime := q5.ToInt32(*rows.GetByIndex(2)) isFriendship := q5.ToInt32(*rows.GetByIndex(2))
requestTime := q5.ToInt64(*rows.GetByIndex(3))
if user.AccountId == account1Id {
friendship1 := &Friendship{
FriendAccountId: account2Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user.Friendships[account2Id] = friendship1
// load profile user2 := NewUser(account2Id)
cacheMgr.LoadPlayerProfile(senderAccountId, func(playerProfile *PlayerProfile) {}) friendMgr.AddUser(account2Id, user2)
friendship2 := &Friendship{
friendRequest := &FriendRequest{ FriendAccountId: account1Id,
AccountId: senderAccountId, IsFriendship: isFriendship,
IsFriendship: 0, RequestTime: requestTime,
RequestTime: requestTime, }
user2.Friendships[account1Id] = friendship2
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
}
if user.AccountId == account2Id {
friendUser := NewUser(account1Id)
friendMgr.AddUser(account1Id, friendUser)
friendship := &Friendship{
FriendAccountId: account1Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user.Friendships[account1Id] = friendship
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
} }
user.FriendRequest[senderAccountId] = friendRequest
} }
}, },
) )
} }
// loadPendingRequests 加载等待验证好友请求 // loadPendingRequests 加载等待验证好友请求
func (fm *FriendsMgr) loadBlacklist(user *User) { func (fm *FriendsMgr) loadBlacklist() {
fields := []string{"account_id", "blocked_account_id"} fields := []string{"account_id", "blocked_account_id"}
where := [][]string{ where := [][]string{
{"account_id", user.AccountId},
{"is_removed", "0"}, {"is_removed", "0"},
} }
f5.GetJsStyleDb().Select( f5.GetJsStyleDb().Select(
FRIEND_DB, FRIEND_DB,
"t_friend_blacklist", "t_friend_blacklist",
@ -280,13 +305,22 @@ func (fm *FriendsMgr) loadBlacklist(user *User) {
return return
} }
for rows.Next() { for rows.Next() {
account1Id := q5.ToString(*rows.GetByIndex(0))
account2Id := q5.ToString(*rows.GetByIndex(1)) account2Id := q5.ToString(*rows.GetByIndex(1))
friendBlackList := &FriendBlackList{ friendBlackList := &FriendBlackList{
AccountId: account2Id, AccountId: account2Id,
IsRemoved: 0, IsRemoved: 0,
} }
user := fm.GetUser(account1Id)
user.FriendBlackList[account2Id] = friendBlackList user.FriendBlackList[account2Id] = friendBlackList
} }
}, },
) )
} }
func SwapAccountIds(account1Id, account2Id string) (string, string) {
if strings.Compare(account1Id, account2Id) > 0 {
account1Id, account2Id = account2Id, account1Id
}
return account1Id, account2Id
}

View File

@ -4,8 +4,8 @@ import (
"cs" "cs"
"f5" "f5"
"q5" "q5"
"strings"
"sync" "sync"
"time"
) )
type FriendsMgr struct { type FriendsMgr struct {
@ -18,6 +18,7 @@ type FriendsMgr struct {
func (fm *FriendsMgr) init() { func (fm *FriendsMgr) init() {
fm.Users = make(map[string]*User) fm.Users = make(map[string]*User)
fm.UserCount = 0 fm.UserCount = 0
fm.loadFromDB()
} }
func (fm *FriendsMgr) unInit() { func (fm *FriendsMgr) unInit() {
@ -25,6 +26,13 @@ func (fm *FriendsMgr) unInit() {
// 2. Struct Data Persist to DB // 2. Struct Data Persist to DB
} }
func (fm *FriendsMgr) loadFromDB() {
// 加载好友关系表 列表
fm.loadFriendships()
// 加载黑名单列表
// fm.loadBlacklist()
}
// SearchByAccountId 搜索指定用户 // SearchByAccountId 搜索指定用户
func (fm *FriendsMgr) SearchByAccountId(accountId string, cb func(errCode int32, errMsg string, playerProfile *PlayerProfile)) { func (fm *FriendsMgr) SearchByAccountId(accountId string, cb func(errCode int32, errMsg string, playerProfile *PlayerProfile)) {
friendMgr.findPlayer(accountId, func(err error, profile *PlayerProfile) { friendMgr.findPlayer(accountId, func(err error, profile *PlayerProfile) {
@ -54,32 +62,22 @@ func (fm *FriendsMgr) SearchUsers(accountId, username string, sinceId int64, cb
// AddFriendRequest 添加好友请求 By default, account1Id is me // AddFriendRequest 添加好友请求 By default, account1Id is me
func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb func(errCode int32, errMsg string)) { func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb func(errCode int32, errMsg string)) {
//player2 := new(PlayerProfile)
//cacheMgr.GetPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {
// if playerProfile != nil {
// player2 = playerProfile
// } else {
// cb(ERR_CODE_USERS_NO_EXISTS, "AddFriendRequest user no exists")
// return
// }
//})
user1 := fm.GetUser(account1Id) user1 := fm.GetUser(account1Id)
user2 := fm.GetUser(account2Id) user2 := fm.GetUser(account2Id)
if user2 == nil { if user2 == nil {
user2 = fm.LoadUser(account2Id) user2 = fm.LoadUser(account2Id)
} }
found1 := user1.IsInBlacklist(account2Id) //found1 := user1.IsInBlacklist(account2Id)
found2 := user2.IsInBlacklist(account1Id) //found2 := user2.IsInBlacklist(account1Id)
if found1 || found2 { //if found1 || found2 {
cb(ERR_CODE_USER_IN_BLACKLIST, "AddFriendRequest user in blacklist") // cb(ERR_CODE_USER_IN_BLACKLIST, "AddFriendRequest user in blacklist")
return // return
} //}
// 已发送请求 // 已发送请求
friendRequest, ok := user1.FriendRequest[account2Id] friendship, exists := user1.Friendships[account2Id]
if ok && friendRequest.IsFriendship == 1 { if exists && friendship.IsFriendship == 0 {
cb(ERR_CODE_OK, "AddFriendRequest ok") cb(ERR_CODE_OK, "AddFriendRequest ok")
return return
} }
@ -96,18 +94,18 @@ func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb
cb(ERR_CODE_PENDING_REQUEST_IS_FULL, "AddFriendRequest pending request is full") cb(ERR_CODE_PENDING_REQUEST_IS_FULL, "AddFriendRequest pending request is full")
return return
} }
requestTime := time.Now().Unix()
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDefault), func(err error) { fm.upsertFriendShip(account1Id, account2Id, FriendshipStatusPending, requestTime, func(err error) {
if err != nil { if err != nil {
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AddFriendRequest update pending request db error") cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AddFriendRequest update friendship db error")
f5.GetSysLog().Info("AddFriendRequest update pending request db error") f5.GetSysLog().Info("AddFriendRequest update friendship db error")
return return
} }
user1.AddFriendRequest(account2Id) user1.AddFriendRequest(account2Id, FriendshipStatusPending, requestTime)
user2.AddFriendRequest(account1Id) user2.AddFriendRequest(account1Id, FriendshipStatusPending, requestTime)
// user2 profile add to cache // Load user2 profile to cache
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
cb(ERR_CODE_OK, "AddFriendRequest ok") cb(ERR_CODE_OK, "AddFriendRequest ok")
@ -132,43 +130,31 @@ func (fm *FriendsMgr) AcceptFriendRequest(account1Id string, account2Id string,
return return
} }
// step1. update reqs requestTime := time.Now().Unix()
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusOk), func(err error) { fm.upsertFriendShip(account1Id, account2Id, FriendshipStatusOK, requestTime, func(err error) {
if err != nil { if err != nil {
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AcceptFriendRequest update pending request db error") cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AcceptFriendRequest update friendship db error")
return return
} }
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusOk), func(err error) {
if err != nil {
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AcceptFriendRequest update pending request db error")
return
}
// step2. insert friendship
a1, a2 := SwapAccountIds(account1Id, account2Id)
fm.upsertFriendShip(a1, a2, FriendshipStatusOk, func(err error) {
if err != nil {
cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AcceptFriendRequest update friendship db error")
return
}
// step3. create a new friendship friendship1 := &Friendship{
friendship := &Friendship{ FriendAccountId: account2Id,
User1: fm.Users[account1Id], IsFriendship: FriendshipStatusOK,
User2: fm.Users[account2Id], RequestTime: time.Now().Unix(),
} }
user1.Friendships = append(user1.Friendships, friendship) user1.Friendships[account2Id] = friendship1
user2.Friendships = append(user2.Friendships, friendship)
// step4. remove requests
user1.RemoveFriendRequest(account2Id)
user2.RemoveFriendRequest(account1Id)
// step5. add cache friendship2 := &Friendship{
cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) FriendAccountId: account1Id,
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) IsFriendship: FriendshipStatusOK,
RequestTime: time.Now().Unix(),
}
user2.Friendships[account1Id] = friendship2
cb(ERR_CODE_OK, "") cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {})
}) cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
})
cb(ERR_CODE_OK, "")
}) })
} }
@ -183,8 +169,8 @@ func (fm *FriendsMgr) RejectFriendRequest(account1Id string, account2Id string,
// cb(ERR_CODE_USERS_NO_EXISTS, "RejectFriendRequest user no exists") // cb(ERR_CODE_USERS_NO_EXISTS, "RejectFriendRequest user no exists")
// return // return
//} //}
requestTime := time.Now().Unix()
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusReject), func(err error) { fm.upsertFriendShip(account2Id, account1Id, FriendshipStatusReject, requestTime, func(err error) {
if err != nil { if err != nil {
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "RejectFriendRequest update pending request db error") cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "RejectFriendRequest update pending request db error")
return return
@ -208,44 +194,18 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er
cb(ERR_CODE_USERS_NO_EXISTS, "DeleteFriendShip user no exists") cb(ERR_CODE_USERS_NO_EXISTS, "DeleteFriendShip user no exists")
return return
} }
user1.RemoveFriendShip(account2Id)
user2.RemoveFriendShip(account1Id)
var found bool updateFields := [][]string{{"is_friendship", q5.ToString(FriendshipStatusDeleted)}}
found, friendShipIndex := fm.FindFriendshipIndex(account1Id, account2Id) fm.updateFriendShip(account1Id, account2Id, updateFields, func(err error) {
if found { if err != nil {
// 删除好友请求, upsert 不存在则新增,存在则替换值 f5.GetSysLog().Info("DeleteFriendShip, updateFriendShip accountId:[%s-%s], db error:%s", account1Id, account2Id, err)
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDeleted), func(err error) { cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "DeleteFriendShip update friendship db error")
if err != nil { return
f5.GetSysLog().Info("DeleteFriendShip me, accountId:[%s-%s], db error:%s", account1Id, account2Id, err) }
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "DeleteFriendShip update pending request db error") cb(ERR_CODE_OK, "")
return })
}
user1.Friendships = append(user1.Friendships[:friendShipIndex], user1.Friendships[friendShipIndex+1:]...)
found2, friendShipIndex2 := fm.FindFriendshipIndex(account2Id, account1Id)
if found2 {
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusDeleted), func(err error) {
if err != nil {
f5.GetSysLog().Info("DeleteFriendShip, accountId:[%s-%s], db error:%s", account1Id, account2Id, err)
cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "DeleteFriendShip update pending request db error")
return
}
user2.Friendships = append(user2.Friendships[:friendShipIndex2], user2.Friendships[friendShipIndex2+1:]...)
// Delete friendship DB
a1, a2 := SwapAccountIds(account1Id, account2Id)
fields := [][]string{{"is_delete_friendship", q5.ToString(FriendshipStatusDeleted)}}
fm.updateFriendShip(a1, a2, fields, func(err error) {
if err != nil {
f5.GetSysLog().Info("DeleteFriendShip, updateFriendShip accountId:[%s-%s], db error:%s", account1Id, account2Id, err)
cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "DeleteFriendShip update friendship db error")
return
}
cb(ERR_CODE_OK, "")
})
})
}
})
}
} }
// AddBlacklist 加入黑名单 // AddBlacklist 加入黑名单
@ -316,7 +276,13 @@ func (fm *FriendsMgr) GetFriendCount(accountId string) int {
func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int { func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int {
user := fm.GetUser(accountId) user := fm.GetUser(accountId)
if user != nil { if user != nil {
return len(user.FriendRequest) count := 0
for _, friendship := range user.Friendships {
if friendship.IsFriendship == 0 {
count++
}
}
return count
} }
return 0 return 0
} }
@ -324,24 +290,20 @@ func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int {
func (fm *FriendsMgr) CleanFriendship(accountID string) { func (fm *FriendsMgr) CleanFriendship(accountID string) {
user := fm.GetUser(accountID) user := fm.GetUser(accountID)
if user != nil { if user != nil {
user.Friendships = make([]*Friendship, 0, MaxFriendMembers) user.Friendships = make(map[string]*Friendship, MaxFriendMembers)
} }
} }
func (fm *FriendsMgr) AddFriendshipToMap(accountID string, friendship *Friendship) { func (fm *FriendsMgr) AddFriendshipToMap(accountID string, friendship *Friendship) {
user := fm.GetUser(accountID) user := fm.GetUser(accountID)
user.Friendships = append(user.Friendships, friendship) user.Friendships[friendship.FriendAccountId] = friendship
} }
func (fm *FriendsMgr) GetFriendByAccountId(account1Id, account2Id string) *User { func (fm *FriendsMgr) GetFriendByAccountId(account1Id, account2Id string) *User {
user := fm.GetUser(account1Id) user := fm.GetUser(account1Id)
for _, friendship := range user.Friendships { friendship, exists := user.Friendships[account2Id]
if friendship.User1.AccountId == account2Id { if exists && friendship.IsFriendship == 1 {
return friendship.User1 return fm.GetUser(account2Id)
}
if friendship.User2.AccountId == account2Id {
return friendship.User2
}
} }
return nil return nil
} }
@ -349,6 +311,8 @@ func (fm *FriendsMgr) GetFriendByAccountId(account1Id, account2Id string) *User
func (fm *FriendsMgr) AddUser(accountId string, user *User) { func (fm *FriendsMgr) AddUser(accountId string, user *User) {
if _, exists := fm.Users[accountId]; !exists { if _, exists := fm.Users[accountId]; !exists {
fm.Users[accountId] = user fm.Users[accountId] = user
//fm.UserCount = len(fm.Users)
fm.UserCount++
} }
} }
@ -365,36 +329,15 @@ func (fm *FriendsMgr) LoadUser(accountId string) *User {
user := NewUser(accountId) user := NewUser(accountId)
friendMgr.AddUser(accountId, user) friendMgr.AddUser(accountId, user)
// 加载我的好友关系表 // 加载我的好友关系表
friendMgr.loadFriendships(user, [][]string{ friendMgr.loadUserFriendships(user, [][]string{
{"account1_id", user.AccountId}, {"account1_id", user.AccountId},
}) })
friendMgr.loadFriendships(user, [][]string{ friendMgr.loadUserFriendships(user, [][]string{
{"account2_id", user.AccountId}, {"account2_id", user.AccountId},
}) })
// 加载我的等待验证好友请求
friendMgr.loadPendingRequests(user)
// 加载我的黑名单列表 // 加载我的黑名单列表
friendMgr.loadBlacklist(user) // friendMgr.loadBlacklist(user)
return user return user
} }
return existsUser return existsUser
} }
func (fm *FriendsMgr) FindFriendshipIndex(account1Id, account2Id string) (bool, int) {
user := fm.GetUser(account1Id)
// 通常 account1Id,指自己, account2Id 指对方
User1Friendships := user.Friendships
for i, friendship := range User1Friendships {
if friendship.User1.AccountId == account2Id || friendship.User2.AccountId == account2Id {
return true, i
}
}
return false, -1
}
func SwapAccountIds(account1Id, account2Id string) (string, string) {
if strings.Compare(account1Id, account2Id) > 0 {
account1Id, account2Id = account2Id, account1Id
}
return account1Id, account2Id
}

View File

@ -93,8 +93,8 @@ func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendin
user := friendMgr.GetUser(accountId) user := friendMgr.GetUser(accountId)
rspMsg := &cs.SMListPendingFriendRequest{} rspMsg := &cs.SMListPendingFriendRequest{}
for targetAccountId, friendRequest := range user.FriendRequest { for targetAccountId, friendRequest := range user.Friendships {
if friendRequest.IsFriendship != FriendReqsStatusDefault { if friendRequest.IsFriendship != FriendshipStatusPending {
continue continue
} }
profile := cacheMgr.GetPlayerProfile(targetAccountId) profile := cacheMgr.GetPlayerProfile(targetAccountId)
@ -130,9 +130,9 @@ func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
rspMsg := &cs.SMListFriend{} rspMsg := &cs.SMListFriend{}
rspMsg.Users = make([]*cs.MFUser, 0) rspMsg.Users = make([]*cs.MFUser, 0)
for _, friendship := range user.Friendships { for _, friendship := range user.Friendships {
friendAccountId := friendship.User2.AccountId friendAccountId := friendship.FriendAccountId
if friendship.User1.AccountId != accountId { if friendAccountId == accountId || friendship.IsFriendship != FriendshipStatusOK {
friendAccountId = friendship.User1.AccountId continue
} }
playerProfile := cacheMgr.GetPlayerProfile(friendAccountId) playerProfile := cacheMgr.GetPlayerProfile(friendAccountId)
if playerProfile != nil { if playerProfile != nil {

View File

@ -92,13 +92,14 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt
Errcode int `json:"errcode"` Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"` Errmsg string `json:"errmsg"`
Info struct { Info struct {
Activated string `json:"activated"` Activated string `json:"activated"`
RenameCount string `json:"rename_count"` RenameCount string `json:"rename_count"`
AccountID string `json:"account_id"` AccountID string `json:"account_id"`
Name string `json:"name"` Name string `json:"name"`
Avatar string `json:"head_id"` Avatar string `json:"head_id"`
AvatarHead string `json:"head_frame"` AvatarHead string `json:"head_frame"`
Rank string `json:"rank"` Rank string `json:"rank"`
LastLoginTime string `json:"last_login_time"`
} `json:"info"` } `json:"info"`
}{} }{}
err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj) err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj)
@ -111,7 +112,6 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt
return return
} }
accountId := msg.GetAccountId() accountId := msg.GetAccountId()
player := Player{ player := Player{
socket: hdr.GetSocket(), socket: hdr.GetSocket(),
accountId: accountId, accountId: accountId,
@ -128,8 +128,8 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt
Avatar: q5.ToInt32(resObj.Info.Avatar), Avatar: q5.ToInt32(resObj.Info.Avatar),
AvatarHead: q5.ToInt32(resObj.Info.AvatarHead), AvatarHead: q5.ToInt32(resObj.Info.AvatarHead),
Rank: q5.ToInt32(resObj.Info.Rank), Rank: q5.ToInt32(resObj.Info.Rank),
OnlineStatus: 0, OnlineStatus: OnlineStatus,
LastLoginTime: 0, LastLoginTime: q5.ToInt32(resObj.Info.LastLoginTime),
} }
cacheMgr.SetProfile(accountId, playerProfile) cacheMgr.SetProfile(accountId, playerProfile)
// Add friends // Add friends

2
third_party/q5 vendored

@ -1 +1 @@
Subproject commit fe65489de27eef96e2c1879cb19dd84ee172be9c Subproject commit 2ce4c46c710cce553a5a059dda83fdf72a038a46