48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
// Package scenario holds the single in-memory scenario state.
|
|
package scenario
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"pulsecast/api/internal/model"
|
|
)
|
|
|
|
// State is safe for concurrent readers and writers.
|
|
type State struct {
|
|
mu sync.RWMutex
|
|
current model.ScenarioState
|
|
}
|
|
|
|
// New returns the initial base scenario at version 1.
|
|
func New() *State {
|
|
return &State{current: model.ScenarioState{
|
|
Version: 1,
|
|
Scenario: model.ScenarioInput{
|
|
Scenario: "base",
|
|
Weather: model.WeatherInput{},
|
|
Event: model.EventInput{},
|
|
},
|
|
At: time.Now().UTC().Truncate(time.Second),
|
|
}}
|
|
}
|
|
|
|
// Get returns a consistent snapshot.
|
|
func (s *State) Get() model.ScenarioState {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.current
|
|
}
|
|
|
|
// Set stores a new scenario and returns its snapshot.
|
|
func (s *State) Set(in model.ScenarioInput) model.ScenarioState {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.current = model.ScenarioState{
|
|
Version: s.current.Version + 1,
|
|
Scenario: in,
|
|
At: time.Now().UTC().Truncate(time.Second),
|
|
}
|
|
return s.current
|
|
}
|