From 868edf1e8b22de1c9203a0c7176cb4f528d99930 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Fri, 30 Dec 2022 15:13:45 +0800 Subject: [PATCH] =?UTF-8?q?session=E6=B8=85=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/v2/FyneApp.toml | 4 +- modules/user/module.go | 84 ++++++++++++++++++++++++++++++++++++------ utils/utils_test.go | 16 ++++---- 3 files changed, 83 insertions(+), 21 deletions(-) diff --git a/cmd/v2/FyneApp.toml b/cmd/v2/FyneApp.toml index 1e39ea0ea..ece785254 100644 --- a/cmd/v2/FyneApp.toml +++ b/cmd/v2/FyneApp.toml @@ -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 diff --git a/modules/user/module.go b/modules/user/module.go index 85796dcc7..044204b48 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -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 } - total := u.Ps + yu - if total <= pconf.PsCeiling { - if isession, ok := this.ModuleBase.GetUserSession(u.Uid); ok { - if code := this.AddAttributeValue(isession, comm.ResPs, yu, false); code == pb.ErrorCode_Success { - update := map[string]interface{}{ - "lastRecoverVitSec": cur, - } - if err := this.modelUser.Change(u.Uid, update); err == nil { + if u.Ps < pconf.PsCeiling { + total := u.Ps + yu + 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, 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}, ) } } diff --git a/utils/utils_test.go b/utils/utils_test.go index 29bd83e45..556cbe082 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -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)) } \ No newline at end of file