Permalink
Browse files

Room - RegisterOnEnter

  • Loading branch information...
bobbymcr committed Dec 2, 2018
1 parent 7d0f4ef commit 590414bb6f16bd84c09fb58e5177c1cd6d6b027a
Showing with 93 additions and 0 deletions.
  1. +46 −0 core/src/Room.cs
  2. +47 −0 core/test/RoomTest.cs
@@ -0,0 +1,46 @@
// <copyright file="Room.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
using System;
using System.Collections.Generic;

public abstract class Room
{
private readonly MessageBus bus;
private readonly Dictionary<string, Action<Word, Word>> verbs;

protected Room(MessageBus bus)
{
this.bus = bus;
this.verbs = new Dictionary<string, Action<Word, Word>>();
}

public void Enter()
{
this.bus.Subscribe<SentenceMessage>(this.Process);
this.EnterCore();
}

protected virtual void EnterCore()
{
}

protected void Register(string verb, Action<Word, Word> handler)
{
this.verbs.Add(verb, handler);
}

protected void Output(string message)
{
this.bus.Send(new OutputMessage(message));
}

private void Process(SentenceMessage message)
{
this.verbs[message.Verb.Primary](message.Verb, message.Noun);
}
}
}
@@ -0,0 +1,47 @@
// <copyright file="RoomTest.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure.Test
{
using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

public sealed class RoomTest
{
[Fact]
public void RegisterOnEnter()
{
MessageBus bus = new MessageBus();
List<string> output = new List<string>();
Action<OutputMessage> subscriber = m => output.Add(m.Text);
bus.Subscribe(subscriber);
Room room = new TestRoom(bus);

room.Enter();
bus.Send(new SentenceMessage(new Word("hello", "hello"), new Word("world", "world")));

output.Should().ContainSingle().Which.Should().Be("Hello, world!");
}

private sealed class TestRoom : Room
{
public TestRoom(MessageBus bus)
: base(bus)
{
}

protected override void EnterCore()
{
this.Register("hello", this.Hello);
}

private void Hello(Word verb, Word noun)
{
this.Output("Hello, " + noun + "!");
}
}
}
}

0 comments on commit 590414b

Please sign in to comment.