refactor(login): 重构登录重置逻辑,使用 defer 替代 goroutine

将每日任务重置逻辑从 goroutine 改为 defer 执行,确保在函数结束时正确重置任务状态。
同时将 TaskInfo 中的 Info 字段重命名为 Data,保持结构一致性。

refactor(task): 统一任务数据字段名为 Data

将 task 相关结构体中的 Info 字段统一
This commit is contained in:
2025-09-23 13:24:40 +08:00
parent 1ee43e1319
commit ea1148039e
10 changed files with 56 additions and 49 deletions

View File

@@ -1,7 +1,5 @@
go 1.20 go 1.20
toolchain go1.21.4
use ( use (
./common ./common
./common/contrib/drivers/mysql ./common/contrib/drivers/mysql

View File

@@ -48,14 +48,14 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c *player.Conn) (result *us
//每天login时候检查重置时间然后把电池任务挖矿重置 //每天login时候检查重置时间然后把电池任务挖矿重置
//挖矿需要单独存,因为防止多开挖矿 //挖矿需要单独存,因为防止多开挖矿
t.Info.TimeToday = 0 //重置电池 t.Info.TimeToday = 0 //重置电池
go func() { defer func() {
for i := 400; i < 500; i++ { //每日任务区段 for i := 400; i < 500; i++ { //每日任务区段
t.Info.TaskList[i] = 0 //重置每日任务 t.Info.TaskList[i] = 0 //重置每日任务
_, ok := t.Service.TaskInfo((uint32(i))) _, ok := t.Service.TaskInfo((uint32(i)))
if ok { if ok {
t.Service.TaskSet((uint32(i)), model.TaskInfo{ t.Service.TaskSet((uint32(i)), model.TaskInfo{
Info: []uint32{}, Data: []uint32{},
}) })
} }
} }

View File

@@ -39,12 +39,12 @@ func (h Controller) AddTaskBuf(data *task.AddTaskBufInboundInfo, c *player.Playe
_, ok := c.Service.TaskInfo(data.TaskId) _, ok := c.Service.TaskInfo(data.TaskId)
if ok { if ok {
c.Service.TaskSet(data.TaskId, model.TaskInfo{ c.Service.TaskSet(data.TaskId, model.TaskInfo{
Info: data.TaskList, Data: data.TaskList,
}) })
} else { } else {
c.Service.TaskADD(data.TaskId, model.TaskInfo{ c.Service.TaskADD(data.TaskId, model.TaskInfo{
Info: data.TaskList, Data: data.TaskList,
}) })
} }
@@ -118,7 +118,7 @@ func (h Controller) Get_Task_Buf(data *task.GetTaskBufInboundInfo, c *player.Pla
info, _ := c.Service.TaskInfo(data.TaskId) info, _ := c.Service.TaskInfo(data.TaskId)
result = &task.GetTaskBufOutboundInfo{} result = &task.GetTaskBufOutboundInfo{}
result.TaskId = data.TaskId result.TaskId = data.TaskId
result.TaskList = info.Info result.TaskList = info.Data
return result, 0 return result, 0
} }

View File

@@ -41,11 +41,11 @@ func (f *FightC) GetInputByPlayer(c common.PlayerI, isOpposite bool) *input.Inpu
// 判断当前玩家是否为我方玩家 // 判断当前玩家是否为我方玩家
isOurPlayer := c == f.Our.Player isOurPlayer := c == f.Our.Player
// 逻辑简化:当"是否为我方玩家"与"是否需要对方"状态一致时,返回对方输入,否则返回我方输入 // 当isOurPlayer与isOpposite值不同时返回我方相同时返回对方
if isOurPlayer == isOpposite { if isOurPlayer != isOpposite {
return f.Opp return f.Our
} }
return f.Our return f.Opp
} }
func (f *FightC) GetInputByAction(c BattleActionI, isOpposite bool) *input.Input { func (f *FightC) GetInputByAction(c BattleActionI, isOpposite bool) *input.Input {
@@ -320,35 +320,34 @@ func (f *FightC) battleLoop() {
switch { switch {
case faction.ItemID >= 30001 && faction.ItemID <= 300010: //胶囊 case faction.ItemID >= 30001 && faction.ItemID <= 300010: //胶囊
f.Broadcast(func(ff *input.Input) {
//todo 将血量和技能pp传回enterturn
tt, ok := ff.Player.(*player.Player)
mo, ism := f.Opp.Player.(*player.AI_player)
if ok && ism && mo.CanCapture { //如果获取玩家 //todo 将血量和技能pp传回enterturn
tt, ok := f.Our.Player.(*player.Player)
mo, ism := f.Opp.Player.(*player.AI_player)
ok, _ := f.Our.Capture(f.Opp.CurrentPet, faction.ItemID, -1) if ok && ism && mo.CanCapture { //如果获取玩家
if ok { //todo 待补充
tt.Service.PetAdd(*f.Opp.CurrentPet.Info)
tt.CatchPetInfo(info.CatchMonsterOutboundInfo{
CatchTime: uint32(f.Opp.CurrentPet.Info.CatchTime),
PetId: uint32(f.Opp.CurrentPet.ID),
})
ff.Player.SendFightEndInfo(info.FightOverInfo{
WinnerId: f.ownerID, ok, _ := f.Our.Capture(f.Opp.CurrentPet, faction.ItemID, -1)
}) if ok { //todo 待补充
f.closefight = true tt.Service.PetAdd(*f.Opp.CurrentPet.Info)
} else { tt.CatchPetInfo(info.CatchMonsterOutboundInfo{
tt.CatchPetInfo(info.CatchMonsterOutboundInfo{}) CatchTime: uint32(f.Opp.CurrentPet.Info.CatchTime),
} PetId: uint32(f.Opp.CurrentPet.ID),
})
tt.SendFightEndInfo(info.FightOverInfo{
} else { //说明不是可以捕捉的 WinnerId: f.ownerID,
})
f.closefight = true
} else {
tt.CatchPetInfo(info.CatchMonsterOutboundInfo{}) tt.CatchPetInfo(info.CatchMonsterOutboundInfo{})
} }
}) } else { //说明不是可以捕捉的
tt.CatchPetInfo(info.CatchMonsterOutboundInfo{})
}
// 当 ItemID 在 30001-300010 之间时执行的逻辑 // 当 ItemID 在 30001-300010 之间时执行的逻辑
fmt.Println("ItemID 在范围内") fmt.Println("ItemID 在范围内")
case faction.ItemID == 300001: case faction.ItemID == 300001:
@@ -465,6 +464,9 @@ func (f *FightC) processSkillAttack(attacker, defender *input.Input, a *SelectSk
func (f *FightC) enterturn(fattack, sattack BattleActionI) { func (f *FightC) enterturn(fattack, sattack BattleActionI) {
if f.closefight { //战斗结束
return
}
f.initAttackers(fattack) //初始化先后手 f.initAttackers(fattack) //初始化先后手
var attacker, defender *input.Input var attacker, defender *input.Input

View File

@@ -28,7 +28,7 @@ type Input struct {
func NewInput(c common.FightI, p common.PlayerI) *Input { func NewInput(c common.FightI, p common.PlayerI) *Input {
ret := &Input{FightC: c, Player: p} ret := &Input{FightC: c, Player: p}
t := NodeM[1000000] t, _ := ret.GetDamageEffect(1)
ret.AddEffect(deepcopy.Copy(t).(Effect)) //添加默认基类,实现继承 ret.AddEffect(deepcopy.Copy(t).(Effect)) //添加默认基类,实现继承
p.SetFightC(c) //给玩家设置战斗容器 p.SetFightC(c) //给玩家设置战斗容器
return ret return ret

View File

@@ -44,8 +44,13 @@ func (f *FightC) ChangePet(c common.PlayerI, id uint32) {
BaseAction: NewBaseAction(c.GetInfo().UserID), BaseAction: NewBaseAction(c.GetInfo().UserID),
} }
f.Switch = append(f.Switch, ret) f.Switch = append(f.Switch, ret)
if c.GetInfo().UserID == f.ownerID {
f.GetInputByPlayer(c, false).CurrentPet, ret.Reason = f.GetInputByPlayer(c, false).GetPet(id) f.GetInputByPlayer(c, false).CurrentPet, ret.Reason = f.GetInputByPlayer(c, false).GetPet(id)
} else {
f.GetInputByPlayer(c, true).CurrentPet, ret.Reason = f.GetInputByPlayer(c, true).GetPet(id)
}
f.actionChan <- ret f.actionChan <- ret
} }

View File

@@ -17,7 +17,7 @@ func NewPlayer(opts ...PlayerOption) *Player {
p.StopChan = make(chan struct{}) p.StopChan = make(chan struct{})
// 启动刷怪协程 // 启动刷怪协程
go func(stopChan chan struct{}, currentMap int) { go func(stopChan chan struct{}) {
ticker := time.NewTicker(10 * time.Second) ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop() defer ticker.Stop()
@@ -35,7 +35,7 @@ func NewPlayer(opts ...PlayerOption) *Player {
} }
} }
}(p.StopChan, int(p.Info.MapID)) }(p.StopChan)
for _, opt := range opts { for _, opt := range opts {
opt(p) opt(p)
} }

View File

@@ -56,6 +56,11 @@ type Pet struct {
Data string `gorm:"type:text;not null;comment:'精灵全部数据'" json:"data"` Data string `gorm:"type:text;not null;comment:'精灵全部数据'" json:"data"`
} }
type PetS struct {
Pet
Data []PetInfo `orm:"data" dc:"资源规格"`
}
func LastFourElements[T any](s []T) []T { func LastFourElements[T any](s []T) []T {
n := len(s) n := len(s)
if n <= 4 { if n <= 4 {

View File

@@ -17,10 +17,11 @@ type Task struct {
// TaskInfo 单个任务的详细信息,包含任务步骤状态和整体状态 // TaskInfo 单个任务的详细信息,包含任务步骤状态和整体状态
type TaskInfo struct { type TaskInfo struct {
Task
// TaskInfo 任务步骤信息, // TaskInfo 任务步骤信息,
// struc:"[20]byte" 确保二进制序列化时固定20字节长度json标签指定JSON字段名 // struc:"[20]byte" 确保二进制序列化时固定20字节长度json标签指定JSON字段名
//TaskID uint32 `json:"task_id"` //区分是每日任务还是常规任务,常规为0,每日为1 //TaskID uint32 `json:"task_id"` //区分是每日任务还是常规任务,常规为0,每日为1
Info []uint32 `struc:"[20]byte" json:"task_info"` Data []uint32 `orm:"data" struc:"[20]byte" json:"task_info"`
//LastResetTime time.Time `gorm:"not null;comment:'上次重置时间UTC'" json:"last_reset_time"` //这里是每天重置 //LastResetTime time.Time `gorm:"not null;comment:'上次重置时间UTC'" json:"last_reset_time"` //这里是每天重置
// Status 任务整体状态0-未接受1-已接受2-已完成未领取3-已完成已领取 // Status 任务整体状态0-未接受1-已接受2-已完成未领取3-已完成已领取
// json标签指定JSON字段名与业务状态说明保持一致 // json标签指定JSON字段名与业务状态说明保持一致

View File

@@ -8,19 +8,15 @@ import (
// 获取精灵信息 0是仓库,1是背包,2是放生 // 获取精灵信息 0是仓库,1是背包,2是放生
func (s *UserService) GetPetList(flag int) (ret []model.PetInfo) { func (s *UserService) GetPetList(flag int) (ret []model.PetInfo) {
ret = make([]model.PetInfo, 0)
m := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("in_bag", flag) m := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("in_bag", flag)
var tt []model.Pet var tt model.PetS
m.Scan(&tt) err := m.Scan(&tt)
if err != nil {
for _, v := range tt { panic(err)
var ret11 model.PetInfo
json.Unmarshal([]byte(v.Data), &ret11)
ret = append(ret, ret11)
} }
return return tt.Data
} }