module Codec.Goat.Story
( Story(..)
, storyAppend
, storyDump
, storyNew
, storyQuery
) where
import Data.Word
import Codec.Goat.Fluid
import Codec.Goat.TimeFrame
import Codec.Goat.Util
import Codec.Goat.ValueFrame
data Story = Story Word32 Word32 (Fluid Word32 TimeFrame) (Fluid Float ValueFrame)
instance Show Story where
show (Story wsz win ft fv) = unwords
[ "Story"
, "wsz=" ++ show wsz
, "win=" ++ show win
, "times=" ++ show ft
, "values=" ++ show fv ]
storyNew :: Word32
-> Word32
-> Story
storyNew wsz win = Story wsz win (fluidNew (12, 74)) (fluidNew (12, 74))
storyAppend :: Story
-> (Word32, Float)
-> Maybe Story
storyAppend (Story wsz win ft fv) (newTime, newValue)
| invalid = Nothing
| otherwise = Just $ Story wsz win2 newFt newFv
where
valueInv = isNaN newValue || isInfinite newValue
timeInv = maybe True (newTime>) (fluidFirst ft)
invalid = valueInv || timeInv
newWin = mod newTime wsz
win2 = bool win newWin (win == newWin)
newFt = fluidAppend (bool ft (fluidShift ft) (newWin == win)) newTime
newFv = fluidAppend (bool fv (fluidShift fv) (newWin == win)) newValue
storyQuery :: Story
-> (Word32, Word32)
-> [(Word32, Float)]
storyQuery (Story _ _ ft fv) ival
| all (==False) heads = []
| otherwise = zip times values
where
times = fluidSelect ft heads
values = fluidSelect fv heads
heads = map (maybe False (inBounds ival)) (fluidHeads ft)
storyDump :: Story
-> [(Word32, Float)]
storyDump (Story _ _ ft fv) = zip (fluidDump ft) (fluidDump fv)