diff options
Diffstat (limited to 'internal/combat')
| -rw-r--r-- | internal/combat/state.go | 7 | ||||
| -rw-r--r-- | internal/combat/state_test.go | 21 |
2 files changed, 27 insertions, 1 deletions
diff --git a/internal/combat/state.go b/internal/combat/state.go index 8e4c68d..e1ee8e7 100644 --- a/internal/combat/state.go +++ b/internal/combat/state.go @@ -27,10 +27,15 @@ func NewTracker() *Tracker { } } -// Enter records that playerName is now fighting mobID. +// Enter records that playerName is now fighting mobID. If the player was +// already fighting, the previous mob is released so it does not stay marked +// "in combat" forever (which would block every other player from attacking it). func (t *Tracker) Enter(playerName, mobID string) { t.mu.Lock() defer t.mu.Unlock() + if prev, ok := t.combatants[playerName]; ok { + delete(t.mobTargets, prev.MobID) + } t.combatants[playerName] = &State{ PlayerName: playerName, MobID: mobID, diff --git a/internal/combat/state_test.go b/internal/combat/state_test.go new file mode 100644 index 0000000..8005ba3 --- /dev/null +++ b/internal/combat/state_test.go @@ -0,0 +1,21 @@ +package combat + +import "testing" + +// Regression: re-entering combat must release the previous mob, or that mob +// stays "in combat" forever and no other player can attack it. +func TestTrackerEnterReleasesPreviousMob(t *testing.T) { + tr := NewTracker() + tr.Enter("alice", "mobA") + tr.Enter("alice", "mobB") + if tr.IsMobInCombat("mobA") { + t.Fatal("mobA still marked in combat after alice switched targets") + } + if !tr.IsMobInCombat("mobB") { + t.Fatal("mobB not marked in combat") + } + tr.Leave("alice") + if tr.IsMobInCombat("mobB") { + t.Fatal("mobB still marked in combat after leave") + } +} |
