From 77356e7aadf05e4ab2e651b4af2a7d25d35038e6 Mon Sep 17 00:00:00 2001 From: ben_singer Date: Tue, 22 Oct 2024 14:10:09 +0100 Subject: [PATCH 1/3] Added ExaminationScene and ExaminationRequest --- NetAF.Tests/Assets/ExaminableObject_Tests.cs | 12 ++-- NetAF/Assets/ExaminableObject.cs | 25 ++++---- NetAF/Assets/ExaminationCallback.cs | 4 +- NetAF/Assets/ExaminationRequest.cs | 48 +++++++++++++++ NetAF/Assets/ExaminationScene.cs | 58 +++++++++++++++++++ NetAF/Assets/IExaminable.cs | 3 +- NetAF/Assets/Locations/Overworld.cs | 5 +- NetAF/Assets/Locations/Region.cs | 3 +- NetAF/Assets/Locations/Room.cs | 3 +- NetAF/Commands/Game/Examine.cs | 2 +- .../Color/ColorSceneFrameBuilder.cs | 2 +- 11 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 NetAF/Assets/ExaminationRequest.cs create mode 100644 NetAF/Assets/ExaminationScene.cs diff --git a/NetAF.Tests/Assets/ExaminableObject_Tests.cs b/NetAF.Tests/Assets/ExaminableObject_Tests.cs index a4c54312..77f22bf2 100644 --- a/NetAF.Tests/Assets/ExaminableObject_Tests.cs +++ b/NetAF.Tests/Assets/ExaminableObject_Tests.cs @@ -14,7 +14,7 @@ public void GivenExamine_WhenDefaultExamination_ThenExaminableDescriptionIsInclu { var i = new Item("Test", "Test Description."); - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains(i.Description.GetDescription())); } @@ -30,7 +30,7 @@ public void GivenExamine_WhenDefaultExaminationWith1CustomCommand_ThenCustomComm ] }; - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains(i.Commands[0].Help.Command)); } @@ -47,7 +47,7 @@ public void GivenExamine_WhenDefaultExaminationWith2CustomCommands_ThenCustomCom ] }; - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains(i.Commands[0].Help.Command)); Assert.IsTrue(result.Description.Contains(i.Commands[1].Help.Command)); @@ -58,7 +58,7 @@ public void GivenExamine_WhenNoDescription_ThenResultIncludesIdentifierName() { var i = new Item("Test", string.Empty); - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains("Test")); } @@ -68,7 +68,7 @@ public void GivenExamine_WhenNoDescriptionOrIdentifierName_ThenResultIncludesCla { var i = new Item(string.Empty, string.Empty); - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains("Item")); } @@ -79,7 +79,7 @@ public void GivenExamine_WhenSomeAttributes_ThenResultIncludesAttributeName() var i = new Item("Test", string.Empty); i.Attributes.Add("Attribute", 1); - var result = i.Examine(); + var result = i.Examine(ExaminationScene.NoScene); Assert.IsTrue(result.Description.Contains("Attribute")); } diff --git a/NetAF/Assets/ExaminableObject.cs b/NetAF/Assets/ExaminableObject.cs index 8924200f..8400fd96 100644 --- a/NetAF/Assets/ExaminableObject.cs +++ b/NetAF/Assets/ExaminableObject.cs @@ -17,21 +17,21 @@ public class ExaminableObject : IExaminable /// /// Get or set the callback handling all examination of this object. /// - public ExaminationCallback Examination { get; set; } = obj => + public ExaminationCallback Examination { get; set; } = request => { var description = string.Empty; - if (obj.Description != null) - description = obj.Description.GetDescription(); + if (request.Examinable.Description != null) + description = request.Examinable.Description.GetDescription(); - if (obj.Commands?.Any() ?? false) + if (request.Examinable.Commands?.Any() ?? false) { if (!string.IsNullOrEmpty(description)) description += " "; - description += $"{Environment.NewLine}{Environment.NewLine}{obj.Identifier.Name} provides the following commands: "; + description += $"{Environment.NewLine}{Environment.NewLine}{request.Examinable.Identifier.Name} provides the following commands: "; - foreach (var customCommand in obj.Commands) + foreach (var customCommand in request.Examinable.Commands) description += $"{Environment.NewLine}\"{customCommand.Help.Command}\" - {customCommand.Help.Description.RemoveSentenceEnd()}, "; if (description.EndsWith(", ")) @@ -42,13 +42,13 @@ public class ExaminableObject : IExaminable } if (string.IsNullOrEmpty(description)) - description = obj.Identifier.Name; + description = request.Examinable.Identifier.Name; if (string.IsNullOrEmpty(description)) - description = obj.GetType().Name; + description = request.Examinable.GetType().Name; - if (obj.Attributes.Count > 0) - description += $"\n\n{StringUtilities.ConstructAttributesAsString(obj.Attributes.GetAsDictionary())}"; + if (request.Examinable.Attributes.Count > 0) + description += $"\n\n{StringUtilities.ConstructAttributesAsString(request.Examinable.Attributes.GetAsDictionary())}"; return new ExaminationResult(description); }; @@ -93,10 +93,11 @@ public override string ToString() /// /// Examine this object. /// + /// The scene this object is being examined from. /// A ExaminationResult detailing the examination of this object. - public virtual ExaminationResult Examine() + public virtual ExaminationResult Examine(ExaminationScene scene) { - return Examination(this); + return Examination(new ExaminationRequest(this, scene)); } #endregion diff --git a/NetAF/Assets/ExaminationCallback.cs b/NetAF/Assets/ExaminationCallback.cs index 8b8ef231..b23a18d1 100644 --- a/NetAF/Assets/ExaminationCallback.cs +++ b/NetAF/Assets/ExaminationCallback.cs @@ -3,7 +3,7 @@ /// /// Represents the callback for examinations. /// - /// The object to examine. + /// The examination request. /// A string representing the result of the examination. - public delegate ExaminationResult ExaminationCallback(IExaminable obj); + public delegate ExaminationResult ExaminationCallback(ExaminationRequest request); } diff --git a/NetAF/Assets/ExaminationRequest.cs b/NetAF/Assets/ExaminationRequest.cs new file mode 100644 index 00000000..6ed935f6 --- /dev/null +++ b/NetAF/Assets/ExaminationRequest.cs @@ -0,0 +1,48 @@ +using NetAF.Logic; + +namespace NetAF.Assets +{ + /// + /// Represents a request to examin an IExaminable. + /// + public class ExaminationRequest + { + #region Propeties + + /// + /// Get the examinable object. + /// + public IExaminable Examinable { get; private set; } + + /// + /// Get the examination scene. + /// + public ExaminationScene Scene { get; private set; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the ExaminationRequest class. + /// + /// The object being examined. + /// The scene the object is being examined from. + public ExaminationRequest(IExaminable examinable, ExaminationScene scene) + { + Examinable = examinable; + Scene = scene; + } + + /// + /// Initializes a new instance of the ExaminationRequest class. + /// + /// The object being examined. + /// The executing game. + public ExaminationRequest(IExaminable examinable, Game game) : this(examinable, new ExaminationScene(game)) + { + } + + #endregion + } +} diff --git a/NetAF/Assets/ExaminationScene.cs b/NetAF/Assets/ExaminationScene.cs new file mode 100644 index 00000000..e5dc774b --- /dev/null +++ b/NetAF/Assets/ExaminationScene.cs @@ -0,0 +1,58 @@ +using NetAF.Assets.Characters; +using NetAF.Assets.Locations; +using NetAF.Logic; + +namespace NetAF.Assets +{ + /// + /// Represents a scene that an examination occurs in. + /// + public class ExaminationScene + { + #region StaticProperties + + /// + /// Get a default value for when there is no scene. + /// + public static ExaminationScene NoScene { get; } = new ExaminationScene(null, null); + + #endregion + + #region Propeties + + /// + /// Get the examiner. + /// + public Character Examiner { get; private set; } + + /// + /// Get the room the examinable is being examined from. + /// + public Room Room { get; private set; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the ExaminationScene class. + /// + /// The character who is examining the object. + /// The room the examinable is being examined from. + public ExaminationScene(Character examiner, Room room) + { + Examiner = examiner; + Room = room; + } + + /// + /// Initializes a new instance of the ExaminationScene class. + /// + /// The executing game. + public ExaminationScene(Game game) : this(game.Player, game.Overworld?.CurrentRegion?.CurrentRoom) + { + } + + #endregion + } +} diff --git a/NetAF/Assets/IExaminable.cs b/NetAF/Assets/IExaminable.cs index 8e630915..a369faff 100644 --- a/NetAF/Assets/IExaminable.cs +++ b/NetAF/Assets/IExaminable.cs @@ -27,7 +27,8 @@ public interface IExaminable : IPlayerVisible /// /// Examine this object. /// + /// The scene this object is being examined from. /// A ExaminationResult detailing the examination of this object. - ExaminationResult Examine(); + ExaminationResult Examine(ExaminationScene scene); } } \ No newline at end of file diff --git a/NetAF/Assets/Locations/Overworld.cs b/NetAF/Assets/Locations/Overworld.cs index bb14a5e3..1a59f2d7 100644 --- a/NetAF/Assets/Locations/Overworld.cs +++ b/NetAF/Assets/Locations/Overworld.cs @@ -110,7 +110,7 @@ public bool Move(Region region) CurrentRegion = region; return true; } - + #endregion #region Overrides of ExaminableObject @@ -118,8 +118,9 @@ public bool Move(Region region) /// /// Examine this object. /// + /// The scene this object is being examined from. /// A ExaminationResult detailing the examination of this object. - public override ExaminationResult Examine() + public override ExaminationResult Examine(ExaminationScene scene) { return new ExaminationResult(Description.GetDescription()); } diff --git a/NetAF/Assets/Locations/Region.cs b/NetAF/Assets/Locations/Region.cs index 9da02971..ffc56390 100644 --- a/NetAF/Assets/Locations/Region.cs +++ b/NetAF/Assets/Locations/Region.cs @@ -323,8 +323,9 @@ internal static void NextPosition(int x, int y, int z, Direction direction, out /// /// Examine this object. /// + /// The scene this object is being examined from. /// A ExaminationResult detailing the examination of this object. - public override ExaminationResult Examine() + public override ExaminationResult Examine(ExaminationScene scene) { return new ExaminationResult(Identifier + ": " + Description.GetDescription()); } diff --git a/NetAF/Assets/Locations/Room.cs b/NetAF/Assets/Locations/Room.cs index 7fa2ca53..adf48390 100644 --- a/NetAF/Assets/Locations/Room.cs +++ b/NetAF/Assets/Locations/Room.cs @@ -217,8 +217,9 @@ private InteractionResult InteractWithItem(Item item) /// /// Handle examination this Room. /// + /// The scene this object is being examined from. /// The result of this examination. - public override ExaminationResult Examine() + public override ExaminationResult Examine(ExaminationScene scene) { if (!Items.Any(i => i.IsPlayerVisible)) return new ExaminationResult("There is nothing to examine."); diff --git a/NetAF/Commands/Game/Examine.cs b/NetAF/Commands/Game/Examine.cs index 163bd91f..5ac53c85 100644 --- a/NetAF/Commands/Game/Examine.cs +++ b/NetAF/Commands/Game/Examine.cs @@ -42,7 +42,7 @@ public Reaction Invoke(Logic.Game game) if (Examinable == null) return new Reaction(ReactionResult.Error, "Nothing to examine."); - return new Reaction(ReactionResult.OK, Examinable.Examine().Description); + return new Reaction(ReactionResult.OK, Examinable.Examine(new ExaminationScene(game)).Description); } #endregion diff --git a/NetAF/Rendering/FrameBuilders/Color/ColorSceneFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/Color/ColorSceneFrameBuilder.cs index 2d9fb023..33e6cf72 100644 --- a/NetAF/Rendering/FrameBuilders/Color/ColorSceneFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/Color/ColorSceneFrameBuilder.cs @@ -145,7 +145,7 @@ public IFrame Build(Room room, ViewPoint viewPoint, PlayableCharacter player, st var extendedDescription = string.Empty; if (room.Items.Any()) - extendedDescription = extendedDescription.AddSentence(room.Examine().Description.EnsureFinishedSentence()); + extendedDescription = extendedDescription.AddSentence(room.Examine(new ExaminationScene(player, room)).Description.EnsureFinishedSentence()); else extendedDescription = extendedDescription.AddSentence("There are no items in this area."); From 454dbc4dd522c52b3b4b45d38d95de9337b8a92f Mon Sep 17 00:00:00 2001 From: ben_singer Date: Tue, 22 Oct 2024 14:16:14 +0100 Subject: [PATCH 2/3] Spelling fix --- NetAF/Assets/ExaminationRequest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetAF/Assets/ExaminationRequest.cs b/NetAF/Assets/ExaminationRequest.cs index 6ed935f6..500ae051 100644 --- a/NetAF/Assets/ExaminationRequest.cs +++ b/NetAF/Assets/ExaminationRequest.cs @@ -3,7 +3,7 @@ namespace NetAF.Assets { /// - /// Represents a request to examin an IExaminable. + /// Represents a request to examine an IExaminable. /// public class ExaminationRequest { From e1e07ea599007724f9b0ab19034beb88a7eb5160 Mon Sep 17 00:00:00 2001 From: ben_singer Date: Tue, 22 Oct 2024 14:28:58 +0100 Subject: [PATCH 3/3] Added tests --- .../Assets/ExaminationRequest_Tests.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 NetAF.Tests/Assets/ExaminationRequest_Tests.cs diff --git a/NetAF.Tests/Assets/ExaminationRequest_Tests.cs b/NetAF.Tests/Assets/ExaminationRequest_Tests.cs new file mode 100644 index 00000000..25d8fefd --- /dev/null +++ b/NetAF.Tests/Assets/ExaminationRequest_Tests.cs @@ -0,0 +1,44 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NetAF.Assets; +using NetAF.Assets.Characters; +using NetAF.Assets.Locations; +using NetAF.Logic; + +namespace NetAF.Tests.Assets +{ + [TestClass] + public class ExaminationRequest_Tests + { + [TestMethod] + public void GivenCreate_WhenGameSpecified_ThenExaminerSetFromPlayer() + { + var player = new PlayableCharacter(string.Empty, string.Empty); + var room = new Room(string.Empty, string.Empty); + var region = new Region(string.Empty, string.Empty); + var overworld = new Overworld(string.Empty, string.Empty); + region.AddRoom(room, 0, 0, 0); + overworld.AddRegion(region); + var gameCreator = Game.Create(string.Empty, string.Empty, string.Empty, () => overworld, () => player, g => EndCheckResult.NotEnded, g => EndCheckResult.NotEnded); + + var result = new ExaminationRequest(player, gameCreator()); + + Assert.AreEqual(player, result.Scene.Examiner); + } + + [TestMethod] + public void GivenCreate_WhenGameSpecified_ThenRoomSetFromPlayer() + { + var player = new PlayableCharacter(string.Empty, string.Empty); + var room = new Room(string.Empty, string.Empty); + var region = new Region(string.Empty, string.Empty); + var overworld = new Overworld(string.Empty, string.Empty); + region.AddRoom(room, 0, 0, 0); + overworld.AddRegion(region); + var gameCreator = Game.Create(string.Empty, string.Empty, string.Empty, () => overworld, () => player, g => EndCheckResult.NotEnded, g => EndCheckResult.NotEnded); + + var result = new ExaminationRequest(player, gameCreator()); + + Assert.AreEqual(room, result.Scene.Room); + } + } +}