Permalink
Browse files

Game - handle 'quit' verb

  • Loading branch information...
bobbymcr committed Nov 30, 2018
1 parent 13e1579 commit 72fc4d9c1785d8f2c6234cac0a94fe38901e7c7a
Showing with 14 additions and 4 deletions.
  1. +12 −4 sample/src/Game.cs
  2. +2 −0 sample/test/walkthrough.in
@@ -20,26 +20,34 @@ public Game(TextReader reader, TextWriter writer)

public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(m.Verb)))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(cts, m.Verb)))
{
this.console.Run(CancellationToken.None);
this.console.Run(cts.Token);
}
}

private void ProcessVerb(string verb)
private void ProcessVerb(CancellationTokenSource cts, string verb)
{
string output = null;
if (verb == "hello")
{
output = "world";
}
else if (verb == "quit")
{
cts.Cancel();
}
else
{
output = "I don't know what '" + verb + "' means.";
}

this.bus.Send(new OutputMessage(output));
if (output != null)
{
this.bus.Send(new OutputMessage(output));
}
}
}
}
@@ -1,2 +1,4 @@
hello
heeyyyyyyyy
quit
-- END --

0 comments on commit 72fc4d9

Please sign in to comment.