1{-# LANGUAGE FlexibleContexts #-}
3-- This example is adapted from Gabriel Gonzalez's pipes-concurrency package.
4-- https://hackage.haskell.org/package/pipes-concurrency-2.0.8/docs/Pipes-Concurrent-Tutorial.html
6module Streamly.Examples.AcidRainGame where
9import Control.Concurrent (threadDelay)
10import Control.Monad (when)
11import Control.Monad.State (MonadState, get, modify, runStateT)
12import Data.Semigroup (cycle1)
14data Event = Harm Int | Heal Int | Quit deriving (Show)
16userAction :: MonadIO m => StreamT m Event
17userAction = cycle1 $ liftIO askUser
22 "potion" -> return (Heal 10)
24 _ -> putStrLn "What?" >> askUser
26acidRain :: MonadIO m => StreamT m Event
27acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
29game :: (MonadAsync m, MonadState Int m) => StreamT m ()
31 event <- userAction <|> acidRain
33 Harm n -> modify $ \h -> h - n
34 Heal n -> modify $ \h -> h + n
38 when (h <= 0) $ fail "You die!"
39 liftIO $ putStrLn $ "Health = " ++ show h
43 putStrLn "Your health is deteriorating due to acid rain,\
44 \ type \"potion\" or \"quit\""
45 _ <- runStateT (runStreamT game) 60
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list