|
1 | 1 | // ReSharper disable AccessToDisposedClosure, UnusedVariable |
2 | 2 |
|
3 | | -using System.Diagnostics.CodeAnalysis; |
4 | 3 | using System.Net; |
5 | 4 | using Backdash; |
6 | 5 | using Backdash.Core; |
|
56 | 55 | // not a spectator, creating a `remote` game session |
57 | 56 | else |
58 | 57 | { |
59 | | - var players = ParsePlayers(playerCount, endpoints); |
| 58 | + var players = ParsePlayers(endpoints); |
60 | 59 | var localPlayer = players.SingleOrDefault(x => x.IsLocal()) |
61 | 60 | ?? throw new InvalidOperationException("No local player defined"); |
62 | 61 | builder |
63 | 62 | // Write logs in a file with player number |
64 | | - .WithFileLogWriter($"log_player_{localPlayer.Number}.log", append: false) |
| 63 | + .WithFileLogWriter($"log_player_{port}.log", append: false) |
65 | 64 | .WithPlayers(players) |
66 | 65 | .ForRemote(); |
67 | 66 | } |
68 | 67 |
|
69 | | - |
70 | 68 | var session = builder.Build(); |
71 | 69 |
|
72 | 70 | // create the actual game |
|
97 | 95 |
|
98 | 96 | return; |
99 | 97 |
|
100 | | -static Player[] ParsePlayers(int totalNumberOfPlayers, IEnumerable<string> endpoints) |
| 98 | +static NetcodePlayer[] ParsePlayers(IEnumerable<string> endpoints) |
101 | 99 | { |
102 | | - var players = endpoints |
103 | | - .Select((x, i) => TryParsePlayer(totalNumberOfPlayers, i + 1, x, out var player) |
104 | | - ? player |
105 | | - : throw new InvalidOperationException("Invalid endpoint address")) |
106 | | - .ToArray(); |
| 100 | + var players = endpoints.Select(ParsePlayer).ToArray(); |
107 | 101 |
|
108 | 102 | if (!players.Any(x => x.IsLocal())) |
109 | 103 | throw new InvalidOperationException("No defined local player"); |
110 | 104 |
|
111 | 105 | return players; |
112 | 106 | } |
113 | 107 |
|
114 | | -static bool TryParsePlayer( |
115 | | - int totalNumber, |
116 | | - int number, string address, |
117 | | - [NotNullWhen(true)] out Player? player) |
| 108 | +static NetcodePlayer ParsePlayer(string address) |
118 | 109 | { |
119 | 110 | if (address.Equals("local", StringComparison.OrdinalIgnoreCase)) |
120 | | - { |
121 | | - player = new LocalPlayer(number); |
122 | | - return true; |
123 | | - } |
| 111 | + return NetcodePlayer.CreateLocal(); |
| 112 | + |
| 113 | + if (address.StartsWith("s:", StringComparison.OrdinalIgnoreCase)) |
| 114 | + if (IPEndPoint.TryParse(address[2..], out var hostEndPoint)) |
| 115 | + return NetcodePlayer.CreateSpectator(hostEndPoint); |
| 116 | + else |
| 117 | + throw new InvalidOperationException("Invalid spectator endpoint"); |
124 | 118 |
|
125 | 119 | if (IPEndPoint.TryParse(address, out var endPoint)) |
126 | 120 | { |
127 | | - if (number <= totalNumber) |
128 | | - player = new RemotePlayer(number, endPoint); |
129 | | - else |
130 | | - player = new Spectator(endPoint); |
131 | | - return true; |
| 121 | + return NetcodePlayer.CreateRemote(endPoint); |
132 | 122 | } |
133 | 123 |
|
134 | | - player = null; |
135 | | - return false; |
| 124 | + throw new InvalidOperationException($"Invalid player argument: {address}"); |
136 | 125 | } |
0 commit comments