1 /*
2  * Copyright (c) 2021 ScalaQuest Team.
3  *
4  * This file is part of ScalaQuest, and is distributed under the terms of the MIT License as
5  * described in the file LICENSE in the ScalaQuest distribution's top directory.
6  */
7 
8 package io.github.scalaquest.core.model.behaviorBased.simple.impl
9 
10 import io.github.scalaquest.core.model.{Direction, Model}
11 
12 /**
13  * Integrates some additional functionalities for state inspection and re-generation, by the use of
14  * [[monocle.Lens]].
15  */
16 trait StateUtilsExt extends Model {
17 
18   /**
19    * Add some utility methods for [[State]] implementation.
20    * @param state
21    *   A state ([[S]])
22    */
23   implicit class StateUtils(state: S) {
24     implicit val s: S = state
25 
26     /**
27      * Check if an [[Item]] is in Bag.
28      * @param item
29      *   The item ([[I]]) to check.
30      * @return
31      *   True if item is in bag, false otherwise.
32      */
33     def isInBag(item: I): Boolean = state.bag.contains(item)
34 
35     /**
36      * Check if an [[Item]] is in the actual player location [[Room]].
37      * @param item
38      *   The item ([[I]]) to check.
39      * @return
40      *   True if item is in location, false otherwise.
41      */
42     def isInLocation(item: I): Boolean = state.location.items.contains(item)
43 
44     /**
45      * Check if an [[Item]] is visible. Two possible conditions:
46      *   - item is in the actual player location [[Room]].
47      *   - item is in bag.
48      * @param item
49      *   the item ([[I]]) to check.
50      * @return
51      *   True if item is in location or in bag, false otherwise.
52      */
53     def isInScope(item: I): Boolean = state.isInLocation(item) || state.isInBag(item)
54 
55     /**
56      * Check if player have a neighbor [[Room]] for a given Direction.
57      * @param direction
58      *   The given [[Direction]].
59      * @return
60      *   [[Option]] that contain the neighbor Room ([[RM]]) if present, [[None]] otherwise.
61      */
62     def locationNeighbor(direction: Direction): Option[RM] = state.location.neighbor(direction)
63 
64     /**
65      * Responds with all the [[Item]] s present in the player location [[Room]].
66      * @return
67      *   All the [[Item]] s present in the player location [[Room]].
68      */
69     def locationItems: Set[I] = state.location.items
70 
71     /**
72      * Copy the State [[S]] and add an item to the player location Room.
73      * @param item
74      *   The <b>item</b> [[I]] to add.
75      * @return
76      *   A new <b>State</b> with the <b>item</b> added into the player's location.
77      */
78     def copyWithItemInLocation(item: I): S = {
79       val stateWithTarget    = itemsLens.modify(_ + (item.ref -> item))(state)
80       val currRoomWithTarget = roomItemsLens.modify(_ + item.ref)(state.location)
81       roomsLens.modify(_ + (currRoomWithTarget.ref -> currRoomWithTarget))(stateWithTarget)
82     }
83 
84     /**
85      * Copy the State [[S]] and add an item to the player's bag.
86      * @param item
87      *   The <b>item</b> [[I]] to add to the State.
88      * @return
89      *   The new <b>State</b> with the <b>item</b> in the player's <b>bag</b>.
90      */
91     def copyWithItemInBag(item: I): S = {
92       val stateWithTarget = itemsLens.modify(_ + (item.ref -> item))(state)
93       bagLens.modify(_ + item.ref)(stateWithTarget)
94     }
95   }
96 }
Line Stmt Id Pos Tree Symbol Tests Code
24 663 693 - 698 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.state StateUtils.this.state
33 664 911 - 935 Apply scala.collection.SetOps.contains StateUtils.this.state.bag.contains(item)
42 665 1190 - 1225 Apply scala.collection.SetOps.contains StateUtils.this.state.location.items(StateUtils.this.s).contains(item)
53 666 1567 - 1572 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.state StateUtils.this.state
53 667 1595 - 1614 Apply io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.isInBag StateUtilsExt.this.StateUtils(StateUtils.this.state).isInBag(item)
53 668 1567 - 1614 Apply scala.Boolean.|| StateUtilsExt.this.StateUtils(StateUtils.this.state).isInLocation(item).||(StateUtilsExt.this.StateUtils(StateUtils.this.state).isInBag(item))
62 669 1952 - 1952 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.s StateUtils.this.s
62 670 1929 - 1963 ApplyToImplicitArgs io.github.scalaquest.core.model.Model.Room.neighbor StateUtils.this.state.location.neighbor(direction)(StateUtils.this.s)
69 671 2193 - 2193 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.s StateUtils.this.s
69 672 2178 - 2198 ApplyToImplicitArgs io.github.scalaquest.core.model.Model.Room.items StateUtils.this.state.location.items(StateUtils.this.s)
79 673 2545 - 2561 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item)
79 674 2540 - 2562 Apply scala.collection.immutable.MapOps.+ x$1.+[StateUtilsExt.this.I](scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item))
79 675 2564 - 2569 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.state StateUtils.this.state
79 676 2523 - 2570 Apply scala.Function1.apply StateUtilsExt.this.itemsLens.modify(((x$1: Map[io.github.scalaquest.core.model.ItemRef,StateUtilsExt.this.I]) => x$1.+[StateUtilsExt.this.I](scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item)))).apply(StateUtils.this.state)
80 677 2627 - 2635 Select io.github.scalaquest.core.model.Model.Item.ref item.ref
80 678 2623 - 2635 Apply scala.collection.immutable.SetOps.+ x$2.+(item.ref)
80 679 2637 - 2651 Select io.github.scalaquest.core.model.Model.State.location StateUtils.this.state.location
80 680 2602 - 2652 Apply scala.Function1.apply StateUtilsExt.this.roomItemsLens.modify(((x$2: Set[io.github.scalaquest.core.model.ItemRef]) => x$2.+(item.ref))).apply(StateUtils.this.state.location)
81 681 2659 - 2744 Apply scala.Function1.apply StateUtilsExt.this.roomsLens.modify(((x$3: Map[io.github.scalaquest.core.model.RoomRef,StateUtilsExt.this.RM]) => x$3.+[StateUtilsExt.this.RM](scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.RoomRef](currRoomWithTarget.ref).->[StateUtilsExt.this.RM](currRoomWithTarget)))).apply(stateWithTarget)
92 682 3090 - 3106 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item)
92 683 3085 - 3107 Apply scala.collection.immutable.MapOps.+ x$4.+[StateUtilsExt.this.I](scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item))
92 684 3109 - 3114 Select io.github.scalaquest.core.model.behaviorBased.simple.impl.StateUtilsExt.StateUtils.state StateUtils.this.state
92 685 3068 - 3115 Apply scala.Function1.apply StateUtilsExt.this.itemsLens.modify(((x$4: Map[io.github.scalaquest.core.model.ItemRef,StateUtilsExt.this.I]) => x$4.+[StateUtilsExt.this.I](scala.Predef.ArrowAssoc[io.github.scalaquest.core.model.ItemRef](item.ref).->[StateUtilsExt.this.I](item)))).apply(StateUtils.this.state)
93 686 3122 - 3167 Apply scala.Function1.apply StateUtilsExt.this.bagLens.modify(((x$5: Set[io.github.scalaquest.core.model.ItemRef]) => x$5.+(item.ref))).apply(stateWithTarget)