Refactor project structure and update documentation. Replace PixiJS with plain DOM for UI rendering, enhance README with game features, and revise protocol documentation for HTTP API. Remove unused files and streamline client code for better maintainability.
This commit is contained in:
@@ -1,42 +1,28 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace HSchool.Server.Net;
|
||||
|
||||
/// <summary>Tracks live connections and hands out player ids.</summary>
|
||||
internal sealed class ClientRegistry
|
||||
{
|
||||
private readonly ConcurrentDictionary<uint, GameClient> _clients = new();
|
||||
private uint _nextPlayerId;
|
||||
|
||||
public int Count => _clients.Count;
|
||||
|
||||
public GameClient Add(WebSocket socket)
|
||||
{
|
||||
var playerId = Interlocked.Increment(ref _nextPlayerId);
|
||||
var client = new GameClient(playerId, socket);
|
||||
_clients[playerId] = client;
|
||||
return client;
|
||||
}
|
||||
|
||||
public void Remove(uint playerId) => _clients.TryRemove(playerId, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Queues the same frame for every client that finished its handshake; the buffer must not
|
||||
/// be reused afterwards.
|
||||
/// </summary>
|
||||
public int Broadcast(ReadOnlyMemory<byte> frame)
|
||||
{
|
||||
var recipients = 0;
|
||||
|
||||
foreach (var client in _clients.Values)
|
||||
{
|
||||
if (client.IsReady && client.TrySend(frame))
|
||||
{
|
||||
recipients++;
|
||||
}
|
||||
}
|
||||
|
||||
return recipients;
|
||||
}
|
||||
}
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace HSchool.Server.Net;
|
||||
|
||||
/// <summary>Tracks live connections and hands out client ids.</summary>
|
||||
internal sealed class ClientRegistry
|
||||
{
|
||||
private readonly ConcurrentDictionary<uint, GameClient> _clients = new();
|
||||
private uint _nextPlayerId;
|
||||
|
||||
public int Count => _clients.Count;
|
||||
|
||||
/// <summary>Snapshot-free enumeration; safe because the dictionary is concurrent.</summary>
|
||||
public IEnumerable<GameClient> All => _clients.Values;
|
||||
|
||||
public GameClient Add(WebSocket socket)
|
||||
{
|
||||
var playerId = Interlocked.Increment(ref _nextPlayerId);
|
||||
var client = new GameClient(playerId, socket);
|
||||
_clients[playerId] = client;
|
||||
return client;
|
||||
}
|
||||
|
||||
public GameClient? Find(uint playerId) => _clients.GetValueOrDefault(playerId);
|
||||
|
||||
public void Remove(uint playerId) => _clients.TryRemove(playerId, out _);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user