This commit is contained in:
aozhiwei 2024-02-17 20:29:43 +08:00
parent f5ffe77505
commit eae4eba9f1
4 changed files with 33 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import (
"q5" "q5"
"time" "time"
"main/constant" "main/constant"
"main/common"
. "main/global" . "main/global"
) )
@ -102,7 +103,7 @@ func (cm *CacheMgr) loadUserProfile(accountId string) {
for rows.Next() { for rows.Next() {
aId := q5.ToString(rows.GetByName("account_id")) aId := q5.ToString(rows.GetByName("account_id"))
onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId) onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &PlayerProfile{ profile := &common.PlayerProfile{
AccountId: aId, AccountId: aId,
Username: q5.ToString(rows.GetByName("name")), Username: q5.ToString(rows.GetByName("name")),
Avatar: q5.ToInt32(rows.GetByName("head_id")), Avatar: q5.ToInt32(rows.GetByName("head_id")),
@ -124,7 +125,7 @@ func (cm *CacheMgr) getCacheVersion() int {
return week return week
} }
func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *PlayerProfile)) { func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *common.PlayerProfile)) {
cm.loadUserProfile(accountId) cm.loadUserProfile(accountId)
profile := cm.GetPlayerProfile(accountId) profile := cm.GetPlayerProfile(accountId)
cb(nil, profile) cb(nil, profile)

View File

@ -2,30 +2,13 @@ package cache
import ( import (
"sync" "sync"
"main/common"
) )
type PlayerProfile struct {
AccountId string
Username string // 用户名
Avatar int32 // 头像
AvatarHead int32 // 头像框
Star int32 // 星星
TotalKills int32 // 总击杀数
TotalWinTimes int32 // 总赢数
Rank int32 // 排位赛段位
OnlineStatus int32 // 在线状态
LastLoginTime int32 // 上次登录时间
}
type CachePlayerProfile struct {
version int
data *PlayerProfile
}
type CacheMgr struct { type CacheMgr struct {
mu sync.Mutex mu sync.Mutex
cacheMutex sync.Mutex cacheMutex sync.Mutex
cachePlayerProfiles map[string]*CachePlayerProfile cachePlayerProfiles map[string]*common.CachePlayerProfile
} }
type Set map[string]struct{} type Set map[string]struct{}
@ -44,7 +27,7 @@ func (s Set) Delete(key string) {
} }
func (cm *CacheMgr) init() { func (cm *CacheMgr) init() {
cm.cachePlayerProfiles = make(map[string]*CachePlayerProfile) cm.cachePlayerProfiles = make(map[string]*common.CachePlayerProfile)
cm.LoadFromDB() cm.LoadFromDB()
} }
@ -66,7 +49,7 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
cm.cacheMutex.Lock() cm.cacheMutex.Lock()
cp, exists := cm.cachePlayerProfiles[accountId] cp, exists := cm.cachePlayerProfiles[accountId]
cm.cacheMutex.Unlock() cm.cacheMutex.Unlock()
if exists && cp.version == cm.getCacheVersion() { if exists && cp.Version == cm.getCacheVersion() {
mu.Lock() mu.Lock()
successCount++ successCount++
mu.Unlock() mu.Unlock()
@ -88,16 +71,16 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
} }
} }
func (cm *CacheMgr) AddPlayerProfile(version int, playerProfile *PlayerProfile) { func (cm *CacheMgr) AddPlayerProfile(version int, playerProfile *common.PlayerProfile) {
cm.cachePlayerProfiles[playerProfile.AccountId] = &CachePlayerProfile{ cm.cachePlayerProfiles[playerProfile.AccountId] = &common.CachePlayerProfile{
version: version, Version: version,
data: playerProfile, Data: playerProfile,
} }
} }
func (cm *CacheMgr) GetPlayerProfile(accountId string) *PlayerProfile { func (cm *CacheMgr) GetPlayerProfile(accountId string) *common.PlayerProfile {
if profile, exists := cm.cachePlayerProfiles[accountId]; exists { if profile, exists := cm.cachePlayerProfiles[accountId]; exists {
return profile.data return profile.Data
} }
return nil return nil
} }

View File

@ -70,3 +70,21 @@ type GuildMgr interface {
GetGuildByAccountId(string) Guild GetGuildByAccountId(string) Guild
GetGuildIdByAccountId(string) int64 GetGuildIdByAccountId(string) int64
} }
type PlayerProfile struct {
AccountId string
Username string // 用户名
Avatar int32 // 头像
AvatarHead int32 // 头像框
Star int32 // 星星
TotalKills int32 // 总击杀数
TotalWinTimes int32 // 总赢数
Rank int32 // 排位赛段位
OnlineStatus int32 // 在线状态
LastLoginTime int32 // 上次登录时间
}
type CachePlayerProfile struct {
Version int
Data *PlayerProfile
}

View File

@ -3,6 +3,8 @@ package initialize
import ( import (
_ "main/app" _ "main/app"
_ "main/cache" _ "main/cache"
_ "main/chat"
_ "main/friend"
_ "main/listener" _ "main/listener"
. "main/global" . "main/global"