Skip to content

Commit cf29755

Browse files
authored
Change Player API (#122)
1 parent 3d29c75 commit cf29755

30 files changed

+438
-371
lines changed

samples/ConsoleGame/Program.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// ReSharper disable AccessToDisposedClosure, UnusedVariable
22

3-
using System.Diagnostics.CodeAnalysis;
43
using System.Net;
54
using Backdash;
65
using Backdash.Core;
@@ -56,17 +55,16 @@
5655
// not a spectator, creating a `remote` game session
5756
else
5857
{
59-
var players = ParsePlayers(playerCount, endpoints);
58+
var players = ParsePlayers(endpoints);
6059
var localPlayer = players.SingleOrDefault(x => x.IsLocal())
6160
?? throw new InvalidOperationException("No local player defined");
6261
builder
6362
// 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)
6564
.WithPlayers(players)
6665
.ForRemote();
6766
}
6867

69-
7068
var session = builder.Build();
7169

7270
// create the actual game
@@ -97,40 +95,31 @@
9795

9896
return;
9997

100-
static Player[] ParsePlayers(int totalNumberOfPlayers, IEnumerable<string> endpoints)
98+
static NetcodePlayer[] ParsePlayers(IEnumerable<string> endpoints)
10199
{
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();
107101

108102
if (!players.Any(x => x.IsLocal()))
109103
throw new InvalidOperationException("No defined local player");
110104

111105
return players;
112106
}
113107

114-
static bool TryParsePlayer(
115-
int totalNumber,
116-
int number, string address,
117-
[NotNullWhen(true)] out Player? player)
108+
static NetcodePlayer ParsePlayer(string address)
118109
{
119110
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");
124118

125119
if (IPEndPoint.TryParse(address, out var endPoint))
126120
{
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);
132122
}
133123

134-
player = null;
135-
return false;
124+
throw new InvalidOperationException($"Invalid player argument: {address}");
136125
}

samples/ConsoleGame/View.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void DrawHeader(NonGameState nonGameState)
2525
if (nonGameState.LocalPlayer is { } localPlayer)
2626
{
2727
Console.Title = $"Player {localPlayer.Number}";
28-
Console.ForegroundColor = playerColors[localPlayer.Number - 1];
28+
Console.ForegroundColor = playerColors[localPlayer.Index];
2929
Console.WriteLine($"-- Player {localPlayer.Number} --\n");
3030
}
3131
else
@@ -53,10 +53,10 @@ void DrawScore(in GameState state)
5353

5454
void DrawField(in GameState currentState, NonGameState nonGameState)
5555
{
56-
var status1 = nonGameState.RemotePlayer.Number is 1
56+
var status1 = nonGameState.RemotePlayer.Index is 0
5757
? nonGameState.RemotePlayerStatus
5858
: PlayerStatus.Running;
59-
var status2 = nonGameState.RemotePlayer.Number is 2
59+
var status2 = nonGameState.RemotePlayer.Index is 1
6060
? nonGameState.RemotePlayerStatus
6161
: PlayerStatus.Running;
6262
for (var row = 0; row < GameLogic.GridSize; row++)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dotnet build -c Debug
2-
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
2+
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 s:127.0.0.1:9100
33
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local
44
start dotnet run --no-build -- 9100 2 spectate 127.0.0.1:9000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dotnet build -c Debug
2-
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
3-
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local 127.0.0.1:9101
2+
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 s:127.0.0.1:9100
3+
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local s:127.0.0.1:9101
44
start dotnet run --no-build -- 9100 2 spectate 127.0.0.1:9000
55
start dotnet run --no-build -- 9101 2 spectate 127.0.0.1:9001

samples/SpaceWar.Lobby/Scenes/LobbyScene.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,17 @@ void StartPlayerBattleScene()
357357
if (lobbyInfo is null) return;
358358

359359
currentState = LobbyState.Starting;
360-
List<Player> players = [];
360+
List<NetcodePlayer> players = [];
361361

362362
for (var i = 0; i < lobbyInfo.Players.Length; i++)
363363
{
364364
var player = lobbyInfo.Players[i];
365-
var playerNumber = i + 1;
366365

367-
players.Add(player.PeerId == user.PeerId
368-
? new LocalPlayer(playerNumber)
369-
: new RemotePlayer(playerNumber,
370-
LobbyUdpClient.GetFallbackEndpoint(user, player)));
366+
players.Add(
367+
player.PeerId == user.PeerId
368+
? NetcodePlayer.CreateLocal()
369+
: NetcodePlayer.CreateRemote(LobbyUdpClient.GetFallbackEndpoint(user, player))
370+
);
371371
}
372372

373373
if (lobbyInfo.SpectatorMapping.SingleOrDefault(m => m.Host == user.PeerId)
@@ -377,7 +377,7 @@ void StartPlayerBattleScene()
377377
foreach (var spectator in spectators)
378378
{
379379
var spectatorEndpoint = LobbyUdpClient.GetFallbackEndpoint(user, spectator);
380-
players.Add(new Spectator(spectatorEndpoint));
380+
players.Add(NetcodePlayer.CreateSpectator(spectatorEndpoint));
381381
}
382382
}
383383

samples/SpaceWar.Shared/GameSession.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ void HandleNoGameInput(KeyboardState keyboard)
5151
if (keyboard.IsKeyDown(Keys.D4)) DisconnectPlayer(3);
5252
}
5353

