This commit is contained in:
殷勇 2023-08-23 16:15:34 +08:00
parent 3f1efa3ee4
commit 8165b22cb1
6 changed files with 414 additions and 469 deletions

View File

@ -23,12 +23,12 @@ UNIQUE KEY `friendship` (`account1_id`,`account2_id`)
COMMENT "已经成为好友"
;
drop table if exists `t_friend_pending_requests`;
CREATE TABLE t_friend_pending_requests (
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 '',
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 '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),

File diff suppressed because it is too large Load Diff

View File

@ -6,22 +6,29 @@ import (
"q5"
)
func (fm *FriendsMgr) insertFriendRequest(account1Id string, account2Id string) {
func (fm *FriendsMgr) insertFriendRequest(account1Id string, account2Id string, is_friendship string) {
fields := [][]string{
{"sender_account_id", account1Id},
{"receiver_account_id", account2Id},
{"is_friendship", is_friendship},
}
f5.GetJsStyleDb().Insert(
f5.GetJsStyleDb().Replace(
FRIEND_DB,
"t_friend_pending_requests",
"t_friend_pending_request",
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) {
fields := [][]string{
{"flag", q5.ToString(flag)},
func (fm *FriendsMgr) updateFriendRequest(account1Id string, account2Id string, fields [][]string) {
if len(fields) <= 0 {
return
}
where := [][]string{
{"sender_account_id", account1Id},
@ -29,29 +36,40 @@ func (fm *FriendsMgr) updateFriendRequest(account1Id string, account2Id string,
}
f5.GetJsStyleDb().Update(
FRIEND_DB,
"t_friend_pending_requests",
"t_friend_pending_request",
fields,
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) {
fields := [][]string{
{"user1_id", account1Id},
{"user2_id", account2Id},
{"account1_id", account1Id},
{"account2_id", account2Id},
}
f5.GetJsStyleDb().Insert(
FRIEND_DB,
"t_friend_ships",
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() {
fmt.Printf("loadUsers from db\n")
// Load DB users to struct FriendsMgr.user
fields := []string{"account_id", "name"}
f5.GetJsStyleDb().Select(
GAME_DB,
@ -64,11 +82,11 @@ func (fm *FriendsMgr) loadUsers() {
func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
if err != nil {
fmt.Printf("loadUsersFromDBResult err:%v \n", err)
fmt.Printf("loadUsersResult err:%v \n", err)
return
}
fm.users = make(map[string]*User)
fm.users = make(map[string]*User, 100)
for rows.Next() {
user := &User{
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.userCount = len(fm.users)
}
// loadFriendships 加载好友关系表
func (fm *FriendsMgr) loadFriendships() {
fmt.Printf("loadFriendships from db\n")
fields := []string{"account1_id", "account2_id"}
f5.GetJsStyleDb().Select(
FRIEND_DB,
@ -93,7 +111,7 @@ func (fm *FriendsMgr) loadFriendships() {
func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
if err != nil {
fmt.Printf("loadFriendshipsFromDBResult err:%v \n", err)
fmt.Printf("loadFriendshipsResult err:%v \n", err)
return
}
fm.friendships = make(map[string][]*Friendship)
@ -103,7 +121,6 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
if user1 == nil || user2 == nil {
continue
}
friendship := &Friendship{}
friendship.User1 = user1
friendship.User2 = user2
@ -112,16 +129,15 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
}
}
// loadPendingRequests 加载等待验证好友请求
func (fm *FriendsMgr) loadPendingRequests() {
fmt.Printf("loadPendingRequests from db\n")
fields := []string{"sender_account_id", "receiver_account_id"}
f5.GetJsStyleDb().Select(
FRIEND_DB,
"t_friend_pending_requests",
"t_friend_pending_request",
fields,
[][]string{
{"flag", "0"},
{"is_friendship", "0"},
},
fm.loadPendingRequestsResult,
)
@ -129,13 +145,14 @@ func (fm *FriendsMgr) loadPendingRequests() {
func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
if err != nil {
fmt.Printf("loadFriendshipsFromDBResult err:%v \n", err)
fmt.Printf("loadPendingRequestsResult err:%v \n", err)
return
}
fm.pendingReqs = make(map[string]map[string]bool, MaxPendingFriendReqs)
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] = true
}
}

View File

@ -16,7 +16,8 @@ type FriendsMgr struct {
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
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 {
@ -24,17 +25,8 @@ type SearchCache struct {
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() {
fm.userCount = 0
// 加载所有用户
fm.loadUsers()
// 加载好友关系表 列表
@ -49,6 +41,7 @@ func (fm *FriendsMgr) unInit() {
// 2. Struct Data Persist to DB
}
// 搜索指定用户
func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
if user, ok := fm.users[accountId]; ok {
return user
@ -56,6 +49,7 @@ func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
return nil
}
// 搜索用户
func (fm *FriendsMgr) searchUsers(keyword string) []*User {
if len(keyword) > SearchWord {
return nil
@ -97,40 +91,6 @@ func (fm *FriendsMgr) searchUsers(keyword string) []*User {
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 添加好友请求
func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) error {
_, exists1 := fm.users[account1Id]
@ -139,17 +99,11 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
return errors.New("users not exist")
}
// step1. check in blocked
for _, blockedAccountId := range fm.blockedUser[account1Id] {
if blockedAccountId == account2Id {
return errors.New("users in blocked")
}
}
for _, blockedAccountId := range fm.blockedUser[account2Id] {
if blockedAccountId == account2Id {
return errors.New("users in blocked")
}
err := fm.checkInBlackList(account1Id, account2Id)
if err != nil {
return err
}
// 已发送请求
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
return nil
@ -167,22 +121,28 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
return fmt.Errorf("player:%s, friends are full", account2Id)
}
// 为 account2Id 创建等待验证好友请求 记录,由 account1Id 申请的
if fm.pendingReqs[account2Id] == nil {
fm.pendingReqs[account2Id] = make(map[string]bool, 20)
if fm.pendingReqs == nil {
fm.pendingReqs = make(map[string]map[string]bool, 200)
}
if fm.pendingReqs[account1Id] == nil {
fm.pendingReqs[account1Id] = make(map[string]bool, MaxPendingFriendReqs)
}
fm.pendingReqs[account1Id][account2Id] = true
if fm.pendingReqs[account2Id] == nil {
fm.pendingReqs[account2Id] = make(map[string]bool, MaxPendingFriendReqs)
}
fm.pendingReqs[account2Id][account1Id] = true
// persist to db
fm.insertFriendRequest(account1Id, account2Id)
fm.insertFriendRequest(account1Id, account2Id, "0")
return nil
}
// acceptFriendRequest 接受好友请求
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")
}
@ -193,10 +153,9 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
return fmt.Errorf("player:%s, friends are full", account2Id)
}
// step1. update friend reqs
flag := 1
fm.updateFriendRequest(account1Id, account2Id, flag)
// step2. add t_friend_ships
// step1. update reqs
fm.insertFriendRequest(account2Id, account1Id, "1")
// step2. insert friendship
compareResult := strings.Compare(account1Id, account2Id)
if compareResult > 0 {
temp := account1Id
@ -220,15 +179,18 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
// rejectFriendRequest 拒绝好友请求
func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error {
if fm.pendingReqs[account1Id] == nil {
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")
}
flag := 2
fm.updateFriendRequest(account1Id, account2Id, flag)
//fields := [][]string{{"is_friendship", q5.ToString(2)}}
//fm.updateFriendRequest(account1Id, account2Id, fields)
// 申请表,申请者,目标者,
fm.insertFriendRequest(account2Id, account1Id, "2")
delete(fm.pendingReqs[account1Id], account2Id)
delete(fm.pendingReqs[account2Id], account1Id)
@ -305,6 +267,40 @@ func (fm *FriendsMgr) listFriend(accountId string) []*User {
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) {
if fm.friendships[accountID] == nil {
fm.friendships[accountID] = []*Friendship{friendship}
@ -337,8 +333,8 @@ func PrintUsers(str string, userList []*User) {
}
func (fm *FriendsMgr) findBlockedUserIndex(Account1Id, Account2Id string) int {
if fm.blockedUser[Account1Id] != nil {
for i, blockedAccountId := range fm.blockedUser[Account1Id] {
if fm.blackList[Account1Id] != nil {
for i, blockedAccountId := range fm.blackList[Account1Id] {
if blockedAccountId == Account2Id {
return i
}
@ -367,3 +363,17 @@ func (fm *FriendsMgr) getUser(accountId string) *User {
}
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
}

View File

@ -34,11 +34,11 @@ func (p *Player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
// CMSearchUserByAccountId 搜索指定用户
func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
rspMsg := new(cs.SMSearchUserByAccountId)
user := friendMgr.searchByAccountId(msg.GetSearchKeyword())
user := friendMgr.searchByAccountId(msg.GetAccountId())
if user != nil {
rspMsg.AccountId = &user.AccountId
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)
}
@ -91,13 +91,12 @@ func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendReq
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.acceptFriendRequest(user1Id, user2Id)
err := friendMgr.rejectFriendRequest(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMAcceptFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
f5.GetSysLog().Info("CMRejectFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
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) {
rspMsg := &cs.SMListPendingFriendRequest{}
accountId := p.accountId
for accountId, flag := range friendMgr.pendingReqs[accountId] {
row := &cs.MFPendingFriendRequest{
AccountId: &accountId,
Flag: &flag,
}
rspMsg.PendingFriendRequest = append(rspMsg.PendingFriendRequest, row)
resAccountIds := make([]string, MaxPendingFriendReqs)
for accountId, _ := range friendMgr.pendingReqs[accountId] {
resAccountIds = append(resAccountIds, accountId)
}
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)
}

View File

@ -133,7 +133,7 @@ message SMSearchUser
// 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
{
repeated MFPendingFriendRequest pendingFriendRequest = 1;
repeated string accountIds = 1;
}
//
@ -223,10 +223,6 @@ message MFUser {
optional string account_id = 1;
optional string username = 2;
}
message MFPendingFriendRequest {
optional string account_id = 1;
optional bool flag = 2;
}
//
message CMGuildInfo