380 lines
10 KiB
Go
380 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"cs"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
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]true
|
|
blackList map[string][]string // AccountId -> 黑名单列表 []AccountIds
|
|
userCount int // 用户总数
|
|
}
|
|
|
|
type SearchCache struct {
|
|
Users []*User
|
|
LastModified time.Time
|
|
}
|
|
|
|
func (fm *FriendsMgr) init() {
|
|
fm.userCount = 0
|
|
// 加载所有用户
|
|
fm.loadUsers()
|
|
// 加载好友关系表 列表
|
|
fm.loadFriendships()
|
|
// 加载等待验证好友请求 列表
|
|
fm.loadPendingRequests()
|
|
fm.searchCaches = make(map[string]SearchCache)
|
|
}
|
|
|
|
func (fm *FriendsMgr) unInit() {
|
|
// 1. Loop struct data
|
|
// 2. Struct Data Persist to DB
|
|
}
|
|
|
|
// 搜索指定用户
|
|
func (fm *FriendsMgr) searchByAccountId(accountId string) *User {
|
|
if user, ok := fm.users[accountId]; ok {
|
|
return user
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 搜索用户
|
|
func (fm *FriendsMgr) searchUsers(keyword string) []*User {
|
|
if len(keyword) > SearchWord {
|
|
return nil
|
|
}
|
|
|
|
if cachedResult, ok := fm.searchCaches[keyword]; ok {
|
|
if time.Since(cachedResult.LastModified) <= 10*time.Hour {
|
|
return cachedResult.Users
|
|
}
|
|
}
|
|
|
|
maxUsers := 20
|
|
listFriend := make([]*User, 0, maxUsers)
|
|
lowercaseQuery := strings.ToLower(keyword)
|
|
for _, u := range fm.users {
|
|
if strings.Contains(strings.ToLower(u.Username), lowercaseQuery) {
|
|
uEntity := &User{
|
|
AccountId: u.AccountId,
|
|
Username: u.Username,
|
|
}
|
|
listFriend = append(listFriend, uEntity)
|
|
}
|
|
// 显示搜索结果 20条
|
|
if len(listFriend) >= maxUsers {
|
|
break
|
|
}
|
|
}
|
|
|
|
// Add to caches
|
|
fm.searchCaches[keyword] = SearchCache{
|
|
Users: listFriend,
|
|
LastModified: time.Now(),
|
|
}
|
|
fm.clearExpiredCaches()
|
|
|
|
// Print result:
|
|
PrintUsers("Search result:", listFriend)
|
|
|
|
return listFriend
|
|
}
|
|
|
|
// addFriendRequest 添加好友请求
|
|
func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) error {
|
|
_, exists1 := fm.users[account1Id]
|
|
_, exists2 := fm.users[account2Id]
|
|
if !exists1 || !exists2 {
|
|
return errors.New("users not exist")
|
|
}
|
|
|
|
err := fm.checkInBlackList(account1Id, account2Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 已发送请求
|
|
if _, ok := fm.pendingReqs[account1Id][account2Id]; ok {
|
|
return nil
|
|
}
|
|
// 自己好友已满
|
|
if fm.getFriendCount(account1Id) >= MaxFriendMembers {
|
|
return fmt.Errorf("player:%s, friends are full", account1Id)
|
|
}
|
|
// 对方好友已满
|
|
if fm.getFriendCount(account2Id) >= MaxFriendMembers {
|
|
return fmt.Errorf("player:%s, friends are full", account2Id)
|
|
}
|
|
// 对方待请求已满
|
|
if fm.getFriendRequestCount(account2Id) >= MaxPendingFriendReqs {
|
|
return fmt.Errorf("player:%s, friends are full", account2Id)
|
|
}
|
|
|
|
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, "0")
|
|
|
|
return nil
|
|
}
|
|
|
|
// acceptFriendRequest 接受好友请求
|
|
func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) error {
|
|
if _, ok := fm.pendingReqs[account1Id][account2Id]; !ok {
|
|
return errors.New("no pending friend request from account1Id to account2Id")
|
|
}
|
|
|
|
if fm.getFriendCount(account1Id) >= MaxFriendMembers {
|
|
return fmt.Errorf("player:%s, friends are full", account1Id)
|
|
}
|
|
if fm.getFriendCount(account2Id) >= MaxFriendMembers {
|
|
return fmt.Errorf("player:%s, friends are full", account2Id)
|
|
}
|
|
|
|
// step1. update reqs
|
|
fm.insertFriendRequest(account2Id, account1Id, "1")
|
|
// step2. insert friendship
|
|
compareResult := strings.Compare(account1Id, account2Id)
|
|
if compareResult > 0 {
|
|
temp := account1Id
|
|
account1Id = account2Id
|
|
account2Id = temp
|
|
}
|
|
fm.insertFriendShip(account1Id, account2Id)
|
|
|
|
// Create a new friendship
|
|
friendship := &Friendship{
|
|
User1: fm.users[account1Id],
|
|
User2: fm.users[account2Id],
|
|
}
|
|
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)
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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 {
|
|
return errors.New("no pending friend request from user1 to user2")
|
|
}
|
|
|
|
//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)
|
|
|
|
return nil
|
|
}
|
|
|
|
// deleteFriendShip 删除好友
|
|
func (fm *FriendsMgr) deleteFriendShip(account1ID, account2ID string) error {
|
|
fm.mu.Lock()
|
|
defer fm.mu.Unlock()
|
|
|
|
user1Friendships := fm.friendships[account1ID]
|
|
user2Friendships := fm.friendships[account2ID]
|
|
|
|
var found bool
|
|
for i, friendship := range user1Friendships {
|
|
if friendship.User1.Username == account2ID || friendship.User2.Username == account2ID {
|
|
fm.friendships[account1ID] = append(user1Friendships[:i], user1Friendships[i+1:]...)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return errors.New("friendship not found")
|
|
}
|
|
|
|
for i, friendship := range user2Friendships {
|
|
if friendship.User1.Username == account1ID || friendship.User2.Username == account1ID {
|
|
fm.friendships[account2ID] = append(user2Friendships[:i], user2Friendships[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// getFriendCount 好友数量
|
|
func (fm *FriendsMgr) getFriendCount(accountId string) int {
|
|
if _, ok := fm.friendships[accountId]; ok {
|
|
return len(fm.friendships[accountId])
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// getFriendRequestCount 等待请求数量
|
|
func (fm *FriendsMgr) getFriendRequestCount(accountId string) int {
|
|
if _, ok := fm.pendingReqs[accountId]; ok {
|
|
return len(fm.pendingReqs[accountId])
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// listFriend 我的好友列表
|
|
func (fm *FriendsMgr) listFriend(accountId string) []*User {
|
|
// By default, Users member data count:10
|
|
var users []*User
|
|
for _, friendship := range fm.friendships[accountId] {
|
|
if friendship.User1.AccountId != accountId {
|
|
uEntity := &User{
|
|
AccountId: friendship.User1.AccountId,
|
|
Username: friendship.User1.Username,
|
|
}
|
|
users = append(users, uEntity)
|
|
} else {
|
|
uEntity := &User{
|
|
AccountId: friendship.User2.AccountId,
|
|
Username: friendship.User2.Username,
|
|
}
|
|
users = append(users, uEntity)
|
|
}
|
|
}
|
|
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}
|
|
} else {
|
|
fm.friendships[accountID] = append(fm.friendships[accountID], friendship)
|
|
}
|
|
}
|
|
|
|
func (fm *FriendsMgr) registerUser(accountId string, username string) error {
|
|
if fm.users[accountId] == nil {
|
|
fm.users[accountId] = &User{AccountId: accountId, Username: username}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("user exists")
|
|
}
|
|
|
|
func (fm *FriendsMgr) clearExpiredCaches() {
|
|
expirationTime := time.Now().Add(-24 * time.Hour)
|
|
for key, entry := range fm.searchCaches {
|
|
if entry.LastModified.Before(expirationTime) {
|
|
delete(fm.searchCaches, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func PrintUsers(str string, userList []*User) {
|
|
for _, user := range userList {
|
|
fmt.Printf("[%s]:accountId:%s, username:%s \n", str, user.AccountId, user.Username)
|
|
}
|
|
}
|
|
|
|
func (fm *FriendsMgr) findBlockedUserIndex(Account1Id, Account2Id string) int {
|
|
if fm.blackList[Account1Id] != nil {
|
|
for i, blockedAccountId := range fm.blackList[Account1Id] {
|
|
if blockedAccountId == Account2Id {
|
|
return i
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1
|
|
}
|
|
|
|
func (fm *FriendsMgr) findFriendShipIndex(Account1Id, Account2Id string) int {
|
|
// 通常 account1Id,指自己, account2Id 指对方
|
|
if _, exists := fm.friendships[Account1Id]; exists {
|
|
for i, friendship := range fm.friendships[Account1Id] {
|
|
if friendship.User2.AccountId == Account2Id {
|
|
return i
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1
|
|
}
|
|
|
|
func (fm *FriendsMgr) getUser(accountId string) *User {
|
|
if user, ok := fm.users[accountId]; ok {
|
|
return 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
|
|
}
|