Skip to content

Commit

Permalink
Game - move quit handling logic back from MainRoom
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Dec 5, 2018
1 parent cc4cefc commit 35acc2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 13 additions & 1 deletion sample/src/Game.cs
Expand Up @@ -23,10 +23,11 @@ public Game(TextReader reader, TextWriter writer)
public void Run() public void Run()
{ {
using (CancellationTokenSource cts = new CancellationTokenSource()) using (CancellationTokenSource cts = new CancellationTokenSource())
using (this.bus.Subscribe<SentenceMessage>(m => this.HandleQuit(m.Verb, cts)))
using (new SentenceParser(this.bus, this.words)) using (new SentenceParser(this.bus, this.words))
using (InputLoop loop = this.console.NewLoop()) using (InputLoop loop = this.console.NewLoop())
{ {
Room room = new MainRoom(this.bus, cts); Room room = new MainRoom(this.bus);
room.Enter(); room.Enter();
loop.Run(cts.Token); loop.Run(cts.Token);
} }
Expand All @@ -40,5 +41,16 @@ private static Words InitializeWords()
w.Add(Verb.Take, "get"); w.Add(Verb.Take, "get");
return w; return w;
} }

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

return false;
}
} }
} }
8 changes: 1 addition & 7 deletions sample/src/MainRoom.cs
Expand Up @@ -4,22 +4,16 @@


namespace Adventure.Sample namespace Adventure.Sample
{ {
using System.Threading;

internal sealed class MainRoom : Room internal sealed class MainRoom : Room
{ {
private readonly CancellationTokenSource cts; public MainRoom(MessageBus bus)

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


protected override void EnterCore() protected override void EnterCore()
{ {
this.Register(Verb.Greet, (_, __) => this.Output("You say, \"Hello,\" to no one in particular. No one answers.")); 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.")); this.Register(Verb.Take, (_, n) => this.Output("There is no " + n.Actual.ToLowerInvariant() + " here."));
} }
} }
Expand Down

0 comments on commit 35acc2f

Please sign in to comment.