Skip to content

Commit

Permalink
Do town actions only if we made it to town
Browse files Browse the repository at this point in the history
Has retry mechanism, if still fails after 3 attempt will continue run rather then error out .   InRunReturnTownRoutine()  will be detected by goroutine shortly after.

This fix issue where bot is doing town actions like identifying items, interacting with npc, stashing ( was doing some step dancing) next to portal  in farming area.
  • Loading branch information
elobo91 committed Jan 27, 2025
1 parent 5c53c0d commit 938dd7a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 11 additions & 1 deletion internal/action/town.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package action

import (
"fmt"

"github.com/hectorgimenez/d2go/pkg/data/skill"
"github.com/hectorgimenez/koolo/internal/action/step"
"github.com/hectorgimenez/koolo/internal/context"
Expand Down Expand Up @@ -65,7 +67,15 @@ func PreRun(firstRun bool) error {
func InRunReturnTownRoutine() error {
ctx := context.Get()

ReturnTown()
if err := ReturnTown(); err != nil {
return fmt.Errorf("failed to return to town: %w", err)
}

// Validate we're actually in town before proceeding
if !ctx.Data.PlayerUnit.Area.IsTown() {
return fmt.Errorf("failed to verify town location after portal")
}

step.SetSkill(skill.Vigor)
RecoverCorpse()
ManageBelt()
Expand Down
21 changes: 19 additions & 2 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,26 @@ func (b *Bot) Run(ctx context.Context, firstRun bool, runs []run.Run) error {

b.ctx.Logger.Info("Going back to town", "reason", reason)

action.InRunReturnTownRoutine()
}
// Try to return to town up to 3 times
maxRetries := 3
var lastError error
for attempt := 0; attempt < maxRetries; attempt++ {
if err := action.InRunReturnTownRoutine(); err != nil {
lastError = err
b.ctx.Logger.Warn("Failed to return to town", "error", err, "attempt", attempt+1)
// Wait a bit before retrying
time.Sleep(500 * time.Millisecond)
continue
}
lastError = nil
break
}

// If we still failed after retries, log it but continue running
if lastError != nil {
b.ctx.Logger.Error("Failed to return to town after all retries", "error", lastError)
}
}
b.ctx.SwitchPriority(botCtx.PriorityNormal)
}
}
Expand Down

0 comments on commit 938dd7a

Please sign in to comment.