From 2f23b0be33d159f1548548788c06e1772054f2de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Tue, 26 Sep 2023 13:16:58 +0800 Subject: [PATCH 1/5] save --- server/imserver/cachemgr.go | 4 ++-- server/imserver/constant.go | 1 + server/imserver/friendsdbmgr.go | 5 +--- server/imserver/friendsmgr.go | 41 ++++++++++++++++++++++++++------- server/imserver/guild.go | 6 ++--- server/imserver/player.go | 12 ++++++++-- server/imserver/playermgr.go | 33 +++++++++++++++++--------- 7 files changed, 72 insertions(+), 30 deletions(-) diff --git a/server/imserver/cachemgr.go b/server/imserver/cachemgr.go index 40679daf..2ae451bc 100644 --- a/server/imserver/cachemgr.go +++ b/server/imserver/cachemgr.go @@ -10,8 +10,8 @@ type PlayerProfile struct { Avatar int32 // 头像 AvatarHead int32 // 头像框 Star int32 // 星星 - totalKills int32 // 总击杀数 - totalWinTimes int32 // 总赢数 + TotalKills int32 // 总击杀数 + TotalWinTimes int32 // 总赢数 Rank int32 // 排位赛段位 OnlineStatus int32 // 在线状态 LastLoginTime int32 // 上次登录时间 diff --git a/server/imserver/constant.go b/server/imserver/constant.go index 5adbb6c1..e3b33a0c 100644 --- a/server/imserver/constant.go +++ b/server/imserver/constant.go @@ -111,6 +111,7 @@ const ( ERR_CODE_SEARCH_USER_DB_FAIL = 11015 ERR_CODE_SEARCH_NO_RESULT = 11016 ERR_CODE_FRIEND_NO_EXISTS = 11017 + ERR_CODE_ALREADY_FRIEND = 11018 // Guild ERR_CODE_GUILD_NO_EXISTS = 12001 diff --git a/server/imserver/friendsdbmgr.go b/server/imserver/friendsdbmgr.go index 4a8cbcef..a3ec7f04 100644 --- a/server/imserver/friendsdbmgr.go +++ b/server/imserver/friendsdbmgr.go @@ -38,6 +38,7 @@ func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isF } 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}, @@ -147,7 +148,6 @@ func (fm *FriendsMgr) loadFriendships() { account2Id := q5.ToString(*rows.GetByIndex(1)) isFriendship := q5.ToInt32(*rows.GetByIndex(2)) requestTime := q5.ToInt64(*rows.GetByIndex(3)) - // 检查用户是否已经存在,如果不存在则创建 user1, exists1 := userMap[account1Id] if !exists1 { @@ -175,9 +175,6 @@ func (fm *FriendsMgr) loadFriendships() { RequestTime: requestTime, } user2.Friendships[account1Id] = friendship2 - // 加载用户配置信息 - //cacheMgr.LoadPlayerProfile(user1, func(playerProfile *PlayerProfile) {}) - //cacheMgr.LoadPlayerProfile(user2, func(playerProfile *PlayerProfile) {}) } }, ) diff --git a/server/imserver/friendsmgr.go b/server/imserver/friendsmgr.go index 36fc1144..a9501982 100644 --- a/server/imserver/friendsmgr.go +++ b/server/imserver/friendsmgr.go @@ -75,11 +75,17 @@ func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb // return //} - // 已发送请求 + // 检查已发送请求 friendship, exists := user1.Friendships[account2Id] - if exists && friendship.IsFriendship == 0 { - cb(ERR_CODE_OK, "AddFriendRequest ok") - return + if exists { + if friendship.IsFriendship == 0 { + cb(ERR_CODE_OK, "AddFriendRequest ok") + return + } + if friendship.IsFriendship == 1 { + cb(ERR_CODE_ALREADY_FRIEND, "Already a friend") + return + } } // 好友已满 @@ -105,9 +111,6 @@ func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb user1.AddFriendRequest(account2Id, FriendshipStatusPending, requestTime) user2.AddFriendRequest(account1Id, FriendshipStatusPending, requestTime) - // Load user2 profile to cache - // cacheMgr.LoadPlayerProfile(user2, func(playerProfile *PlayerProfile) {}) - cb(ERR_CODE_OK, "AddFriendRequest ok") }) } @@ -120,10 +123,15 @@ func (fm *FriendsMgr) AcceptFriendRequest(account1Id string, account2Id string, user2 = fm.LoadUser(account2Id) } - if !user1.IsInReq(account2Id) { + friendship, exists := user1.Friendships[account2Id] + if !exists { cb(ERR_CODE_NO_IN_REQ, "AcceptFriendRequest user2 no in user1 pending request") return } + if friendship.IsFriendship != 0 { + cb(ERR_CODE_ALREADY_FRIEND, "Already a friend") + return + } if fm.GetFriendCount(account1Id) >= MaxFriendMembers || fm.GetFriendCount(account2Id) >= MaxFriendMembers { cb(ERR_CODE_USERS_IS_FULL, "AcceptFriendRequest users is full") @@ -167,6 +175,16 @@ func (fm *FriendsMgr) RejectFriendRequest(account1Id string, account2Id string, return } + friendship, exists := user1.Friendships[account2Id] + if !exists { + cb(ERR_CODE_NO_IN_REQ, "AcceptFriendRequest user2 no in user1 pending request") + return + } + if friendship.IsFriendship == 1 { + cb(ERR_CODE_ALREADY_FRIEND, "Already a friend") + return + } + requestTime := time.Now().Unix() fm.upsertFriendShip(account2Id, account1Id, FriendshipStatusReject, requestTime, func(err error) { if err != nil { @@ -191,6 +209,13 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er cb(ERR_CODE_USERS_NO_EXISTS, "DeleteFriendShip user no exists") return } + + friendship, exists := user1.Friendships[account2Id] + if !exists || friendship.IsFriendship != 1 { + cb(ERR_CODE_FRIEND_NO_EXISTS, "DeleteFriendShip user no exists") + return + } + user1.RemoveFriendShip(account2Id) user2.RemoveFriendShip(account1Id) diff --git a/server/imserver/guild.go b/server/imserver/guild.go index 9dbe3d93..64642bd2 100644 --- a/server/imserver/guild.go +++ b/server/imserver/guild.go @@ -16,9 +16,9 @@ type Guild struct { Notice string // 公告 JoinCond int32 // 公会加入条件 JoinCondValue int32 // 公会加入条件值 - TotalStars int32 // 公会统计信息, 总星星数量 - TotalKills int32 // 公会统计信息, 单局总击杀数 - ChickenDinners int32 // 公会统计信息, 单局第一名数 + TotalStars int32 // 公会统计信息, 总星星数量 从api获取 + TotalKills int32 // 公会统计信息, 单局总击杀数 从api获取 + ChickenDinners int32 // 公会统计信息, 单局第一名数 从api获取 MaxMembers int32 // 公会最大成员数 default 30 Members map[string]*GuildMember // accountId -> GuildMember PendingReqs map[string]int32 // pendingAccountId -> status 0,1,2,3 pending, accept, reject, leave diff --git a/server/imserver/player.go b/server/imserver/player.go index 7b587317..1294da7e 100644 --- a/server/imserver/player.go +++ b/server/imserver/player.go @@ -748,6 +748,8 @@ func (p *Player) FillMFGuildMember(member *GuildMember) *cs.MFGuildMember { func (p *Player) FillMFGuild(guild *Guild) *cs.MFGuild { // 总星星数 var totalStar int32 = 0 + var totalKills int32 = 0 + var totalWinTimes int32 = 0 var guildMembers []*cs.MFGuildMember for _, member := range guild.Members { @@ -757,6 +759,12 @@ func (p *Player) FillMFGuild(guild *Guild) *cs.MFGuild { } guildMembers = append(guildMembers, guildMember) totalStar += guildMember.GetStar() + + profile := cacheMgr.GetPlayerProfile(member.AccountId) + if profile != nil { + totalKills += profile.TotalKills + totalWinTimes += profile.TotalWinTimes + } } var resGuild *cs.MFGuild @@ -770,8 +778,8 @@ func (p *Player) FillMFGuild(guild *Guild) *cs.MFGuild { JoinCond: &guild.JoinCond, JoinCondValue: &guild.JoinCondValue, TotalStars: &totalStar, - TotalKills: &guild.TotalKills, - ChickenDinners: &guild.ChickenDinners, + TotalKills: &totalKills, + ChickenDinners: &totalWinTimes, MaxMembers: &guild.MaxMembers, Members: guildMembers, } diff --git a/server/imserver/playermgr.go b/server/imserver/playermgr.go index 0f783d00..b249108e 100644 --- a/server/imserver/playermgr.go +++ b/server/imserver/playermgr.go @@ -74,7 +74,7 @@ func (this *PlayerMgr) unInit() { func (this *PlayerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) { params := map[string]string{ "c": "User", - "a": "info", + "a": "detailInfo", "account_id": msg.GetAccountId(), "session_id": msg.GetSessionId(), "target_id": msg.GetAccountId(), @@ -88,20 +88,25 @@ func (this *PlayerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) { }) } +type HistorySeasons struct { + TotalKills int `json:"total_kills"` + WinTimes int `json:"win_times"` +} + func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse) { resObj := struct { Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` Info struct { - Activated string `json:"activated"` - RenameCount string `json:"rename_count"` - AccountID string `json:"account_id"` - Name string `json:"name"` - Avatar string `json:"head_id"` - AvatarHead string `json:"head_frame"` - Star string `json:"current_star_num"` - Rank string `json:"rank"` - LastLoginTime string `json:"last_login_time"` + Activated string `json:"activated"` + AccountID string `json:"account_id"` + Name string `json:"name"` + Avatar string `json:"head_id"` + AvatarHead string `json:"head_frame"` + Star string `json:"current_star_num"` + Rank string `json:"current_rank"` + LastLoginTime string `json:"last_login_time"` + HistorySeasons []HistorySeasons `json:"history_seasons"` } `json:"info"` }{} err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj) @@ -122,7 +127,6 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt // Add to online user this.addPlayer(&player) this.addSocketHash(hdr.GetSocket(), &player) - // Add player profile playerProfile := &PlayerProfile{ AccountId: accountId, @@ -134,6 +138,13 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt OnlineStatus: OnlineStatus, LastLoginTime: q5.ToInt32(resObj.Info.LastLoginTime), } + if len(resObj.Info.HistorySeasons) == 1 { + playerProfile.TotalKills = q5.ToInt32(resObj.Info.HistorySeasons[0].TotalKills) + playerProfile.TotalWinTimes = q5.ToInt32(resObj.Info.HistorySeasons[0].WinTimes) + } else { + playerProfile.TotalKills = 0 + playerProfile.TotalWinTimes = 0 + } cacheMgr.AddCacheProfile(1, playerProfile) friendMgr.LoadUser(accountId) From 01911660fd081d5ecf14d3cd416a4252408d80ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Tue, 26 Sep 2023 16:25:21 +0800 Subject: [PATCH 2/5] save --- server/imserver/app.go | 16 ++++ server/imserver/cachedbmgr.go | 126 ++++++++++++++++++++++--------- server/imserver/friendsdbmgr.go | 2 +- server/imserver/guilddbmgr.go | 6 +- server/imserver/guildmgr_test.go | 22 ------ 5 files changed, 111 insertions(+), 61 deletions(-) diff --git a/server/imserver/app.go b/server/imserver/app.go index e3a2c4df..ccdcd498 100644 --- a/server/imserver/app.go +++ b/server/imserver/app.go @@ -85,6 +85,14 @@ func (this *App) registerDataSources() { mt.Table.GameDb.GetById(0).GetPasswd(), mt.Table.GameDb.GetById(0).GetDatabase(), 30) + f5.GetGoStyleDb().RegisterDataSource( + GAME_DB, + 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(), + 30) f5.GetJsStyleDb().RegisterDataSource( FRIEND_DB, mt.Table.FriendDb.GetById(0).GetHost(), @@ -93,4 +101,12 @@ func (this *App) registerDataSources() { mt.Table.FriendDb.GetById(0).GetPasswd(), mt.Table.FriendDb.GetById(0).GetDatabase(), 30) + f5.GetGoStyleDb().RegisterDataSource( + FRIEND_DB, + 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(), + 30) } diff --git a/server/imserver/cachedbmgr.go b/server/imserver/cachedbmgr.go index 2720f787..978f1d53 100644 --- a/server/imserver/cachedbmgr.go +++ b/server/imserver/cachedbmgr.go @@ -3,54 +3,110 @@ package main import ( "f5" "fmt" - "mt" "q5" ) -var tableName = make(map[string]string) - func (cm *CacheMgr) LoadFromDB() { - tableName["user"] = fmt.Sprintf("%s.t_user", mt.Table.GameDb.GetById(0).GetDatabase()) - tableName["friendships"] = fmt.Sprintf("%s.t_friend_ships", mt.Table.FriendDb.GetById(0).GetDatabase()) - tableName["guild_member"] = fmt.Sprintf("%s.t_guild_members", mt.Table.FriendDb.GetById(0).GetDatabase()) - // 加载所有好友信息 - cm.loadAllFriendUserProfile() - // 加载所有工会成员信息 - cm.loadAllGuildUserProfile() + uniAccountIds := Set{} + uniAccountIds = make(Set) + + // Load friendships + { + lastIdx := int64(0) + done := false + for !done { + f5.GetGoStyleDb().SyncSelectCustomQuery( + FRIEND_DB, + fmt.Sprintf("SELECT idx, account1_id, account2_id FROM t_friend_ships WHERE idx > %d limit 1000", lastIdx), + func(err error, rows *f5.DataSet) { + if err != nil { + f5.GetSysLog().Info("LoadFromDB FRIENDSHIP err:%v \n", err) + panic(err) + } + empty := true + for rows.Next() { + empty = false + account1Id := q5.ToString(*rows.GetByName("account1_id")) + account2Id := q5.ToString(*rows.GetByName("account2_id")) + + if !uniAccountIds.Has(account1Id) { + cm.loadUserProfile(account1Id) + } + uniAccountIds.Add(account1Id) + + if !uniAccountIds.Has(account2Id) { + cm.loadUserProfile(account2Id) + } + uniAccountIds.Add(account2Id) + + lastIdx = q5.ToInt64(*rows.GetByName("idx")) + } + if empty { + done = true + } + }, + ) + } + } + + // Load guild members + { + lastIdx := int64(0) + done := false + for !done { + f5.GetGoStyleDb().SyncSelectCustomQuery( + FRIEND_DB, + fmt.Sprintf("SELECT idx, account_id FROM t_guild_members WHERE idx > %d", lastIdx), + func(err error, rows *f5.DataSet) { + if err != nil { + f5.GetSysLog().Info("LoadFromDB GUILD err:%v \n", err) + panic(err) + } + empty := true + for rows.Next() { + empty = false + accountId := q5.ToString(*rows.GetByName("account_id")) + + if !uniAccountIds.Has(accountId) { + cm.loadUserProfile(accountId) + } + uniAccountIds.Add(accountId) + + lastIdx = q5.ToInt64(*rows.GetByName("idx")) + } + if empty { + done = true + } + }, + ) + } + } + + uniAccountIds = nil } -// TODO 重加载数据,1000 迭代 -func (cm *CacheMgr) loadAllFriendUserProfile() { - sql := fmt.Sprintf("SELECT account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM ( SELECT account1_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account1_id = u.account_id UNION SELECT account2_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account2_id = u.account_id) AS friend_info_table;", tableName["friendships"], tableName["user"], tableName["friendships"], tableName["user"]) - cm.loadUsersProfile(sql) -} - -// loadGuildFromDB 加载公会成员信息 -func (cm *CacheMgr) loadAllGuildUserProfile() { - sql := fmt.Sprintf("select a.account_id, a.name, a.head_id, a.head_frame, a.star_num, a.`rank`, a.last_login_time from %s a,%s b where a.account_id = b.account_id", tableName["user"], tableName["guild_member"]) - cm.loadUsersProfile(sql) -} - -func (cm *CacheMgr) loadUsersProfile(sql string) { - f5.GetJsStyleDb().SelectCustomQuery( - FRIEND_DB, +func (cm *CacheMgr) loadUserProfile(accountId string) { + //select a.account_id, a.name, a.head_id, a.head_frame, a.star_num, a.`rank`, a.last_login_time + sql := fmt.Sprintf("select * from t_user where account_id='%s'", accountId) + f5.GetGoStyleDb().SyncSelectCustomQuery( + GAME_DB, sql, func(err error, rows *f5.DataSet) { if err != nil { f5.GetSysLog().Info("loadUsersProfile err:%v \n", err) - return + panic(err) } for rows.Next() { - accountId := q5.ToString(*rows.GetByIndex(0)) + aId := q5.ToString(*rows.GetByName("account_id")) onlineStatus := playerMgr.GetOnlineStatus(accountId) profile := &PlayerProfile{ - AccountId: accountId, - Username: q5.ToString(*rows.GetByIndex(1)), - Avatar: q5.ToInt32(*rows.GetByIndex(2)), - AvatarHead: q5.ToInt32(*rows.GetByIndex(3)), - Star: q5.ToInt32(*rows.GetByIndex(4)), - Rank: q5.ToInt32(*rows.GetByIndex(5)), - LastLoginTime: q5.ToInt32(*rows.GetByIndex(6)), + AccountId: aId, + Username: q5.ToString(*rows.GetByName("name")), + Avatar: q5.ToInt32(*rows.GetByName("head_id")), + AvatarHead: q5.ToInt32(*rows.GetByName("head_frame")), + Star: q5.ToInt32(*rows.GetByName("star_num")), + Rank: q5.ToInt32(*rows.GetByName("rank")), + LastLoginTime: q5.ToInt32(*rows.GetByName("last_login_time")), OnlineStatus: onlineStatus, } cm.AddCacheProfile(1, profile) @@ -89,7 +145,7 @@ func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, p } cb(nil, profile) } - cb(nil, nil) + cb(fmt.Errorf("no rows"), nil) }, ) } diff --git a/server/imserver/friendsdbmgr.go b/server/imserver/friendsdbmgr.go index a3ec7f04..b12d2921 100644 --- a/server/imserver/friendsdbmgr.go +++ b/server/imserver/friendsdbmgr.go @@ -140,7 +140,7 @@ func (fm *FriendsMgr) loadFriendships() { where, func(err error, rows *f5.DataSet) { if err != nil { - return + panic(err) } userMap := make(map[string]*User) for rows.Next() { diff --git a/server/imserver/guilddbmgr.go b/server/imserver/guilddbmgr.go index 546ab563..428ad011 100644 --- a/server/imserver/guilddbmgr.go +++ b/server/imserver/guilddbmgr.go @@ -37,7 +37,7 @@ func (gm *GuildMgr) loadGuildFromDB() { func (gm *GuildMgr) loadGuildFromDBResult(err error, rows *f5.DataSet) { if err != nil { f5.GetSysLog().Info("loadGuildFromDBResult err:%v \n", err) - return + panic(err) } for rows.Next() { guildId := q5.ToInt64(*rows.GetByIndex(1)) @@ -80,7 +80,7 @@ func (gm *GuildMgr) loadGuildMemberFromDB() { func (gm *GuildMgr) loadGuildMemberFromDBResult(err error, rows *f5.DataSet) { if err != nil { f5.GetSysLog().Info("loadGuildMemberFromDBResult err:%v \n", err) - return + panic(err) } for rows.Next() { var ( @@ -121,7 +121,7 @@ func (gm *GuildMgr) loadPendingReqsFromDB() { func (gm *GuildMgr) loadPendingReqsFromDBResult(err error, rows *f5.DataSet) { if err != nil { f5.GetSysLog().Info("loadPendingReqsFromDBResult err:%v \n", err) - return + panic(err) } for rows.Next() { var ( diff --git a/server/imserver/guildmgr_test.go b/server/imserver/guildmgr_test.go index 7c70e8f3..c4566cf5 100644 --- a/server/imserver/guildmgr_test.go +++ b/server/imserver/guildmgr_test.go @@ -17,28 +17,6 @@ func TestInit(t *testing.T) { fmt.Printf("test init") } -func TestCreateGuild(t *testing.T) { - guildMgr.CreateGuild( - randomGuildName(), - leaderId, - func(errCode int32, errMsg string, guildId int64) { - newGuildId = guildId - fmt.Println("Created guild:", guildId) - }) -} - -func TestGuildMember(t *testing.T) { - guildMgr.ApplyToGuild( - newGuildId, member1Id, - func(errCode int32, errMsg string) { - if errCode != 0 { - t.Errorf("Error:%s", errMsg) - } - fmt.Println("Applied to guild") - }, - ) -} - func randomGuildName() string { return q5.RandomString(6) } From 6aef67c2f6168cad764616ef6b4122557c29472c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Tue, 26 Sep 2023 16:26:24 +0800 Subject: [PATCH 3/5] save --- third_party/f5 | 2 +- third_party/q5 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/third_party/f5 b/third_party/f5 index f7961841..eb482c3b 160000 --- a/third_party/f5 +++ b/third_party/f5 @@ -1 +1 @@ -Subproject commit f79618418ed89343a56ca53b8acc9a3cbd07ac30 +Subproject commit eb482c3b277db6792f5a63a7d9eea835fee3fc90 diff --git a/third_party/q5 b/third_party/q5 index aa48a0c5..72f4aa81 160000 --- a/third_party/q5 +++ b/third_party/q5 @@ -1 +1 @@ -Subproject commit aa48a0c5241057cd9fe00ab712638c61acd23a21 +Subproject commit 72f4aa81328cc4705a44276f2b0d5ecb3cd4763b From 38e61da64225160ca5ef646b3df1eede77c522fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Wed, 27 Sep 2023 10:43:54 +0800 Subject: [PATCH 4/5] save --- server/imserver/cachemgr.go | 8 +++----- server/imserver/guildmgr.go | 2 -- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/server/imserver/cachemgr.go b/server/imserver/cachemgr.go index 2ae451bc..5dd4108a 100644 --- a/server/imserver/cachemgr.go +++ b/server/imserver/cachemgr.go @@ -123,10 +123,8 @@ func (cm *CacheMgr) LoadPlayerProfile(user *User, cb func(*PlayerProfile)) { } func (cm *CacheMgr) AddCacheProfile(version int, playerProfile *PlayerProfile) { - if _, exists := cm.cachePlayerProfiles[playerProfile.AccountId]; !exists { - cm.cachePlayerProfiles[playerProfile.AccountId] = &CachePlayerProfile{ - version: version, - data: playerProfile, - } + cm.cachePlayerProfiles[playerProfile.AccountId] = &CachePlayerProfile{ + version: version, + data: playerProfile, } } diff --git a/server/imserver/guildmgr.go b/server/imserver/guildmgr.go index 38c37399..64274910 100644 --- a/server/imserver/guildmgr.go +++ b/server/imserver/guildmgr.go @@ -766,9 +766,7 @@ func (gm *GuildMgr) SetNameConsume(player *Player, itemId, itemNum int32, cb fun "item_num": q5.ToString(itemNum), } url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl()) - f5.GetSysLog().Info("SetNameConsume url:%s, params:%+v\n", url, params) - f5.GetHttpCliMgr().SendJsStyleRequest( url, params, From 8523be30e8ed3b3836534892693f93200ae28aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Wed, 27 Sep 2023 11:30:57 +0800 Subject: [PATCH 5/5] save --- server/imserver/cachedbmgr.go | 39 +++----------------- server/imserver/cachemgr.go | 50 +++++++------------------- server/imserver/friendsdbmgr.go | 4 +-- server/imserver/guildmgr.go | 4 --- server/imserver/playermgr.go | 64 ++++++++++++++++++++++++++++++++- 5 files changed, 82 insertions(+), 79 deletions(-) diff --git a/server/imserver/cachedbmgr.go b/server/imserver/cachedbmgr.go index 978f1d53..4ab8718f 100644 --- a/server/imserver/cachedbmgr.go +++ b/server/imserver/cachedbmgr.go @@ -86,7 +86,7 @@ func (cm *CacheMgr) LoadFromDB() { } func (cm *CacheMgr) loadUserProfile(accountId string) { - //select a.account_id, a.name, a.head_id, a.head_frame, a.star_num, a.`rank`, a.last_login_time + // "account_id", "name", "head_id", "head_frame", "star_num", "rank", "last_login_time" sql := fmt.Sprintf("select * from t_user where account_id='%s'", accountId) f5.GetGoStyleDb().SyncSelectCustomQuery( GAME_DB, @@ -109,43 +109,14 @@ func (cm *CacheMgr) loadUserProfile(accountId string) { LastLoginTime: q5.ToInt32(*rows.GetByName("last_login_time")), OnlineStatus: onlineStatus, } - cm.AddCacheProfile(1, profile) + cm.AddPlayerProfile(1, profile) } }, ) } func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *PlayerProfile)) { - fields := []string{"account_id", "name", "head_id", "head_frame", "star_num", "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 - } - if rows.Next() { - aId := q5.ToString(*rows.GetByIndex(0)) - onlineStatue := playerMgr.GetOnlineStatus(aId) - 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)), - Star: q5.ToInt32(*rows.GetByIndex(4)), - Rank: q5.ToInt32(*rows.GetByIndex(5)), - LastLoginTime: q5.ToInt32(*rows.GetByIndex(6)), - OnlineStatus: onlineStatue, - } - cb(nil, profile) - } - cb(fmt.Errorf("no rows"), nil) - }, - ) + cm.loadUserProfile(accountId) + profile := cm.GetPlayerProfile(accountId) + cb(nil, profile) } diff --git a/server/imserver/cachemgr.go b/server/imserver/cachemgr.go index 5dd4108a..7f27b4d4 100644 --- a/server/imserver/cachemgr.go +++ b/server/imserver/cachemgr.go @@ -51,13 +51,6 @@ func (cm *CacheMgr) init() { func (cm *CacheMgr) UnInit() { } -func (cm *CacheMgr) GetPlayerProfile(accountId string) *PlayerProfile { - if profile, exists := cm.cachePlayerProfiles[accountId]; exists { - return profile.data - } - return nil -} - func (cm *CacheMgr) SyncGetUsers(accountIds []string, cb func(bool)) { } @@ -74,7 +67,6 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) { cm.cacheMutex.Lock() _, exists := cm.cachePlayerProfiles[accountId] cm.cacheMutex.Unlock() - if exists { mu.Lock() successCount++ @@ -82,20 +74,10 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) { return } - cm.GetProfileByAccountId(accountId, func(err error, playerProfile *PlayerProfile) { - if err != nil { - cb(false) - return - } - - cm.cacheMutex.Lock() - cm.AddCacheProfile(1, playerProfile) - cm.cacheMutex.Unlock() - - mu.Lock() - successCount++ - mu.Unlock() - }) + cm.loadUserProfile(accountId) + mu.Lock() + successCount++ + mu.Unlock() }(accountId) } @@ -107,24 +89,16 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) { } } -func (cm *CacheMgr) LoadPlayerProfile(user *User, cb func(*PlayerProfile)) { - if profile, exists := cm.cachePlayerProfiles[user.AccountId]; exists { - cb(profile.data) - return - } - cm.GetProfileByAccountId(user.AccountId, func(err error, profile *PlayerProfile) { - if err != nil { - cb(nil) - return - } - cm.cachePlayerProfiles[user.AccountId].data = profile - cb(profile) - }) -} - -func (cm *CacheMgr) AddCacheProfile(version int, playerProfile *PlayerProfile) { +func (cm *CacheMgr) AddPlayerProfile(version int, playerProfile *PlayerProfile) { cm.cachePlayerProfiles[playerProfile.AccountId] = &CachePlayerProfile{ version: version, data: playerProfile, } } + +func (cm *CacheMgr) GetPlayerProfile(accountId string) *PlayerProfile { + if profile, exists := cm.cachePlayerProfiles[accountId]; exists { + return profile.data + } + return nil +} diff --git a/server/imserver/friendsdbmgr.go b/server/imserver/friendsdbmgr.go index b12d2921..2e081fe1 100644 --- a/server/imserver/friendsdbmgr.go +++ b/server/imserver/friendsdbmgr.go @@ -212,7 +212,7 @@ func (fm *FriendsMgr) loadUserFriendships(user *User, where [][]string) { RequestTime: requestTime, } user2.Friendships[account1Id] = friendship2 - cacheMgr.LoadPlayerProfile(user2, func(playerProfile *PlayerProfile) {}) + cacheMgr.loadUserProfile(account2Id) } if user.AccountId == account2Id { @@ -224,7 +224,7 @@ func (fm *FriendsMgr) loadUserFriendships(user *User, where [][]string) { RequestTime: requestTime, } user.Friendships[account1Id] = friendship - cacheMgr.LoadPlayerProfile(friendUser, func(playerProfile *PlayerProfile) {}) + cacheMgr.loadUserProfile(account1Id) } } }, diff --git a/server/imserver/guildmgr.go b/server/imserver/guildmgr.go index 64274910..ea94fdff 100644 --- a/server/imserver/guildmgr.go +++ b/server/imserver/guildmgr.go @@ -368,10 +368,6 @@ func (gm *GuildMgr) JoinGuild(guild *Guild, accountId string, cb func(errCode in gm.AddUserGuild(accountId, guildId) cb(ERR_CODE_OK, "ApplyToGuild OK", guild) - - user := friendMgr.GetUser(accountId) - cacheMgr.LoadPlayerProfile(user, func(playerProfile *PlayerProfile) {}) - // Add event prop := make(map[string]string) prop["guild_id"] = q5.ToString(guild.GuildId) diff --git a/server/imserver/playermgr.go b/server/imserver/playermgr.go index b249108e..28f8c864 100644 --- a/server/imserver/playermgr.go +++ b/server/imserver/playermgr.go @@ -145,7 +145,7 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt playerProfile.TotalKills = 0 playerProfile.TotalWinTimes = 0 } - cacheMgr.AddCacheProfile(1, playerProfile) + cacheMgr.AddPlayerProfile(1, playerProfile) friendMgr.LoadUser(accountId) serverInfo := proto.String(mt.Table.IMCluster.GetServerInfo()) @@ -231,3 +231,65 @@ func (this *PlayerMgr) CMReconnect(hdr *f5.MsgHdr, msg *cs.CMReconnect) { hum.ReBind(hdr.GetSocket()) wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg) } + +// GetRemotePlayerInfo TODO 优化 +func (this *PlayerMgr) GetRemotePlayerInfo(player *Player, cb func(errCode int32, errMsg string, p *PlayerProfile)) { + params := map[string]string{ + "c": "User", + "a": "detailInfo", + "account_id": player.GetAccountId(), + "session_id": player.GetSessionId(), + "target_id": player.GetAccountId(), + } + url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl()) + f5.GetSysLog().Info("GetPlayerInfo url:%s, params:%+v\n", url, params) + f5.GetHttpCliMgr().SendJsStyleRequest( + url, + params, + func(rsp f5.HttpCliResponse) { + resObj := struct { + ErrCode int32 `json:"errcode"` + ErrMsg string `json:"errmsg"` + Info struct { + Activated string `json:"activated"` + AccountId string `json:"account_id"` + Name string `json:"name"` + Avatar string `json:"head_id"` + AvatarHead string `json:"head_frame"` + Star string `json:"current_star_num"` + Rank string `json:"current_rank"` + LastLoginTime string `json:"last_login_time"` + HistorySeasons []HistorySeasons `json:"history_seasons"` + } `json:"info"` + }{} + err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj) + if err != nil { + cb(ERR_CODE_GUILD_SETNAME_API_ERROR, "SetNameConsume Api服务器JSON解析错误", nil) + f5.GetSysLog().Info("SetNameConsume Api服务器JSON解析错误:%s\n", err) + return + } + if resObj.ErrCode != 0 { + cb(resObj.ErrCode, "Api服务器errcode", nil) + f5.GetSysLog().Error("Api服务器errcode:%d", resObj.ErrCode) + return + } + playerProfile := &PlayerProfile{ + AccountId: resObj.Info.AccountId, + Username: resObj.Info.Name, + Avatar: q5.ToInt32(resObj.Info.Avatar), + AvatarHead: q5.ToInt32(resObj.Info.AvatarHead), + Star: q5.ToInt32(resObj.Info.Star), + Rank: q5.ToInt32(resObj.Info.Rank), + OnlineStatus: this.GetOnlineStatus(resObj.Info.AccountId), + LastLoginTime: q5.ToInt32(resObj.Info.LastLoginTime), + } + if len(resObj.Info.HistorySeasons) == 1 { + playerProfile.TotalKills = q5.ToInt32(resObj.Info.HistorySeasons[0].TotalKills) + playerProfile.TotalWinTimes = q5.ToInt32(resObj.Info.HistorySeasons[0].WinTimes) + } else { + playerProfile.TotalKills = 0 + playerProfile.TotalWinTimes = 0 + } + cb(resObj.ErrCode, resObj.ErrMsg, playerProfile) + }) +}