0.2.0-dev Download ZIP
31 lines ·903 B Raw
| 1 | using EvilEngine.Host.Sessions; |
| 2 | |
| 3 | namespace EvilEngine.Shared.Sessions; |
| 4 | |
| 5 | public interface ILocalServer |
| 6 | { |
| 7 | ServerSession Session { get; } |
| 8 | |
| 9 | void Stop(string reason); |
| 10 | } |
| 11 | |
| 12 | public static class LocalServer |
| 13 | { |
| 14 | public static Func<ServerSession, ILocalServer>? Starter { get; set; } |
| 15 | |
| 16 | public static ILocalServer Start(ServerSession session) |
| 17 | { |
| 18 | return (Starter ?? |
| 19 | throw new InvalidOperationException("No server realm is registered. The game's boot registers it.")) |
| 20 | .Invoke(session); |
| 21 | } |
| 22 | |
| 23 | public static (ILocalServer Server, ClientSession Client) Play(SessionStart start, string name, |
| 24 | PlayerChoices? choices = null) |
| 25 | { |
| 26 | var server = Start(new ServerSession(start)); |
| 27 | var (serverEnd, clientEnd) = Loopback.Pair(); |
| 28 | server.Session.Connect(serverEnd); |
| 29 | return (server, new ClientSession(clientEnd, name, choices)); |
| 30 | } |
| 31 | } |