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
@@ -0,0 +1,447 @@
|
||||
using ViaFerrea.Core.Common;
|
||||
using ViaFerrea.Core.Rail;
|
||||
|
||||
namespace ViaFerrea.Protocol.Messages;
|
||||
|
||||
// Record delle entità inviati al client. Ogni record ha Write/Read e un byte di estensione finale.
|
||||
|
||||
/// <summary>Record serializzabile (entità, archi, eventi).</summary>
|
||||
public interface IWritable
|
||||
{
|
||||
void Write(ref SpanWriter w);
|
||||
}
|
||||
|
||||
/// <summary>Tipo di entità nei riferimenti e nelle rimozioni.</summary>
|
||||
public enum EntityType : byte { Company = 0, Town = 1, Industry = 2, Hub = 3, Node = 4, Station = 5, Depot = 6, Line = 7, Vehicle = 8, Train = 9, Edge = 10 }
|
||||
|
||||
public sealed class WorldSummary : IWritable
|
||||
{
|
||||
public ulong Seed { get; init; }
|
||||
public int SizeKm { get; init; }
|
||||
public int StartYear { get; init; }
|
||||
public long Tick { get; init; }
|
||||
public string Date { get; init; } = "";
|
||||
public int CompanyId { get; init; }
|
||||
public string CompanyName { get; init; } = "";
|
||||
public string RegionId { get; init; } = "";
|
||||
public int WidthCells { get; init; }
|
||||
public int HeightCells { get; init; }
|
||||
public double CellSizeM { get; init; }
|
||||
public int ChunkCells { get; init; }
|
||||
public byte Speed { get; init; }
|
||||
public ulong TerrainHash { get; init; }
|
||||
/// <summary>Moltiplicatore dei costi di costruzione della compagnia (difficoltà), per le anteprime.</summary>
|
||||
public double CostMultiplier { get; init; } = 1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U64(Seed); w.I32(SizeKm); w.I32(StartYear); w.I64(Tick); w.String(Date);
|
||||
w.I32(CompanyId); w.String(CompanyName); w.String(RegionId);
|
||||
w.I32(WidthCells); w.I32(HeightCells); w.F64(CellSizeM); w.I32(ChunkCells);
|
||||
w.U8(Speed); w.U64(TerrainHash); w.F64(CostMultiplier);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static WorldSummary Read(ref SpanReader r)
|
||||
{
|
||||
var m = new WorldSummary
|
||||
{
|
||||
Seed = r.U64(), SizeKm = r.I32(), StartYear = r.I32(), Tick = r.I64(), Date = r.String(),
|
||||
CompanyId = r.I32(), CompanyName = r.String(), RegionId = r.String(),
|
||||
WidthCells = r.I32(), HeightCells = r.I32(), CellSizeM = r.F64(), ChunkCells = r.I32(),
|
||||
Speed = r.U8(), TerrainHash = r.U64(), CostMultiplier = r.F64(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CompanyInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public string Color { get; init; } = "";
|
||||
public bool IsPlayer { get; init; }
|
||||
public Money Cash { get; init; }
|
||||
public Money NetWorth { get; init; }
|
||||
public string Rating { get; init; } = "";
|
||||
public byte Tier { get; init; }
|
||||
public bool Bankrupt { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Name); w.String(Color); w.Bool(IsPlayer); w.Money(Cash); w.Money(NetWorth);
|
||||
w.String(Rating); w.U8(Tier); w.Bool(Bankrupt);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static CompanyInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new CompanyInfo
|
||||
{
|
||||
Id = r.I32(), Name = r.String(), Color = r.String(), IsPlayer = r.Bool(), Cash = r.Money(), NetWorth = r.Money(),
|
||||
Rating = r.String(), Tier = r.U8(), Bankrupt = r.Bool(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TownInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
public int Population { get; init; }
|
||||
public double RadiusCells { get; init; }
|
||||
public float Satisfaction { get; init; }
|
||||
public int Services { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Name); w.Vec2(Position); w.I32(Population); w.F64(RadiusCells); w.F32(Satisfaction); w.I32(Services);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static TownInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new TownInfo { Id = r.I32(), Name = r.String(), Position = r.Vec2(), Population = r.I32(), RadiusCells = r.F64(), Satisfaction = r.F32(), Services = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class IndustryInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Type { get; init; } = "";
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
public int FootprintCells { get; init; }
|
||||
public int Level { get; init; }
|
||||
public bool Closed { get; init; }
|
||||
public float Efficiency { get; init; }
|
||||
public float Condition { get; init; }
|
||||
public int Owner { get; init; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Type); w.String(Name); w.Vec2(Position); w.I32(FootprintCells); w.I32(Level); w.Bool(Closed);
|
||||
w.F32(Efficiency); w.F32(Condition); w.I32(Owner);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static IndustryInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new IndustryInfo
|
||||
{
|
||||
Id = r.I32(), Type = r.String(), Name = r.String(), Position = r.Vec2(), FootprintCells = r.I32(), Level = r.I32(), Closed = r.Bool(),
|
||||
Efficiency = r.F32(), Condition = r.F32(), Owner = r.I32(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class HubInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Kind { get; init; } = "";
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Kind); w.String(Name); w.Vec2(Position);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static HubInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new HubInfo { Id = r.I32(), Kind = r.String(), Name = r.String(), Position = r.Vec2() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NodeInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public double HeightM { get; init; }
|
||||
public int Owner { get; init; } = -1;
|
||||
public int Station { get; init; } = -1;
|
||||
public int Depot { get; init; } = -1;
|
||||
public int[] Edges { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.Vec2(Position); w.F64(HeightM); w.I32(Owner); w.I32(Station); w.I32(Depot);
|
||||
w.Count(Edges.Length);
|
||||
foreach (int e in Edges) w.I32(e);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static NodeInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32();
|
||||
var pos = r.Vec2();
|
||||
double h = r.F64();
|
||||
int owner = r.I32(), station = r.I32(), depot = r.I32();
|
||||
var edges = new int[r.Count()];
|
||||
for (int i = 0; i < edges.Length; i++) edges[i] = r.I32();
|
||||
r.Flags();
|
||||
return new NodeInfo { Id = id, Position = pos, HeightM = h, Owner = owner, Station = station, Depot = depot, Edges = edges };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StationInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Node { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public int Platforms { get; init; }
|
||||
public int WaitingUnits { get; init; }
|
||||
public int[] Industries { get; init; } = [];
|
||||
public int[] Towns { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.I32(Platforms); w.I32(WaitingUnits);
|
||||
w.Count(Industries.Length);
|
||||
foreach (int i in Industries) w.I32(i);
|
||||
w.Count(Towns.Length);
|
||||
foreach (int t in Towns) w.I32(t);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static StationInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String();
|
||||
int node = r.I32();
|
||||
var pos = r.Vec2();
|
||||
int platforms = r.I32(), waiting = r.I32();
|
||||
var industries = new int[r.Count()];
|
||||
for (int i = 0; i < industries.Length; i++) industries[i] = r.I32();
|
||||
var towns = new int[r.Count()];
|
||||
for (int i = 0; i < towns.Length; i++) towns[i] = r.I32();
|
||||
r.Flags();
|
||||
return new StationInfo { Id = id, Owner = owner, Name = name, Node = node, Position = pos, Platforms = platforms, WaitingUnits = waiting, Industries = industries, Towns = towns };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DepotInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Node { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public string Size { get; init; } = "";
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.String(Size);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static DepotInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new DepotInfo { Id = r.I32(), Owner = r.I32(), Name = r.String(), Node = r.I32(), Position = r.Vec2(), Size = r.String() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LineInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public string Color { get; init; } = "";
|
||||
public LineStop[] Stops { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.String(Color);
|
||||
w.Count(Stops.Length);
|
||||
foreach (var s in Stops)
|
||||
{
|
||||
w.I32(s.Station.Value); w.Bool(s.Load); w.Bool(s.Unload); w.Bool(s.WaitFull); w.I32(s.MaxWaitDays);
|
||||
}
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static LineInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String(), color = r.String();
|
||||
var stops = new LineStop[r.Count()];
|
||||
for (int i = 0; i < stops.Length; i++) stops[i] = new LineStop(new StationId(r.I32()), r.Bool(), r.Bool(), r.Bool(), r.I32());
|
||||
r.Flags();
|
||||
return new LineInfo { Id = id, Owner = owner, Name = name, Color = color, Stops = stops };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VehicleInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Type { get; init; } = "";
|
||||
public int Depot { get; init; } = -1;
|
||||
public int Train { get; init; } = -1;
|
||||
public float Condition { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Type); w.I32(Depot); w.I32(Train); w.F32(Condition);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static VehicleInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new VehicleInfo { Id = r.I32(), Owner = r.I32(), Type = r.String(), Depot = r.I32(), Train = r.I32(), Condition = r.F32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TrainInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Line { get; init; } = -1;
|
||||
public byte State { get; init; }
|
||||
public int[] Vehicles { get; init; } = [];
|
||||
public float MaxSpeedKmh { get; init; }
|
||||
public float LengthM { get; init; }
|
||||
public int Load { get; init; }
|
||||
public int Capacity { get; init; }
|
||||
public int Depot { get; init; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Line); w.U8(State);
|
||||
w.Count(Vehicles.Length);
|
||||
foreach (int v in Vehicles) w.I32(v);
|
||||
w.F32(MaxSpeedKmh); w.F32(LengthM); w.I32(Load); w.I32(Capacity); w.I32(Depot);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static TrainInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String();
|
||||
int line = r.I32();
|
||||
byte state = r.U8();
|
||||
var vehicles = new int[r.Count()];
|
||||
for (int i = 0; i < vehicles.Length; i++) vehicles[i] = r.I32();
|
||||
var m = new TrainInfo
|
||||
{
|
||||
Id = id, Owner = owner, Name = name, Line = line, State = state, Vehicles = vehicles,
|
||||
MaxSpeedKmh = r.F32(), LengthM = r.F32(), Load = r.I32(), Capacity = r.I32(), Depot = r.I32(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Arco di binario con i campioni del profilo (TrackWorks): precisione piena per le anteprime del client.</summary>
|
||||
public sealed class EdgeInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public int A { get; init; }
|
||||
public int B { get; init; }
|
||||
public byte TrackClass { get; init; }
|
||||
public bool DoubleTrack { get; init; }
|
||||
public bool Electrified { get; init; }
|
||||
public byte State { get; init; }
|
||||
public float Condition { get; init; }
|
||||
public float StructureCondition { get; init; }
|
||||
/// <summary>Avanzamento del cantiere 0–1 (1 = completato).</summary>
|
||||
public float Progress { get; init; } = 1;
|
||||
public double[] S { get; init; } = [];
|
||||
public double[] X { get; init; } = [];
|
||||
public double[] Y { get; init; } = [];
|
||||
public double[] H { get; init; } = [];
|
||||
public double[] Ground { get; init; } = [];
|
||||
public byte[] Kinds { get; init; } = [];
|
||||
public double[] SpeedLimitKmh { get; init; } = [];
|
||||
public double[] Signals { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.I32(A); w.I32(B); w.U8(TrackClass); w.Bool(DoubleTrack); w.Bool(Electrified); w.U8(State);
|
||||
w.F32(Condition); w.F32(StructureCondition); w.F32(Progress);
|
||||
int n = S.Length;
|
||||
w.Count(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
w.F64(S[i]); w.F64(X[i]); w.F64(Y[i]); w.F64(H[i]); w.F64(Ground[i]); w.U8(Kinds[i]); w.F64(SpeedLimitKmh[i]);
|
||||
}
|
||||
w.Count(Signals.Length);
|
||||
foreach (double s in Signals) w.F64(s);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EdgeInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32(), a = r.I32(), b = r.I32();
|
||||
byte cls = r.U8();
|
||||
bool dbl = r.Bool(), el = r.Bool();
|
||||
byte state = r.U8();
|
||||
float cond = r.F32(), sc = r.F32(), progress = r.F32();
|
||||
int n = r.Count();
|
||||
var s = new double[n]; var x = new double[n]; var y = new double[n]; var h = new double[n]; var g = new double[n];
|
||||
var k = new byte[n]; var v = new double[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
s[i] = r.F64(); x[i] = r.F64(); y[i] = r.F64(); h[i] = r.F64(); g[i] = r.F64(); k[i] = r.U8(); v[i] = r.F64();
|
||||
}
|
||||
var signals = new double[r.Count()];
|
||||
for (int i = 0; i < signals.Length; i++) signals[i] = r.F64();
|
||||
r.Flags();
|
||||
return new EdgeInfo
|
||||
{
|
||||
Id = id, Owner = owner, A = a, B = b, TrackClass = cls, DoubleTrack = dbl, Electrified = el, State = state,
|
||||
Condition = cond, StructureCondition = sc, Progress = progress, S = s, X = x, Y = y, H = h, Ground = g, Kinds = k,
|
||||
SpeedLimitKmh = v, Signals = signals,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Evento della simulazione (notifiche, ticker, resoconto giornaliero).</summary>
|
||||
public sealed class EventInfo : IWritable
|
||||
{
|
||||
public long Day { get; init; }
|
||||
public byte Kind { get; init; }
|
||||
public byte Severity { get; init; }
|
||||
public string Text { get; init; } = "";
|
||||
public byte Entity { get; init; }
|
||||
public int EntityIndex { get; init; } = -1;
|
||||
public int Company { get; init; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day); w.U8(Kind); w.U8(Severity); w.String(Text); w.U8(Entity); w.I32(EntityIndex); w.I32(Company);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EventInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EventInfo { Day = r.I64(), Kind = r.U8(), Severity = r.U8(), Text = r.String(), Entity = r.U8(), EntityIndex = r.I32(), Company = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Tratto di moto di un treno: arco percorso nel verso indicato tra due progressive.</summary>
|
||||
public readonly record struct MotionSegment(int Edge, bool Forward, double FromS, double ToS)
|
||||
{
|
||||
public double Length => Math.Abs(ToS - FromS);
|
||||
}
|
||||
Reference in new issue
Block a user