Skip to content

Commit bfe4d5d

Browse files
committed
Move WMWithProperty to a better location, properly handle match words
1 parent 6d7adf1 commit bfe4d5d

39 files changed

+157
-77
lines changed

yaifl/run_no

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
808
1+
815

yaifl/src/Yaifl/Core/Action.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module Yaifl.Core.Action
2525
, withActionInterrupt'
2626

2727
, oneTouchableThing
28+
29+
, getMatchingThing
30+
, getMatching
2831
) where
2932

3033
import Yaifl.Prelude hiding (Reader)
@@ -44,6 +47,8 @@ import Yaifl.Core.Rules.RuleEffects
4447
import Yaifl.Core.Refreshable
4548
import Yaifl.Text.Say
4649

50+
import Yaifl.Core.Query.Object
51+
4752
type ParseArgumentEffects wm es = (WithMetadata es, NoMissingObjects wm es, RuleEffects wm es)
4853

4954
data ParseArgumentResult wm v =
@@ -171,4 +176,17 @@ oneTouchableThing ::
171176
ArgsHaveMainObject (Args wm v) (Thing wm)
172177
=> Args wm v
173178
-> [Thing wm]
174-
oneTouchableThing a = one $ view argsMainObject a
179+
oneTouchableThing a = one $ view argsMainObject a
180+
181+
getMatchingThing :: RuleEffects wm es => Text -> UnverifiedArgs wm params -> Eff es (Maybe (Thing wm))
182+
getMatchingThing matchElement args = do
183+
e <- getMatching matchElement args
184+
case e of
185+
Just (ObjectParameter o) -> getThingMaybe o
186+
Just (ThingParameter t) -> return (Just t)
187+
_ -> return Nothing
188+
189+
getMatching :: Text -> UnverifiedArgs wm params -> Eff es (Maybe (NamedActionParameter wm))
190+
getMatching matchElement (UnverifiedArgs args) = do
191+
let mbMatch = args ^? #variables % _2 % to (find ((== matchElement) . fst) ) % _Just % _2
192+
return mbMatch

yaifl/src/Yaifl/Core/HasProperty.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ Optics for accessing a property from the sum type of object specifics.
1010
module Yaifl.Core.HasProperty (
1111
-- * Has
1212
MayHaveProperty(..)
13+
, HasProperty(..)
14+
, WMWithProperty
1315
) where
1416

1517
import Yaifl.Prelude
18+
import Yaifl.Core.WorldModel
1619

1720
-- | An `AffineTraversal` is an optic that focuses on 0-1 objects; it's a `Prism` without
1821
-- the condition that you can build it back up again..which works great for the possibility
@@ -36,4 +39,11 @@ instance MayHaveProperty a v => MayHaveProperty (Maybe a) v where
3639
Just y -> Right y)
3740
(\case
3841
Nothing -> const Nothing
39-
Just a -> \v -> Just $ a & propertyAT .~ v)
42+
Just a -> \v -> Just $ a & propertyAT .~ v)
43+
44+
type WMWithProperty wm v = MayHaveProperty (WMObjSpecifics wm) v
45+
46+
class HasProperty w o v where
47+
propertyL :: w -> Lens' o v
48+
default propertyL :: MayHaveProperty o v => w -> Lens' o v
49+
propertyL _ = lens (fromMaybe (error "property witness was violated") . preview propertyAT) (flip (set propertyAT))

yaifl/src/Yaifl/Core/ObjectLike.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class HasID o => RoomLike wm o where
4343
instance (ObjectLike wm o) => ObjectLike wm (TaggedObject o tagEntity) where
4444
getObject = getObject . unTagObject
4545

