Skip to content
Permalink
Browse files

Room - ProcessLookItemUpperLevelSubscriber

  • Loading branch information...
bobbymcr committed Dec 15, 2018
1 parent c48836d commit 0d7eafd4153ba30847ef515349026c30a3982d0a
Showing with 52 additions and 10 deletions.
  1. +16 −0 core/src/LookItemMessage.cs
  2. +15 −10 core/src/Room.cs
  3. +21 −0 core/test/RoomTest.cs
@@ -0,0 +1,16 @@
// <copyright file="LookItemMessage.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
public sealed class LookItemMessage
{
public LookItemMessage(Word noun)
{
this.Noun = noun;
}

public Word Noun { get; }
}
}
@@ -13,7 +13,8 @@ public abstract class Room
private readonly Dictionary<string, Action<Word, Word>> verbs;
private readonly Items items;

private IDisposable sub;
private IDisposable process;
private IDisposable look;

protected Room(MessageBus bus)
{
@@ -26,28 +27,29 @@ protected Room(MessageBus bus)

public void Enter()
{
if (this.sub != null)
if (this.process != null)
{
throw new InvalidOperationException("Cannot Enter again.");
}

this.items.Activate();
this.sub = this.bus.Subscribe<SentenceMessage>(m => this.Process(m));
this.process = this.bus.Subscribe<SentenceMessage>(m => this.Process(m));
this.look = this.bus.Subscribe<LookItemMessage>(m => this.LookAt(m.Noun));
this.EnterCore();
this.Look(new Word(string.Empty, string.Empty));
}

public void Leave()
{
if (this.sub == null)
if (this.process == null)
{
throw new InvalidOperationException("Cannot Leave before Enter.");
}

this.items.Deactivate();
this.verbs.Clear();
this.sub.Dispose();
this.sub = null;
this.process.Dispose();
this.process = null;
}

public void Add(string name, Item item)
@@ -85,9 +87,9 @@ protected void Look(Word noun)
{
this.LookAround();
}
else if (!this.LookAt(noun))
else
{
this.Output("You see nothing of interest.");
this.bus.Send(new LookItemMessage(noun));
}
}

@@ -164,9 +166,12 @@ private void LookAround()
this.items.Look("There is {0} here.");
}

private bool LookAt(Word noun)
private void LookAt(Word noun)
{
return this.items.LookAt(noun) || this.LookAtCore(noun);
if (!this.items.LookAt(noun) && !this.LookAtCore(noun))
{
this.Output("You see nothing of interest.");
}
}
}
}
@@ -168,6 +168,27 @@ public void ProcessLookItem()
lastOutput.Should().Be("It is solid gold.");
}

[Fact]
public void ProcessLookItemUpperLevelSubscriber()
{
MessageBus bus = new MessageBus();
string lastOutput = null;
bus.Subscribe<OutputMessage>(m => lastOutput = m.Text);
Dictionary<string, Item> items = new Dictionary<string, Item>();
items.Add("key", new TestKey());
bus.Subscribe<LookItemMessage>(m =>
{
bus.Send(new OutputMessage(items[m.Noun.Primary].LongDescription));
return true;
});
Room room = new TestRoom(bus);

room.Enter();
bus.Send(new SentenceMessage(new Word("look", "LOOK"), new Word("key", "KEY")));

lastOutput.Should().Be("It is solid gold.");
}

[Fact]
public void ProcessTake()
{

0 comments on commit 0d7eafd

Please sign in to comment.
You can’t perform that action at this time.