summaryrefslogtreecommitdiff
path: root/src/Sequence
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-06-02 23:29:36 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2016-06-02 23:29:36 +0200
commit1b7f21ba636e4034f76495deafd0ac6ca9758a4e (patch)
treef8cdffa91a9c6643df51d851d367de0daafb6d5d /src/Sequence
parente4fe9710287960438856fa78b697ccae64b7e2eb (diff)
download2017-01-16_17:13:37-1b7f21ba636e4034f76495deafd0ac6ca9758a4e.tar
2017-01-16_17:13:37-1b7f21ba636e4034f76495deafd0ac6ca9758a4e.tar.gz
2017-01-16_17:13:37-1b7f21ba636e4034f76495deafd0ac6ca9758a4e.tar.bz2
2017-01-16_17:13:37-1b7f21ba636e4034f76495deafd0ac6ca9758a4e.tar.xz
2017-01-16_17:13:37-1b7f21ba636e4034f76495deafd0ac6ca9758a4e.zip
rudimentary state
Diffstat (limited to 'src/Sequence')
-rw-r--r--src/Sequence/Types.hs101
-rw-r--r--src/Sequence/Utils.hs46
2 files changed, 147 insertions, 0 deletions
diff --git a/src/Sequence/Types.hs b/src/Sequence/Types.hs
new file mode 100644
index 0000000..afe1060
--- /dev/null
+++ b/src/Sequence/Types.hs
@@ -0,0 +1,101 @@
1{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeSynonymInstances, ViewPatterns, OverloadedStrings, TemplateHaskell, GeneralizedNewtypeDeriving #-}
2
3module Sequence.Types
4 ( GameState(..), gEntities, gEntityNames
5 , Faction, faction, faction'
6 , SeqVal(..), seqVal
7 , Entity(..), eFaction, eSeqVal
8 , EntityName(..), entityName
9 , EntityIdentifier(..), entityId
10 , inhabitedFactions, priorityQueue
11 ) where
12
13import Control.Lens
14
15import System.Console.Shell
16
17import Data.Default
18import Data.CaseInsensitive (CI)
19import qualified Data.CaseInsensitive as CI
20
21import Data.Map.Strict (Map)
22import qualified Data.Map.Strict as Map
23
24import Data.Bimap (Bimap)
25import qualified Data.Bimap as Bimap
26
27import Data.List
28import Data.Maybe
29import Data.Tuple
30import Data.Ord
31
32
33newtype Faction = Faction { _lFaction :: Maybe (CI String) }
34 deriving (Show, Eq, Ord)
35makeLenses ''Faction
36
37instance Default Faction where
38 def = Faction Nothing
39
40unaligned :: CI String
41unaligned = "Unaligned"
42
43faction :: Getter Faction (Maybe (CI String))
44faction = lFaction
45
46faction' :: Lens' Faction String
47faction' = lens (CI.original . fromMaybe unaligned . view faction) (\s a -> s { _lFaction = parseFaction a })
48 where
49 parseFaction str@(CI.mk -> str')
50 | str' == unaligned = Nothing
51 | null str = Nothing
52 | otherwise = Just str'
53
54
55newtype SeqVal = SeqVal { _seqVal :: Integer }
56 deriving (Show, Ord, Eq, Num, Integral, Enum, Real)
57makeLenses ''SeqVal
58
59
60data Entity = Entity
61 { _eFaction :: Faction
62 , _eSeqVal :: Maybe SeqVal
63 }
64 deriving (Show)
65makeLenses ''Entity
66
67instance Default Entity where
68 def = Entity
69 { _eFaction = def
70 , _eSeqVal = def
71 }
72
73newtype EntityName = EntityName { _entityName :: CI String }
74 deriving (Show, Eq, Ord)
75makeLenses ''EntityName
76
77newtype EntityIdentifier = EntityIdentifier { _entityId :: Integer }
78 deriving (Show, Eq, Ord)
79makeLenses ''EntityIdentifier
80
81data GameState = GameState
82 { _gEntities :: Map EntityIdentifier Entity
83 , _gEntityNames :: Bimap EntityIdentifier EntityName
84 }
85makeLenses ''GameState
86
87instance Default GameState where
88 def = GameState
89 { _gEntities = def
90 , _gEntityNames = Bimap.empty
91 }
92
93inhabitedFactions :: Getter GameState [Faction]
94inhabitedFactions = to $ nub . sort . Map.elems . fmap (view eFaction) . view gEntities
95
96priorityQueue :: Getter GameState [(SeqVal, EntityIdentifier)]
97priorityQueue = to priorityQueue'
98 where
99 priorityQueue' (Map.toAscList . view gEntities -> entities) = sortBy (comparing $ Down . snd) . concat . map filter . map (over _1 $ view eSeqVal) . map swap $ entities
100 filter (Nothing, _) = mempty
101 filter (Just val, id) = pure (val, id)
diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs
new file mode 100644
index 0000000..7d28b83
--- /dev/null
+++ b/src/Sequence/Utils.hs
@@ -0,0 +1,46 @@
1{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses #-}
2
3module Sequence.Utils
4 ( toName, fromName
5 ) where
6
7import Sequence.Types
8
9import Control.Monad.State.Strict
10
11import Control.Lens
12
13import Data.Bimap (Bimap)
14import qualified Data.Bimap as Bimap
15
16import Data.CaseInsensitive (CI)
17import qualified Data.CaseInsensitive as CI
18
19import Data.Map.Strict (Map)
20import qualified Data.Map.Strict as Map
21
22import Data.Function
23import Data.Default
24import Data.Maybe
25import Text.Read
26
27import Data.List
28
29import System.Console.Shell
30
31unaligned = view faction' def
32
33toName :: MonadState GameState m => EntityIdentifier -> m String
34toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames
35
36fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier)
37fromName (readMaybe -> n@(Just _)) = return $ EntityIdentifier <$> n
38fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames
39
40instance Completion EntityIdentifier GameState where
41 completableLabel _ = "<entity>"
42 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . map ((evalState ?? st) . toName) . Map.keys $ st ^. gEntities
43
44instance Completion Faction GameState where
45 completableLabel _ = "<faction>"
46 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . nub . sort $ unaligned : map (view faction') (st ^. inhabitedFactions)