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) + }) +}