session清理优化

This commit is contained in:
wh_zcy 2022-12-30 15:13:45 +08:00
parent ba1d564673
commit 868edf1e8b
3 changed files with 83 additions and 21 deletions

View File

@ -4,5 +4,5 @@ Website = "http://legu.cc"
Icon = "app.png"
Name = "RobotGUI"
ID = "cc.legu.app"
Version = "1.2.3"
Build = 31
Version = "1.2.4"
Build = 32

View File

@ -12,6 +12,7 @@ import (
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"strings"
"sync"
"time"
"go_dreamfactory/lego/base"
@ -55,6 +56,8 @@ type User struct {
configure *configureComp
globalConf *cfg.GameGlobalData
modelSign *ModelSign // 签到
timerLock sync.Mutex
timerMap map[string]*time.Ticker
}
func (this *User) GetType() core.M_Modules {
@ -64,6 +67,7 @@ func (this *User) GetType() core.M_Modules {
func (this *User) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
this.timerMap = make(map[string]*time.Ticker)
return
}
@ -78,6 +82,7 @@ func (this *User) Start() (err error) {
if this.globalConf == nil {
err = errors.New("global config not found")
}
this.ResetSession()
return
}
@ -132,8 +137,20 @@ func (this *User) GetUserSession(uid string) *pb.CacheUser {
return this.modelSession.getUserSession(uid)
}
func (this *User) ResetSession() {
us, err := this.UserOnlineList()
if err != nil {
return
}
for _, v := range us {
this.modelSession.DelListlds(comm.RDS_EMPTY, v.Uid)
}
}
// 清除session
func (this *User) CleanSession(session comm.IUserSession) {
this.stopTicker(session.GetUserId())
if !this.IsCross() {
this.modelUser.updateOfflineTime(session.GetUserId())
}
@ -615,9 +632,29 @@ func (this *User) BingoSetUserLv(session comm.IUserSession, lv int32) error {
return nil
}
func (this *User) Update() {
if this.IsCross() {
return
}
cu, err := this.UserOnlineList()
if err != nil {
return
}
for _, v := range cu {
if isession, ok := this.ModuleBase.GetUserSession(v.Uid); ok {
//del session
log.Debug("del session", log.Field{Key: "uid", Value: v.Uid}, log.Field{Key: "isLogin", Value: isession.IsLogin()})
}
}
}
func (this *User) RecoverUserVitStart(uid string) {
go func(uid string) {
timeSec := time.NewTicker(time.Second * 30)
this.timerLock.Lock()
this.timerMap[uid] = timeSec
this.timerLock.Unlock()
for {
select {
case <-timeSec.C:
@ -627,6 +664,15 @@ func (this *User) RecoverUserVitStart(uid string) {
}(uid)
}
func (this *User) stopTicker(uid string) {
if t, ok := this.timerMap[uid]; ok {
if t != nil {
t.Stop()
delete(this.timerMap, uid)
}
}
}
// 玩家体力恢复
func (this *User) recoverUserVit(uid string) {
if this.IsCross() {
@ -642,7 +688,11 @@ func (this *User) recoverUserVit(uid string) {
return
}
var yu int32
var (
yu int32
add int32
changed int32
)
cur := time.Now().Unix()
if u.LastRecoverVitSec == 0 {
update := map[string]interface{}{
@ -660,19 +710,31 @@ func (this *User) recoverUserVit(uid string) {
if pconf == nil {
return
}
if u.Ps < pconf.PsCeiling {
total := u.Ps + yu
if total <= pconf.PsCeiling {
if total > pconf.PsCeiling {
add = pconf.PsCeiling - u.Ps
changed = pconf.PsCeiling
} else {
add = yu
changed = total
}
} else {
add = 0
}
if isession, ok := this.ModuleBase.GetUserSession(u.Uid); ok {
if code := this.AddAttributeValue(isession, comm.ResPs, yu, false); code == pb.ErrorCode_Success {
if code := this.AddAttributeValue(isession, comm.ResPs, add, false); code == pb.ErrorCode_Success {
update := map[string]interface{}{
"lastRecoverVitSec": cur,
}
if err := this.modelUser.Change(u.Uid, update); err == nil {
if changed > 0 {
if err := this.SendMsgToUser(string(this.GetType()), "vitchanged",
&pb.UserVitChangedPush{Ps: total}, u.Uid); err != nil {
&pb.UserVitChangedPush{Ps: changed}, u.Uid); err != nil {
this.Error("玩家体力变化 UserVitChangedPush推送失败",
log.Field{Key: "uid", Value: u.Uid},
log.Field{Key: comm.ResPs, Value: total},
log.Field{Key: comm.ResPs, Value: changed},
)
}
}

View File

@ -77,18 +77,18 @@ func TestMatrxing(t *testing.T) {
utils.MatrixingHour("2022-10-11 00:00:00")
}
func TestCompre(t *testing.T){
a:=[]int32{1,2,4,5}
b:=[]int32{1,2,4}
fmt.Println(utils.ForContainer(a,b))
func TestCompre(t *testing.T) {
a := []int32{1, 2, 4, 5}
b := []int32{1, 2, 4}
fmt.Println(utils.ForContainer(a, b))
}
func TestDeletex(t *testing.T){
a:=[]int32{1,2,3,4}
c:=utils.Deletex(a, 2)
func TestDeletex(t *testing.T) {
a := []int32{1, 2, 3, 4}
c := utils.Deletex(a, 2)
fmt.Println(c)
}
func TestDiffDays(t *testing.T){
func TestDiffDays(t *testing.T) {
fmt.Println(utils.DiffDays(1614527999, 1614614400))
}