diff options
Diffstat (limited to 'internal/game/action.go')
| -rw-r--r-- | internal/game/action.go | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/internal/game/action.go b/internal/game/action.go index 8d50eea..3146f1a 100644 --- a/internal/game/action.go +++ b/internal/game/action.go @@ -293,7 +293,7 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool { } if c.Flag != "" { - val, present := g.WorldFlags[c.Flag] + val, present := g.Flags.Get(c.Flag) return flagMatches(present, val, c.Value, c.Not) } if c.HasItem != "" { @@ -318,13 +318,15 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool { // 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. +// the flag must be present and equal to it. `not` inverts the result. Numeric +// values are compared by coercing both sides through a common numeric type so +// that an int flag set in code matches an int64/float64 decoded from YAML. 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 + match = present && valuesEqual(val, want) } if not { return !match @@ -332,6 +334,31 @@ func flagMatches(present bool, val, want any, not bool) bool { return match } +// valuesEqual compares two any-typed values, normalizing numeric types (int, +// int64, float64) so that e.g. int(3) == int64(3) == float64(3). Non-numeric +// types fall back to ==. +func valuesEqual(a, b any) bool { + ai, aok := numericValue(a) + bi, bok := numericValue(b) + if aok && bok { + return ai == bi + } + return a == b +} + +// numericValue returns the value as a float64 if it is a numeric type. +func numericValue(v any) (float64, bool) { + switch x := v.(type) { + case int: + return float64(x), true + case int64: + return float64(x), true + case float64: + return x, true + } + return 0, false +} + func isTruthy(v any) bool { switch x := v.(type) { case nil: |
