Skip to content
Permalink
Browse files

Inventory - DropDisallowedItem

  • Loading branch information...
bobbymcr committed Dec 14, 2018
1 parent 24b0a21 commit b463652cd0913f874ac1dc429dc21ffa981ad564
Showing with 55 additions and 2 deletions.
  1. +9 −2 core/src/Inventory.cs
  2. +10 −0 core/src/Item.cs
  3. +36 −0 core/test/InventoryTest.cs
@@ -56,8 +56,15 @@ private void Add(Word verb, Word noun, Item item)
private void Drop(Word verb, Word noun, Items targetItems)
{
Item item = this.items.Take(noun.Primary);
targetItems.Add(noun.Primary, item);
this.Output($"You {verb} the {noun}.");
if (item.Drop(this.bus))
{
targetItems.Add(noun.Primary, item);
this.Output($"You {verb} the {noun}.");
}
else
{
this.items.Add(noun.Primary, item);
}
}

private void Output(string text)
@@ -24,6 +24,11 @@ public bool Take(MessageBus bus)
return this.TakeCore(bus);
}

public bool Drop(MessageBus bus)
{
return this.DropCore(bus);
}

protected virtual bool DoCore(MessageBus bus, Word verb, Word noun)
{
return false;
@@ -33,5 +38,10 @@ protected virtual bool TakeCore(MessageBus bus)
{
return true;
}

protected virtual bool DropCore(MessageBus bus)
{
return true;
}
}
}
@@ -127,6 +127,24 @@ public void DropAllowedItem()
}
}

[Fact]
public void DropDisallowedItem()
{
MessageBus bus = new MessageBus();
List<string> messages = new List<string>();
bus.Subscribe<OutputMessage>(m => messages.Add(m.Text));
Items items = new Items(bus);
using (Inventory inv = new Inventory(bus))
{
inv.Add("key", new TestItem(false));
bus.Send(new InventoryDropMessage(items, new Word("drop", "THROW"), new Word("key", "KEY")));
bus.Send(new InventoryRequestedMessage());

messages.Should().Equal("I won't let you drop this!", "You are carrying:", "a key");
items.Look("{0}").Should().Be(0);
}
}

[Fact]
public void DropItemAfterDispose()
{
@@ -175,9 +193,27 @@ public void AddInventoryAfterDispose()

private sealed class TestItem : Item
{
private readonly bool canDrop;

public TestItem(bool canDrop = true)
{
this.canDrop = canDrop;
}

public override string ShortDescription => "a key";

public override string LongDescription => throw new System.NotImplementedException();

protected override bool DropCore(MessageBus bus)
{
if (!this.canDrop)
{
bus.Send(new OutputMessage("I won't let you drop this!"));
return false;
}

return base.DropCore(bus);
}
}

private sealed class TestItem2 : Item

0 comments on commit b463652

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