Skip to content

Commit

Permalink
simplify an effect out of fullGameLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
lehmacdj committed Aug 18, 2020
1 parent 1a8f47f commit aa65e21
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
13 changes: 10 additions & 3 deletions app/console/Main.hs
Expand Up @@ -31,23 +31,30 @@ handleIndexError = (>>= either (const writeErrorMessage) pure) . runError
where
writeErrorMessage = writeln "error: unrecoverable index out of bounds error"

handleQuitGame ::
Member ConsoleIO r => Sem (Error QuitGame : r) () -> Sem r ()
handleQuitGame = (>>= either (const writeErrorMessage) pure) . runError
where
writeErrorMessage = writeln "goodbye! :)"

playGame ::
GameState ->
Sem
[ Output Turn,
PlayerIO,
Error GameOver,
State GameState,
Error CardDoesNotExist,
ConsoleIO
]
Void ->
() ->
IO ()
playGame startingState game =
game
& ignoreOutput @Turn . outputTurnToConsoleIO
& raiseUnder @(Error QuitGame)
& runPlayerIOAsConsoleUI
& runGame startingState
& handleQuitGame
& fmap fst . runState startingState
& (=<<) printEndgameResult
& handleIndexError
& raiseUnder @(Embed (InputT IO))
Expand Down
10 changes: 6 additions & 4 deletions src/ConsoleUI.hs
Expand Up @@ -124,18 +124,20 @@ getInputCommandFromConsoleIO = runInputSem go
Left e -> writeln (errorBundlePretty e) >> go
Right command -> pure command

data QuitGame = QuitGame

-- | interpreting a command returns the turn that the command produces if it
-- is one and otherwise executes the command in the effect context
interpretCommand ::
Throws '[GameOver] r =>
Throws '[QuitGame] r =>
Command ->
Sem r RawTurn
interpretCommand = \case
Quit -> throw GameOver
Quit -> throw QuitGame
TakeTurn t -> pure t

runPlayerIOAsConsoleUI ::
Members [ConsoleIO, Error GameOver] r =>
Members [ConsoleIO, Error QuitGame] r =>
Sem (PlayerIO : r) a ->
Sem r a
runPlayerIOAsConsoleUI =
Expand All @@ -152,5 +154,5 @@ runPlayerIOAsConsoleUI =
. raiseUnder @(Input RawTurn)
. raiseUnder @(Output (Player', Information))
-- final effects we want to interpret in terms of
. raiseUnder @(Error GameOver)
. raiseUnder @(Error QuitGame)
. raiseUnder @ConsoleIO
6 changes: 3 additions & 3 deletions src/Game.hs
Expand Up @@ -615,15 +615,15 @@ gameLoop currentPlayer = do
-- | takes care of giving info about initial cards to players
fullGameLoop ::
( Members [PlayerIO, State GameState, Output Turn] r,
Throws [CardDoesNotExist, GameOver] r
Throws '[CardDoesNotExist] r
) =>
Sem r Void
Sem r ()
fullGameLoop = do
s <- get @GameState
for_ (s ^@.. #hands . #underlyingMap . (itraversed <.> itraversed)) $
\((p, cix), c) ->
for_ (s ^.. playersExcept p) $ giveInfo $ CardSatisfies p cix $ cardIs c
gameLoop (firstPlayer s)
runError' @GameOver (gameLoop (firstPlayer s)) $> ()

-- | A deck is only valid if it is a permutation of all of the cards
startingDeckValid :: Deck -> Bool
Expand Down

0 comments on commit aa65e21

Please sign in to comment.