Skip to content
This repository was archived by the owner on Mar 3, 2021. It is now read-only.

Commit 94faa4a

Browse files
committed
Add console input using SusLine
1 parent a56eed2 commit 94faa4a

File tree

7 files changed

+460
-9
lines changed

7 files changed

+460
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Impostor.Api.Events.Input
2+
{
3+
public interface IConsoleInputEvent : IEvent
4+
{
5+
string Input { get; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Impostor.Server.Config
2+
{
3+
public class ConsoleInputConfig
4+
{
5+
public const string Section = "ConsoleInput";
6+
7+
public bool Enabled { get; set; } = true;
8+
9+
public bool SusLine { get; set; } = true;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Impostor.Api.Events.Input;
2+
3+
namespace Impostor.Server.Input
4+
{
5+
public class ConsoleInputEvent : IConsoleInputEvent
6+
{
7+
public ConsoleInputEvent(string input)
8+
{
9+
Input = input;
10+
}
11+
12+
public string Input { get; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Impostor.Api.Events.Managers;
5+
using Impostor.Server.Config;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Options;
9+
10+
namespace Impostor.Server.Input
11+
{
12+
public class ConsoleInputService : BackgroundService
13+
{
14+
private readonly ILogger<ConsoleInputService> _logger;
15+
private readonly ConsoleInputConfig _config;
16+
private readonly IEventManager _eventManager;
17+
18+
private SusLine? _susLine;
19+
20+
public ConsoleInputService(ILogger<ConsoleInputService> logger, IOptions<ConsoleInputConfig> config, IEventManager eventManager)
21+
{
22+
_logger = logger;
23+
_config = config.Value;
24+
_eventManager = eventManager;
25+
}
26+
27+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
28+
{
29+
if (_config.SusLine)
30+
{
31+
_susLine = new SusLine();
32+
}
33+
34+
await Task.Yield();
35+
while (!stoppingToken.IsCancellationRequested)
36+
{
37+
try
38+
{
39+
var line = _susLine?.ReadLine(stoppingToken) ?? Console.ReadLine();
40+
41+
if (string.IsNullOrEmpty(line))
42+
{
43+
continue;
44+
}
45+
46+
Console.WriteLine("> " + line);
47+
48+
_logger.LogTrace("Console input received: {line}", line);
49+
await _eventManager.CallAsync(new ConsoleInputEvent(line));
50+
}
51+
catch (Exception e)
52+
{
53+
_logger.LogError(e, "Exception caught handling console input");
54+
}
55+
56+
_susLine?.Update();
57+
}
58+
59+
_susLine?.Dispose();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)