using RailEnterprise.Core.Common;
namespace RailEnterprise.Protocol.Messages;
// Messaggi della fase F6: pannello «Mercato globale» (immagine di riferimento 06). Il quadro del
// mercato arriva su richiesta (RequestDetail con DetailKind.Market) ed è calcolato dal server con
// MarketOutlook (ADR 0015): il client lo mostra com'è, senza logica di gioco.
/// Prezzo globale di un giorno (centesimi per unità).
public sealed class PricePointInfo
{
public long Day { get; init; }
public double Price { get; init; }
public void Write(ref SpanWriter w)
{
w.I64(Day); w.F64(Price);
w.Flags(0);
}
public static PricePointInfo Read(ref SpanReader r)
{
var m = new PricePointInfo { Day = r.I64(), Price = r.F64() };
r.Flags();
return m;
}
}
/// Prezzo previsto a una distanza in mesi, con la banda bassa e alta (centesimi).
public sealed class ForecastInfo
{
public byte Months { get; init; }
public double Price { get; init; }
public double Low { get; init; }
public double High { get; init; }
public void Write(ref SpanWriter w)
{
w.U8(Months); w.F64(Price); w.F64(Low); w.F64(High);
w.Flags(0);
}
public static ForecastInfo Read(ref SpanReader r)
{
var m = new ForecastInfo { Months = r.U8(), Price = r.F64(), Low = r.F64(), High = r.F64() };
r.Flags();
return m;
}
}
/// Destinazione di vendita: Kind 0 industria, 1 hub; Line −1 se la compagnia non la collega.
public sealed class SellPlaceInfo
{
public const byte Industry = 0;
public const byte Hub = 1;
public byte Kind { get; init; }
public int Target { get; init; }
public double Price { get; init; }
public int Line { get; init; } = -1;
public void Write(ref SpanWriter w)
{
w.U8(Kind); w.I32(Target); w.F64(Price); w.I32(Line);
w.Flags(0);
}
public static SellPlaceInfo Read(ref SpanReader r)
{
var m = new SellPlaceInfo { Kind = r.U8(), Target = r.I32(), Price = r.F64(), Line = r.I32() };
r.Flags();
return m;
}
}
/// Quadro di un bene: prezzo, variazione, offerta e domanda, scorte, storico, previsione, vendite.
public sealed class GoodMarketInfo
{
public int Good { get; init; }
public bool Available { get; init; }
public double Price { get; init; }
/// Variazione a 30 giorni (frazione).
public float Change { get; init; }
/// Offerta e domanda della mappa in un mese (unità).
public double SupplyMonth { get; init; }
public double DemandMonth { get; init; }
/// Quota mondiale scambiata dagli hub in un mese (unità).
public double WorldSupplyMonth { get; init; }
public double WorldDemandMonth { get; init; }
public double StockUnits { get; init; }
/// Scorte in mesi di domanda della mappa (−1 senza domanda).
public float StockMonths { get; init; } = -1;
public List History { get; init; } = [];
public List Forecast { get; init; } = [];
public List SellPlaces { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Good); w.Bool(Available); w.F64(Price); w.F32(Change);
w.F64(SupplyMonth); w.F64(DemandMonth); w.F64(WorldSupplyMonth); w.F64(WorldDemandMonth); w.F64(StockUnits); w.F32(StockMonths);
w.Count(History.Count);
foreach (var p in History) p.Write(ref w);
w.Count(Forecast.Count);
foreach (var f in Forecast) f.Write(ref w);
w.Count(SellPlaces.Count);
foreach (var s in SellPlaces) s.Write(ref w);
w.Flags(0);
}
public static GoodMarketInfo Read(ref SpanReader r)
{
var m = new GoodMarketInfo
{
Good = r.I32(), Available = r.Bool(), Price = r.F64(), Change = r.F32(),
SupplyMonth = r.F64(), DemandMonth = r.F64(), WorldSupplyMonth = r.F64(), WorldDemandMonth = r.F64(), StockUnits = r.F64(), StockMonths = r.F32(),
};
for (int n = r.Count(), i = 0; i < n; i++) m.History.Add(PricePointInfo.Read(ref r));
for (int n = r.Count(), i = 0; i < n; i++) m.Forecast.Add(ForecastInfo.Read(ref r));
for (int n = r.Count(), i = 0; i < n; i++) m.SellPlaces.Add(SellPlaceInfo.Read(ref r));
r.Flags();
return m;
}
}
/// Hub verso il mercato mondiale: Kind 0 porto, 1 valico; Line −1 se la compagnia non lo collega.
public sealed class HubMarketInfo
{
public int Hub { get; init; }
public byte Kind { get; init; }
public int MonthlyCapacity { get; init; }
public float Duty { get; init; }
public int UsedThisMonth { get; init; }
public int Line { get; init; } = -1;
public void Write(ref SpanWriter w)
{
w.I32(Hub); w.U8(Kind); w.I32(MonthlyCapacity); w.F32(Duty); w.I32(UsedThisMonth); w.I32(Line);
w.Flags(0);
}
public static HubMarketInfo Read(ref SpanReader r)
{
var m = new HubMarketInfo { Hub = r.I32(), Kind = r.U8(), MonthlyCapacity = r.I32(), Duty = r.F32(), UsedThisMonth = r.I32(), Line = r.I32() };
r.Flags();
return m;
}
}
///
/// Bene nel magazzino di una stazione della compagnia (compravendita in proprio, ADR 0016): unità
/// possedute e quotazione della prossima unità al venditore più conveniente e al compratore migliore
/// del bacino (−1 senza controparti), con le unità che offrono o accettano.
///
public sealed class WarehouseGoodInfo
{
public int Good { get; init; }
public int Units { get; init; }
public double BuyPrice { get; init; } = -1;
public int BuyUnits { get; init; }
public double SellPrice { get; init; } = -1;
public int SellUnits { get; init; }
public void Write(ref SpanWriter w)
{
w.I32(Good); w.I32(Units); w.F64(BuyPrice); w.I32(BuyUnits); w.F64(SellPrice); w.I32(SellUnits);
w.Flags(0);
}
public static WarehouseGoodInfo Read(ref SpanReader r)
{
var m = new WarehouseGoodInfo { Good = r.I32(), Units = r.I32(), BuyPrice = r.F64(), BuyUnits = r.I32(), SellPrice = r.F64(), SellUnits = r.I32() };
r.Flags();
return m;
}
}
/// Magazzino della compagnia in una sua stazione: capacità, merce in arrivo e beni.
public sealed class WarehouseInfo
{
public int Station { get; init; }
public int Capacity { get; init; }
/// Unità di merce propria in viaggio verso la stazione (occupano già il magazzino).
public int Incoming { get; init; }
public List Goods { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I32(Station); w.I32(Capacity); w.I32(Incoming);
w.Count(Goods.Count);
foreach (var g in Goods) g.Write(ref w);
w.Flags(0);
}
public static WarehouseInfo Read(ref SpanReader r)
{
var m = new WarehouseInfo { Station = r.I32(), Capacity = r.I32(), Incoming = r.I32() };
for (int n = r.Count(), i = 0; i < n; i++) m.Goods.Add(WarehouseGoodInfo.Read(ref r));
r.Flags();
return m;
}
}
///
/// Quadro del mercato globale per la compagnia della sessione (F6): indice materie prime, beni con
/// storico mensile e previsione, hub, evento in corso (shock mondiale ed eventi del mondo sulla domanda
/// di un bene) e avanzamento verso la compravendita in proprio.
///
public sealed class MarketReport : IMessage
{
public MessageId Id => MessageId.MarketReport;
public long Day { get; init; }
/// Indice materie prime (100 = prezzi base) e variazione a 30 giorni (frazione).
public float Index { get; init; }
public float IndexChange { get; init; }
public List Goods { get; init; } = [];
public List Hubs { get; init; } = [];
/// Evento in corso: bene (−1 nessuno), shock mondiale con segno (frazione del prezzo base), mesi stimati.
public int EventGood { get; init; } = -1;
public float EventShock { get; init; }
public float EventMonths { get; init; }
/// Compravendita in proprio: sbloccata, livello che la sblocca, avanzamento 0–1 e requisiti.
public bool TradingUnlocked { get; init; }
public byte TradingTier { get; init; }
public float TradingProgress { get; init; }
public Money NetWorth { get; init; }
public Money NetWorthRequired { get; init; }
public int TownsConnected { get; init; }
public int TownsRequired { get; init; }
public int TownPopulation { get; init; }
public string Rating { get; init; } = "";
public string RatingRequired { get; init; } = "";
public bool RatingMet { get; init; }
/// Blocco 1: magazzini della compagnia nelle sue stazioni vive (ADR 0016).
public List Warehouses { get; init; } = [];
/// Blocco 2 (ADR 0017): eventi del mondo in corso sulla domanda di un bene (boom o crollo in una zona).
public List GoodEvents { get; init; } = [];
public void Write(ref SpanWriter w)
{
w.I64(Day); w.F32(Index); w.F32(IndexChange);
w.Count(Goods.Count);
foreach (var g in Goods) g.Write(ref w);
w.Count(Hubs.Count);
foreach (var h in Hubs) h.Write(ref w);
w.I32(EventGood); w.F32(EventShock); w.F32(EventMonths);
w.Bool(TradingUnlocked); w.U8(TradingTier); w.F32(TradingProgress); w.Money(NetWorth); w.Money(NetWorthRequired);
w.I32(TownsConnected); w.I32(TownsRequired); w.I32(TownPopulation); w.String(Rating); w.String(RatingRequired); w.Bool(RatingMet);
w.Flags((byte)((Warehouses.Count > 0 ? 1 : 0) | (GoodEvents.Count > 0 ? 2 : 0)));
if (Warehouses.Count > 0)
{
w.Count(Warehouses.Count);
foreach (var h in Warehouses) h.Write(ref w);
}
if (GoodEvents.Count > 0)
{
w.Count(GoodEvents.Count);
foreach (var e in GoodEvents) e.Write(ref w);
}
}
public static MarketReport Read(ref SpanReader r)
{
long day = r.I64();
float index = r.F32(), change = r.F32();
var goods = new List();
for (int n = r.Count(), i = 0; i < n; i++) goods.Add(GoodMarketInfo.Read(ref r));
var hubs = new List();
for (int n = r.Count(), i = 0; i < n; i++) hubs.Add(HubMarketInfo.Read(ref r));
var m = new MarketReport
{
Day = day, Index = index, IndexChange = change, Goods = goods, Hubs = hubs,
EventGood = r.I32(), EventShock = r.F32(), EventMonths = r.F32(),
TradingUnlocked = r.Bool(), TradingTier = r.U8(), TradingProgress = r.F32(), NetWorth = r.Money(), NetWorthRequired = r.Money(),
TownsConnected = r.I32(), TownsRequired = r.I32(), TownPopulation = r.I32(), Rating = r.String(), RatingRequired = r.String(), RatingMet = r.Bool(),
};
byte flags = r.Flags();
if ((flags & 1) != 0)
{
for (int n = r.Count(), i = 0; i < n; i++) m.Warehouses.Add(WarehouseInfo.Read(ref r));
}
if ((flags & 2) != 0)
{
for (int n = r.Count(), i = 0; i < n; i++) m.GoodEvents.Add(WorldEventInfo.Read(ref r));
}
return m;
}
}