package main import ( "f5" "fmt" "q5" ) func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string, isFriendship string, cb func(error)) { where := [][]string{ {"sender_account_id", account1Id}, {"receiver_account_id", account2Id}, } insertKv := [][]string{ {"sender_account_id", account1Id}, {"receiver_account_id", account2Id}, {"is_friendship", isFriendship}, } updateKv := [][]string{ {"is_friendship", isFriendship}, } f5.GetJsStyleDb().Upsert( FRIEND_DB, "t_friend_pending_request", where, updateKv, insertKv, func(err error, lastInsertId int64, rowsAffected int64) { cb(err) }, ) } func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isDeleteFriendship int, cb func(error)) { where := [][]string{ {"account1_id", account1Id}, {"account2_id", account2Id}, } insertKv := [][]string{ {"account1_id", account1Id}, {"account2_id", account2Id}, {"is_delete_friendship", q5.ToString(isDeleteFriendship)}, } updateKv := [][]string{ {"is_delete_friendship", q5.ToString(isDeleteFriendship)}, } 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)) { 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) findPlayer(accountId string, cb func(err error, profile *PlayerProfile)) { fields := []string{"account_id", "name", "head_id", "head_frame", "rank", "last_login_time"} where := [][]string{ {"account_id", accountId}, } f5.GetJsStyleDb().SelectOne( GAME_DB, "t_user", fields, where, func(err error, rows *f5.DataSet) { if err != nil { cb(err, nil) return } profile := &PlayerProfile{} if rows.Next() { 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)), } } cb(nil, profile) }, ) } func (fm *FriendsMgr) findUsersByUsername(username string, cb func(err error, profiles []*PlayerProfile)) { fields := []string{"account_id", "name", "head_id", "head_frame", "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, MaxSearchResults, func(err error, rows *f5.DataSet) { if err != nil { cb(err, nil) return } profiles := make([]*PlayerProfile, 0, 20) for rows.Next() { 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)), } profiles = append(profiles, profile) } cb(nil, profiles) }, ) } // loadFriendships 加载好友关系表 func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) { fields := []string{"account1_id", "account2_id"} 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)) 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 { user2 := NewUser(account1Id) friendMgr.AddUser(account1Id, user2) friendship := &Friendship{} friendship.User1 = user friendship.User2 = user2 fm.AddFriendshipToMap(user.AccountId, friendship) cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) } } }, ) } // loadPendingRequests 加载等待验证好友请求 func (fm *FriendsMgr) loadPendingRequests(user *User) { fields := []string{"sender_account_id", "receiver_account_id", "createtime"} // 还未成功好友关系的 请求列表 where := [][]string{ {"receiver_account_id", user.AccountId}, {"is_friendship", "0"}, } f5.GetJsStyleDb().Select( FRIEND_DB, "t_friend_pending_request", fields, where, func(err error, rows *f5.DataSet) { if err != nil { return } for rows.Next() { senderAccountId := q5.ToString(*rows.GetByIndex(0)) //receiverAccountId := q5.ToString(*rows.GetByIndex(1)) requestTime := q5.ToInt32(*rows.GetByIndex(2)) // load profile cacheMgr.LoadPlayerProfile(senderAccountId, func(playerProfile *PlayerProfile) {}) friendRequest := &FriendRequest{ AccountId: senderAccountId, IsFriendship: 0, RequestTime: requestTime, } user.FriendRequest[senderAccountId] = friendRequest } }, ) } // loadPendingRequests 加载等待验证好友请求 func (fm *FriendsMgr) loadBlacklist(user *User) { fields := []string{"account_id", "blocked_account_id"} where := [][]string{ {"account_id", user.AccountId}, {"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() { account2Id := q5.ToString(*rows.GetByIndex(1)) friendBlackList := &FriendBlackList{ AccountId: account2Id, IsRemoved: 0, } user.FriendBlackList[account2Id] = friendBlackList } }, ) }