-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelParser.hs
More file actions
62 lines (51 loc) · 2.29 KB
/
LevelParser.hs
File metadata and controls
62 lines (51 loc) · 2.29 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
54
55
56
57
58
59
60
61
62
{-# LANGUAGE OverloadedStrings #-}
module LevelParser where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM, mzero)
import qualified Data.HashMap.Strict as HM
import Data.Maybe (fromJust)
import Data.Scientific (coefficient)
import qualified Data.Text as T
import qualified Data.Vector as V
import Data.Aeson (FromJSON (..), Value (..), (.:), (.:?))
import ParitySolver hiding (getBoard, getColors)
data Level = Level { getType :: String
, getID :: Int
, getMode :: String
, getBoard :: [Int]
, getColors :: Maybe [Color]
, getInitialPos :: Position
, getSolution :: Maybe [Direction]
} deriving (Show)
fromLevel :: Level -> GameState
fromLevel l = GameState (getInitialPos l) b
where
b = case getMode l of
"b&w" -> BWBoard (fromJust $ getColors l) (getBoard l)
"vanilla" -> StdBoard (getBoard l)
x -> error $ "Unknown board type: " ++ x
instance FromJSON Level where
parseJSON (Object v) = Level
<$> v .: "type"
<*> v .: "number"
<*> v .: "mode"
<*> v .: "contents"
<*> liftM parseColors (v .:? "colors")
<*> liftM parseInitPos (v .: "initialSelected")
<*> liftM parseDirection (v .:? "solution")
parseJSON _ = mzero
parseInitPos :: Value -> Position
parseInitPos (Object ref) = (getField "x", getField "y")
where
getField field = case fromJust $ HM.lookup field ref of
(Number x) -> fromIntegral $ coefficient x
_ -> 0
parseInitPos _ = error "Invalid initial position"
parseDirection :: Maybe Value -> Maybe [Direction]
parseDirection (Just (Array xs)) = Just . map read $ prepareADTList xs
parseDirection _ = Nothing
parseColors :: Maybe Value -> Maybe [Color]
parseColors (Just (Array xs)) = Just . map read $ prepareADTList xs
parseColors _ = Nothing
prepareADTList :: V.Vector Value -> [String]
prepareADTList = map (\(String x) -> T.unpack (T.toUpper x)) . V.toList