我的好友列表

This commit is contained in:
殷勇 2023-08-15 14:05:51 +08:00
parent a5aa8a4be5
commit 0f8f37c93e
6 changed files with 419 additions and 60 deletions

View File

@ -21,6 +21,7 @@ func (this *App) Init() {
handlerMgr.init()
playerMgr.init()
friendMgr.init()
wspListener.init()
httpListener.init()
}

View File

@ -749,6 +749,189 @@ func (x *SMReconnect) GetErrmsg() string {
return ""
}
// 请求我的好友列表
type CMListFriend struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ServerId *int32 `protobuf:"varint,1,opt,name=server_id,json=serverId" json:"server_id,omitempty"` //保留
TeamUuid *string `protobuf:"bytes,2,opt,name=team_uuid,json=teamUuid" json:"team_uuid,omitempty"` //保留
AccountId *string `protobuf:"bytes,3,opt,name=account_id,json=accountId" json:"account_id,omitempty"` //账号id
ProtoVersion *int32 `protobuf:"varint,5,opt,name=proto_version,json=protoVersion" json:"proto_version,omitempty"` //协议版本号Constant_e.ProtoVersion
SessionId *string `protobuf:"bytes,20,opt,name=session_id,json=sessionId" json:"session_id,omitempty"` //账号id
}
func (x *CMListFriend) Reset() {
*x = CMListFriend{}
if protoimpl.UnsafeEnabled {
mi := &file_cs_proto_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CMListFriend) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CMListFriend) ProtoMessage() {}
func (x *CMListFriend) ProtoReflect() protoreflect.Message {
mi := &file_cs_proto_proto_msgTypes[11]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CMListFriend.ProtoReflect.Descriptor instead.
func (*CMListFriend) Descriptor() ([]byte, []int) {
return file_cs_proto_proto_rawDescGZIP(), []int{11}
}
func (x *CMListFriend) GetServerId() int32 {
if x != nil && x.ServerId != nil {
return *x.ServerId
}
return 0
}
func (x *CMListFriend) GetTeamUuid() string {
if x != nil && x.TeamUuid != nil {
return *x.TeamUuid
}
return ""
}
func (x *CMListFriend) GetAccountId() string {
if x != nil && x.AccountId != nil {
return *x.AccountId
}
return ""
}
func (x *CMListFriend) GetProtoVersion() int32 {
if x != nil && x.ProtoVersion != nil {
return *x.ProtoVersion
}
return 0
}
func (x *CMListFriend) GetSessionId() string {
if x != nil && x.SessionId != nil {
return *x.SessionId
}
return ""
}
// 回复我的好友列表
type MFFriend struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
AccountId *string `protobuf:"bytes,1,opt,name=account_id,json=accountId" json:"account_id,omitempty"`
Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"`
}
func (x *MFFriend) Reset() {
*x = MFFriend{}
if protoimpl.UnsafeEnabled {
mi := &file_cs_proto_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *MFFriend) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*MFFriend) ProtoMessage() {}
func (x *MFFriend) ProtoReflect() protoreflect.Message {
mi := &file_cs_proto_proto_msgTypes[12]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use MFFriend.ProtoReflect.Descriptor instead.
func (*MFFriend) Descriptor() ([]byte, []int) {
return file_cs_proto_proto_rawDescGZIP(), []int{12}
}
func (x *MFFriend) GetAccountId() string {
if x != nil && x.AccountId != nil {
return *x.AccountId
}
return ""
}
func (x *MFFriend) GetUsername() string {
if x != nil && x.Username != nil {
return *x.Username
}
return ""
}
type SMListFriend struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Friends []*MFFriend `protobuf:"bytes,1,rep,name=friends" json:"friends,omitempty"`
}
func (x *SMListFriend) Reset() {
*x = SMListFriend{}
if protoimpl.UnsafeEnabled {
mi := &file_cs_proto_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SMListFriend) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SMListFriend) ProtoMessage() {}
func (x *SMListFriend) ProtoReflect() protoreflect.Message {
mi := &file_cs_proto_proto_msgTypes[13]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SMListFriend.ProtoReflect.Descriptor instead.
func (*SMListFriend) Descriptor() ([]byte, []int) {
return file_cs_proto_proto_rawDescGZIP(), []int{13}
}
func (x *SMListFriend) GetFriends() []*MFFriend {
if x != nil {
return x.Friends
}
return nil
}
var File_cs_proto_proto protoreflect.FileDescriptor
var file_cs_proto_proto_rawDesc = []byte{
@ -811,10 +994,29 @@ var file_cs_proto_proto_rawDesc = []byte{
0x53, 0x4d, 0x52, 0x65, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x65,
0x72, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x65, 0x72,
0x72, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x65, 0x72, 0x72, 0x6d, 0x73, 0x67, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x65, 0x72, 0x72, 0x6d, 0x73, 0x67, 0x2a, 0x22, 0x0a,
0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x5f, 0x65, 0x12, 0x14, 0x0a, 0x0c, 0x50,
0x72, 0x6f, 0x74, 0x6f, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x10, 0xd1, 0xa2, 0xd5, 0xc4,
0x07, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x63, 0x73,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x65, 0x72, 0x72, 0x6d, 0x73, 0x67, 0x22, 0xab, 0x01,
0x0a, 0x0c, 0x43, 0x4d, 0x4c, 0x69, 0x73, 0x74, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x12, 0x1b,
0x0a, 0x09, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x05, 0x52, 0x08, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x74,
0x65, 0x61, 0x6d, 0x5f, 0x75, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08,
0x74, 0x65, 0x61, 0x6d, 0x55, 0x75, 0x69, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x61, 0x63, 0x63, 0x6f,
0x75, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x61, 0x63,
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0c,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x0a, 0x0a,
0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x18, 0x14, 0x20, 0x01, 0x28, 0x09,
0x52, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x22, 0x45, 0x0a, 0x08, 0x4d,
0x46, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x61, 0x63, 0x63, 0x6f, 0x75,
0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x61, 0x63, 0x63,
0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61,
0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61,
0x6d, 0x65, 0x22, 0x36, 0x0a, 0x0c, 0x53, 0x4d, 0x4c, 0x69, 0x73, 0x74, 0x46, 0x72, 0x69, 0x65,
0x6e, 0x64, 0x12, 0x26, 0x0a, 0x07, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x63, 0x73, 0x2e, 0x4d, 0x46, 0x46, 0x72, 0x69, 0x65, 0x6e,
0x64, 0x52, 0x07, 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x73, 0x2a, 0x22, 0x0a, 0x0a, 0x43, 0x6f,
0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x5f, 0x65, 0x12, 0x14, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x74,
0x6f, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x10, 0xd1, 0xa2, 0xd5, 0xc4, 0x07, 0x42, 0x06,
0x5a, 0x04, 0x2e, 0x3b, 0x63, 0x73,
}
var (
@ -830,7 +1032,7 @@ func file_cs_proto_proto_rawDescGZIP() []byte {
}
var file_cs_proto_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_cs_proto_proto_msgTypes = make([]protoimpl.MessageInfo, 11)
var file_cs_proto_proto_msgTypes = make([]protoimpl.MessageInfo, 14)
var file_cs_proto_proto_goTypes = []interface{}{
(ConstantE)(0), // 0: cs.Constant_e
(*CMPing)(nil), // 1: cs.CMPing
@ -844,13 +1046,17 @@ var file_cs_proto_proto_goTypes = []interface{}{
(*SMLogin)(nil), // 9: cs.SMLogin
(*CMReconnect)(nil), // 10: cs.CMReconnect
(*SMReconnect)(nil), // 11: cs.SMReconnect
(*CMListFriend)(nil), // 12: cs.CMListFriend
(*MFFriend)(nil), // 13: cs.MFFriend
(*SMListFriend)(nil), // 14: cs.SMListFriend
}
var file_cs_proto_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
13, // 0: cs.SMListFriend.friends:type_name -> cs.MFFriend
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_cs_proto_proto_init() }
@ -991,6 +1197,42 @@ func file_cs_proto_proto_init() {
return nil
}
}
file_cs_proto_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CMListFriend); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_cs_proto_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*MFFriend); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_cs_proto_proto_msgTypes[13].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SMListFriend); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
@ -998,7 +1240,7 @@ func file_cs_proto_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_cs_proto_proto_rawDesc,
NumEnums: 1,
NumMessages: 11,
NumMessages: 14,
NumExtensions: 0,
NumServices: 0,
},

