1
This commit is contained in:
parent
d35fb38e12
commit
f857921035
@ -4,13 +4,11 @@ import (
|
||||
)
|
||||
|
||||
type MailMgr struct {
|
||||
allMailHash map[int64]*Mail
|
||||
currReqId int64
|
||||
lastFetchMailTick int64
|
||||
idHash map[int64]*Mail
|
||||
}
|
||||
|
||||
func (this *MailMgr) Init() {
|
||||
this.allMailHash = make(map[int64]*Mail)
|
||||
this.idHash = make(map[int64]*Mail)
|
||||
this.FetchMailFromDB()
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"main/global"
|
||||
)
|
||||
|
||||
var _playerMgr = new(PlayerMgr)
|
||||
var _playerMgr = new(playerMgr)
|
||||
|
||||
func init() {
|
||||
global.RegModule(constant.PLAYER_MGR_MODULE_IDX, _playerMgr)
|
||||
|
@ -2,11 +2,6 @@ package player
|
||||
|
||||
import (
|
||||
"f5"
|
||||
//"fmt"
|
||||
//"google.golang.org/protobuf/proto"
|
||||
//"main/constant"
|
||||
//"main/ss"
|
||||
"q5"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -23,7 +18,7 @@ type DeletedMail struct {
|
||||
expireTime int32
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
type player struct {
|
||||
sync.Mutex
|
||||
AccountId string
|
||||
SessionId string
|
||||
@ -36,164 +31,3 @@ type Player struct {
|
||||
attacher *f5.TimerAttacher
|
||||
dirty bool // 标记数据已修改
|
||||
}
|
||||
|
||||
func NewPlayer(accountId string) *Player {
|
||||
return &Player{
|
||||
AccountId: accountId,
|
||||
ReadMailHash: make(map[int64]*ReadMail),
|
||||
DeletedMailHash: make(map[int64]*DeletedMail),
|
||||
CacheExpiration: time.Now().Add(20 * time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Player) GetAccountId() string {
|
||||
return p.AccountId
|
||||
}
|
||||
|
||||
func (p *Player) GetRegisterTime() int32 {
|
||||
return p.RegisterTime
|
||||
}
|
||||
|
||||
func (p *Player) IsUnreadMail(mailId int64) bool {
|
||||
_, exists := p.ReadMailHash[mailId]
|
||||
return !exists
|
||||
}
|
||||
|
||||
func (p *Player) IsDeletedMail(mailId int64) bool {
|
||||
m := p.DeletedMailHash[mailId]
|
||||
return m != nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
|
||||
var nextDaySec int32 = 3600 * 24
|
||||
nowUnixSec := int32(time.Now().Unix())
|
||||
for _, MFReadMail := range accountPB.GetReadMailList() {
|
||||
expireTime := MFReadMail.GetExpireTime() + nextDaySec
|
||||
if expireTime > nowUnixSec {
|
||||
readMail := &ReadMail{
|
||||
mailId: MFReadMail.GetMailId(),
|
||||
readTime: MFReadMail.GetReadTime(),
|
||||
expireTime: MFReadMail.GetExpireTime(),
|
||||
}
|
||||
p.ReadMailHash[readMail.mailId] = readMail
|
||||
|
||||
if readMail.readTime <= 1590577793 {
|
||||
delMail := &DeletedMail{
|
||||
mailId: readMail.mailId,
|
||||
deleteTime: readMail.readTime,
|
||||
expireTime: readMail.expireTime,
|
||||
}
|
||||
p.DeletedMailHash[delMail.mailId] = delMail
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, MFDeletedMail := range accountPB.GetDeletedMailList() {
|
||||
expireTime := MFDeletedMail.GetExpireTime() + nextDaySec
|
||||
if expireTime > nowUnixSec {
|
||||
deleteMail := &DeletedMail{
|
||||
mailId: MFDeletedMail.GetMailId(),
|
||||
deleteTime: MFDeletedMail.GetDeleteTime(),
|
||||
expireTime: MFDeletedMail.GetExpireTime(),
|
||||
}
|
||||
p.DeletedMailHash[deleteMail.mailId] = deleteMail
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (p *Player) Serialize(accountPB *ss.MFAccountData) {
|
||||
var nextDaySec int32 = 3600 * 24
|
||||
nowUnixSec := int32(time.Now().Unix())
|
||||
for _, readMail := range p.ReadMailHash {
|
||||
if readMail.expireTime+nextDaySec > nowUnixSec {
|
||||
p2 := &ss.MFReadMail{
|
||||
MailId: &readMail.mailId,
|
||||
ReadTime: &readMail.readTime,
|
||||
ExpireTime: &readMail.expireTime,
|
||||
}
|
||||
accountPB.ReadMailList = append(accountPB.ReadMailList, p2)
|
||||
}
|
||||
}
|
||||
|
||||
for _, deletedMail := range p.DeletedMailHash {
|
||||
if deletedMail.expireTime+nextDaySec > nowUnixSec {
|
||||
p3 := &ss.MFDeletedMail{
|
||||
MailId: &deletedMail.mailId,
|
||||
DeleteTime: &deletedMail.deleteTime,
|
||||
ExpireTime: &deletedMail.expireTime,
|
||||
}
|
||||
accountPB.DeletedMailList = append(accountPB.DeletedMailList, p3)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func (p *Player) UpdateExpire() {
|
||||
p.CacheExpiration = time.Now().Add(20 * time.Second)
|
||||
}
|
||||
|
||||
func (p *Player) IsCacheNotExpired() bool {
|
||||
return time.Now().Before(p.CacheExpiration)
|
||||
}
|
||||
|
||||
func (p *Player) IsCacheExpired() bool {
|
||||
return time.Now().After(p.CacheExpiration)
|
||||
}
|
||||
|
||||
func (p *Player) MarkDirty() {
|
||||
// f5.GetApp().RegisterMainThreadCb(func() {})
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
p.dirty = true // 标记数据已修改
|
||||
p.ScheduleSave()
|
||||
}
|
||||
|
||||
func (p *Player) ScheduleSave() {
|
||||
if p.dirtyTimer != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.attacher = f5.GetTimer().NewTimerAttacher()
|
||||
p.dirtyTimer = f5.GetTimer().SetTimeoutExWp(
|
||||
10000,
|
||||
func(e int32, args *q5.Args) {
|
||||
if e == q5.TIMER_EXEC_EVENT {
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
if p.dirty {
|
||||
//p.SaveToDB() // Persistence, Save TO DB
|
||||
p.dirty = false // 重置标志
|
||||
}
|
||||
}
|
||||
},
|
||||
p.attacher)
|
||||
}
|
||||
|
||||
/*
|
||||
func (p *Player) SaveToDB() {
|
||||
accountPB := ss.MFAccountData{}
|
||||
p.Serialize(&accountPB)
|
||||
blobData, err := proto.Marshal(&accountPB)
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("SaveToDB proto.Marshal Error:%v accountId:%s \n", err, p.GetAccountId())
|
||||
return
|
||||
}
|
||||
blobDataStr := string(blobData)
|
||||
nowUnixSec := time.Now().Unix()
|
||||
sql := fmt.Sprintf("INSERT INTO account_data (accountid, blobdata, createtime, modifytime) VALUES ('%s', '%s', %d, %d) ON DUPLICATE KEY UPDATE blobdata = '%s', modifytime = %d",
|
||||
p.GetAccountId(), blobDataStr, nowUnixSec, nowUnixSec, blobDataStr, nowUnixSec)
|
||||
|
||||
f5.GetGoStyleDb().SyncSelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
sql,
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("SaveToDB Error:%v accountId:%s\n", err, p.GetAccountId())
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
*/
|
||||
|
@ -1,38 +0,0 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"fmt"
|
||||
//"github.com/golang/protobuf/proto"
|
||||
"main/constant"
|
||||
//"main/ss"
|
||||
//"q5"
|
||||
)
|
||||
|
||||
func (pm *PlayerMgr) LoadPlayer(accountId string, cb func(err error, p *Player)) {
|
||||
sql := fmt.Sprintf("SELECT accountid, blobdata FROM account_data WHERE accountid='%s'", accountId)
|
||||
f5.GetGoStyleDb().SyncSelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
sql,
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("loadPlayer err:%v \n", err)
|
||||
cb(fmt.Errorf("loadPlayer err"), nil)
|
||||
return
|
||||
}
|
||||
/*
|
||||
if rows.Next() {
|
||||
aId := q5.ToString(*rows.GetByName("accountid"))
|
||||
data := q5.ToString(*rows.GetByName("blobdata"))
|
||||
|
||||
profile := NewPlayer(aId)
|
||||
accountPB := &ss.MFAccountData{}
|
||||
err = proto.Unmarshal([]byte(data), accountPB)
|
||||
profile.Deserialize(accountPB)
|
||||
cb(nil, profile)
|
||||
} else {
|
||||
cb(fmt.Errorf("player nil"), nil)
|
||||
}*/
|
||||
},
|
||||
)
|
||||
}
|
@ -1,51 +1,15 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type PlayerMgr struct {
|
||||
accountIdHash map[string]*Player
|
||||
type playerMgr struct {
|
||||
accountIdHash map[string]*player
|
||||
}
|
||||
|
||||
func (pm *PlayerMgr) Init() {
|
||||
pm.accountIdHash = make(map[string]*Player)
|
||||
func (this *playerMgr) Init() {
|
||||
this.accountIdHash = make(map[string]*player)
|
||||
}
|
||||
|
||||
func (pm *PlayerMgr) UnInit() {
|
||||
}
|
||||
|
||||
func (pm *PlayerMgr) AddPlayer(p *Player) {
|
||||
pm.accountIdHash[p.AccountId] = p
|
||||
}
|
||||
|
||||
func (pm *PlayerMgr) GetPlayer(accountId string) *Player {
|
||||
if p, exists := pm.accountIdHash[accountId]; exists {
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
|
||||
p := pm.GetPlayer(accountId)
|
||||
if p != nil && p.IsCacheNotExpired() {
|
||||
return p
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
var player *Player = nil
|
||||
go func(accountId string) {
|
||||
pm.LoadPlayer(accountId, func(err error, p *Player) {
|
||||
defer wg.Done()
|
||||
if err == nil && p != nil {
|
||||
player = p
|
||||
pm.accountIdHash[p.GetAccountId()] = p
|
||||
}
|
||||
})
|
||||
}(accountId)
|
||||
wg.Wait()
|
||||
|
||||
return player
|
||||
func (this *playerMgr) UnInit() {
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user