aboutsummaryrefslogtreecommitdiff
path: root/internal/game/action.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/game/action.go')
-rw-r--r--internal/game/action.go57
1 files changed, 45 insertions, 12 deletions
diff --git a/internal/game/action.go b/internal/game/action.go
index 9a07419..8d50eea 100644
--- a/internal/game/action.go
+++ b/internal/game/action.go
@@ -284,22 +284,17 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool {
}
if c.PlayerFlag != "" {
- if p == nil || p.Flags == nil {
- return c.Not
+ var val any
+ present := false
+ if p != nil && p.Flags != nil {
+ val, present = p.Flags[c.PlayerFlag]
}
- val, ok := p.Flags[c.PlayerFlag]
- if c.Not {
- return !ok || val != c.Value
- }
- return ok && val == c.Value
+ return flagMatches(present, val, c.Value, c.Not)
}
if c.Flag != "" {
- val, ok := g.WorldFlags[c.Flag]
- if c.Not {
- return !ok || val != c.Value
- }
- return ok && val == c.Value
+ val, present := g.WorldFlags[c.Flag]
+ return flagMatches(present, val, c.Value, c.Not)
}
if c.HasItem != "" {
has := p != nil && p.HasItem(c.HasItem)
@@ -317,3 +312,41 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool {
}
return true
}
+
+// flagMatches evaluates a flag/player_flag condition.
+//
+// If the condition specifies no `value`, the flag matches when it is present
+// and truthy (so `{player_flag: x}` means "x is set" and
+// `{player_flag: x, not: true}` means "x is not set"). If a `value` is given,
+// the flag must be present and equal to it. `not` inverts the result.
+func flagMatches(present bool, val, want any, not bool) bool {
+ var match bool
+ if want == nil {
+ match = present && isTruthy(val)
+ } else {
+ match = present && val == want
+ }
+ if not {
+ return !match
+ }
+ return match
+}
+
+func isTruthy(v any) bool {
+ switch x := v.(type) {
+ case nil:
+ return false
+ case bool:
+ return x
+ case int:
+ return x != 0
+ case int64:
+ return x != 0
+ case float64:
+ return x != 0
+ case string:
+ return x != ""
+ default:
+ return true
+ }
+}