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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-08 15:14:57 +01:00
1 parent 767661957b
commit 19a3dd3af1
33 files changed
+2089 -64

No files matched your search

@@ -59,6 +59,15 @@ public sealed class ClientWorld(GameData data)
/// <summary>Quadro del rinnovo della flotta (F6, ADR 0020), su richiesta: lo usano i pannelli Treni e Officine.</summary>
public FleetReport? Fleet { get; private set; }
/// <summary>
/// Spiegazioni «perché» ricevute (F6, ADR 0024), per soggetto e argomenti: l'ultima di ciascun valore. Il
/// tooltip la mostra e la richiede di nuovo quando è di un giorno passato.
/// </summary>
public Dictionary<(byte Subject, int Target, int Arg), ExplanationReport> Explanations { get; } = [];
/// <summary>Contatore delle spiegazioni ricevute (separato dai dettagli: non ricostruisce i pannelli).</summary>
public int ExplanationVersion { get; private set; }
/// <summary>Stato del server dall'ultimo Welcome (F6, ADR 0021): versione, sistema, attività, tick, sessioni.</summary>
public ServerInfo? Server { get; private set; }
@@ -161,6 +170,10 @@ public sealed class ClientWorld(GameData data)
Fleet = m;
DetailVersion++;
return true;
case ExplanationReport m:
Explanations[m.Key] = m;
ExplanationVersion++;
return true;
case SaveList m:
Saves = m;
DetailVersion++;
@@ -188,6 +201,7 @@ public sealed class ClientWorld(GameData data)
Climb = null;
Finance = null;
Fleet = null;
Explanations.Clear();
Alerts.Clear();
Summary = m.Summary;
Server = m.Server;
+2
View File
@@ -49,6 +49,8 @@ public enum MessageId : ushort
FinanceReport = 135,
/// <summary>Quadro del rinnovo della flotta: ere, veicoli superati, successori, valori (F6).</summary>
FleetReport = 136,
/// <summary>Spiegazione «perché» di un valore: formula, termini con i valori di oggi, note (F6, ADR 0024).</summary>
ExplanationReport = 137,
}
/// <summary>Messaggio del protocollo: si serializza a mano, campo per campo.</summary>
@@ -245,26 +245,52 @@ public enum DetailKind : byte
Finance = 9,
/// <summary>Quadro del rinnovo della flotta (risposta FleetReport, F6, ADR 0020; Id ignorato).</summary>
Fleet = 10,
/// <summary>
/// Spiegazione «perché» di un valore (risposta ExplanationReport, F6, ADR 0024): Target è il primo argomento,
/// soggetto e secondo argomento viaggiano nel blocco 1 della richiesta.
/// </summary>
Explain = 11,
}
/// <summary>Chiede al server un dettaglio che non viaggia con le entità (aggiunto in F4).</summary>
/// <summary>
/// Chiede al server un dettaglio che non viaggia con le entità (aggiunto in F4). Blocco 1 (F6, ADR 0024):
/// soggetto della spiegazione (ExplainSubject del Core) e secondo argomento, solo per <see cref="DetailKind.Explain"/>.
/// </summary>
public sealed class RequestDetail : IMessage
{
public MessageId Id => MessageId.RequestDetail;
public DetailKind Kind { get; init; }
public int Target { get; init; }
/// <summary>Soggetto della spiegazione (0 = nessuno: niente blocco 1).</summary>
public byte Subject { get; init; }
/// <summary>Secondo argomento della spiegazione (−1 se non serve).</summary>
public int Arg { get; init; } = -1;
public void Write(ref SpanWriter w)
{
w.U8((byte)Kind);
w.I32(Target);
w.Flags(0);
bool block1 = Subject != 0;
w.Flags(block1 ? (byte)1 : (byte)0);
if (block1)
{
w.U8(Subject);
w.I32(Arg);
}
}
public static RequestDetail Read(ref SpanReader r)
{
var m = new RequestDetail { Kind = (DetailKind)r.U8(), Target = r.I32() };
r.Flags();
return m;
var kind = (DetailKind)r.U8();
int target = r.I32();
byte flags = r.Flags();
byte subject = 0;
int arg = -1;
if ((flags & 1) != 0)
{
subject = r.U8();
arg = r.I32();
}
return new RequestDetail { Kind = kind, Target = target, Subject = subject, Arg = arg };
}
}
@@ -0,0 +1,92 @@
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;
}
}
@@ -35,6 +35,7 @@ public static class MessageCodec
MessageId.ClimbReport => ClimbReport.Read(ref r),
MessageId.FinanceReport => FinanceReport.Read(ref r),
MessageId.FleetReport => FleetReport.Read(ref r),
MessageId.ExplanationReport => ExplanationReport.Read(ref r),
MessageId.MarketTape => MarketTape.Read(ref r),
MessageId.CommandResult => CommandResultMessage.Read(ref r),
MessageId.Notification => Notification.Read(ref r),