save
This commit is contained in:
parent
a1d42ce6e6
commit
ef23d1aad3
@ -4,6 +4,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"main/global"
|
||||
"main/mail"
|
||||
"main/player"
|
||||
)
|
||||
|
||||
type ApiGroup struct {
|
||||
@ -12,6 +13,14 @@ type ApiGroup struct {
|
||||
|
||||
var ApiGroupApp = new(ApiGroup)
|
||||
|
||||
func GetPlayerMgr() *player.PlayerMgr {
|
||||
playerMgrPtr, ok := global.GetPlayerMgr().(*player.PlayerMgr)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return playerMgrPtr
|
||||
}
|
||||
|
||||
func GetMailMgr() *mail.MailMgr {
|
||||
mailMgrPtr, ok := global.GetMailMgr().(*mail.MailMgr)
|
||||
if !ok {
|
||||
|
@ -6,8 +6,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type MailApi struct {
|
||||
}
|
||||
type MailApi struct{}
|
||||
|
||||
// 获取我的邮件列表
|
||||
type getMailsReq struct {
|
||||
@ -24,6 +23,17 @@ func (api *MailApi) GetMailList(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 加载player 慢
|
||||
playerMgr := GetPlayerMgr()
|
||||
accountObj := playerMgr.AsyncGetPlayer(req.AccountId)
|
||||
if accountObj == nil {
|
||||
err := fmt.Errorf("account is null")
|
||||
c.JSON(http.StatusBadRequest, errorResponse(400, err))
|
||||
return
|
||||
}
|
||||
accountObj.Mutex.Lock()
|
||||
defer accountObj.Mutex.Unlock()
|
||||
|
||||
mailMgr := GetMailMgr()
|
||||
if mailMgr == nil {
|
||||
err := fmt.Errorf("mailMgr is null")
|
||||
@ -31,6 +41,7 @@ func (api *MailApi) GetMailList(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// accountObj
|
||||
mails := mailMgr.GetMails(req.AccountId)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"errcode": 0,
|
||||
|
@ -26,6 +26,10 @@ func GetMailMgr() common.MailMgr {
|
||||
return mailMgr
|
||||
}
|
||||
|
||||
func GetPlayerMgr() common.MailMgr {
|
||||
return playerMgr
|
||||
}
|
||||
|
||||
func RegModule(idx int32, m q5.Module) {
|
||||
fmt.Printf("RegModule module %d\n", idx)
|
||||
modules[idx] = m
|
||||
|
@ -1,4 +1,4 @@
|
||||
module mailsever
|
||||
module mailserver
|
||||
|
||||
go 1.20
|
||||
|
||||
@ -14,6 +14,7 @@ require main v1.0.0
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
google.golang.org/protobuf v1.31.0
|
||||
gorm.io/driver/mysql v1.5.1
|
||||
gorm.io/gorm v1.25.4
|
||||
)
|
||||
@ -29,6 +30,7 @@ require (
|
||||
github.com/go-playground/validator/v10 v10.15.4 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/golang/protobuf v1.5.0 // indirect
|
||||
github.com/gomodule/redigo v1.8.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
@ -46,7 +48,6 @@ require (
|
||||
golang.org/x/net v0.15.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
|
@ -29,6 +29,7 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/gomodule/redigo v1.8.3 h1:HR0kYDX2RJZvAup8CsiJwxB4dTCSC0AaUq6S4SiLwUc=
|
||||
github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
|
||||
|
@ -1,62 +0,0 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"fmt"
|
||||
"main/constant"
|
||||
"q5"
|
||||
)
|
||||
|
||||
var LoadRound uint32 = 0
|
||||
|
||||
func (mm *MailMgr) LoadFromDB() {
|
||||
// LoadRound++
|
||||
// f5.GetSysLog().Info("LoadFromDB Start round:%d", LoadRound)
|
||||
|
||||
lastIdx := int64(0)
|
||||
// unixSec := time.Now().Unix()
|
||||
unixSec := 0
|
||||
limit := 1000
|
||||
|
||||
done := false
|
||||
for !done {
|
||||
sql := fmt.Sprintf("SELECT idx, gameid, mailid, _from, _to, subject, content, mailtype, mailsubtype, sendtime, expiretime, attachments, createtime, ext, usertype FROM mailbox WHERE idx > %d AND deleted=0 AND expiretime > %d limit %d", lastIdx, unixSec, limit)
|
||||
f5.GetGoStyleDb().SyncSelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
sql,
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("LoadFromDB Error:%v \n", err)
|
||||
panic(err)
|
||||
}
|
||||
empty := true
|
||||
for rows.Next() {
|
||||
empty = false
|
||||
|
||||
m := newMail()
|
||||
m.Gameid = q5.ToInt(*rows.GetByName("gameid"))
|
||||
m.MailId = q5.ToInt64(*rows.GetByName("mailid"))
|
||||
m.From = q5.ToString(*rows.GetByName("_from"))
|
||||
m.To = q5.ToString(*rows.GetByName("_to"))
|
||||
m.Subject = q5.ToString(*rows.GetByName("subject"))
|
||||
m.Content = q5.ToString(*rows.GetByName("content"))
|
||||
m.Mailtype = q5.ToInt(*rows.GetByName("mailtype"))
|
||||
m.Mailsubtype = q5.ToInt(*rows.GetByName("mailsubtype"))
|
||||
m.Usertype = q5.ToInt(*rows.GetByName("usertype"))
|
||||
m.SendTime = q5.ToInt(*rows.GetByName("sendtime"))
|
||||
m.Expiretime = q5.ToInt(*rows.GetByName("expiretime"))
|
||||
m.Createtime = q5.ToInt(*rows.GetByName("createtime"))
|
||||
// parse ATT
|
||||
m.ParseAttachments(q5.ToString(*rows.GetByName("attachments")))
|
||||
|
||||
mm.AddMail(m)
|
||||
lastIdx = q5.ToInt64(*rows.GetByName("idx"))
|
||||
}
|
||||
if empty {
|
||||
done = true
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
// f5.GetSysLog().Info("LoadFromDB Finished round:%d, mails:%d", LoadRound, len(mm.allMailHash))
|
||||
}
|
@ -2,7 +2,9 @@ package mail
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"main/common"
|
||||
"fmt"
|
||||
"main/constant"
|
||||
"main/player"
|
||||
"q5"
|
||||
)
|
||||
|
||||
@ -12,7 +14,7 @@ type MailMgr struct {
|
||||
playerMailHash map[string]map[int64]*Mail
|
||||
currReqId int64
|
||||
lastFetchMailTick int64
|
||||
accountHash map[string]*common.Player
|
||||
accountHash map[string]*player.Player
|
||||
}
|
||||
|
||||
func (mm *MailMgr) Init() {
|
||||
@ -34,8 +36,50 @@ func (mm *MailMgr) FetchMailFromDB() {
|
||||
})
|
||||
}
|
||||
|
||||
func (mm *MailMgr) GetAccountObject(accountId string) *common.Player {
|
||||
return nil
|
||||
//p := itr = account_hash_.find(accountid);
|
||||
//return itr != account_hash_.end() ? itr->second : nullptr
|
||||
func (mm *MailMgr) LoadFromDB() {
|
||||
lastIdx := int64(0)
|
||||
// unixSec := time.Now().Unix()
|
||||
unixSec := 0
|
||||
limit := 1000
|
||||
|
||||
done := false
|
||||
for !done {
|
||||
sql := fmt.Sprintf("SELECT idx, gameid, mailid, _from, _to, subject, content, mailtype, mailsubtype, sendtime, expiretime, attachments, createtime, ext, usertype FROM mailbox WHERE idx > %d AND deleted=0 AND expiretime > %d limit %d", lastIdx, unixSec, limit)
|
||||
f5.GetGoStyleDb().SyncSelectCustomQuery(
|
||||
constant.MAIL_DB,
|
||||
sql,
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("LoadFromDB Error:%v \n", err)
|
||||
panic(err)
|
||||
}
|
||||
empty := true
|
||||
for rows.Next() {
|
||||
empty = false
|
||||
|
||||
m := newMail()
|
||||
m.Gameid = q5.ToInt(*rows.GetByName("gameid"))
|
||||
m.MailId = q5.ToInt64(*rows.GetByName("mailid"))
|
||||
m.From = q5.ToString(*rows.GetByName("_from"))
|
||||
m.To = q5.ToString(*rows.GetByName("_to"))
|
||||
m.Subject = q5.ToString(*rows.GetByName("subject"))
|
||||
m.Content = q5.ToString(*rows.GetByName("content"))
|
||||
m.Mailtype = q5.ToInt(*rows.GetByName("mailtype"))
|
||||
m.Mailsubtype = q5.ToInt(*rows.GetByName("mailsubtype"))
|
||||
m.Usertype = q5.ToInt(*rows.GetByName("usertype"))
|
||||
m.SendTime = q5.ToInt(*rows.GetByName("sendtime"))
|
||||
m.Expiretime = q5.ToInt(*rows.GetByName("expiretime"))
|
||||
m.Createtime = q5.ToInt(*rows.GetByName("createtime"))
|
||||
// parse ATT
|
||||
m.ParseAttachments(q5.ToString(*rows.GetByName("attachments")))
|
||||
|
||||
mm.AddMail(m)
|
||||
lastIdx = q5.ToInt64(*rows.GetByName("idx"))
|
||||
}
|
||||
if empty {
|
||||
done = true
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -1,19 +1,35 @@
|
||||
package player
|
||||
|
||||
import "main/mail"
|
||||
import "main/ss"
|
||||
import (
|
||||
"main/ss"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
sync.Mutex
|
||||
AccountId string
|
||||
SessionId string
|
||||
RegisterTime int
|
||||
ReadMailHash map[int64]*mail.Mail
|
||||
DeletedMailHash map[int64]*mail.Mail
|
||||
ReadMailHash map[int64]*ReadMail
|
||||
DeletedMailHash map[int64]*DeletedMail
|
||||
}
|
||||
|
||||
func (p *Player) init() {
|
||||
p.ReadMailHash = make(map[int64]*mail.Mail)
|
||||
p.DeletedMailHash = make(map[int64]*mail.Mail)
|
||||
type ReadMail struct {
|
||||
mailId int64
|
||||
readTime int32
|
||||
expireTime int32
|
||||
}
|
||||
|
||||
type DeletedMail struct {
|
||||
mailId int64
|
||||
deleteTime int32
|
||||
expireTime int32
|
||||
}
|
||||
|
||||
func (p *Player) Init() {
|
||||
p.ReadMailHash = make(map[int64]*ReadMail)
|
||||
p.DeletedMailHash = make(map[int64]*DeletedMail)
|
||||
}
|
||||
|
||||
func (p *Player) IsUnreadMail(mailId int64) bool {
|
||||
@ -31,7 +47,37 @@ func (p *Player) DeleteMails(mailIds string) {}
|
||||
func (p *Player) AddToReadList(mailIds string) {}
|
||||
func (p *Player) GetAttachment(mailIds string) {}
|
||||
|
||||
func (p *Player) Deserialize(accountPB ss.MFAccountData) {}
|
||||
func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
|
||||
var nextDaySec int32 = 3600 * 24
|
||||
unixSec := int32(time.Now().Unix())
|
||||
|
||||
for _, MFReadMail := range accountPB.GetReadMailList() {
|
||||
expireTime := MFReadMail.GetExpireTime() + nextDaySec
|
||||
if expireTime < unixSec {
|
||||
continue
|
||||
}
|
||||
readMail := &ReadMail{
|
||||
mailId: MFReadMail.GetMailId(),
|
||||
readTime: MFReadMail.GetReadTime(),
|
||||
expireTime: MFReadMail.GetExpireTime(),
|
||||
}
|
||||
p.ReadMailHash[readMail.mailId] = readMail
|
||||
}
|
||||
|
||||
for _, MFDeletedMail := range accountPB.GetDeletedMailList() {
|
||||
expireTime := MFDeletedMail.GetExpireTime() + nextDaySec
|
||||
if expireTime < unixSec {
|
||||
continue
|
||||
}
|
||||
|
||||
deleteMail := &DeletedMail{
|
||||
mailId: MFDeletedMail.GetMailId(),
|
||||
deleteTime: MFDeletedMail.GetDeleteTime(),
|
||||
expireTime: MFDeletedMail.GetExpireTime(),
|
||||
}
|
||||
p.DeletedMailHash[deleteMail.mailId] = deleteMail
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Player) Serialize(accountPB ss.MFAccountData) {
|
||||
|
||||
|
@ -3,11 +3,13 @@ package player
|
||||
import (
|
||||
"f5"
|
||||
"fmt"
|
||||
"mailsever/constant"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"main/constant"
|
||||
"main/ss"
|
||||
"q5"
|
||||
)
|
||||
|
||||
func (pm *playerMgr) LoadPlayer(accountId string) {
|
||||
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,
|
||||
@ -15,14 +17,25 @@ func (pm *playerMgr) LoadPlayer(accountId string) {
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("loadPlayer err:%v \n", err)
|
||||
panic(err)
|
||||
cb(fmt.Errorf("loadPlayer err"), nil)
|
||||
return
|
||||
}
|
||||
if rows.Next() {
|
||||
aId := q5.ToString(*rows.GetByName("account_id"))
|
||||
aId := q5.ToString(*rows.GetByName("accountid"))
|
||||
data := q5.ToString(*rows.GetByName("blobdata"))
|
||||
|
||||
profile := &Player{
|
||||
AccountId: aId,
|
||||
}
|
||||
pm.AddPlayer(profile)
|
||||
profile.Init()
|
||||
|
||||
accountPB := &ss.MFAccountData{}
|
||||
err = proto.Unmarshal([]byte(data), accountPB)
|
||||
profile.Deserialize(accountPB)
|
||||
|
||||
cb(nil, profile)
|
||||
} else {
|
||||
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -1,23 +1,41 @@
|
||||
package player
|
||||
|
||||
type playerMgr struct {
|
||||
import "sync"
|
||||
|
||||
type PlayerMgr struct {
|
||||
accountIdHash map[string]*Player
|
||||
}
|
||||
|
||||
func (pm *playerMgr) Init() {
|
||||
func (pm *PlayerMgr) Init() {
|
||||
pm.accountIdHash = make(map[string]*Player)
|
||||
}
|
||||
|
||||
func (pm *playerMgr) UnInit() {
|
||||
func (pm *PlayerMgr) UnInit() {
|
||||
}
|
||||
|
||||
func (pm *playerMgr) AddPlayer(p *Player) {
|
||||
func (pm *PlayerMgr) AddPlayer(p *Player) {
|
||||
pm.accountIdHash[p.AccountId] = p
|
||||
}
|
||||
|
||||
func (pm *playerMgr) GetPlayer(accountId string) *Player {
|
||||
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 {
|
||||
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()
|
||||
player = p
|
||||
})
|
||||
}(accountId)
|
||||
wg.Wait()
|
||||
|
||||
return player
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user