feat(login): 重构登录逻辑并迁移每日重置功能到 Personself 方法

将原本在 Controller.Login 中处理的每日重置逻辑(如电池、任务等)迁移到
service.Info.Personself 方法中,并移除对 gtime.Now().Time 的旧引用。同时更新了
相关的时间判断函数 IsToday,使其支持 *gtime.Time 类型。

此外,清理无用导入包,优化日志打印方式,并修复部分结构体字段定义与使用问题。
This commit is contained in:
2025-12-07 19:23:44 +08:00
parent eef702e25a
commit 294cb2e3fd
9 changed files with 64 additions and 38 deletions

View File

@@ -50,6 +50,7 @@ func (h Controller) ItemUsePet(data *item.C2S_USE_PET_ITEM_OUT_OF_FIGHT, c *play
copier.CopyWithOption(onpet, oldpet.Data, copier.Option{DeepCopy: true})
onpet.CatchTime = oldpetc
onpet.EffectInfo = oldpet.Data.EffectInfo
} else {
hd := item.PetItemRegistry.GetHandler(data.ItemID)
if hd == nil {

View File

@@ -2,7 +2,6 @@ package controller
import (
"blazing/common/data/share"
"blazing/common/data/xmlres"
"blazing/cool"
"fmt"
@@ -17,7 +16,6 @@ import (
"context"
"time"
"github.com/gogf/gf/v2/os/gtime"
"github.com/panjf2000/gnet/v2"
)
@@ -59,7 +57,7 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c gnet.Conn) (result *user.
}
t.Service = blservice.NewUserService(data.Head.UserID)
t.User = service.NewBaseSysUserService()
t.Info = t.Service.Info.Person(data.Head.UserID)
t.Info = t.Service.Info.Personself()
if t.Info == nil {
defer c.Close()
return
@@ -67,32 +65,6 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c gnet.Conn) (result *user.
t.Info.UserID = data.Head.UserID
t.Logintime = uint32(time.Now().Unix()) //保存时间戳
cool.Loger.Info(context.Background(), "用户上次重置日期", t.Info.LastResetTime.String())
if !IsToday(t.Info.LastResetTime) { //判断是否是今天
t.Info.LastResetTime = gtime.Now().Time
//每天login时候检查重置时间然后把电池任务挖矿重置
//挖矿需要单独存,因为防止多开挖矿
t.Info.TimeToday = 0 //重置电池
for i := 400; i < 500; i++ { //每日任务区段
tttL, ok := xmlres.TaskMap[i]
if ok {
if tttL.Type == 1 { //日常任务
t.Info.TaskList[i-1] = 0 //重置每日任务
}
}
}
for i := 0; i < 50; i++ { //每日任务区段
t.Info.DailyResArr[i] = 0 //重置每日任务
}
//defer t.Service.Talk_Reset()
}
t.CompleteLogin() //通知客户端登录成功
result = user.NewOutInfo() //设置登录消息

View File

@@ -28,6 +28,7 @@ func (e *Effect8) DamageFloor(t *info.DamageZone) bool {
if t.Type == info.DamageType.Red {
t.Damage = alpacadecimal.NewFromInt(utils.Min(t.Damage.IntPart(),
int64(e.Ctx().Opp.CurrentPet.Info.Hp)-1))
e.max = t.Damage
}

View File

@@ -175,7 +175,7 @@ func (f *FightC) handleTimeout(ourID, oppID uint32, actions map[uint32]action.Ba
if _, exists := actions[pid]; exists {
continue
}
cool.Loger.Debug(context.Background(), "玩家%d 超时\n", pid)
cool.Loger.Debug(context.Background(), "玩家超时", pid)
f.Reason = info.BattleOverReason.PlayerOVerTime
f.closefight = true

View File

@@ -163,6 +163,7 @@ func init() {
PetItemRegistry.RegisterExact(300024, func(itemid uint32, onpet *model.PetInfo) bool {
onpet.Downgrade(1)
onpet.NextLvExp = 0
onpet.Update_EXP()
onpet.Ev = [6]uint32{}
onpet.Dv = uint32(grand.Intn(32))

View File

@@ -211,7 +211,8 @@ func (h *ClientData) OnEvent(v []byte) {
return
}
}
cool.Loger.Debug(context.TODO(), "接收数据", header.UserID, header.CMD)
fmt.Println("接收数据", header.UserID, header.CMD)
// cool.Loger.Debug(context.TODO(), "接收数据", header.UserID, header.CMD)
h.Recv(header)
}

View File

@@ -3,17 +3,18 @@ package model
import (
"blazing/cool"
"math"
"time"
"github.com/creasty/defaults"
"github.com/gogf/gf/v2/os/gtime"
)
const TableNamePlayerInfo = "player_info"
type Player struct {
*cool.Model
PlayerID uint64 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
Data string `gorm:"type:jsonb;not null;comment:'全部数据'" json:"data"`
PlayerID uint64 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
LastResetTime *gtime.Time `struc:"skip" json:"last_reset_time"` // 重置时间,比如电池和每日任务
Data string `gorm:"type:jsonb;not null;comment:'全部数据'" json:"data"`
}
type PlayerEX struct {
Player
@@ -67,9 +68,9 @@ func NewPlayerInfo() PlayerInfo {
}
type PlayerInfo struct {
ExpPool uint32 `struc:"skip" json:"exp_pool"` // 累计经验池
LastResetTime time.Time `struc:"skip" json:"last_reset_time"` // 重置时间,比如电池和每日任务
OnlineTime uint32 `struc:"skip" json:"online_time"` //在线分钟数
ExpPool uint32 `struc:"skip" json:"exp_pool"` // 累计经验池
//LastResetTime time.Time `struc:"skip" json:"last_reset_time"` // 重置时间,比如电池和每日任务
OnlineTime uint32 `struc:"skip" json:"online_time"` //在线分钟数
// OutInfo 字段
UserID uint32 `struc:"uint32" json:"user_id"` // 米米号 通过sid拿到
RegisterTime uint32 `struc:"uint32" json:"register_time"` // 注册时间(秒时间戳)

View File

@@ -1,12 +1,14 @@
package service
import (
"blazing/common/data/xmlres"
"blazing/cool"
"blazing/modules/blazing/model"
"context"
"time"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gtime"
)
// 是否注册,如果注册过,那么就会产生用户player信息
@@ -60,6 +62,46 @@ func (s *InfoService) Person(userid uint32) *model.PlayerInfo {
if err != nil {
return nil
}
ret := tt.Data
return &ret
}
func (s *InfoService) Personself() *model.PlayerInfo {
m := cool.DBM(s.Model).Where("player_id", s.userid)
var tt model.PlayerEX
err := m.Scan(&tt)
if err != nil {
return nil
}
if !IsToday(tt.LastResetTime) { //判断是否是今天
//每天login时候检查重置时间然后把电池任务挖矿重置
//挖矿需要单独存,因为防止多开挖矿
tt.LastResetTime = gtime.Now()
for i := 400; i < 500; i++ { //每日任务区段
tttL, ok := xmlres.TaskMap[i]
if ok {
if tttL.Type == 1 { //日常任务
tt.Data.TaskList[i-1] = 0 //重置每日任务
}
}
}
for i := 0; i < 50; i++ { //每日任务区段
tt.Data.DailyResArr[i] = 0 //重置每日任务
}
//defer t.Service.Talk_Reset()
_, err := m.Save(tt)
if err != nil {
panic(err)
}
}
ret := tt.Data
return &ret

View File

@@ -4,6 +4,8 @@ import (
"blazing/cool"
"blazing/modules/blazing/model"
"time"
"github.com/gogf/gf/v2/os/gtime"
)
// func Exec[T cool.UserModel, F any](userid uint32, s *cool.Service, processFunc func(F) F) bool {
@@ -53,7 +55,12 @@ func (s *TaskService) Exec(id uint32, t func(*model.TaskEX) bool) {
}
// IsToday 判断给定时间是否是今天
func IsToday(t time.Time) bool {
func IsToday(t1 *gtime.Time) bool {
if t1 == nil {
return false
}
t := t1.Time
// 获取当前时间
now := time.Now()