module Game.LambdaHack.Client.MonadClient
(
MonadClient( getsClient
, modifyClient
, liftIO
)
, getClient, putClient
, debugPossiblyPrint, rndToAction, rndToActionForget
) where
import Prelude ()
import Game.LambdaHack.Common.Prelude
import qualified Control.Monad.Trans.State.Strict as St
import qualified Data.Text.IO as T
import System.IO (hFlush, stdout)
import Game.LambdaHack.Client.ClientOptions
import Game.LambdaHack.Client.State
import Game.LambdaHack.Common.MonadStateRead
import Game.LambdaHack.Common.Random
class MonadStateRead m => MonadClient m where
getsClient :: (StateClient -> a) -> m a
modifyClient :: (StateClient -> StateClient) -> m ()
liftIO :: IO a -> m a
getClient :: MonadClient m => m StateClient
getClient = getsClient id
putClient :: MonadClient m => StateClient -> m ()
putClient s = modifyClient (const s)
debugPossiblyPrint :: MonadClient m => Text -> m ()
debugPossiblyPrint t = do
sdbgMsgCli <- getsClient $ sdbgMsgCli . soptions
when sdbgMsgCli $ liftIO $ do
T.hPutStrLn stdout t
hFlush stdout
rndToAction :: MonadClient m => Rnd a -> m a
rndToAction r = do
gen1 <- getsClient srandom
let (a, gen2) = St.runState r gen1
modifyClient $ \cli -> cli {srandom = gen2}
return a
rndToActionForget :: MonadClient m => Rnd a -> m a
rndToActionForget r = do
gen <- getsClient srandom
return $! St.evalState r gen