Files
bl/logic/service/player/save.go

75 lines
1.7 KiB
Go
Raw Normal View History

package player
import (
"blazing/common/data/share"
"blazing/cool"
"fmt"
2025-11-15 22:17:43 +00:00
"blazing/logic/service/space"
"context"
"time"
)
// Save 保存玩家数据
func (p *Player) Save() {
cool.CacheManager.Remove(context.TODO(), fmt.Sprintf("player:%d", p.Info.UserID))
if cool.Config.ServerInfo.IsVip != 0 {
cool.Logger.Info(context.TODO(), "测试服不保存玩家数据", p.Info.UserID)
return
}
if p.Info == nil {
return
}
2026-01-27 06:40:49 +00:00
// if p.FightC != nil {
2026-01-27 06:40:49 +00:00
// //ov := make(chan struct{})
// go func() {
2026-01-27 06:40:49 +00:00
// defer func() {
// if err := recover(); err != nil { // 恢复 panicerr 为 panic 错误值
// // 1. 打印错误信息
2026-01-27 06:40:49 +00:00
// cool.Logger.Error(context.TODO(), "panic 错误:", err)
2026-01-27 06:40:49 +00:00
// }
// }()
// p.FightC.Over(p, info.BattleOverReason.PlayerOffline) //玩家逃跑,但是不能锁线程
// }()
// //<-ov
// select {
// case <-p.FightC.GetOverChan(): //等待结束
// case <-time.After(time.Second * 5): //等待5秒
// cool.Logger.Error(context.TODO(), "战斗崩溃", p.Info.UserID)
2026-01-27 06:40:49 +00:00
// }
2026-01-27 06:40:49 +00:00
// }
newtime := uint32(time.Now().Unix())
p.Info.TimeToday = p.Info.TimeToday + newtime - uint32(p.Logintime) //保存电池时间
2025-11-16 20:30:17 +00:00
p.Info.OnlineTime = p.Info.OnlineTime + (newtime-uint32(p.Logintime))/60 //每次退出时候保存已经在线的分钟数
p.Service.Info.Save(*p.Info)
2025-11-15 22:17:43 +00:00
space.GetSpace(p.Info.MapID).LeaveMap(p)
p.MapNPC.Stop() //停止刷怪
Mainplayer.Delete(p.Info.UserID)
share.ShareManager.DeleteUserOnline(p.Info.UserID) //设置用户登录服务器
}
// 是否可以获得经验
func (p *Player) CanGetExp() bool {
if p.Info.TimeToday >= p.Info.TimeLimit {
return false
}
ttt := p.Info.TimeLimit - p.Info.TimeToday
return (uint32(time.Now().Unix()) - uint32(p.Logintime)) <= ttt
}