54-
void DisconnectPlayer(int number)
54+
void DisconnectPlayer(int index)
5555
{
56-
if (nonGameState.NumberOfPlayers <= number) return;
57-
var handle = nonGameState.Players[number].Handle;
56+
if (nonGameState.NumberOfPlayers <= index) return;
57+
var handle = nonGameState.Players[index].Handle;
5858
session.DisconnectPlayer(in handle);
5959
nonGameState.StatusText.Clear();
6060
nonGameState.StatusText.Append("Disconnected player ");

samples/SpaceWar/Program.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static INetcodeSession<PlayerInputs> ParseSessionArgs(string[] args)
7474
lastArgs = argsAfterSave;
7575
}
7676

77-
var players = lastArgs.Select((x, i) => ParsePlayer(playerCount, i + 1, x)).ToArray();
77+
var players = lastArgs.Select(ParsePlayer).ToArray();
7878

7979
if (!players.Any(x => x.IsLocal()))
8080
throw new InvalidOperationException("No local player defined");
@@ -86,17 +86,21 @@ static INetcodeSession<PlayerInputs> ParseSessionArgs(string[] args)
8686
}
8787
}
8888

89-
static Player ParsePlayer(int totalNumber, int number, string address)
89+
static NetcodePlayer ParsePlayer(string address)
9090
{
9191
if (address.Equals("local", StringComparison.OrdinalIgnoreCase))
92-
return new LocalPlayer(number);
92+
return NetcodePlayer.CreateLocal();
93+
94+
if (address.StartsWith("s:", StringComparison.OrdinalIgnoreCase))
95+
if (IPEndPoint.TryParse(address[2..], out var hostEndPoint))
96+
return NetcodePlayer.CreateSpectator(hostEndPoint);
97+
else
98+
throw new InvalidOperationException("Invalid spectator endpoint");
9399

94100
if (IPEndPoint.TryParse(address, out var endPoint))
95101
{
96-
if (number <= totalNumber)
97-
return new RemotePlayer(number, endPoint);
98-
return new Spectator(endPoint);
102+
return NetcodePlayer.CreateRemote(endPoint);
99103
}
100104

101-
throw new InvalidOperationException($"Invalid player {number} argument: {address}");
105+
throw new InvalidOperationException($"Invalid player argument: {address}");
102106
}

samples/SpaceWar/scripts/linux/start_2players_1spec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
dotnet build -c Release "$(dirname "$0")/../.."
33
pushd "$(dirname "$0")/../../bin/Release/net8.0" || exit
44
rm ./*.log
5-
dotnet SpaceWar.dll 9000 2 local 127.0.0.1:9001 127.0.0.1:9100 &
5+
dotnet SpaceWar.dll 9000 2 local 127.0.0.1:9001 s:127.0.0.1:9100 &
66
dotnet SpaceWar.dll 9001 2 127.0.0.1:9000 local &
77
dotnet SpaceWar.dll 9100 2 spectate 127.0.0.1:9000 &
88
popd || exit

samples/SpaceWar/scripts/linux/start_2players_2spec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
dotnet build -c Release "$(dirname "$0")/../.."
33
pushd "$(dirname "$0")/../../bin/Release/net8.0" || exit
44
rm ./*.log
5-
dotnet SpaceWar.dll 9000 2 local 127.0.0.1:9001 127.0.0.1:9100 &
6-
dotnet SpaceWar.dll 9001 2 127.0.0.1:9000 local 127.0.0.1:9101 &
5+
dotnet SpaceWar.dll 9000 2 local 127.0.0.1:9001 s:127.0.0.1:9100 &
6+
dotnet SpaceWar.dll 9001 2 127.0.0.1:9000 local s:127.0.0.1:9101 &
77
dotnet SpaceWar.dll 9100 2 spectate 127.0.0.1:9000 &
88
dotnet SpaceWar.dll 9101 2 spectate 127.0.0.1:9001 &
99
popd || exit

samples/SpaceWar/scripts/linux/start_4players_2spec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
dotnet build -c Release "$(dirname "$0")/../.."
33
pushd "$(dirname "$0")/../../bin/Release/net8.0" || exit
44
rm ./*.log
5-
dotnet SpaceWar.dll 9000 4 local 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9100 &
6-
dotnet SpaceWar.dll 9001 4 127.0.0.1:9000 local 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9101 &
5+
dotnet SpaceWar.dll 9000 4 local 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 s:127.0.0.1:9100 &
6+
dotnet SpaceWar.dll 9001 4 127.0.0.1:9000 local 127.0.0.1:9002 127.0.0.1:9003 s:127.0.0.1:9101 &
77
dotnet SpaceWar.dll 9002 4 127.0.0.1:9000 127.0.0.1:9001 local 127.0.0.1:9003 &
88
dotnet SpaceWar.dll 9003 4 127.0.0.1:9000 127.0.0.1:9001 127.0.0.1:9002 local &
99
dotnet SpaceWar.dll 9100 4 spectate 127.0.0.1:9000 &

0 commit comments

Comments
 (0)