{-# LANGUAGE OverloadedStrings #-} module Automaton ( initialState , start ) where import Network.WebSockets (Connection, receiveData, sendTextData) import Data.Map (Map, empty) import Data.ByteString.Lazy.Char8 (ByteString, putStrLn) import Control.Concurrent (threadDelay) import Control.Monad.Reader (ReaderT, ask) import Control.Monad.State (StateT) import Control.Monad.Trans (lift) import Prelude hiding (putStrLn) data State = State { key :: Maybe Int , room :: Map Int String } initialState :: State initialState = State { key = Nothing , room = empty } type App a = ReaderT Connection (StateT State IO) a liftIO :: IO a -> App a liftIO = lift . lift start :: App () start = do connection <- ask s <- liftIO $ receiveData connection liftIO $ putStrLn s liftIO $ sendTextData connection ("{\"tag\": \"LogIn\", \"name\": \"Hannah\"}" :: ByteString) s2 <- liftIO $ receiveData connection liftIO $ putStrLn s2 liftIO $ threadDelay $ 1000 * ms where ms = 1000