{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Terminal.Game.Layer.Object.Record where
import Terminal.Game.Layer.Object.Interface
import Terminal.Game.Layer.Object.IO
import qualified Control.Concurrent as CC
import qualified Control.Monad.Catch as MC
import qualified Control.Monad.Reader as R
import qualified Control.Monad.Trans as T
import qualified Data.ByteString as BS
import qualified Data.Serialize as S
newtype Record a = Record (R.ReaderT (CC.MVar [Event]) IO a)
deriving (Functor, Applicative, Monad,
T.MonadIO,
MC.MonadThrow, MC.MonadCatch, MC.MonadMask)
runRecord :: Record a -> CC.MVar [Event] -> IO a
runRecord (Record r) me = R.runReaderT r me
instance MonadInput Record where
startEvents fps = Record $
R.ask >>= \ve ->
T.liftIO $ startIOInput (Just ve) fps
pollEvents ve = T.liftIO $ CC.swapMVar ve []
stopEvents ts = T.liftIO $ stopEventsIO ts
writeMoves :: FilePath -> CC.MVar [Event] -> IO ()
writeMoves fp ve = CC.readMVar ve >>= \es ->
BS.writeFile fp (S.encode es)