aozhiwei 7e2b19c64b 1
2024-02-17 19:58:58 +08:00

132 lines
3.2 KiB
Go

package main
import (
"f5"
"fmt"
"q5"
"time"
"main/constant"
. "main/global"
)
func (cm *CacheMgr) LoadFromDB() {
uniAccountIds := Set{}
uniAccountIds = make(Set)
// Load friendships
{
lastIdx := int64(0)
done := false
for !done {
f5.GetGoStyleDb().SyncSelectCustomQuery(
constant.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(
constant.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
}
func (cm *CacheMgr) loadUserProfile(accountId string) {
// "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(
constant.GAME_DB,
sql,
func(err error, rows *f5.DataSet) {
if err != nil {
f5.GetSysLog().Info("loadUsersProfile err:%v \n", err)
panic(err)
}
for rows.Next() {
aId := q5.ToString(rows.GetByName("account_id"))
onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &PlayerProfile{
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.AddPlayerProfile(cm.getCacheVersion(), profile)
}
},
)
}
func (cm *CacheMgr) getCacheVersion() int {
today := time.Now()
_, week := today.ISOWeek()
return week
}
func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *PlayerProfile)) {
cm.loadUserProfile(accountId)
profile := cm.GetPlayerProfile(accountId)
cb(nil, profile)
}