Completa la finanza della guida 6.3 partendo da un'analisi delle lacune (ADR 0019): prestiti, fido per livello, rate e bancarotta ai giorni di difficulty.json c'erano già; mancavano gli stipendi delle compagnie, un rating spiegabile, il conto economico per voce e gli avvisi. - Prestiti a tasso fisso (base della difficoltà + spread del rating all'accensione), per non innescare la spirale rating -> rata -> utile; rimborso anticipato parziale e totale con messaggi chiari in italiano. - Rating mensile da margine (utile/ricavi sugli ultimi 12 mesi chiusi) e leva (debiti/patrimonio netto); il motore conserva le misure e FinanceOutlook spiega soglie, tassi e cosa serve per salire. - Voci nuove del registro (stazioni e depositi, magazzini, dazi, dividendi incassati) e stipendi del personale presente nei depositi, così il conto economico per voce somma esattamente all'utile operativo del motore. - Mesi e anni chiusi per voce nel salvataggio (versione 7, con migrazione e nell'hash); avvisi di cassa negativa alle scadenze fisse prima del limite della difficoltà, esito dell'ADR 0012 invariato. - Protocollo: DetailKind.Finance e FinanceReport; client: pannello «Finanze» a tutto schermo con conto economico, rating, prestiti e barra della cassa negativa, schermata 18_finanze nel pilota automatico. - Test in Economy/FinanceTests.cs, round-trip del protocollo e quadro via WebSocket; IA e accettazioni F4/F5 invariate. Refs #39. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
269 lines
7.7 KiB
C#
269 lines
7.7 KiB
C#
using RailEnterprise.Core.Commands;
|
|
|
|
namespace RailEnterprise.Protocol.Messages;
|
|
|
|
/// <summary>Apertura della sessione: versione, token, sessione da riprendere (0 = nuova) e hash dei dati del client.</summary>
|
|
public sealed class Hello : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Hello;
|
|
public ushort ProtocolVersion { get; init; } = Frame.ProtocolVersion;
|
|
public string ClientVersion { get; init; } = "";
|
|
public string Token { get; init; } = "";
|
|
public ulong ResumeSessionId { get; init; }
|
|
/// <summary>Hash dei dati del client (0 = non verificare).</summary>
|
|
public ulong DataHash { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U16(ProtocolVersion);
|
|
w.String(ClientVersion);
|
|
w.String(Token);
|
|
w.U64(ResumeSessionId);
|
|
w.U64(DataHash);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static Hello Read(ref SpanReader r)
|
|
{
|
|
var m = new Hello { ProtocolVersion = r.U16(), ClientVersion = r.String(), Token = r.String(), ResumeSessionId = r.U64(), DataHash = r.U64() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
public sealed class Ping : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Ping;
|
|
public ulong Nonce { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U64(Nonce);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static Ping Read(ref SpanReader r)
|
|
{
|
|
var m = new Ping { Nonce = r.U64() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
/// <summary>Area di interesse in celle (estremi inclusi) e livello di dettaglio (0 pieno, 1 medio, 2 lontano).</summary>
|
|
public sealed class Subscribe : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Subscribe;
|
|
public int X0 { get; init; }
|
|
public int Y0 { get; init; }
|
|
public int X1 { get; init; }
|
|
public int Y1 { get; init; }
|
|
public byte Lod { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.I32(X0);
|
|
w.I32(Y0);
|
|
w.I32(X1);
|
|
w.I32(Y1);
|
|
w.U8(Lod);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static Subscribe Read(ref SpanReader r)
|
|
{
|
|
var m = new Subscribe { X0 = r.I32(), Y0 = r.I32(), X1 = r.I32(), Y1 = r.I32(), Lod = r.U8() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
/// <summary>Comando di gioco con il numero di sequenza del client; il payload segue <see cref="CommandCodec"/>.</summary>
|
|
public sealed class CommandMessage : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Command;
|
|
public uint Seq { get; init; }
|
|
public required GameCommand Command { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U32(Seq);
|
|
w.U16((ushort)Command.Type);
|
|
// payload con lunghezza: un lettore vecchio può saltarlo anche se il comando cresce
|
|
var inner = new SpanWriter(64);
|
|
CommandCodec.Write(ref inner, Command);
|
|
w.Bytes(inner.Written);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static CommandMessage Read(ref SpanReader r)
|
|
{
|
|
uint seq = r.U32();
|
|
var type = (CommandType)r.U16();
|
|
byte[] payload = r.Bytes();
|
|
var inner = new SpanReader(payload);
|
|
var command = CommandCodec.Read(type, ref inner) with { Seq = seq };
|
|
r.Flags();
|
|
return new CommandMessage { Seq = seq, Command = command };
|
|
}
|
|
}
|
|
|
|
/// <summary>Velocità della simulazione: 0 (pausa), 1, 2, 4, 8.</summary>
|
|
public sealed class SetSpeed : IMessage
|
|
{
|
|
public MessageId Id => MessageId.SetSpeed;
|
|
public byte Speed { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U8(Speed);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static SetSpeed Read(ref SpanReader r)
|
|
{
|
|
var m = new SetSpeed { Speed = r.U8() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
public sealed class ListSaves : IMessage
|
|
{
|
|
public MessageId Id => MessageId.ListSaves;
|
|
|
|
public void Write(ref SpanWriter w) => w.Flags(0);
|
|
|
|
public static ListSaves Read(ref SpanReader r)
|
|
{
|
|
r.Flags();
|
|
return new ListSaves();
|
|
}
|
|
}
|
|
|
|
public sealed class Save : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Save;
|
|
public string Name { get; init; } = "";
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.String(Name);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static Save Read(ref SpanReader r)
|
|
{
|
|
var m = new Save { Name = r.String() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
public sealed class Load : IMessage
|
|
{
|
|
public MessageId Id => MessageId.Load;
|
|
public string SaveId { get; init; } = "";
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.String(SaveId);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static Load Read(ref SpanReader r)
|
|
{
|
|
var m = new Load { SaveId = r.String() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
/// <summary>Nuova partita: seme, lato della mappa (km), anno d'inizio (0 = predefinito), difficoltà, ritocchi e opzioni del gioco infinito.</summary>
|
|
public sealed class NewGame : IMessage
|
|
{
|
|
public MessageId Id => MessageId.NewGame;
|
|
public ulong Seed { get; init; }
|
|
public int SizeKm { get; init; }
|
|
public int StartYear { get; init; }
|
|
public string DifficultyPreset { get; init; } = "";
|
|
/// <summary>Ritocchi della difficoltà «Personalizzata»: nome del campo (camelCase) e valore.</summary>
|
|
public List<(string Key, double Value)> Overrides { get; init; } = [];
|
|
public uint EndlessFlags { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U64(Seed);
|
|
w.I32(SizeKm);
|
|
w.I32(StartYear);
|
|
w.String(DifficultyPreset);
|
|
w.Count(Overrides.Count);
|
|
foreach (var (key, value) in Overrides)
|
|
{
|
|
w.String(key);
|
|
w.F64(value);
|
|
}
|
|
w.U32(EndlessFlags);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static NewGame Read(ref SpanReader r)
|
|
{
|
|
ulong seed = r.U64();
|
|
int size = r.I32();
|
|
int year = r.I32();
|
|
string preset = r.String();
|
|
int n = r.Count();
|
|
var overrides = new List<(string, double)>(n);
|
|
for (int i = 0; i < n; i++) overrides.Add((r.String(), r.F64()));
|
|
var m = new NewGame { Seed = seed, SizeKm = size, StartYear = year, DifficultyPreset = preset, Overrides = overrides, EndlessFlags = r.U32() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|
|
|
|
/// <summary>Dettaglio richiesto dal client (F4).</summary>
|
|
public enum DetailKind : byte
|
|
{
|
|
/// <summary>Cartella di manutenzione di un treno (risposta MaintenanceCard).</summary>
|
|
TrainCard = 1,
|
|
/// <summary>Registro officina di un deposito (risposta WorkshopRegister).</summary>
|
|
Workshop = 2,
|
|
/// <summary>Registro degli impieghi della compagnia (risposta EmploymentReport; Id ignorato).</summary>
|
|
Employment = 3,
|
|
/// <summary>Scheda di una compagnia (risposta CompanyCard, F5).</summary>
|
|
Company = 4,
|
|
/// <summary>Scheda del proprietario di un'industria (risposta IndustryCard, F5).</summary>
|
|
Industry = 5,
|
|
/// <summary>Cronaca finale (risposta Chronicle, F5; Id ignorato).</summary>
|
|
Chronicle = 6,
|
|
/// <summary>Quadro del mercato globale (risposta MarketReport, F6; Id ignorato).</summary>
|
|
Market = 7,
|
|
/// <summary>Quadro della scalata (risposta ClimbReport, F6; Id ignorato).</summary>
|
|
Climb = 8,
|
|
/// <summary>Quadro delle finanze (risposta FinanceReport, F6; Id ignorato).</summary>
|
|
Finance = 9,
|
|
}
|
|
|
|
/// <summary>Chiede al server un dettaglio che non viaggia con le entità (aggiunto in F4).</summary>
|
|
public sealed class RequestDetail : IMessage
|
|
{
|
|
public MessageId Id => MessageId.RequestDetail;
|
|
public DetailKind Kind { get; init; }
|
|
public int Target { get; init; }
|
|
|
|
public void Write(ref SpanWriter w)
|
|
{
|
|
w.U8((byte)Kind);
|
|
w.I32(Target);
|
|
w.Flags(0);
|
|
}
|
|
|
|
public static RequestDetail Read(ref SpanReader r)
|
|
{
|
|
var m = new RequestDetail { Kind = (DetailKind)r.U8(), Target = r.I32() };
|
|
r.Flags();
|
|
return m;
|
|
}
|
|
}
|