feat: add multiplayer session support with ENet host/join

This commit is contained in:
2026-08-23 18:43:31 +02:00
parent fc14cfe0c3
commit 5ee80df4c1
29 changed files with 327 additions and 5 deletions
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Game.Features.Console.Contracts;
using Game.Features.Multiplayer.Contracts;
using Game.Features.Multiplayer.Contracts.Exceptions;
namespace Game.Features.Console.Application.Commands;
public sealed class HostCommand(IMultiplayerPeer multiplayer) : ICommand
{
public string Name => "host";
public string Description => "Starts hosting a multiplayer session on the given port.";
public IReadOnlyList<CommandArgSpec> Args { get; } = new CommandArgsBuilder().AddIntArg("port").Build();
public CommandResult Execute(CommandArgValues args)
{
int port = args.GetInt(0);
try
{
multiplayer.Host(port);
}
catch (MultiplayerException ex)
{
return CommandResult.Fail(ex.Message);
}
return CommandResult.Ok($"Hosting on port {port}.");
}
}
@@ -0,0 +1 @@
uid://crmnspnxcut3y
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using Game.Features.Console.Contracts;
using Game.Features.Multiplayer.Contracts;
using Game.Features.Multiplayer.Contracts.Exceptions;
namespace Game.Features.Console.Application.Commands;
public sealed class JoinCommand(IMultiplayerPeer multiplayer) : ICommand
{
private const string AddressPattern =
@"^(localhost|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$";
public string Name => "join";
public string Description => "Joins a multiplayer session at the given address and port.";
public IReadOnlyList<CommandArgSpec> Args { get; } =
new CommandArgsBuilder().AddRegexArg("address", AddressPattern).AddIntArg("port").Build();
public CommandResult Execute(CommandArgValues args)
{
string address = args.GetString(0);
int port = args.GetInt(1);
try
{
multiplayer.Join(address, port);
}
catch (MultiplayerException ex)
{
return CommandResult.Fail(ex.Message);
}
return CommandResult.Ok($"Connecting to {address}:{port}...");
}
}
@@ -0,0 +1 @@
uid://c80rskv3u7su7