aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-06-25 17:13:27 -0500
committerFlorian Fischer <florian.fischer@muhq.space>2025-07-03 22:01:22 -0400
commitd3e190fd03ef7363d72152fa55c79ed46547d695 (patch)
tree4ba6ce9dd88b4b6d9c32b70f96bd78f0f763de60
parent31f98408521a893099c180e3c971ce8c122b5c36 (diff)
downloadmuhqs-game-d3e190fd03ef7363d72152fa55c79ed46547d695.tar.gz
muhqs-game-d3e190fd03ef7363d72152fa55c79ed46547d695.zip
prevent targeting the same object multiple times
-rw-r--r--go/game/targets.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/go/game/targets.go b/go/game/targets.go
index 02f63378..2e29d64e 100644
--- a/go/game/targets.go
+++ b/go/game/targets.go
@@ -1,6 +1,7 @@
package game
import (
+ "errors"
"fmt"
"log"
"strconv"
@@ -183,7 +184,18 @@ func (d *TargetDesc) requirement() TargetRequirement {
}
}
-func (t *Target) AddSelection(sel interface{}) (err error) {
+var (
+ ErrAlreadySelected = errors.New("already selected")
+)
+
+// AddSelection adds an object as selection to the target.
+// If the provided object does not satisfy the target contraint err != nil is returned.
+// Selecting the same object multiple times reaul%s in an error.
+func (t *Target) AddSelection(sel any) (err error) {
+ if slices.Contains(t.sel, sel) {
+ return ErrAlreadySelected
+ }
+
if err = t.constraint(sel); err == nil {
t.sel = append(t.sel, sel)
}
@@ -227,7 +239,8 @@ func (t *Target) CheckSelection(s *LocalState) error {
return nil
}
-func (t *Targets) AddSelection(sel interface{}) error { return t.ts[t.idx].AddSelection(sel) }
+// AddSelection adds an object as selection to the current target.
+func (t *Targets) AddSelection(sel any) error { return t.ts[t.idx].AddSelection(sel) }
func (t *Targets) HasSelections() bool {
for _, t := range t.ts {