using RailEnterprise.Core.Common;
namespace RailEnterprise.Protocol.Messages;
// Messaggi della fase F6: ere e obsolescenza dei veicoli (ADR 0020). Il quadro del rinnovo della flotta
// arriva su richiesta (RequestDetail con DetailKind.Fleet) ed è calcolato dal server con FleetOutlook: il
// client lo mostra com'è, senza calcolare età, valori, successori o modelli in produzione.
///
/// Veicolo della compagnia per il rinnovo: modello, treno o deposito, età e vita utile, condizione media
/// (0–1), produzione, obsolescenza (anno da cui è superato, 0 se non ha successore), successore consigliato
/// ("" se non superato o senza successore in produzione), valore di libro e di rivendita, moltiplicatore dei
/// costi di manutenzione.
///
public sealed class FleetVehicleInfo
{
public int Vehicle { get; init; }
public string Type { get; init; } = "";
public int Train { get; init; } = -1;
public int Depot { get; init; } = -1;
public float AgeYears { get; init; }
public int LifeYears { get; init; }
public float Condition { get; init; }
public bool InProduction { get; init; }
public bool Superseded { get; init; }
public int SupersededYear { get; init; }
public string Successor { get; init; } = "";
public Money BookValue { get; init; }
public Money Resale { get; init; }
public float MaintenanceFactor { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Vehicle); w.String(Type); w.I32(Train); w.I32(Depot); w.F32(AgeYears); w.I32(LifeYears); w.F32(Condition);
w.Bool(InProduction); w.Bool(Superseded); w.I32(SupersededYear); w.String(Successor);
w.Money(BookValue); w.Money(Resale); w.F32(MaintenanceFactor);
w.Flags(0);
}
public static FleetVehicleInfo Read(ref SpanReader r)
{
var m = new FleetVehicleInfo
{
Vehicle = r.I32(), Type = r.String(), Train = r.I32(), Depot = r.I32(), AgeYears = r.F32(), LifeYears = r.I32(), Condition = r.F32(),
InProduction = r.Bool(), Superseded = r.Bool(), SupersededYear = r.I32(), Successor = r.String(),
BookValue = r.Money(), Resale = r.Money(), MaintenanceFactor = r.F32(),
};
r.Flags();
return m;
}
}
///
/// Modello in produzione per il catalogo d'acquisto: superato o no, anno da cui lo è (0 se non ha
/// successore), successore consigliato, ultimo anno di produzione (0 se non previsto).
///
public sealed class VehicleModelInfo
{
public string Type { get; init; } = "";
public bool Superseded { get; init; }
public int SupersededYear { get; init; }
public string Successor { get; init; } = "";
public int LastYear { get; init; }
public void Write(ref SpanWriter w)
{
w.String(Type); w.Bool(Superseded); w.I32(SupersededYear); w.String(Successor); w.I32(LastYear);
w.Flags(0);
}
public static VehicleModelInfo Read(ref SpanReader r)
{
var m = new VehicleModelInfo { Type = r.String(), Superseded = r.Bool(), SupersededYear = r.I32(), Successor = r.String(), LastYear = r.I32() };
r.Flags();
return m;
}
}
///
/// Treno con veicoli superati da rinnovare: quanti, spesa netta (successori − rivendite + lavoro), se la cassa
/// basta al netto degli altri rinnovi prenotati e se il rinnovo è già prenotato (in viaggio o in coda all'officina).
///
public sealed class RenewalTrainInfo
{
public int Train { get; init; }
public int Vehicles { get; init; }
public Money NetCost { get; init; }
public bool Affordable { get; init; }
public bool Booked { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Train); w.I32(Vehicles); w.Money(NetCost); w.Bool(Affordable); w.Bool(Booked);
w.Flags(0);
}
public static RenewalTrainInfo Read(ref SpanReader r)
{
var m = new RenewalTrainInfo { Train = r.I32(), Vehicles = r.I32(), NetCost = r.Money(), Affordable = r.Bool(), Booked = r.Bool() };
r.Flags();
return m;
}
}
///
/// Quadro del rinnovo della flotta della compagnia della sessione (F6, ADR 0020): era in corso e prossima
/// (con l'anno; "" e 0 all'ultima), veicoli con età, obsolescenza, successore e valori, catalogo dei
/// modelli in produzione, treni da rinnovare con l'intervento «renewal».
///
public sealed class FleetReport : IMessage
{
public MessageId Id => MessageId.FleetReport;
public long Day { get; init; }
public int Company { get; init; }
public int Year { get; init; }
public string Era { get; init; } = "";
public string NextEra { get; init; } = "";
public int NextEraYear { get; init; }
/// Id dell'intervento di rinnovo in maintenance.json (per ScheduleService).
public string RenewalIntervention { get; init; } = "";
public Money BookValueTotal { get; init; }
public List Vehicles { get; init; } = [];
public List Models { get; init; } = [];
public List Trains { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I64(Day); w.I32(Company); w.I32(Year); w.String(Era); w.String(NextEra); w.I32(NextEraYear); w.String(RenewalIntervention);
w.Money(BookValueTotal);
w.Count(Vehicles.Count);
foreach (var v in Vehicles) v.Write(ref w);
w.Count(Models.Count);
foreach (var m in Models) m.Write(ref w);
w.Count(Trains.Count);
foreach (var t in Trains) t.Write(ref w);
w.Flags(0);
}
public static FleetReport Read(ref SpanReader r)
{
long day = r.I64();
int company = r.I32(), year = r.I32();
string era = r.String(), next = r.String();
int nextYear = r.I32();
string renewal = r.String();
var book = r.Money();
var vehicles = new List();
for (int n = r.Count(), i = 0; i < n; i++) vehicles.Add(FleetVehicleInfo.Read(ref r));
var models = new List();
for (int n = r.Count(), i = 0; i < n; i++) models.Add(VehicleModelInfo.Read(ref r));
var trains = new List();
for (int n = r.Count(), i = 0; i < n; i++) trains.Add(RenewalTrainInfo.Read(ref r));
var m = new FleetReport
{
Day = day, Company = company, Year = year, Era = era, NextEra = next, NextEraYear = nextYear, RenewalIntervention = renewal,
BookValueTotal = book, Vehicles = vehicles, Models = models, Trains = trains,
};
r.Flags();
return m;
}
}