46-
instance (RoomLike wm o) => RoomLike wm (TaggedObject o tagEntity) where
46+
instance {-# OVERLAPPABLE #-} (RoomLike wm o) => RoomLike wm (TaggedObject o tagEntity) where
4747
getRoom = getRoom . snd . unTagObject
4848

49-
instance (ThingLike wm o) => ThingLike wm (TaggedObject o tagEntity) where
49+
instance {-# OVERLAPPABLE #-} (ThingLike wm o) => ThingLike wm (TaggedObject o tagEntity) where
5050
getThing = getThing . snd . unTagObject
5151

5252
instance ObjectLike wm (Thing wm) where
@@ -74,10 +74,10 @@ instance ObjectLike wm (TaggedEntity anyTag) where
7474
getObject e = getObject (unTag e)
7575

7676
instance ThingLike wm DoorEntity where
77-
getThing = getThing . coerceTag @_ @ThingTag
77+
getThing = getThing . coerceTag @ThingTag
7878

7979
instance ThingLike wm PersonEntity where
80-
getThing = getThing . coerceTag @_ @ThingTag
80+
getThing = getThing . coerceTag @ThingTag
8181

8282
instance ObjectLike wm Entity where
8383
getObject e = if isThing (getID e)
@@ -87,6 +87,12 @@ instance ObjectLike wm Entity where
8787
instance ObjectLike wm o => ObjectLike wm (TaggedEntity e, o) where
8888
getObject = getObject . snd
8989

90+
instance ThingLike wm (TaggedObject (Thing wm) o) where
91+
getThing = pure . snd . unTagObject
92+
93+
instance RoomLike wm (TaggedObject (Room wm) o) where
94+
getRoom = pure . snd . unTagObject
95+
9096
objectIsKind ::
9197
NoMissingObjects wm es
9298
=> ObjectLike wm o

yaifl/src/Yaifl/Core/Query/Enclosing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import Yaifl.Core.Kinds.Thing
3030
import Yaifl.Core.ObjectLike
3131
import Yaifl.Core.Query.Property
3232
import Yaifl.Core.Tag
33-
import Yaifl.Core.WorldModel
3433
import Yaifl.Core.Query.Object
3534
import qualified Data.EnumSet as ES
35+
import Yaifl.Core.HasProperty
3636

3737
data IncludeScenery = IncludeScenery | ExcludeScenery
3838
data IncludeDoors = IncludeDoors | ExcludeDoors

yaifl/src/Yaifl/Core/Tag.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ instance Taggable (TaggedEntity PersonTag) ThingTag
8282
-- | If we can tagEntity a `TaggedEntity a` as a @b@, we can just coerce the entity
8383
-- rather than passing it twice.
8484
coerceTag ::
85+
forall b a.
8586
Taggable (TaggedEntity a) b
8687
=> TaggedEntity a
8788
-> TaggedEntity b

yaifl/src/Yaifl/Core/WorldModel.hs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ module Yaifl.Core.WorldModel (
2828
, WMOrd
2929
, WMEq
3030

31-
, WMWithProperty
3231
) where
3332

3433
import Yaifl.Prelude
35-
import Yaifl.Core.HasProperty
3634

3735
-- | All the various type parameters wrapped into a single type.
3836
-- This allows us to tie the knot in some weird way - we need some way to refer
@@ -102,7 +100,4 @@ type WMRead wm = WMConstr Read wm
102100
-- | Constraint that object specifics, values, and directions are Ordable.
103101
type WMOrd wm = WMConstr Ord wm
104102
-- | Constraint that object specifics, values, and directions are Eqable.
105-
type WMEq wm = WMConstr Eq wm
106-
107-
-- | A helper to define that a world model @wm@ has a Property.
108-
type WMWithProperty wm v = MayHaveProperty (WMObjSpecifics wm) v
103+
type WMEq wm = WMConstr Eq wm

yaifl/src/Yaifl/Std/Actions/Closing.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Yaifl.Std.Actions.Imports
77
import Yaifl.Std.Kinds.Openable
88
import Yaifl.Core.Metadata
99
import Yaifl.Core.Kinds.Thing
10+
import Yaifl.Core.HasProperty
1011

1112
data ClosingResponses wm =
1213
CloseReportA

yaifl/src/Yaifl/Std/Actions/Entering.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Yaifl.Core.Kinds.Object
2424
import Yaifl.Core.Query.Enclosing
2525
import Yaifl.Core.Rules.RuleEffects
2626
import Yaifl.Std.Kinds.Door
27+
import Yaifl.Core.HasProperty
2728

2829
data EnteringResponses wm =
2930
EnterAlreadyEnteredA
@@ -63,7 +64,7 @@ enteringAction = (makeAction "entering")
6364
case mbCont of
6465
Nothing ->
6566
case mbDoor of
66-
Just door -> return $ ConversionTo "go" [ThingParameter (fst variables)]
67+
Just _door -> return $ ConversionTo "go" [ThingParameter (fst variables)]
6768
Nothing -> return $ FailedParse "That's not enterable."
6869
Just x -> return $ SuccessfulParse (tagObject x (fst variables))
6970
, beforeRules = makeActionRulebook "before entering rulebook" []

yaifl/src/Yaifl/Std/Actions/Examining.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Yaifl.Core.ObjectLike
1919
import qualified Data.EnumSet as ES
2020
import Yaifl.Core.Kinds.Object
2121
import Yaifl.Std.Kinds.Person
22+
import Yaifl.Core.HasProperty
2223

2324
data ExaminingResponses =
2425
ExamineDirectionA

0 commit comments

Comments
 (0)