View File

@ -5,5 +5,4 @@ var wspListener = new(WSPListener)
var playerMgr = new(PlayerMgr)
var handlerMgr = new(HandlerMgr)
var httpListener = new(HttpListener)
//var friendMgr = new(FriendsMgr)
var friendMgr = new(FriendsMgr)

View File

@ -0,0 +1,19 @@
package main
// User 用户实体
type User struct {
AccountId string
UserName string
}
// Friendship user1, user2 构成一个好友关系
type Friendship struct {
User1 *User
User2 *User
}
type FriendRequest struct {
RequestID int
SenderID int
ReceiverID int
}

View File

@ -3,22 +3,13 @@ package main
import (
"cs"
"errors"
"f5"
"fmt"
"mt"
"q5"
"strings"
)
type User struct {
AccountId string
UserName string
}
// user1, user2 构成一个好友关系
type Friendship struct {
User1 *User
User2 *User
}
type FriendsMgr struct {
cs.MsgHandlerImpl
users map[string]*User
@ -27,30 +18,31 @@ type FriendsMgr struct {
}
func (fm *FriendsMgr) init() {
//dbSetting := struct {
// Host int `json:"errcode"`
//}
conn := q5.NewMysql(
// init gameDB
gameDBStore := q5.NewMysql(
mt.Table.GameDb.GetById(0).GetHost(),
mt.Table.GameDb.GetById(0).GetPort(),
mt.Table.GameDb.GetById(0).GetUser(),
mt.Table.GameDb.GetById(0).GetPasswd(),
mt.Table.GameDb.GetById(0).GetDatabase(),
)
conn.Open()
// make members
fm.users = make(map[string]*User)
fm.friendships = make(map[string][]*Friendship)
fm.pendingReqs = make(map[string]map[string]bool)
// 1. Load DB,
// 2. DB Data Map to Struct
// 3. RegisterUser
}
func (fm *FriendsMgr) parseDBData() {
gameDBStore.Open()
fm.loadUsersFromDB(gameDBStore)
// init friendDB
friendDBStore := q5.NewMysql(
mt.Table.FriendDb.GetById(0).GetHost(),
mt.Table.FriendDb.GetById(0).GetPort(),
mt.Table.FriendDb.GetById(0).GetUser(),
mt.Table.FriendDb.GetById(0).GetPasswd(),
mt.Table.FriendDb.GetById(0).GetDatabase(),
)
friendDBStore.Open()
// 加载好友关系表 列表
fm.loadFriendshipsFromDB(friendDBStore)
// 加载等待验证好友请求 列表
fm.loadPendingRequestsFromDB(friendDBStore)
}
func (fm *FriendsMgr) unInit() {
@ -58,31 +50,26 @@ func (fm *FriendsMgr) unInit() {
// 2. Struct Data Persist to DB
}
func (fm *FriendsMgr) RegisterUser(accountId string, username string) {
fm.users[accountId] = &User{AccountId: accountId, UserName: username}
}
func (fm *FriendsMgr) SearchFriends(query string) []*User {
func (fm *FriendsMgr) searchFriends(usernameKeyword string) []*User {
var results []*User
lowercaseQuery := strings.ToLower(query)
lowercaseQuery := strings.ToLower(usernameKeyword)
for _, user := range fm.users {
if strings.Contains(strings.ToLower(user.UserName), lowercaseQuery) {
results = append(results, user)
}
}
// Search result save to cached, key: keyword, value: serialization(result), expired: 2days
return results
}
// AddFriendRequest 发送好友请求
func (fm *FriendsMgr) AddFriendRequest(user1ID, user2ID string) error {
func (fm *FriendsMgr) addFriendRequest(user1ID, user2ID string) error {
// Check if users exist
_, exists1 := fm.users[user1ID]
_, exists2 := fm.users[user2ID]
if !exists1 || !exists2 {
return errors.New("users not exist")
}
if fm.pendingReqs[user2ID] == nil {
fm.pendingReqs[user2ID] = make(map[string]bool)
}
@ -92,7 +79,7 @@ func (fm *FriendsMgr) AddFriendRequest(user1ID, user2ID string) error {
}
// AcceptFriendRequest 接受好友请求
func (fm *FriendsMgr) AcceptFriendRequest(user1ID, user2ID string) error {
func (fm *FriendsMgr) acceptFriendRequest(user1ID, user2ID string) error {
if !fm.pendingReqs[user1ID][user2ID] {
return errors.New("no pending friend request from user1 to user2")
}
@ -113,15 +100,106 @@ func (fm *FriendsMgr) AcceptFriendRequest(user1ID, user2ID string) error {
return nil
}
// GetFriends 我的好友列表
func (fm *FriendsMgr) GetFriends(accountId string) []*User {
var friends []*User
for _, friendship := range fm.friendships[accountId] {
if friendship.User1.AccountId != accountId {
friends = append(friends, friendship.User1)
// CMListFriend 我的好友列表
func (fm *FriendsMgr) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
myAccountId := msg.GetAccountId()
listFriend := &cs.SMListFriend{}
for _, friendship := range fm.friendships[myAccountId] {
if friendship.User1.AccountId != myAccountId {
friend := &cs.MFFriend{
AccountId: &friendship.User1.AccountId,
Username: &friendship.User1.UserName,
}
listFriend.Friends = append(listFriend.Friends, friend)
} else {
friends = append(friends, friendship.User2)
friend := &cs.MFFriend{
AccountId: &friendship.User2.AccountId,
Username: &friendship.User2.UserName,
}
listFriend.Friends = append(listFriend.Friends, friend)
}
}
return friends
fmt.Printf("my friends count:%d\n", len(listFriend.Friends))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, listFriend)
}
func (fm *FriendsMgr) loadUsersFromDB(conn *q5.Mysql) {
// Load DB users to struct FriendsMgr.user
rows, err := conn.Query("select account_id, name from t_user")
if err != nil {
f5.GetSysLog().Info("mysql error", err)
return
}
fm.users = make(map[string]*User)
for rows.Next() {
user := &User{}
err := rows.Scan(&user.AccountId, &user.UserName)
if err != nil {
continue
}
fm.users[user.AccountId] = user
}
}
func (fm *FriendsMgr) loadFriendshipsFromDB(conn *q5.Mysql) {
// Load DB t_friend_ships to struct FriendsMgr.friendship
rows, err := conn.Query("select account1_id, account2_id from t_friend_ships;")
if err != nil {
f5.GetSysLog().Info("mysql error", err)
return
}
fm.friendships = make(map[string][]*Friendship)
for rows.Next() {
user1 := &User{}
user2 := &User{}
err := rows.Scan(&user1.AccountId, &user2.AccountId)
if err != nil {
continue
}
friendship := &Friendship{}
friendship.User1 = user1
friendship.User2 = user2
fm.addFriendshipToMap(user1.AccountId, friendship)
fm.addFriendshipToMap(user2.AccountId, friendship)
}
}
func (fm *FriendsMgr) loadPendingRequestsFromDB(conn *q5.Mysql) {
rows, err := conn.Query("select sender_account_id, receiver_account_id from t_pending_friend_requests;")
if err != nil {
f5.GetSysLog().Info("mysql error", err)
return
}
pendingReqs := make(map[string]map[string]bool)
for rows.Next() {
var senderAccountId, receiverAccountId string
if err := rows.Scan(&senderAccountId, &receiverAccountId); err != nil {
continue
}
if pendingReqs[senderAccountId] == nil {
pendingReqs[senderAccountId] = make(map[string]bool)
}
pendingReqs[senderAccountId][receiverAccountId] = true
}
if err := rows.Err(); err != nil {
return
}
fm.pendingReqs = pendingReqs
}
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) {
fm.users[accountId] = &User{AccountId: accountId, UserName: username}
}

View File

@ -89,8 +89,6 @@ message CMLogin
optional string account_id = 3; //id
optional int32 proto_version = 5; //Constant_e.ProtoVersion
optional string session_id = 20; //id
}
//
@ -118,3 +116,25 @@ message SMReconnect
optional int32 errcode = 1; // 0 1:
optional string errmsg = 2; //
}
//
message CMListFriend
{
optional int32 server_id = 1; //
optional string team_uuid = 2; //
optional string account_id = 3; //id
optional int32 proto_version = 5; //Constant_e.ProtoVersion
optional string session_id = 20; //id
}
//
message MFFriend {
optional string account_id = 1;
optional string username = 2;
}
message SMListFriend
{
repeated MFFriend friends = 1;
}