less spam on itemfilter

This commit is contained in:
Héctor Giménez
2023-03-14 23:07:30 +09:00
parent 5f285a4660
commit 2900f71000

View File

@@ -5,6 +5,9 @@ import (
"github.com/faiface/beep" "github.com/faiface/beep"
"github.com/faiface/beep/speaker" "github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav" "github.com/faiface/beep/wav"
"github.com/hectorgimenez/d2go/pkg/data"
"github.com/hectorgimenez/d2go/pkg/data/area"
"github.com/hectorgimenez/d2go/pkg/data/item"
"github.com/hectorgimenez/d2go/pkg/itemfilter" "github.com/hectorgimenez/d2go/pkg/itemfilter"
"github.com/hectorgimenez/d2go/pkg/memory" "github.com/hectorgimenez/d2go/pkg/memory"
"github.com/hectorgimenez/d2go/pkg/nip" "github.com/hectorgimenez/d2go/pkg/nip"
@@ -16,7 +19,19 @@ import (
type Watcher struct { type Watcher struct {
gr *memory.GameReader gr *memory.GameReader
rules []nip.Rule rules []nip.Rule
alreadyNotifiedItemIDs map[int]interface{} alreadyNotifiedItemIDs []itemFootprint
}
type itemFootprint struct {
detectedAt time.Time
area area.Area
position data.Position
name item.Name
quality item.Quality
}
func (fp itemFootprint) Match(area area.Area, i data.Item) bool {
return fp.area == area && fp.position == i.Position && fp.name == i.Name && fp.quality == i.Quality
} }
func NewWatcher(gr *memory.GameReader, rules []nip.Rule) *Watcher { func NewWatcher(gr *memory.GameReader, rules []nip.Rule) *Watcher {
@@ -24,7 +39,7 @@ func NewWatcher(gr *memory.GameReader, rules []nip.Rule) *Watcher {
} }
func (w *Watcher) Start(ctx context.Context) error { func (w *Watcher) Start(ctx context.Context) error {
w.alreadyNotifiedItemIDs = make(map[int]interface{}, 0) w.alreadyNotifiedItemIDs = make([]itemFootprint, 0)
audioBuffer, err := initAudio() audioBuffer, err := initAudio()
if err != nil { if err != nil {
return err return err
@@ -43,28 +58,43 @@ func (w *Watcher) Start(ctx context.Context) error {
continue continue
} }
if _, found := w.alreadyNotifiedItemIDs[int(i.UnitID)]; found { found := false
for _, fp := range w.alreadyNotifiedItemIDs {
if fp.Match(d.PlayerUnit.Area, i) {
found = true
break
}
}
if found {
continue continue
} }
log.Printf("%s: Item detected: %s. Quality: %s", time.Now().Format(time.RFC3339), i.Name, i.Quality.ToString()) log.Printf("%s: Item detected: %s. Quality: %s", time.Now().Format(time.RFC3339), i.Name, i.Quality.ToString())
w.alreadyNotifiedItemIDs[int(i.UnitID)] = nil w.alreadyNotifiedItemIDs = append(w.alreadyNotifiedItemIDs, itemFootprint{
detectedAt: time.Now(),
area: d.PlayerUnit.Area,
position: i.Position,
name: i.Name,
quality: i.Quality,
})
speaker.Play(audioBuffer.Streamer(0, audioBuffer.Len())) speaker.Play(audioBuffer.Streamer(0, audioBuffer.Len()))
} }
// Cleanup the already notified list when item is out of range // Cleanup after 10 minute AND out of range
for k := range w.alreadyNotifiedItemIDs { purgedNotifiedItems := make([]itemFootprint, 0)
for _, t := range w.alreadyNotifiedItemIDs {
found := false found := false
for _, i := range d.Items.Ground { for _, it := range d.Items.Ground {
if int(i.UnitID) == k { if t.Match(d.PlayerUnit.Area, it) {
found = true found = true
} }
} }
if !found { if found || time.Since(t.detectedAt) < time.Minute*10 {
delete(w.alreadyNotifiedItemIDs, k) purgedNotifiedItems = append(purgedNotifiedItems, t)
} }
} }
w.alreadyNotifiedItemIDs = purgedNotifiedItems
} }
} }
} }