Files
Rail_Enterprise/src/RailEnterprise.Protocol/Messages/ExplanationMessages.cs
T
Alby96andClaude Opus 5.5 19a3dd3af1 F6: tooltip perché sui valori calcolati dal server
Ogni valore importante dei pannelli spiega al passaggio del mouse da dove
viene: formula, termini con i valori di oggi e note, calcolati dal Core e
mandati dal server, perché il client non deve rifare né ricomporre formule.

- Core: modello comune Explanation ed Explanations, pure funzioni che
  chiamano le stesse funzioni del motore; estratte senza cambiare
  l'aritmetica (hash invariati) Market.NextPrice e LocalPriceTerms,
  Tariffs.FreightTerms e PassengerTerms, TownSystem.GrowthOf,
  Fleet.DailyHazard, GameClock.YearsAt; Industry.LastEfficiency
  transitorio (né salvato né nell'hash).
- Protocollo: RequestDetail kind 11 con un blocco 1 (subject, arg) ed
  ExplanationReport 137; il server risponde sempre, anche con available
  falso. Nessun campo nei delta, formato dei salvataggi invariato.
- Client: WhyTooltip comune (palette 05, apertura dopo 0,45 s, «in
  arrivo…» finché la spiegazione manca) su barra superiore, Mercato,
  Scalata, Finanze, scheda industria, Treni e Officine; testi in
  strings.csv.
- ADR 0024, PROTOCOL.md, ARCHITECTURE.md, STATE.md; 10 test nuovi in
  Economy/ExplanationTests.cs con uguaglianza esatta ai valori del motore.

Refs #44.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 15:14:57 +01:00

93 lines
3.8 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.
namespace RailEnterprise.Protocol.Messages;
// Messaggi della fase F6: tooltip «perché» (ADR 0024). La spiegazione di un valore arriva su richiesta
// (RequestDetail con DetailKind.Explain, bersaglio nel blocco 1 della richiesta) ed è calcolata dal server
// con Explanations del Core: il client la mostra com'è, senza rifare formule né valori.
/// <summary>
/// Termine di una spiegazione: simbolo nella formula, etichetta (testo italiano fisso, chiave di traduzione),
/// valore, unità (ExplainUnit del Core: 0 numero, 1 importo in centesimi, 2 prezzo in centesimi, 3 percentuale,
/// 4 fattore, 5 km, 6 giorni, 7 unità, 8 condizione 0–100, 9 anni, 10 testo) e testo per i valori testuali.
/// </summary>
public sealed class ExplanationTermInfo
{
public string Symbol { get; init; } = "";
public string Label { get; init; } = "";
public double Value { get; init; }
public byte Unit { get; init; }
public string Text { get; init; } = "";
public void Write(ref SpanWriter w)
{
w.String(Symbol); w.String(Label); w.F64(Value); w.U8(Unit); w.String(Text);
w.Flags(0);
}
public static ExplanationTermInfo Read(ref SpanReader r)
{
var m = new ExplanationTermInfo { Symbol = r.String(), Label = r.String(), Value = r.F64(), Unit = r.U8(), Text = r.String() };
r.Flags();
return m;
}
}
/// <summary>
/// Spiegazione di un valore per la compagnia della sessione (F6, ADR 0024): bersaglio (soggetto e due
/// argomenti, come nella richiesta), giorno, disponibilità, titolo e nome dell'oggetto, valore con unità e
/// testo, formula, termini e note. Il valore è lo stesso numero del motore.
/// </summary>
public sealed class ExplanationReport : IMessage
{
public MessageId Id => MessageId.ExplanationReport;
public long Day { get; init; }
public byte Subject { get; init; }
public int Target { get; init; } = -1;
public int Arg { get; init; } = -1;
public bool Available { get; init; }
public string Title { get; init; } = "";
public string Name { get; init; } = "";
public double Value { get; init; }
public byte Unit { get; init; }
public string ValueText { get; init; } = "";
public string Formula { get; init; } = "";
public List<ExplanationTermInfo> Terms { get; init; } = [];
public List<string> Notes { get; init; } = [];
/// <summary>Chiave della cache del client: soggetto e argomenti.</summary>
public (byte Subject, int Target, int Arg) Key => (Subject, Target, Arg);
public void Write(ref SpanWriter w)
{
w.I64(Day); w.U8(Subject); w.I32(Target); w.I32(Arg); w.Bool(Available);
w.String(Title); w.String(Name); w.F64(Value); w.U8(Unit); w.String(ValueText); w.String(Formula);
w.Count(Terms.Count);
foreach (var t in Terms) t.Write(ref w);
w.Count(Notes.Count);
foreach (var n in Notes) w.String(n);
w.Flags(0);
}
public static ExplanationReport Read(ref SpanReader r)
{
long day = r.I64();
byte subject = r.U8();
int target = r.I32(), arg = r.I32();
bool available = r.Bool();
string title = r.String(), name = r.String();
double value = r.F64();
byte unit = r.U8();
string valueText = r.String(), formula = r.String();
var terms = new List<ExplanationTermInfo>();
for (int n = r.Count(), i = 0; i < n; i++) terms.Add(ExplanationTermInfo.Read(ref r));
var notes = new List<string>();
for (int n = r.Count(), i = 0; i < n; i++) notes.Add(r.String());
var m = new ExplanationReport
{
Day = day, Subject = subject, Target = target, Arg = arg, Available = available, Title = title, Name = name,
Value = value, Unit = unit, ValueText = valueText, Formula = formula, Terms = terms, Notes = notes,
};
r.Flags();
return m;
}
}