F3: protocollo binario completo, server con WebSocket e immagine Docker
- Protocol: SpanWriter/SpanReader, frame [u32][u16][payload], tutti i messaggi dei due versi con byte di estensione, codec di tutti i 38 comandi, test di round-trip, troncamento e corruzione. - Comandi delle fasi successive con formato definitivo (NotImplemented). - ClientWorld e ProtocolClient condivisi da client Godot e test: copia del terreno a chunk, entità, rete con gli stessi id, moto dei treni. - TrackPreview estratto dai comandi: anteprima del client e piano del server con lo stesso codice; stazioni e depositi demoliti esclusi dagli agganci. - Server: GameLoop con accumulatore e velocità effettiva, sessioni con coda a due classi e ripresa, delta per hash, TrackWorks a lotti, moto dei treni nel viewport, resoconti giornalieri, /health, /metrics, /api/saves, autosave a rotazione, log JSON, --local e --healthcheck. - Docker: Dockerfile, compose, modello Unraid; build senza rete. - viaferrea-tools smoke: collaudo end-to-end contro un server in esecuzione. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
This commit is contained in:
36 files changed
+4940
-62
No files matched your search
@@ -12,46 +12,28 @@ public static class RailCommandHandlers
|
||||
{
|
||||
public delegate CommandResult Reply(CommandError error, string message, params int[] created);
|
||||
|
||||
/// <summary>Contesto del pianificatore per una compagnia (difficoltà e sconto IA inclusi).</summary>
|
||||
public static TrackPlanContext PlanContext(World w, Company company, ITrackObstacles? obstacles = null) => new()
|
||||
/// <summary>Moltiplicatore dei costi di costruzione di una compagnia (difficoltà e sconto IA).</summary>
|
||||
public static double CostMultiplier(World w, Company company) =>
|
||||
w.Difficulty.ConstructionCostMultiplier * (company.IsPlayer ? 1.0 : 1.0 - w.Difficulty.AiCostDiscount);
|
||||
|
||||
/// <summary>Ingressi dell'anteprima ricavati dal mondo autorevole.</summary>
|
||||
public static TrackPreviewInputs PreviewInputs(World w, Company company) => new()
|
||||
{
|
||||
Data = w.Data,
|
||||
Sampler = w.Sampler,
|
||||
Network = w.Network,
|
||||
Year = w.Clock.Year,
|
||||
CostMultiplier = w.Difficulty.ConstructionCostMultiplier * (company.IsPlayer ? 1.0 : 1.0 - w.Difficulty.AiCostDiscount),
|
||||
Obstacles = obstacles,
|
||||
CostMultiplier = CostMultiplier(w, company),
|
||||
NearestTownEdgeKm = p => w.Towns.Count == 0 ? double.MaxValue : w.Towns.Min(t => w.TownEdgeDistanceKm(t, p)),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prepara il piano di un binario come farebbe il comando: aggancia le estremità a nodi e archi
|
||||
/// esistenti e considera gli attraversamenti. Usato anche per l'anteprima nel client.
|
||||
/// </summary>
|
||||
public static (TrackPlan Plan, AttachPoint Start, AttachPoint End) PreparePlan(World w, Company company, BuildTrackCommand c)
|
||||
{
|
||||
var sp = w.Data.Construction.Stations;
|
||||
var net = w.Network;
|
||||
var start = net.FindAttach(c.Polyline[0], company.Id, sp.SnapM, sp.AreaRadiusM);
|
||||
var end = net.FindAttach(c.Polyline[^1], company.Id, sp.SnapM, sp.AreaRadiusM);
|
||||
var points = c.Polyline.ToList();
|
||||
points[0] = start.Position;
|
||||
points[^1] = end.Position;
|
||||
var request = new TrackPlanRequest
|
||||
{
|
||||
Polyline = points,
|
||||
HeightOffsetM = c.HeightOffsetM,
|
||||
AllowBridges = c.AllowBridges,
|
||||
AllowTunnels = c.AllowTunnels,
|
||||
TrackClass = c.TrackClass,
|
||||
DoubleTrack = c.DoubleTrack,
|
||||
Electrified = c.Electrified,
|
||||
Company = company.Id,
|
||||
StartHeightM = start.IsFree ? null : start.HeightM,
|
||||
EndHeightM = end.IsFree ? null : end.HeightM,
|
||||
};
|
||||
var obstacles = net.Obstacles(start.Position, end.Position, sp.SnapM + 1, sp.AreaRadiusM);
|
||||
return (TrackPlanner.Plan(PlanContext(w, company, obstacles), request), start, end);
|
||||
}
|
||||
/// <summary>Contesto del pianificatore per una compagnia (difficoltà e sconto IA inclusi).</summary>
|
||||
public static TrackPlanContext PlanContext(World w, Company company, ITrackObstacles? obstacles = null) =>
|
||||
TrackPreview.Context(PreviewInputs(w, company), obstacles);
|
||||
|
||||
/// <summary>Prepara il piano di un binario come farebbe il comando (stesso codice dell'anteprima del client).</summary>
|
||||
public static (TrackPlan Plan, AttachPoint Start, AttachPoint End) PreparePlan(World w, Company company, BuildTrackCommand c) =>
|
||||
TrackPreview.Prepare(PreviewInputs(w, company), company.Id, c);
|
||||
|
||||
public static CommandResult BuildTrack(World w, Company company, BuildTrackCommand c, Reply reply)
|
||||
{
|
||||
@@ -111,8 +93,8 @@ public static class RailCommandHandlers
|
||||
var type = w.Sampler.TypeAt(c.Position);
|
||||
if (type is CellType.Water or CellType.Sea or CellType.Town or CellType.Industry)
|
||||
return reply(CommandError.NotAllowed, "La stazione deve stare su terreno libero, fuori da città e industrie");
|
||||
if (w.Network.Stations.Any(s => Vec2.Distance(s.Position, c.Position) < sp.AreaRadiusM * 2)
|
||||
|| w.Network.Depots.Any(d => Vec2.Distance(d.Position, c.Position) < sp.AreaRadiusM * 2))
|
||||
if (w.Network.Stations.Any(s => w.Network.IsAlive(s) && Vec2.Distance(s.Position, c.Position) < sp.AreaRadiusM * 2)
|
||||
|| w.Network.Depots.Any(d => w.Network.IsAlive(d) && Vec2.Distance(d.Position, c.Position) < sp.AreaRadiusM * 2))
|
||||
return reply(CommandError.NotAllowed, "Troppo vicino a un'altra stazione o deposito");
|
||||
var attach = w.Network.FindAttach(c.Position, company.Id, sp.SnapM, 0);
|
||||
if (attach.Node.IsValid)
|
||||
@@ -123,7 +105,7 @@ public static class RailCommandHandlers
|
||||
if (attach.SplitEdge.IsValid && !RailNetwork.IsFree(w.Network.Edges[attach.SplitEdge.Value]))
|
||||
return reply(CommandError.Blocked, "Il binario è occupato da un treno");
|
||||
var cost = new Money(w.Data.Construction.Costs.StationBase + w.Data.Construction.Costs.StationPerPlatform * c.Platforms)
|
||||
* (w.Difficulty.ConstructionCostMultiplier * (company.IsPlayer ? 1.0 : 1.0 - w.Difficulty.AiCostDiscount));
|
||||
* CostMultiplier(w, company);
|
||||
if (company.Cash < cost) return reply(CommandError.InsufficientFunds, $"Servono {cost}");
|
||||
|
||||
var node = attach.Node.IsValid ? w.Network.Nodes[attach.Node.Value]
|
||||
@@ -161,15 +143,15 @@ public static class RailCommandHandlers
|
||||
var type = w.Sampler.TypeAt(c.Position);
|
||||
if (type is CellType.Water or CellType.Sea or CellType.Town or CellType.Industry)
|
||||
return reply(CommandError.NotAllowed, "Il deposito deve stare su terreno libero");
|
||||
if (w.Network.Stations.Any(s => Vec2.Distance(s.Position, c.Position) < sp.AreaRadiusM * 2)
|
||||
|| w.Network.Depots.Any(d => Vec2.Distance(d.Position, c.Position) < sp.AreaRadiusM * 2))
|
||||
if (w.Network.Stations.Any(s => w.Network.IsAlive(s) && Vec2.Distance(s.Position, c.Position) < sp.AreaRadiusM * 2)
|
||||
|| w.Network.Depots.Any(d => w.Network.IsAlive(d) && Vec2.Distance(d.Position, c.Position) < sp.AreaRadiusM * 2))
|
||||
return reply(CommandError.NotAllowed, "Troppo vicino a un'altra stazione o deposito");
|
||||
var attach = w.Network.FindAttach(c.Position, company.Id, sp.SnapM, 0);
|
||||
if (attach.Node.IsValid && (w.Network.Nodes[attach.Node.Value].Station.IsValid || w.Network.Nodes[attach.Node.Value].Depot.IsValid))
|
||||
return reply(CommandError.NotAllowed, "Il nodo ha già una stazione o un deposito");
|
||||
if (attach.SplitEdge.IsValid && !RailNetwork.IsFree(w.Network.Edges[attach.SplitEdge.Value]))
|
||||
return reply(CommandError.Blocked, "Il binario è occupato da un treno");
|
||||
var cost = new Money(baseCost) * (w.Difficulty.ConstructionCostMultiplier * (company.IsPlayer ? 1.0 : 1.0 - w.Difficulty.AiCostDiscount));
|
||||
var cost = new Money(baseCost) * CostMultiplier(w, company);
|
||||
if (company.Cash < cost) return reply(CommandError.InsufficientFunds, $"Servono {cost}");
|
||||
|
||||
var node = attach.Node.IsValid ? w.Network.Nodes[attach.Node.Value]
|
||||
|
||||
Reference in new issue
Block a user