From ea1148039e70e6b1dadf6c892b49e547d0f10ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Tue, 23 Sep 2025 13:24:40 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(login):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=87=8D=E7=BD=AE=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20defer=20=E6=9B=BF=E4=BB=A3=20goroutine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将每日任务重置逻辑从 goroutine 改为 defer 执行,确保在函数结束时正确重置任务状态。 同时将 TaskInfo 中的 Info 字段重命名为 Data,保持结构一致性。 refactor(task): 统一任务数据字段名为 Data 将 task 相关结构体中的 Info 字段统一 --- go.work | 2 -- logic/controller/login.go | 4 +-- logic/controller/task.go | 8 ++--- logic/service/fight/fightc.go | 54 +++++++++++++++-------------- logic/service/fight/input/input.go | 2 +- logic/service/fight/playeraction.go | 7 +++- logic/service/player/new.go | 4 +-- modules/blazing/model/pet.go | 5 +++ modules/blazing/model/task.go | 3 +- modules/blazing/service/pet.go | 16 ++++----- 10 files changed, 56 insertions(+), 49 deletions(-) diff --git a/go.work b/go.work index cc6b4c722..df7fa9f54 100644 --- a/go.work +++ b/go.work @@ -1,7 +1,5 @@ go 1.20 -toolchain go1.21.4 - use ( ./common ./common/contrib/drivers/mysql diff --git a/logic/controller/login.go b/logic/controller/login.go index 8a8bd100f..dad392c80 100644 --- a/logic/controller/login.go +++ b/logic/controller/login.go @@ -48,14 +48,14 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c *player.Conn) (result *us //每天login时候检查重置时间,然后把电池,任务,挖矿重置 //挖矿需要单独存,因为防止多开挖矿 t.Info.TimeToday = 0 //重置电池 - go func() { + defer func() { for i := 400; i < 500; i++ { //每日任务区段 t.Info.TaskList[i] = 0 //重置每日任务 _, ok := t.Service.TaskInfo((uint32(i))) if ok { t.Service.TaskSet((uint32(i)), model.TaskInfo{ - Info: []uint32{}, + Data: []uint32{}, }) } } diff --git a/logic/controller/task.go b/logic/controller/task.go index db2e0286f..84891dc41 100644 --- a/logic/controller/task.go +++ b/logic/controller/task.go @@ -39,12 +39,12 @@ func (h Controller) AddTaskBuf(data *task.AddTaskBufInboundInfo, c *player.Playe _, ok := c.Service.TaskInfo(data.TaskId) if ok { c.Service.TaskSet(data.TaskId, model.TaskInfo{ - Info: data.TaskList, + Data: data.TaskList, }) } else { c.Service.TaskADD(data.TaskId, model.TaskInfo{ - Info: data.TaskList, + Data: data.TaskList, }) } @@ -72,7 +72,7 @@ func (h Controller) Complete_Task(data *task.CompleteTaskInboundInfo, c *player. result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500001, ItemCount: 1}) result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500502, ItemCount: 1}) result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500503, ItemCount: 1}) - + } if data.TaskId == 86 { //新手注册任务 var petid int @@ -118,7 +118,7 @@ func (h Controller) Get_Task_Buf(data *task.GetTaskBufInboundInfo, c *player.Pla info, _ := c.Service.TaskInfo(data.TaskId) result = &task.GetTaskBufOutboundInfo{} result.TaskId = data.TaskId - result.TaskList = info.Info + result.TaskList = info.Data return result, 0 } diff --git a/logic/service/fight/fightc.go b/logic/service/fight/fightc.go index 503788094..deaaecb8f 100644 --- a/logic/service/fight/fightc.go +++ b/logic/service/fight/fightc.go @@ -41,11 +41,11 @@ func (f *FightC) GetInputByPlayer(c common.PlayerI, isOpposite bool) *input.Inpu // 判断当前玩家是否为我方玩家 isOurPlayer := c == f.Our.Player - // 逻辑简化:当"是否为我方玩家"与"是否需要对方"状态一致时,返回对方输入,否则返回我方输入 - if isOurPlayer == isOpposite { - return f.Opp + // 当isOurPlayer与isOpposite值不同时返回我方,相同时返回对方 + if isOurPlayer != isOpposite { + return f.Our } - return f.Our + return f.Opp } func (f *FightC) GetInputByAction(c BattleActionI, isOpposite bool) *input.Input { @@ -320,35 +320,34 @@ func (f *FightC) battleLoop() { switch { 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 { //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{ + if ok && ism && mo.CanCapture { //如果获取玩家 - WinnerId: f.ownerID, - }) - f.closefight = true - } else { - tt.CatchPetInfo(info.CatchMonsterOutboundInfo{}) - } + ok, _ := f.Our.Capture(f.Opp.CurrentPet, faction.ItemID, -1) + 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), + }) + tt.SendFightEndInfo(info.FightOverInfo{ - } else { //说明不是可以捕捉的 + WinnerId: f.ownerID, + }) + f.closefight = true + } else { tt.CatchPetInfo(info.CatchMonsterOutboundInfo{}) - } - }) + } else { //说明不是可以捕捉的 + tt.CatchPetInfo(info.CatchMonsterOutboundInfo{}) + + } + // 当 ItemID 在 30001-300010 之间时执行的逻辑 fmt.Println("ItemID 在范围内") 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) { + if f.closefight { //战斗结束 + return + } f.initAttackers(fattack) //初始化先后手 var attacker, defender *input.Input diff --git a/logic/service/fight/input/input.go b/logic/service/fight/input/input.go index 75958d49f..fcf9148e5 100644 --- a/logic/service/fight/input/input.go +++ b/logic/service/fight/input/input.go @@ -28,7 +28,7 @@ type Input struct { func NewInput(c common.FightI, p common.PlayerI) *Input { ret := &Input{FightC: c, Player: p} - t := NodeM[1000000] + t, _ := ret.GetDamageEffect(1) ret.AddEffect(deepcopy.Copy(t).(Effect)) //添加默认基类,实现继承 p.SetFightC(c) //给玩家设置战斗容器 return ret diff --git a/logic/service/fight/playeraction.go b/logic/service/fight/playeraction.go index 02736b399..46fd5b80f 100644 --- a/logic/service/fight/playeraction.go +++ b/logic/service/fight/playeraction.go @@ -44,8 +44,13 @@ func (f *FightC) ChangePet(c common.PlayerI, id uint32) { BaseAction: NewBaseAction(c.GetInfo().UserID), } 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 } diff --git a/logic/service/player/new.go b/logic/service/player/new.go index 323069747..dbc37be90 100644 --- a/logic/service/player/new.go +++ b/logic/service/player/new.go @@ -17,7 +17,7 @@ func NewPlayer(opts ...PlayerOption) *Player { p.StopChan = make(chan struct{}) // 启动刷怪协程 - go func(stopChan chan struct{}, currentMap int) { + go func(stopChan chan struct{}) { ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() @@ -35,7 +35,7 @@ func NewPlayer(opts ...PlayerOption) *Player { } } - }(p.StopChan, int(p.Info.MapID)) + }(p.StopChan) for _, opt := range opts { opt(p) } diff --git a/modules/blazing/model/pet.go b/modules/blazing/model/pet.go index 08ea7101f..929d5ccc2 100644 --- a/modules/blazing/model/pet.go +++ b/modules/blazing/model/pet.go @@ -56,6 +56,11 @@ type Pet struct { 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 { n := len(s) if n <= 4 { diff --git a/modules/blazing/model/task.go b/modules/blazing/model/task.go index 595e766ca..8b7f6792f 100644 --- a/modules/blazing/model/task.go +++ b/modules/blazing/model/task.go @@ -17,10 +17,11 @@ type Task struct { // TaskInfo 单个任务的详细信息,包含任务步骤状态和整体状态 type TaskInfo struct { + Task // TaskInfo 任务步骤信息, // struc:"[20]byte" 确保二进制序列化时固定20字节长度,json标签指定JSON字段名 //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"` //这里是每天重置 // Status 任务整体状态:0-未接受,1-已接受,2-已完成未领取,3-已完成已领取 // json标签指定JSON字段名,与业务状态说明保持一致 diff --git a/modules/blazing/service/pet.go b/modules/blazing/service/pet.go index 73e517fad..3d051cf01 100644 --- a/modules/blazing/service/pet.go +++ b/modules/blazing/service/pet.go @@ -8,19 +8,15 @@ import ( // 获取精灵信息 0是仓库,1是背包,2是放生 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) - var tt []model.Pet - m.Scan(&tt) - - for _, v := range tt { - var ret11 model.PetInfo - json.Unmarshal([]byte(v.Data), &ret11) - - ret = append(ret, ret11) + var tt model.PetS + err := m.Scan(&tt) + if err != nil { + panic(err) } - return + return tt.Data }