Permalink
Browse files

Game - move quit handling logic back from MainRoom

  • Loading branch information...
bobbymcr committed Dec 5, 2018
1 parent cc4cefc commit 35acc2fc7117cfddbfb716009c27a83ff7508116
Showing with 14 additions and 8 deletions.
  1. +13 −1 sample/src/Game.cs
  2. +1 −7 sample/src/MainRoom.cs
@@ -23,10 +23,11 @@ public Game(TextReader reader, TextWriter writer)
public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (this.bus.Subscribe<SentenceMessage>(m => this.HandleQuit(m.Verb, cts)))
using (new SentenceParser(this.bus, this.words))
using (InputLoop loop = this.console.NewLoop())
{
Room room = new MainRoom(this.bus, cts);
Room room = new MainRoom(this.bus);
room.Enter();
loop.Run(cts.Token);
}
@@ -40,5 +41,16 @@ private static Words InitializeWords()
w.Add(Verb.Take, "get");
return w;
}

private bool HandleQuit(Word verb, CancellationTokenSource cts)
{
if (verb.Primary == Verb.Quit)
{
cts.Cancel();
return true;
}

return false;
}
}
}
@@ -4,22 +4,16 @@

namespace Adventure.Sample
{
using System.Threading;

internal sealed class MainRoom : Room
{
private readonly CancellationTokenSource cts;

public MainRoom(MessageBus bus, CancellationTokenSource cts)
public MainRoom(MessageBus bus)
: base(bus)
{
this.cts = cts;
}

protected override void EnterCore()
{
this.Register(Verb.Greet, (_, __) => this.Output("You say, \"Hello,\" to no one in particular. No one answers."));
this.Register(Verb.Quit, (_, n) => this.cts.Cancel());
this.Register(Verb.Take, (_, n) => this.Output("There is no " + n.Actual.ToLowerInvariant() + " here."));
}
}

0 comments on commit 35acc2f

Please sign in to comment.