aboutsummaryrefslogtreecommitdiff
path: root/internal/game/flagstore.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-06-25 20:33:47 -0400
committerhistoria <[not public]>2026-06-25 20:33:47 -0400
commit84d74ca4351a97148ad8339676f9df7946bc21fb (patch)
treea9f50ac56e8bbcdf2b307ef6f7143b67bce46c61 /internal/game/flagstore.go
parentc55d0e4150b23f2c5c9f96bbc3d4dc2fc6dbaa36 (diff)
downloadthehouseoficarus-84d74ca4351a97148ad8339676f9df7946bc21fb.tar.gz
feat: trigger system for scripted events added.
Diffstat (limited to 'internal/game/flagstore.go')
-rw-r--r--internal/game/flagstore.go39
1 files changed, 35 insertions, 4 deletions
diff --git a/internal/game/flagstore.go b/internal/game/flagstore.go
index 2790d89..bc72c60 100644
--- a/internal/game/flagstore.go
+++ b/internal/game/flagstore.go
@@ -2,18 +2,29 @@ package game
import "sync"
+// FlagChangeCallback is invoked when a world flag value actually changes
+// (old value differs from new, or new flag is created with a truthy value).
+type FlagChangeCallback func(name string, value any)
+
// FlagStore holds world flags — shared mutable state visible to all players
// (e.g. opened doors, quest state). Player-specific flags live on
// *player.Player.Flags instead. FlagStore is safe for concurrent use.
type FlagStore struct {
- mu sync.Mutex
- flags map[string]any
+ mu sync.Mutex
+ flags map[string]any
+ callbacks []FlagChangeCallback
}
func NewFlagStore() *FlagStore {
return &FlagStore{flags: make(map[string]any)}
}
+func (f *FlagStore) OnChange(cb FlagChangeCallback) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.callbacks = append(f.callbacks, cb)
+}
+
func (f *FlagStore) Get(name string) (any, bool) {
f.mu.Lock()
defer f.mu.Unlock()
@@ -23,15 +34,35 @@ func (f *FlagStore) Get(name string) (any, bool) {
func (f *FlagStore) Set(name string, value any) {
f.mu.Lock()
- defer f.mu.Unlock()
+ old, existed := f.flags[name]
f.flags[name] = value
+ cbs := f.callbacks
+ f.mu.Unlock()
+
+ if !existed || !valuesEqual(old, value) {
+ for _, cb := range cbs {
+ cb(name, value)
+ }
+ }
}
func (f *FlagStore) SetAll(m map[string]any) {
f.mu.Lock()
- defer f.mu.Unlock()
+ changed := make(map[string]any)
for k, v := range m {
+ old, existed := f.flags[k]
f.flags[k] = v
+ if !existed || !valuesEqual(old, v) {
+ changed[k] = v
+ }
+ }
+ cbs := f.callbacks
+ f.mu.Unlock()
+
+ for k, v := range changed {
+ for _, cb := range cbs {
+ cb(k, v)
+ }
}
}