This commit is contained in:
aozhiwei 2023-09-13 16:08:52 +08:00
commit f3029951ed
7 changed files with 190 additions and 189 deletions

View File

@ -98,6 +98,8 @@ const (
ERR_CODE_USER_IN_BLACKLIST = 11009
ERR_CODE_USER_NOT_IN_BLACKLIST = 11010
ERR_CODE_BLACKLIST_FULL = 11011
ERR_CODE_SEARCH_USERS_SIZE_FULL = 11012
ERR_CODE_SEARCH_USERS_DB_FAIL = 11013
// Guild
ERR_CODE_GUILD_NO_EXISTS = 10001

View File

@ -1,6 +1,5 @@
package main
// User 用户实体
type User struct {
AccountId string
FriendRequest map[string]*FriendRequest // targetAccountId -> FriendRequest
@ -8,14 +7,6 @@ type User struct {
Friendships []*Friendship
}
type SearchUser struct {
AccountId string
Username string
Avatar int32
AvatarHead int32
Rank int32
}
// Friendship user1, user2 构成一个好友关系
type Friendship struct {
User1 *User

View File

@ -118,12 +118,20 @@ func (fm *FriendsMgr) findPlayer(accountId string, cb func(err error, profile *P
}
profile := &PlayerProfile{}
if rows.Next() {
aId := q5.ToString(*rows.GetByIndex(0))
player := playerMgr.GetPlayerByAccountId(aId)
var onlineStatue int32 = 0
if player != nil {
onlineStatue = 1
}
profile = &PlayerProfile{
AccountId: q5.ToString(*rows.GetByIndex(0)),
Username: q5.ToString(*rows.GetByIndex(1)),
Avatar: q5.ToInt32(*rows.GetByIndex(2)),
AvatarHead: q5.ToInt32(*rows.GetByIndex(3)),
Rank: q5.ToInt32(*rows.GetByIndex(4)),
AccountId: q5.ToString(*rows.GetByIndex(0)),
Username: q5.ToString(*rows.GetByIndex(1)),
Avatar: q5.ToInt32(*rows.GetByIndex(2)),
AvatarHead: q5.ToInt32(*rows.GetByIndex(3)),
Rank: q5.ToInt32(*rows.GetByIndex(4)),
LastLoginTime: q5.ToInt32(*rows.GetByIndex(5)),
OnlineStatus: onlineStatue,
}
}
cb(nil, profile)
@ -158,12 +166,20 @@ func (fm *FriendsMgr) findUsersByUsername(username string, sinceId int64, cb fun
if autoId > lastId {
lastId = autoId
}
accountId := q5.ToString(*rows.GetByIndex(1))
player := playerMgr.GetPlayerByAccountId(accountId)
var onlineStatue int32 = 0
if player != nil {
onlineStatue = 1
}
profile := &PlayerProfile{
AccountId: q5.ToString(*rows.GetByIndex(1)),
Username: q5.ToString(*rows.GetByIndex(2)),
Avatar: q5.ToInt32(*rows.GetByIndex(3)),
AvatarHead: q5.ToInt32(*rows.GetByIndex(4)),
Rank: q5.ToInt32(*rows.GetByIndex(5)),
AccountId: q5.ToString(*rows.GetByIndex(1)),
Username: q5.ToString(*rows.GetByIndex(2)),
Avatar: q5.ToInt32(*rows.GetByIndex(3)),
AvatarHead: q5.ToInt32(*rows.GetByIndex(4)),
Rank: q5.ToInt32(*rows.GetByIndex(5)),
LastLoginTime: q5.ToInt32(*rows.GetByIndex(6)),
OnlineStatus: onlineStatue,
}
profiles = append(profiles, profile)
}
@ -190,21 +206,19 @@ func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) {
if user.AccountId == account1Id {
user2 := NewUser(account2Id)
friendMgr.AddUser(account2Id, user2)
friendship1 := &Friendship{}
friendship1.User1 = user
friendship1.User2 = user2
fm.AddFriendshipToMap(user.AccountId, friendship1)
cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {})
} else {
}
if user.AccountId == account2Id {
user2 := NewUser(account1Id)
friendMgr.AddUser(account1Id, user2)
//friendMgr.AddUser(account1Id, user2)
friendship := &Friendship{}
friendship.User1 = user
friendship.User2 = user2
fm.AddFriendshipToMap(user.AccountId, friendship)
fm.AddFriendshipToMap(account2Id, friendship)
cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {})
}
}

View File

@ -40,12 +40,12 @@ func (fm *FriendsMgr) SearchByAccountId(accountId string, cb func(errCode int32,
func (fm *FriendsMgr) SearchUsers(accountId, username string, sinceId int64, cb func(errCode int32, errMsg string, lastId int64, listFriend []*PlayerProfile)) {
var profiles = make([]*PlayerProfile, 0, MaxSearchResults)
if len(username) > SearchWord {
cb(1, "xxx", 0, profiles)
cb(ERR_CODE_SEARCH_USERS_SIZE_FULL, "SearchUsers username size full", 0, profiles)
return
}
fm.findUsersByUsername(username, sinceId, func(err error, lastId int64, profiles []*PlayerProfile) {
if err != nil {
cb(1, "xxx", 0, nil)
cb(ERR_CODE_SEARCH_USERS_DB_FAIL, "SearchUsers username db error", 0, nil)
return
}
cb(0, "OK", lastId, profiles)
@ -176,10 +176,13 @@ func (fm *FriendsMgr) AcceptFriendRequest(account1Id string, account2Id string,
func (fm *FriendsMgr) RejectFriendRequest(account1Id string, account2Id string, cb func(errCode int32, errMsg string)) {
User1 := fm.GetUser(account1Id)
user2 := fm.GetUser(account2Id)
if User1 == nil || user2 == nil {
cb(ERR_CODE_USERS_NO_EXISTS, "RejectFriendRequest user no exists")
return
if user2 == nil {
user2 = fm.LoadUser(account2Id)
}
//if User1 == nil || user2 == nil {
// cb(ERR_CODE_USERS_NO_EXISTS, "RejectFriendRequest user no exists")
// return
//}
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusReject), func(err error) {
if err != nil {
@ -197,6 +200,10 @@ func (fm *FriendsMgr) RejectFriendRequest(account1Id string, account2Id string,
func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(errCode int32, errMsg string)) {
user1 := fm.GetUser(account1Id)
user2 := fm.GetUser(account2Id)
if user2 == nil {
user2 = fm.LoadUser(account2Id)
}
if user1 == nil || user2 == nil {
cb(ERR_CODE_USERS_NO_EXISTS, "DeleteFriendShip user no exists")
return
@ -208,14 +215,17 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er
// 删除好友请求, upsert 不存在则新增,存在则替换值
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDeleted), func(err error) {
if err != nil {
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")
return
}
user1.Friendships = append(user1.Friendships[:friendShipIndex], user1.Friendships[friendShipIndex+1:]...)
found2, friendShipIndex2 := fm.FindFriendshipIndex(account2Id, account1Id)
if found2 {
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDeleted), func(err error) {
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
}
@ -225,6 +235,7 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er
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
}
@ -237,26 +248,6 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er
}
}
// ListFriends 我的好友列表
func (fm *FriendsMgr) ListFriends(accountId string) []*User {
user := fm.GetUser(accountId)
var users []*User
for _, friendship := range user.Friendships {
if friendship.User1.AccountId != accountId {
uEntity := &User{
AccountId: friendship.User1.AccountId,
}
users = append(users, uEntity)
} else {
uEntity := &User{
AccountId: friendship.User2.AccountId,
}
users = append(users, uEntity)
}
}
return users
}
// AddBlacklist 加入黑名单
func (fm *FriendsMgr) AddBlacklist(account1Id string, account2Id string, cb func(errCode int32, errMsg string)) {
User1 := fm.GetUser(account1Id)
@ -330,6 +321,13 @@ func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int {
return 0
}
func (fm *FriendsMgr) CleanFriendship(accountID string) {
user := fm.GetUser(accountID)
if user != nil {
user.Friendships = make([]*Friendship, 0, MaxFriendMembers)
}
}
func (fm *FriendsMgr) AddFriendshipToMap(accountID string, friendship *Friendship) {
user := fm.GetUser(accountID)
user.Friendships = append(user.Friendships, friendship)
@ -354,25 +352,6 @@ func (fm *FriendsMgr) AddUser(accountId string, user *User) {
}
}
func (fm *FriendsMgr) LoadUser(accountId string) *User {
user := NewUser(accountId)
friendMgr.AddUser(accountId, user)
// 加载我的好友关系表
friendMgr.loadFriendships(user, [][]string{
{"account1_id", user.AccountId},
})
friendMgr.loadFriendships(user, [][]string{
{"account2_id", user.AccountId},
})
// 加载我的等待验证好友请求
friendMgr.loadPendingRequests(user)
// 加载我的黑名单列表
friendMgr.loadBlacklist(user)
return user
}
func (fm *FriendsMgr) GetUser(accountId string) *User {
if user, ok := fm.Users[accountId]; ok {
return user
@ -380,6 +359,27 @@ func (fm *FriendsMgr) GetUser(accountId string) *User {
return nil
}
func (fm *FriendsMgr) LoadUser(accountId string) *User {
existsUser, exists := fm.Users[accountId]
if !exists {
user := NewUser(accountId)
friendMgr.AddUser(accountId, user)
// 加载我的好友关系表
friendMgr.loadFriendships(user, [][]string{
{"account1_id", user.AccountId},
})
friendMgr.loadFriendships(user, [][]string{
{"account2_id", user.AccountId},
})
// 加载我的等待验证好友请求
friendMgr.loadPendingRequests(user)
// 加载我的黑名单列表
friendMgr.loadBlacklist(user)
return user
}
return existsUser
}
func (fm *FriendsMgr) FindFriendshipIndex(account1Id, account2Id string) (bool, int) {
user := fm.GetUser(account1Id)
// 通常 account1Id,指自己, account2Id 指对方

View File

@ -70,11 +70,12 @@ func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByA
rspMsg.Errmsg = &errMsg
} else {
user := &cs.MFUser{
AccountId: &playerProfile.AccountId,
Username: &playerProfile.Username,
Avatar: &playerProfile.Avatar,
AvatarHead: &playerProfile.AvatarHead,
Rank: &playerProfile.Rank,
AccountId: &playerProfile.AccountId,
Username: &playerProfile.Username,
Avatar: &playerProfile.Avatar,
AvatarHead: &playerProfile.AvatarHead,
Rank: &playerProfile.Rank,
LastLoginTime: &playerProfile.LastLoginTime,
}
rspMsg.Users = user
}
@ -82,6 +83,111 @@ func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByA
})
}
// CMListPendingFriendRequest 等待验证的好友请求
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
var guildId int64 = 0
var guildName = ""
accountId := p.accountId
user := friendMgr.GetUser(accountId)
rspMsg := &cs.SMListPendingFriendRequest{}
for targetAccountId, friendRequest := range user.FriendRequest {
if friendRequest.IsFriendship != FriendReqsStatusDefault {
continue
}
profile := cacheMgr.GetPlayerProfile(targetAccountId)
if profile == nil {
continue
}
pendingUser := &cs.MFUser{
AccountId: &profile.AccountId,
Username: &profile.Username,
Avatar: &profile.Avatar,
AvatarHead: &profile.AvatarHead,
GuildId: &guildId,
GuildName: &guildName,
Rank: &profile.Rank,
OnlineStatus: &profile.OnlineStatus,
LastLoginTime: &profile.LastLoginTime,
}
rspMsg.Users = append(rspMsg.Users, pendingUser)
}
f5.GetSysLog().Info("CMListPendingFriendRequest count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMListFriend 我的好友列表
func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
var guildId int64 = 0
var guildName = ""
accountId := p.accountId
user := friendMgr.GetUser(accountId)
rspMsg := &cs.SMListFriend{}
rspMsg.Users = make([]*cs.MFUser, 0)
for _, friendship := range user.Friendships {
friendAccountId := friendship.User2.AccountId
if friendship.User1.AccountId != accountId {
friendAccountId = friendship.User1.AccountId
}
playerProfile := cacheMgr.GetPlayerProfile(friendAccountId)
if playerProfile != nil {
friend := &cs.MFUser{
AccountId: &friendAccountId,
Username: &playerProfile.Username,
Avatar: &playerProfile.Avatar,
AvatarHead: &playerProfile.AvatarHead,
GuildId: &guildId,
GuildName: &guildName,
Rank: &playerProfile.Rank,
OnlineStatus: &playerProfile.OnlineStatus,
LastLoginTime: &playerProfile.LastLoginTime,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
}
f5.GetSysLog().Info("CMListFriend finished accountId:%s, friends count:%d\n", accountId, len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMBlacklist 黑名单列表
func (p *Player) CMBlacklist(hdr *f5.MsgHdr, msg *cs.CMBlacklist) {
var guildId int64 = 0
var guildName = ""
rspMsg := &cs.SMBlacklist{}
accountId := p.accountId
user := friendMgr.GetUser(accountId)
for _, blackList := range user.FriendBlackList {
if blackList.IsRemoved != 0 {
continue
}
profile := cacheMgr.GetPlayerProfile(blackList.AccountId)
if profile == nil {
continue
}
userEntity := &cs.MFUser{
AccountId: &blackList.AccountId,
Username: &profile.Username,
Avatar: &profile.Avatar,
AvatarHead: &profile.AvatarHead,
GuildId: &guildId,
GuildName: &guildName,
Rank: &profile.Rank,
OnlineStatus: &profile.OnlineStatus,
LastLoginTime: &profile.LastLoginTime,
}
rspMsg.Users = append(rspMsg.Users, userEntity)
}
// TODO debug print users
f5.GetSysLog().Info("CMBlacklist users count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMAddFriendRequest 添加好友请求
func (p *Player) CMAddFriendRequest(hdr *f5.MsgHdr, msg *cs.CMAddFriendRequest) {
user1Id := p.accountId
@ -130,73 +236,6 @@ func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendReq
})
}
// CMListPendingFriendRequest 等待验证的好友请求
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
var defaultValue int32 = 0
var guildId int64 = 0
var guildName = ""
accountId := p.accountId
user := friendMgr.GetUser(accountId)
rspMsg := &cs.SMListPendingFriendRequest{}
for targetAccountId, _ := range user.FriendRequest {
profile := cacheMgr.GetPlayerProfile(targetAccountId)
pendingUser := &cs.MFUser{
AccountId: &profile.AccountId,
Username: &profile.Username,
Avatar: &profile.Avatar,
AvatarHead: &profile.AvatarHead,
GuildId: &guildId,
GuildName: &guildName,
Rank: &profile.Rank,
OnlineStatus: &defaultValue,
LastLoginTime: &defaultValue,
}
rspMsg.Users = append(rspMsg.Users, pendingUser)
}
f5.GetSysLog().Info("CMListPendingFriendRequest count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMListFriend 我的好友列表
func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
var defaultValue int32 = 0
var guildId int64 = 0
var guildName = ""
rspMsg := &cs.SMListFriend{}
accountId := p.accountId
user := friendMgr.GetUser(accountId)
for _, friendship := range user.Friendships {
friendAccountId := friendship.User2.AccountId
if friendship.User1.AccountId != accountId {
friendAccountId = friendship.User1.AccountId
}
playerProfile := cacheMgr.GetPlayerProfile(friendAccountId)
if playerProfile != nil {
friend := &cs.MFUser{
AccountId: &friendship.User1.AccountId,
Username: &playerProfile.Username,
Avatar: &playerProfile.Avatar,
AvatarHead: &playerProfile.AvatarHead,
GuildId: &guildId,
GuildName: &guildName,
Rank: &playerProfile.Rank,
OnlineStatus: &defaultValue,
LastLoginTime: &defaultValue,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
}
f5.GetSysLog().Info("CMListFriend friends count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMDeleteFriendShip 删除好友
func (p *Player) CMDeleteFriendShip(hdr *f5.MsgHdr, msg *cs.CMDeleteFriendShip) {
user1Id := p.accountId
@ -210,37 +249,6 @@ func (p *Player) CMDeleteFriendShip(hdr *f5.MsgHdr, msg *cs.CMDeleteFriendShip)
})
}
// CMBlacklist 黑名单列表
func (p *Player) CMBlacklist(hdr *f5.MsgHdr, msg *cs.CMBlacklist) {
var defaultValue int32 = 0
var guildId int64 = 0
var guildName = ""
rspMsg := &cs.SMBlacklist{}
accountId := p.accountId
user := friendMgr.GetUser(accountId)
for _, blackList := range user.FriendBlackList {
if blackList.IsRemoved != 0 {
continue
}
//targetUser := friendMgr.GetUser(blackList.AccountId)
userEntity := &cs.MFUser{
AccountId: &blackList.AccountId,
Avatar: &defaultValue,
AvatarHead: &defaultValue,
GuildId: &guildId,
GuildName: &guildName,
Rank: &defaultValue,
OnlineStatus: &defaultValue,
LastLoginTime: &defaultValue,
}
rspMsg.Users = append(rspMsg.Users, userEntity)
}
// TODO debug print users
f5.GetSysLog().Info("CMBlacklist users count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMAddBlacklist 加入黑名单
func (p *Player) CMAddBlacklist(hdr *f5.MsgHdr, msg *cs.CMAddBlacklist) {
user1Id := p.accountId
@ -273,20 +281,6 @@ func (p *Player) CMRemoveBlacklist(hdr *f5.MsgHdr, msg *cs.CMRemoveBlacklist) {
})
}
// CMFriendInfo 我的好友信息
func (p *Player) CMFriendInfo(hdr *f5.MsgHdr, msg *cs.CMFriendInfo) {
rspMsg := new(cs.SMFriendInfo)
accountId := p.accountId
// friendships
friends := friendMgr.ListFriends(accountId)
for _, friend := range friends {
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, friend.AccountId)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMSendChatMsg 发送聊天消息
func (p *Player) CMSendChatMsg(hdr *f5.MsgHdr, msg *cs.CMSendChatMsg) {
chatChannel := *msg.ChatChannel

View File

@ -131,8 +131,8 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt
OnlineStatus: 0,
LastLoginTime: 0,
}
cacheMgr.SetProfile(accountId, playerProfile)
// Add friends
friendMgr.LoadUser(accountId)
serverInfo := "192.168.100.39:2000"

2
third_party/f5 vendored

@ -1 +1 @@
Subproject commit 0317dc9f5e4c94cc19a6a868bda5ad2def64051f
Subproject commit 386a55fcfaed2871c5deaa777b94b2a6f7ee55d6