game2006go/server/imserver/friendsdbmgr.go
2024-02-16 19:26:56 +08:00

269 lines
7.1 KiB
Go

package main
import (
"f5"
"fmt"
"q5"
"strings"
)
func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isFriendship int, requestTime int64, cb func(error)) {
account1Id, account2Id = SwapAccountIds(account1Id, account2Id)
where := [][]string{
{"account1_id", account1Id},
{"account2_id", account2Id},
}
insertKv := [][]string{
{"account1_id", account1Id},
{"account2_id", account2Id},
{"is_friendship", q5.ToString(isFriendship)},
{"createtime", q5.ToString(requestTime)},
{"modifytime", q5.ToString(requestTime)},
}
updateKv := [][]string{
{"is_friendship", q5.ToString(isFriendship)},
{"modifytime", q5.ToString(requestTime)},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_friend_ships",
where,
updateKv,
insertKv,
func(err error, lastInsertId int64, rowsAffected int64) {
cb(err)
},
)
}
func (fm *FriendsMgr) updateFriendShip(account1Id string, account2Id string, fields [][]string, cb func(error)) {
account1Id, account2Id = SwapAccountIds(account1Id, account2Id)
where := [][]string{
{"account1_id", account1Id},
{"account2_id", account2Id},
}
f5.GetJsStyleDb().Update(
FRIEND_DB,
"t_friend_ships",
fields,
where,
func(err error, lastInsertId int64, rowsAffected int64) {
cb(err)
},
)
}
func (fm *FriendsMgr) upsertBlacklist(account1Id string, account2Id string, isRemoved int, cb func(error)) {
where := [][]string{
{"account_id", account1Id},
}
insertKv := [][]string{
{"account_id", account1Id},
{"blocked_account_id", account2Id},
{"is_removed", q5.ToString(isRemoved)},
}
updateKv := [][]string{
{"is_removed", q5.ToString(isRemoved)},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_friend_blacklist",
where,
updateKv,
insertKv,
func(err error, lastInsertId int64, rowsAffected int64) {
cb(err)
//if err != nil {
// fmt.Printf("error:%v\n", err)
//}
//fmt.Printf("lastInsertId:%d\n", lastInsertId)
//fmt.Printf("rowsAffected:%d\n", rowsAffected)
},
)
}
func (fm *FriendsMgr) findUsersByUsername(username string, sinceId int64, cb func(err error, lastId int64, profiles []*PlayerProfile)) {
fields := []string{"idx", "account_id", "name", "head_id", "head_frame", "star_num", "rank", "last_login_time"}
var where [][]string
usernameLike := fmt.Sprintf("%%%s%%", username)
likeWhere := [][]string{
{"name", usernameLike},
}
f5.GetJsStyleDb().SelectLike(
GAME_DB,
"t_user",
fields,
where,
likeWhere,
sinceId,
MaxSearchResults,
func(err error, rows *f5.DataSet) {
if err != nil {
cb(err, 0, nil)
return
}
lastId := sinceId
profiles := make([]*PlayerProfile, 0, MaxSearchResults)
for rows.Next() {
autoId := q5.ToInt64(rows.GetByIndex(0))
if autoId > lastId {
lastId = autoId
}
accountId := q5.ToString(rows.GetByIndex(1))
onlineStatus := playerMgr.GetOnlineStatus(accountId)
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)),
Star: q5.ToInt32(rows.GetByIndex(5)),
Rank: q5.ToInt32(rows.GetByIndex(6)),
LastLoginTime: q5.ToInt32(rows.GetByIndex(7)),
OnlineStatus: onlineStatus,
}
profiles = append(profiles, profile)
}
cb(nil, lastId, profiles)
},
)
}
// loadFriendships 加载好友关系表
func (fm *FriendsMgr) loadFriendships() {
fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"}
var where [][]string
f5.GetJsStyleDb().Select(
FRIEND_DB,
"t_friend_ships",
fields,
where,
func(err error, rows *f5.DataSet) {
if err != nil {
panic(err)
}
userMap := make(map[string]*User)
for rows.Next() {
account1Id := q5.ToString(rows.GetByIndex(0))
account2Id := q5.ToString(rows.GetByIndex(1))
isFriendship := q5.ToInt32(rows.GetByIndex(2))
requestTime := q5.ToInt64(rows.GetByIndex(3))
// 检查用户是否已经存在,如果不存在则创建
user1, exists1 := userMap[account1Id]
if !exists1 {
user1 = NewUser(account1Id)
friendMgr.AddUser(account1Id, user1)
userMap[account1Id] = user1
}
user2, exists2 := userMap[account2Id]
if !exists2 {
user2 = NewUser(account2Id)
friendMgr.AddUser(account2Id, user2)
userMap[account2Id] = user2
}
// 创建好友关系并添加到用户对象中
friendship1 := &Friendship{
FriendAccountId: account2Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user1.Friendships[account2Id] = friendship1
friendship2 := &Friendship{
FriendAccountId: account1Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user2.Friendships[account1Id] = friendship2
}
},
)
}
func (fm *FriendsMgr) loadUserFriendships(user *User, where [][]string) {
fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"}
f5.GetJsStyleDb().Select(
FRIEND_DB,
"t_friend_ships",
fields,
where,
func(err error, rows *f5.DataSet) {
if err != nil {
return
}
for rows.Next() {
account1Id := q5.ToString(rows.GetByIndex(0))
account2Id := q5.ToString(rows.GetByIndex(1))
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
user2 := NewUser(account2Id)
friendMgr.AddUser(account2Id, user2)
friendship2 := &Friendship{
FriendAccountId: account1Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user2.Friendships[account1Id] = friendship2
cacheMgr.loadUserProfile(account2Id)
}
if user.AccountId == account2Id {
friendUser := NewUser(account1Id)
friendMgr.AddUser(account1Id, friendUser)
friendship := &Friendship{
FriendAccountId: account1Id,
IsFriendship: isFriendship,
RequestTime: requestTime,
}
user.Friendships[account1Id] = friendship
cacheMgr.loadUserProfile(account1Id)
}
}
},
)
}
// loadPendingRequests 加载黑名单列表
func (fm *FriendsMgr) loadBlacklist() {
fields := []string{"account_id", "blocked_account_id"}
where := [][]string{
{"is_removed", "0"},
}
f5.GetJsStyleDb().Select(
FRIEND_DB,
"t_friend_blacklist",
fields,
where,
func(err error, rows *f5.DataSet) {
if err != nil {
return
}
for rows.Next() {
account1Id := q5.ToString(rows.GetByIndex(0))
account2Id := q5.ToString(rows.GetByIndex(1))
friendBlackList := &FriendBlackList{
AccountId: account2Id,
IsRemoved: 0,
}
user := fm.GetUser(account1Id)
user.FriendBlackList[account2Id] = friendBlackList
}
},
)
}
func SwapAccountIds(account1Id, account2Id string) (string, string) {
if strings.Compare(account1Id, account2Id) > 0 {
account1Id, account2Id = account2Id, account1Id
}
return account1Id, account2Id
}