Skip to content

Commit

Permalink
Merge pull request #81 from benpollarduk/examination-changes
Browse files Browse the repository at this point in the history
Added ExaminationScene and ExaminationRequest
  • Loading branch information
benpollarduk authored Oct 22, 2024
2 parents e99d2ea + e1e07ea commit c4fb8a4
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 27 deletions.
12 changes: 6 additions & 6 deletions NetAF.Tests/Assets/ExaminableObject_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand All @@ -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));
}
Expand All @@ -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));
Expand All @@ -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"));
}
Expand All @@ -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"));
}
Expand All @@ -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"));
}
Expand Down
44 changes: 44 additions & 0 deletions NetAF.Tests/Assets/ExaminationRequest_Tests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
25 changes: 13 additions & 12 deletions NetAF/Assets/ExaminableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ public class ExaminableObject : IExaminable
/// <summary>
/// Get or set the callback handling all examination of this object.
/// </summary>
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(", "))
Expand All @@ -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);
};
Expand Down Expand Up @@ -93,10 +93,11 @@ public override string ToString()
/// <summary>
/// Examine this object.
/// </summary>
/// <param name="scene">The scene this object is being examined from.</param>
/// <returns>A ExaminationResult detailing the examination of this object.</returns>
public virtual ExaminationResult Examine()
public virtual ExaminationResult Examine(ExaminationScene scene)
{
return Examination(this);
return Examination(new ExaminationRequest(this, scene));
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/ExaminationCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents the callback for examinations.
/// </summary>
/// <param name="obj">The object to examine.</param>
/// <param name="request">The examination request.</param>
/// <returns>A string representing the result of the examination.</returns>
public delegate ExaminationResult ExaminationCallback(IExaminable obj);
public delegate ExaminationResult ExaminationCallback(ExaminationRequest request);
}
48 changes: 48 additions & 0 deletions NetAF/Assets/ExaminationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NetAF.Logic;

namespace NetAF.Assets
{
/// <summary>
/// Represents a request to examine an IExaminable.
/// </summary>
public class ExaminationRequest
{
#region Propeties

/// <summary>
/// Get the examinable object.
/// </summary>
public IExaminable Examinable { get; private set; }

/// <summary>
/// Get the examination scene.
/// </summary>
public ExaminationScene Scene { get; private set; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the ExaminationRequest class.
/// </summary>
/// <param name="examinable">The object being examined.</param>
/// <param name="scene">The scene the object is being examined from.</param>
public ExaminationRequest(IExaminable examinable, ExaminationScene scene)
{
Examinable = examinable;
Scene = scene;
}

/// <summary>
/// Initializes a new instance of the ExaminationRequest class.
/// </summary>
/// <param name="examinable">The object being examined.</param>
/// <param name="game">The executing game.</param>
public ExaminationRequest(IExaminable examinable, Game game) : this(examinable, new ExaminationScene(game))
{
}

#endregion
}
}
58 changes: 58 additions & 0 deletions NetAF/Assets/ExaminationScene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using NetAF.Assets.Characters;
using NetAF.Assets.Locations;
using NetAF.Logic;

namespace NetAF.Assets
{
/// <summary>
/// Represents a scene that an examination occurs in.
/// </summary>
public class ExaminationScene
{
#region StaticProperties

/// <summary>
/// Get a default value for when there is no scene.
/// </summary>
public static ExaminationScene NoScene { get; } = new ExaminationScene(null, null);

#endregion

#region Propeties

/// <summary>
/// Get the examiner.
/// </summary>
public Character Examiner { get; private set; }

/// <summary>
/// Get the room the examinable is being examined from.
/// </summary>
public Room Room { get; private set; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the ExaminationScene class.
/// </summary>
/// <param name="examiner">The character who is examining the object.</param>
/// <param name="room">The room the examinable is being examined from.</param>
public ExaminationScene(Character examiner, Room room)
{
Examiner = examiner;
Room = room;
}

/// <summary>
/// Initializes a new instance of the ExaminationScene class.
/// </summary>
/// <param name="game">The executing game.</param>
public ExaminationScene(Game game) : this(game.Player, game.Overworld?.CurrentRegion?.CurrentRoom)
{
}

#endregion
}
}
3 changes: 2 additions & 1 deletion NetAF/Assets/IExaminable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public interface IExaminable : IPlayerVisible
/// <summary>
/// Examine this object.
/// </summary>
/// <param name="scene">The scene this object is being examined from.</param>
/// <returns>A ExaminationResult detailing the examination of this object.</returns>
ExaminationResult Examine();
ExaminationResult Examine(ExaminationScene scene);
}
}
5 changes: 3 additions & 2 deletions NetAF/Assets/Locations/Overworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ public bool Move(Region region)
CurrentRegion = region;
return true;
}

#endregion

#region Overrides of ExaminableObject

/// <summary>
/// Examine this object.
/// </summary>
/// <param name="scene">The scene this object is being examined from.</param>
/// <returns>A ExaminationResult detailing the examination of this object.</returns>
public override ExaminationResult Examine()
public override ExaminationResult Examine(ExaminationScene scene)
{
return new ExaminationResult(Description.GetDescription());
}
Expand Down
3 changes: 2 additions & 1 deletion NetAF/Assets/Locations/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ internal static void NextPosition(int x, int y, int z, Direction direction, out
/// <summary>
/// Examine this object.
/// </summary>
/// <param name="scene">The scene this object is being examined from.</param>
/// <returns>A ExaminationResult detailing the examination of this object.</returns>
public override ExaminationResult Examine()
public override ExaminationResult Examine(ExaminationScene scene)
{
return new ExaminationResult(Identifier + ": " + Description.GetDescription());
}
Expand Down
3 changes: 2 additions & 1 deletion NetAF/Assets/Locations/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ private InteractionResult InteractWithItem(Item item)
/// <summary>
/// Handle examination this Room.
/// </summary>
/// <param name="scene">The scene this object is being examined from.</param>
/// <returns>The result of this examination.</returns>
public override ExaminationResult Examine()
public override ExaminationResult Examine(ExaminationScene scene)
{
if (!Items.Any(i => i.IsPlayerVisible))
return new ExaminationResult("There is nothing to examine.");
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Commands/Game/Examine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down

0 comments on commit c4fb8a4

Please sign in to comment.