Files
Rail_Enterprise/src/RailEnterprise.Protocol/Messages/MarketMessages.cs
T
Alby96andClaude Opus 5.5 1f8da87e51 F6: eventi del mondo con effetti temporanei e notizie
Aggiunge gli eventi della F6: fatti del mondo che cambiano il gioco per
un periodo limitato, leggibili nel notiziario e reversibili.

- Catalogo data/events.json con schema e validazione: incidente e boom
  di un'industria, fiera ed epidemia, boom e crollo della domanda di un
  bene in una zona, maltempo su un'area, sciopero di città o deposito.
- Effetti come moltiplicatori letti da un'unica sorgente
  (World.WorldEvents) dai sistemi esistenti: capacità delle industrie,
  consumi delle città, presenze dei pendolari, velocità dei treni e usura
  del binario. A evento finito il moltiplicatore torna 1.
- EventSystem tra le IA e l'hash, con flusso Rng dedicato «events»,
  bersaglio in ordine di id, frequenza per mappa scalata dalla difficoltà
  (eventFrequencyMultiplier), tetto di 6, niente doppioni, notizie di
  inizio e fine in italiano (EventKind.World).
- Salvataggio corpo versione 5 con gli eventi nell'hash; le versioni 1-4
  si caricano senza eventi e con la frequenza del preset.
- Protocollo: eventi in corso nel DailyDigest e nel MarketReport; client
  con il riquadro «Eventi in corso», avvisi nelle schede e «evento in
  corso» nel Mercato globale.
- Lo scenario Valdoria disattiva gli eventi: con gli eventi attivi la
  flotta F4 senza linea dei ricambi non scendeva più sotto 40, e
  l'accettazione deve misurare la manutenzione, non la fortuna (ADR 0017).
  Accettazione F4 invariata: 35 / 20 guasti senza linea, 75 / 2 con.

Decisioni in docs/adr/0017-eventi-del-mondo.md; 17 test nuovi.

Refs #33.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:59:48 +01:00

288 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
/// <summary>Prezzo globale di un giorno (centesimi per unità).</summary>
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;
}
}
/// <summary>Prezzo previsto a una distanza in mesi, con la banda bassa e alta (centesimi).</summary>
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;
}
}
/// <summary>Destinazione di vendita: Kind 0 industria, 1 hub; Line −1 se la compagnia non la collega.</summary>
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;
}
}
/// <summary>Quadro di un bene: prezzo, variazione, offerta e domanda, scorte, storico, previsione, vendite.</summary>
public sealed class GoodMarketInfo
{
public int Good { get; init; }
public bool Available { get; init; }
public double Price { get; init; }
/// <summary>Variazione a 30 giorni (frazione).</summary>
public float Change { get; init; }
/// <summary>Offerta e domanda della mappa in un mese (unità).</summary>
public double SupplyMonth { get; init; }
public double DemandMonth { get; init; }
/// <summary>Quota mondiale scambiata dagli hub in un mese (unità).</summary>
public double WorldSupplyMonth { get; init; }
public double WorldDemandMonth { get; init; }
public double StockUnits { get; init; }
/// <summary>Scorte in mesi di domanda della mappa (−1 senza domanda).</summary>
public float StockMonths { get; init; } = -1;
public List<PricePointInfo> History { get; init; } = [];
public List<ForecastInfo> Forecast { get; init; } = [];
public List<SellPlaceInfo> 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;
}
}
/// <summary>Hub verso il mercato mondiale: Kind 0 porto, 1 valico; Line −1 se la compagnia non lo collega.</summary>
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;
}
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
/// <summary>Magazzino della compagnia in una sua stazione: capacità, merce in arrivo e beni.</summary>
public sealed class WarehouseInfo
{
public int Station { get; init; }
public int Capacity { get; init; }
/// <summary>Unità di merce propria in viaggio verso la stazione (occupano già il magazzino).</summary>
public int Incoming { get; init; }
public List<WarehouseGoodInfo> 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;
}
}
/// <summary>
/// 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.
/// </summary>
public sealed class MarketReport : IMessage
{
public MessageId Id => MessageId.MarketReport;
public long Day { get; init; }
/// <summary>Indice materie prime (100 = prezzi base) e variazione a 30 giorni (frazione).</summary>
public float Index { get; init; }
public float IndexChange { get; init; }
public List<GoodMarketInfo> Goods { get; init; } = [];
public List<HubMarketInfo> Hubs { get; init; } = [];
/// <summary>Evento in corso: bene (−1 nessuno), shock mondiale con segno (frazione del prezzo base), mesi stimati.</summary>
public int EventGood { get; init; } = -1;
public float EventShock { get; init; }
public float EventMonths { get; init; }
/// <summary>Compravendita in proprio: sbloccata, livello che la sblocca, avanzamento 0–1 e requisiti.</summary>
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; }
/// <summary>Blocco 1: magazzini della compagnia nelle sue stazioni vive (ADR 0016).</summary>
public List<WarehouseInfo> Warehouses { get; init; } = [];
/// <summary>Blocco 2 (ADR 0017): eventi del mondo in corso sulla domanda di un bene (boom o crollo in una zona).</summary>
public List<WorldEventInfo> 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<GoodMarketInfo>();
for (int n = r.Count(), i = 0; i < n; i++) goods.Add(GoodMarketInfo.Read(ref r));
var hubs = new List<HubMarketInfo>();
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;
}
}