-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterpreter.hs
More file actions
53 lines (47 loc) · 2.3 KB
/
Copy pathInterpreter.hs
File metadata and controls
53 lines (47 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Startups.Interpreter where
import Startups.Base
import Startups.Cards
import Startups.GameTypes
import Control.Monad.Operational
import Control.Monad.State.Strict
import Data.List.NonEmpty
data Strategy p m = Strategy { _doPlayerDecision :: Age -> Turn -> PlayerId -> NonEmpty Card -> GameState -> m (p (PlayerAction, Exchange))
, _doAskCard :: Age -> PlayerId -> NonEmpty Card -> GameState -> Message -> m (p Card)
}
data OperationDict p m = OperationDict { _strat :: Strategy p m
, _doGetPromise :: forall a. p a -> m (Either Message a)
, _doMessage :: GameState -> CommunicationType -> m ()
}
runInterpreter :: Monad m
=> OperationDict p m
-> GameState
-> GameMonad p a
-> m (GameState, Either Message a)
runInterpreter dico gamestate m =
case runState (viewT m) gamestate of
(a, nextstate) -> evalInstrGen dico nextstate a
evalInstrGen :: Monad m
=> OperationDict p m
-> GameState
-> ProgramViewT (GameInstr p) (State GameState) a
-> m (GameState, Either Message a)
evalInstrGen _ gamestate (Return x) = return (gamestate, Right x)
evalInstrGen dico gamestate (a :>>= f) =
let runC a' = runInterpreter dico gamestate (f a')
in case a of
PlayerDecision age turn pid clist -> _doPlayerDecision (_strat dico) age turn pid clist gamestate >>= runC
GetPromise x -> do
o <- _doGetPromise dico x
case o of
Left rr -> return (gamestate, Left rr)
Right v -> runC v
AskCard age pid cards msg -> _doAskCard (_strat dico) age pid cards gamestate msg >>= runC
Message com -> _doMessage dico gamestate com >>= runC
ThrowError err -> return (gamestate, Left err)
CatchError n handler -> do
n' <- runInterpreter dico gamestate n
case n' of
(gs', Left rr) -> runInterpreter dico gs' (handler rr >>= f)
(gs', Right x) -> runInterpreter dico gs' (f x)