Add frienddb.sql

This commit is contained in:
殷勇 2023-08-14 17:47:23 +08:00
parent d6fd0137e9
commit a5aa8a4be5
3 changed files with 68 additions and 28 deletions

View File

@ -18,3 +18,28 @@ CREATE TABLE `version` (
UNIQUE KEY `version` (`version`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
drop table if exists `t_friend_ships`;
CREATE TABLE `t_friend_ships` (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
`account1_id` varchar(60) NOT NULL,
`account2_id` varchar(60) NOT NULL,
`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),
UNIQUE KEY `friendship` (`account1_id`,`account2_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin
COMMENT "已经成为好友"
;
drop table if exists `t_pending_friend_requests`;
CREATE TABLE t_pending_friend_requests (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
sender_account_id varchar(60) NOT NULL,
receiver_account_id varchar(60) NOT NULL,
request_time int DEFAULT '0',
`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),
UNIQUE KEY `friend_req` (`sender_account_id`,`receiver_account_id`)
) COMMENT "等待验证的好友请求";

View File

@ -9,8 +9,8 @@ import (
)
type User struct {
ID int
Username string
AccountId string
UserName string
}
// user1, user2 构成一个好友关系
@ -21,12 +21,16 @@ type Friendship struct {
type FriendsMgr struct {
cs.MsgHandlerImpl
users map[int]*User
friendships map[int][]*Friendship
pendingReqs map[int]map[int]bool
users map[string]*User
friendships map[string][]*Friendship
pendingReqs map[string]map[string]bool
}
func (fm *FriendsMgr) init() {
//dbSetting := struct {
// Host int `json:"errcode"`
//}
conn := q5.NewMysql(
mt.Table.GameDb.GetById(0).GetHost(),
mt.Table.GameDb.GetById(0).GetPort(),
@ -37,29 +41,32 @@ func (fm *FriendsMgr) init() {
conn.Open()
// make members
fm.users = make(map[int]*User)
fm.friendships = make(map[int][]*Friendship)
fm.pendingReqs = make(map[int]map[int]bool)
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() {
}
func (fm *FriendsMgr) unInit() {
// 1. Loop struct data
// 2. Struct Data Persist to DB
}
func (fm *FriendsMgr) RegisterUser(id int, username string) {
fm.users[id] = &User{ID: id, Username: username}
func (fm *FriendsMgr) RegisterUser(accountId string, username string) {
fm.users[accountId] = &User{AccountId: accountId, UserName: username}
}
func (fm *FriendsMgr) SearchFriends(query string) []*User {
var results []*User
lowercaseQuery := strings.ToLower(query)
for _, user := range fm.users {
if strings.Contains(strings.ToLower(user.Username), lowercaseQuery) {
if strings.Contains(strings.ToLower(user.UserName), lowercaseQuery) {
results = append(results, user)
}
}
@ -68,24 +75,24 @@ func (fm *FriendsMgr) SearchFriends(query string) []*User {
}
// AddFriendRequest 发送好友请求
func (fm *FriendsMgr) AddFriendRequest(user1ID, user2ID int) error {
//// Check if users exist
//user1, exists1 := fm.users[user1ID]
//user2, exists2 := fm.users[user2ID]
//if !exists1 || !exists2 {
// return errors.New("users not exist")
//}
//
//if fm.pendingReqs[user2ID] == nil {
// fm.pendingReqs[user2ID] = make(map[int]bool)
//}
//fm.pendingReqs[user2ID][user1ID] = true
//
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)
}
fm.pendingReqs[user2ID][user1ID] = true
return nil
}
// AcceptFriendRequest 接受好友请求
func (fm *FriendsMgr) AcceptFriendRequest(user1ID, user2ID int) 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")
}
@ -107,10 +114,10 @@ func (fm *FriendsMgr) AcceptFriendRequest(user1ID, user2ID int) error {
}
// GetFriends 我的好友列表
func (fm *FriendsMgr) GetFriends(userID int) []*User {
func (fm *FriendsMgr) GetFriends(accountId string) []*User {
var friends []*User
for _, friendship := range fm.friendships[userID] {
if friendship.User1.ID != userID {
for _, friendship := range fm.friendships[accountId] {
if friendship.User1.AccountId != accountId {
friends = append(friends, friendship.User1)
} else {
friends = append(friends, friendship.User2)

View File

@ -136,6 +136,14 @@ func (this *PlayerMgr) addPlayer(p *Player) {
this.accountIdHash[p.accountId] = p
}
func (this *PlayerMgr) getPlayer(accountId string) *Player {
player, ok := this.accountIdHash[accountId]
if ok {
return player
}
return nil
}
func (this *PlayerMgr) addSocketHash(wsp f5.WspCliConn, p *Player) {
this.socketHash[wsp] = p
}