Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*, variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos: CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly. Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f6c2a17b4d
commit
c1a0049bcf
300 files changed
+1602
-1045
No files matched your search
@@ -0,0 +1,262 @@
|
||||
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>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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
|
||||
namespace RailEnterprise.Protocol.Messages;
|
||||
|
||||
// Messaggi della fase F5: scheda di una compagnia (soci e storico), scheda del proprietario di
|
||||
// un'industria, Cronaca finale. Arrivano solo su richiesta (RequestDetail) o, la Cronaca, alla fine
|
||||
// della partita del giocatore.
|
||||
|
||||
/// <summary>Quota di un titolare nel libro dei soci (Holder = id compagnia, −1 fondatore, −2 mercato).</summary>
|
||||
public sealed class HoldingInfo
|
||||
{
|
||||
public const int Founder = -1;
|
||||
public const int Market = -2;
|
||||
|
||||
public int Holder { get; init; }
|
||||
public long Shares { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Holder); w.I64(Shares);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static HoldingInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new HoldingInfo { Holder = r.I32(), Shares = r.I64() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Anno di storia di una compagnia (grafici della scheda e della Cronaca).</summary>
|
||||
public sealed class CompanyYearInfo
|
||||
{
|
||||
public int Year { get; init; }
|
||||
public Money NetWorth { get; init; }
|
||||
public Money Cash { get; init; }
|
||||
public Money Revenue { get; init; }
|
||||
public Money Profit { get; init; }
|
||||
public byte Tier { get; init; }
|
||||
public int Trains { get; init; }
|
||||
public float TrackKm { get; init; }
|
||||
public Money SharePrice { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Year); w.Money(NetWorth); w.Money(Cash); w.Money(Revenue); w.Money(Profit); w.U8(Tier); w.I32(Trains); w.F32(TrackKm); w.Money(SharePrice);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static CompanyYearInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new CompanyYearInfo
|
||||
{
|
||||
Year = r.I32(), NetWorth = r.Money(), Cash = r.Money(), Revenue = r.Money(), Profit = r.Money(), Tier = r.U8(), Trains = r.I32(),
|
||||
TrackKm = r.F32(), SharePrice = r.Money(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Scheda di una compagnia (borsa e controllo, 12.2).</summary>
|
||||
public sealed class CompanyCard : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.CompanyCard;
|
||||
public int Company { get; init; }
|
||||
public long TotalShares { get; init; }
|
||||
public long Treasury { get; init; }
|
||||
public List<HoldingInfo> Holdings { get; init; } = [];
|
||||
/// <summary>Prezzo dell'azione a fine mese (centesimi), il più recente in coda.</summary>
|
||||
public List<double> PriceHistory { get; init; } = [];
|
||||
public List<CompanyYearInfo> History { get; init; } = [];
|
||||
public Money AnnualProfit { get; init; }
|
||||
public Money DividendsYear { get; init; }
|
||||
public Money Debt { get; init; }
|
||||
public Money MarketCap { get; init; }
|
||||
/// <summary>Giorni di controllo da parte del titolare di maggioranza (−1 se nessuno).</summary>
|
||||
public long ControlDays { get; init; } = -1;
|
||||
/// <summary>Asta in corso degli attivi (compagnia fallita): base, miglior offerta, chiusura.</summary>
|
||||
public Money AuctionMin { get; init; }
|
||||
public Money AuctionBest { get; init; }
|
||||
public int AuctionBestBidder { get; init; } = -1;
|
||||
public long AuctionEndDay { get; init; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Company); w.I64(TotalShares); w.I64(Treasury);
|
||||
w.Count(Holdings.Count);
|
||||
foreach (var h in Holdings) h.Write(ref w);
|
||||
w.Count(PriceHistory.Count);
|
||||
foreach (double p in PriceHistory) w.F64(p);
|
||||
w.Count(History.Count);
|
||||
foreach (var y in History) y.Write(ref w);
|
||||
w.Money(AnnualProfit); w.Money(DividendsYear); w.Money(Debt); w.Money(MarketCap); w.I64(ControlDays);
|
||||
w.Money(AuctionMin); w.Money(AuctionBest); w.I32(AuctionBestBidder); w.I64(AuctionEndDay);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static CompanyCard Read(ref SpanReader r)
|
||||
{
|
||||
int company = r.I32();
|
||||
long total = r.I64(), treasury = r.I64();
|
||||
var holdings = new List<HoldingInfo>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) holdings.Add(HoldingInfo.Read(ref r));
|
||||
var prices = new List<double>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) prices.Add(r.F64());
|
||||
var history = new List<CompanyYearInfo>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) history.Add(CompanyYearInfo.Read(ref r));
|
||||
var m = new CompanyCard
|
||||
{
|
||||
Company = company, TotalShares = total, Treasury = treasury, Holdings = holdings, PriceHistory = prices, History = history,
|
||||
AnnualProfit = r.Money(), DividendsYear = r.Money(), Debt = r.Money(), MarketCap = r.Money(), ControlDays = r.I64(),
|
||||
AuctionMin = r.Money(), AuctionBest = r.Money(), AuctionBestBidder = r.I32(), AuctionEndDay = r.I64(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Fermo passato o in corso di un'industria.</summary>
|
||||
public sealed class IndustryStopInfo
|
||||
{
|
||||
public long Day { get; init; }
|
||||
public int Days { get; init; }
|
||||
public byte Kind { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day); w.I32(Days); w.U8(Kind);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static IndustryStopInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new IndustryStopInfo { Day = r.I64(), Days = r.I32(), Kind = r.U8() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Scheda del proprietario di un'industria (sezione 11).</summary>
|
||||
public sealed class IndustryCard : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.IndustryCard;
|
||||
public int Industry { get; init; }
|
||||
public float Margin { get; init; }
|
||||
public float ConditionMultiplier { get; init; }
|
||||
/// <summary>Usura e recupero giornalieri attuali (punti di condizione).</summary>
|
||||
public float WearPerDay { get; init; }
|
||||
public float RecoveryPerDay { get; init; }
|
||||
public Money MaintenanceCostPerDay { get; init; }
|
||||
public Money MaintenanceSpentYear { get; init; }
|
||||
public int PartsTarget { get; init; }
|
||||
public int PartsPerMonth { get; init; }
|
||||
public int PartsOrdered { get; init; }
|
||||
public int MachineryRequired { get; init; }
|
||||
public Money UpgradeCost { get; init; }
|
||||
/// <summary>Prezzo chiesto dai proprietari privati (per BuyIndustry).</summary>
|
||||
public Money Valuation { get; init; }
|
||||
public Money Cash { get; init; }
|
||||
public float BreakdownChancePerYear { get; init; }
|
||||
public List<IndustryStopInfo> Stops { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Industry); w.F32(Margin); w.F32(ConditionMultiplier); w.F32(WearPerDay); w.F32(RecoveryPerDay);
|
||||
w.Money(MaintenanceCostPerDay); w.Money(MaintenanceSpentYear); w.I32(PartsTarget); w.I32(PartsPerMonth); w.I32(PartsOrdered);
|
||||
w.I32(MachineryRequired); w.Money(UpgradeCost); w.Money(Valuation); w.Money(Cash); w.F32(BreakdownChancePerYear);
|
||||
w.Count(Stops.Count);
|
||||
foreach (var s in Stops) s.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static IndustryCard Read(ref SpanReader r)
|
||||
{
|
||||
var m = new IndustryCard
|
||||
{
|
||||
Industry = r.I32(), Margin = r.F32(), ConditionMultiplier = r.F32(), WearPerDay = r.F32(), RecoveryPerDay = r.F32(),
|
||||
MaintenanceCostPerDay = r.Money(), MaintenanceSpentYear = r.Money(), PartsTarget = r.I32(), PartsPerMonth = r.I32(), PartsOrdered = r.I32(),
|
||||
MachineryRequired = r.I32(), UpgradeCost = r.Money(), Valuation = r.Money(), Cash = r.Money(), BreakdownChancePerYear = r.F32(),
|
||||
};
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Stops.Add(IndustryStopInfo.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Primato della Cronaca finale (descrizione e valore già formattati in italiano).</summary>
|
||||
public sealed class RecordInfo
|
||||
{
|
||||
public string Title { get; init; } = "";
|
||||
public string Value { get; init; } = "";
|
||||
public int Year { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.String(Title); w.String(Value); w.I32(Year);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static RecordInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new RecordInfo { Title = r.String(), Value = r.String(), Year = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cronaca finale (12.3): storia della compagnia del giocatore, grafici, primati e prestigio.
|
||||
/// Dopo la Cronaca il giocatore può continuare come osservatore o iniziare una Nuova partita+.
|
||||
/// </summary>
|
||||
public sealed class Chronicle : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Chronicle;
|
||||
public bool GameOver { get; init; }
|
||||
/// <summary>Motivo (GameOverReason): 1 fallimento, 2 scalata, 3 riscatto scaduto.</summary>
|
||||
public byte Reason { get; init; }
|
||||
public string Text { get; init; } = "";
|
||||
public int Winner { get; init; } = -1;
|
||||
public long Day { get; init; }
|
||||
public string Company { get; init; } = "";
|
||||
public int FoundedYear { get; init; }
|
||||
public long Prestige { get; init; }
|
||||
public List<CompanyYearInfo> History { get; init; } = [];
|
||||
public List<RecordInfo> Records { get; init; } = [];
|
||||
/// <summary>Fatti salienti della compagnia, dal più vecchio.</summary>
|
||||
public List<string> Highlights { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.Bool(GameOver); w.U8(Reason); w.String(Text); w.I32(Winner); w.I64(Day); w.String(Company); w.I32(FoundedYear); w.I64(Prestige);
|
||||
w.Count(History.Count);
|
||||
foreach (var y in History) y.Write(ref w);
|
||||
w.Count(Records.Count);
|
||||
foreach (var rec in Records) rec.Write(ref w);
|
||||
w.Count(Highlights.Count);
|
||||
foreach (var h in Highlights) w.String(h);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Chronicle Read(ref SpanReader r)
|
||||
{
|
||||
bool over = r.Bool();
|
||||
byte reason = r.U8();
|
||||
string text = r.String();
|
||||
int winner = r.I32();
|
||||
long day = r.I64();
|
||||
string company = r.String();
|
||||
int founded = r.I32();
|
||||
long prestige = r.I64();
|
||||
var history = new List<CompanyYearInfo>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) history.Add(CompanyYearInfo.Read(ref r));
|
||||
var records = new List<RecordInfo>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) records.Add(RecordInfo.Read(ref r));
|
||||
var highlights = new List<string>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) highlights.Add(r.String());
|
||||
r.Flags();
|
||||
return new Chronicle
|
||||
{
|
||||
GameOver = over, Reason = reason, Text = text, Winner = winner, Day = day, Company = company, FoundedYear = founded,
|
||||
Prestige = prestige, History = history, Records = records, Highlights = highlights,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace RailEnterprise.Protocol.Messages;
|
||||
|
||||
/// <summary>Decodifica per id: l'unico punto in cui un id diventa un tipo.</summary>
|
||||
public static class MessageCodec
|
||||
{
|
||||
public static IMessage Read(MessageId id, ref SpanReader r) => id switch
|
||||
{
|
||||
MessageId.Hello => Hello.Read(ref r),
|
||||
MessageId.Ping => Ping.Read(ref r),
|
||||
MessageId.Subscribe => Subscribe.Read(ref r),
|
||||
MessageId.RequestDetail => RequestDetail.Read(ref r),
|
||||
MessageId.Command => CommandMessage.Read(ref r),
|
||||
MessageId.SetSpeed => SetSpeed.Read(ref r),
|
||||
MessageId.ListSaves => ListSaves.Read(ref r),
|
||||
MessageId.Save => Save.Read(ref r),
|
||||
MessageId.Load => Load.Read(ref r),
|
||||
MessageId.NewGame => NewGame.Read(ref r),
|
||||
MessageId.Welcome => Welcome.Read(ref r),
|
||||
MessageId.Reject => Reject.Read(ref r),
|
||||
MessageId.Pong => Pong.Read(ref r),
|
||||
MessageId.MapChunk => MapChunk.Read(ref r),
|
||||
MessageId.EntitySnapshot => EntitySnapshot.Read(ref r),
|
||||
MessageId.EntityDelta => EntityDelta.Read(ref r),
|
||||
MessageId.TrackWorks => TrackWorks.Read(ref r),
|
||||
MessageId.TrainMotion => TrainMotion.Read(ref r),
|
||||
MessageId.DailyDigest => DailyDigest.Read(ref r),
|
||||
MessageId.MaintenanceAlert => MaintenanceAlert.Read(ref r),
|
||||
MessageId.EmploymentReport => EmploymentReport.Read(ref r),
|
||||
MessageId.MaintenanceCard => MaintenanceCard.Read(ref r),
|
||||
MessageId.WorkshopRegister => WorkshopRegister.Read(ref r),
|
||||
MessageId.CompanyCard => CompanyCard.Read(ref r),
|
||||
MessageId.IndustryCard => IndustryCard.Read(ref r),
|
||||
MessageId.Chronicle => Chronicle.Read(ref r),
|
||||
MessageId.MarketTape => MarketTape.Read(ref r),
|
||||
MessageId.CommandResult => CommandResultMessage.Read(ref r),
|
||||
MessageId.Notification => Notification.Read(ref r),
|
||||
MessageId.Newspaper => Newspaper.Read(ref r),
|
||||
MessageId.SaveList => SaveList.Read(ref r),
|
||||
_ => throw new ProtocolException($"messaggio sconosciuto: {(ushort)id}"),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,549 @@
|
||||
using RailEnterprise.Core.Common;
|
||||
using RailEnterprise.Core.Rail;
|
||||
|
||||
namespace RailEnterprise.Protocol.Messages;
|
||||
|
||||
// Record delle entità inviati al client. Ogni record ha Write/Read e un byte di estensione finale.
|
||||
|
||||
/// <summary>Record serializzabile (entità, archi, eventi).</summary>
|
||||
public interface IWritable
|
||||
{
|
||||
void Write(ref SpanWriter w);
|
||||
}
|
||||
|
||||
/// <summary>Tipo di entità nei riferimenti e nelle rimozioni.</summary>
|
||||
public enum EntityType : byte { Company = 0, Town = 1, Industry = 2, Hub = 3, Node = 4, Station = 5, Depot = 6, Line = 7, Vehicle = 8, Train = 9, Edge = 10 }
|
||||
|
||||
public sealed class WorldSummary : IWritable
|
||||
{
|
||||
public ulong Seed { get; init; }
|
||||
public int SizeKm { get; init; }
|
||||
public int StartYear { get; init; }
|
||||
public long Tick { get; init; }
|
||||
public string Date { get; init; } = "";
|
||||
public int CompanyId { get; init; }
|
||||
public string CompanyName { get; init; } = "";
|
||||
public string RegionId { get; init; } = "";
|
||||
public int WidthCells { get; init; }
|
||||
public int HeightCells { get; init; }
|
||||
public double CellSizeM { get; init; }
|
||||
public int ChunkCells { get; init; }
|
||||
public byte Speed { get; init; }
|
||||
public ulong TerrainHash { get; init; }
|
||||
/// <summary>Moltiplicatore dei costi di costruzione della compagnia (difficoltà), per le anteprime.</summary>
|
||||
public double CostMultiplier { get; init; } = 1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U64(Seed); w.I32(SizeKm); w.I32(StartYear); w.I64(Tick); w.String(Date);
|
||||
w.I32(CompanyId); w.String(CompanyName); w.String(RegionId);
|
||||
w.I32(WidthCells); w.I32(HeightCells); w.F64(CellSizeM); w.I32(ChunkCells);
|
||||
w.U8(Speed); w.U64(TerrainHash); w.F64(CostMultiplier);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static WorldSummary Read(ref SpanReader r)
|
||||
{
|
||||
var m = new WorldSummary
|
||||
{
|
||||
Seed = r.U64(), SizeKm = r.I32(), StartYear = r.I32(), Tick = r.I64(), Date = r.String(),
|
||||
CompanyId = r.I32(), CompanyName = r.String(), RegionId = r.String(),
|
||||
WidthCells = r.I32(), HeightCells = r.I32(), CellSizeM = r.F64(), ChunkCells = r.I32(),
|
||||
Speed = r.U8(), TerrainHash = r.U64(), CostMultiplier = r.F64(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CompanyInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public string Color { get; init; } = "";
|
||||
public bool IsPlayer { get; init; }
|
||||
public Money Cash { get; init; }
|
||||
public Money NetWorth { get; init; }
|
||||
public string Rating { get; init; } = "";
|
||||
public byte Tier { get; init; }
|
||||
public bool Bankrupt { get; init; }
|
||||
|
||||
// blocco 1 (F4): politica di manutenzione della flotta
|
||||
public byte MaintenancePolicy { get; set; } = 1;
|
||||
|
||||
// blocco 2 (F5): borsa, controllo, stato societario
|
||||
public string Personality { get; set; } = "";
|
||||
public bool Listed { get; set; }
|
||||
/// <summary>Prezzo per azione in centesimi (frazionario).</summary>
|
||||
public double SharePriceCents { get; set; }
|
||||
public float Sentiment { get; set; } = 1;
|
||||
/// <summary>Quota del fondatore sulle azioni in circolazione.</summary>
|
||||
public float FounderShare { get; set; } = 1;
|
||||
public int ControlledBy { get; set; } = -1;
|
||||
public bool Merged { get; set; }
|
||||
public bool Receivership { get; set; }
|
||||
public long ReceivershipUntilDay { get; set; } = -1;
|
||||
public Money AnnualProfit { get; set; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Name); w.String(Color); w.Bool(IsPlayer); w.Money(Cash); w.Money(NetWorth);
|
||||
w.String(Rating); w.U8(Tier); w.Bool(Bankrupt);
|
||||
w.Flags(3);
|
||||
w.U8(MaintenancePolicy);
|
||||
w.String(Personality); w.Bool(Listed); w.F64(SharePriceCents); w.F32(Sentiment); w.F32(FounderShare);
|
||||
w.I32(ControlledBy); w.Bool(Merged); w.Bool(Receivership); w.I64(ReceivershipUntilDay); w.Money(AnnualProfit);
|
||||
}
|
||||
|
||||
public static CompanyInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new CompanyInfo
|
||||
{
|
||||
Id = r.I32(), Name = r.String(), Color = r.String(), IsPlayer = r.Bool(), Cash = r.Money(), NetWorth = r.Money(),
|
||||
Rating = r.String(), Tier = r.U8(), Bankrupt = r.Bool(),
|
||||
};
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0) m.MaintenancePolicy = r.U8();
|
||||
if ((flags & 2) != 0)
|
||||
{
|
||||
m.Personality = r.String(); m.Listed = r.Bool(); m.SharePriceCents = r.F64(); m.Sentiment = r.F32(); m.FounderShare = r.F32();
|
||||
m.ControlledBy = r.I32(); m.Merged = r.Bool(); m.Receivership = r.Bool(); m.ReceivershipUntilDay = r.I64(); m.AnnualProfit = r.Money();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TownInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
public int Population { get; init; }
|
||||
public double RadiusCells { get; init; }
|
||||
public float Satisfaction { get; init; }
|
||||
public int Services { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Name); w.Vec2(Position); w.I32(Population); w.F64(RadiusCells); w.F32(Satisfaction); w.I32(Services);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static TownInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new TownInfo { Id = r.I32(), Name = r.String(), Position = r.Vec2(), Population = r.I32(), RadiusCells = r.F64(), Satisfaction = r.F32(), Services = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class IndustryInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Type { get; init; } = "";
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
public int FootprintCells { get; init; }
|
||||
public int Level { get; init; }
|
||||
public bool Closed { get; init; }
|
||||
public float Efficiency { get; init; }
|
||||
public float Condition { get; init; }
|
||||
public int Owner { get; init; } = -1;
|
||||
|
||||
// blocco 1 (F5): manutenzione degli impianti (sezione 11)
|
||||
/// <summary>Budget di manutenzione: 0 nessuno, 1 base, 2 alto.</summary>
|
||||
public byte Budget { get; set; } = 1;
|
||||
public int SpareParts { get; set; }
|
||||
public int Machinery { get; set; }
|
||||
/// <summary>Giorno di fine del fermo in corso (−1 se lavora) e motivo (IndustryStop).</summary>
|
||||
public long StoppedUntilDay { get; set; } = -1;
|
||||
public byte StopKind { get; set; }
|
||||
/// <summary>Lavoro programmato in attesa (IndustryWork).</summary>
|
||||
public byte PendingWork { get; set; }
|
||||
public bool PartsShort { get; set; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Type); w.String(Name); w.Vec2(Position); w.I32(FootprintCells); w.I32(Level); w.Bool(Closed);
|
||||
w.F32(Efficiency); w.F32(Condition); w.I32(Owner);
|
||||
w.Flags(1);
|
||||
w.U8(Budget); w.I32(SpareParts); w.I32(Machinery); w.I64(StoppedUntilDay); w.U8(StopKind); w.U8(PendingWork); w.Bool(PartsShort);
|
||||
}
|
||||
|
||||
public static IndustryInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new IndustryInfo
|
||||
{
|
||||
Id = r.I32(), Type = r.String(), Name = r.String(), Position = r.Vec2(), FootprintCells = r.I32(), Level = r.I32(), Closed = r.Bool(),
|
||||
Efficiency = r.F32(), Condition = r.F32(), Owner = r.I32(),
|
||||
};
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
m.Budget = r.U8(); m.SpareParts = r.I32(); m.Machinery = r.I32(); m.StoppedUntilDay = r.I64(); m.StopKind = r.U8();
|
||||
m.PendingWork = r.U8(); m.PartsShort = r.Bool();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class HubInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string Kind { get; init; } = "";
|
||||
public string Name { get; init; } = "";
|
||||
public Vec2 Position { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.String(Kind); w.String(Name); w.Vec2(Position);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static HubInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new HubInfo { Id = r.I32(), Kind = r.String(), Name = r.String(), Position = r.Vec2() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NodeInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public double HeightM { get; init; }
|
||||
public int Owner { get; init; } = -1;
|
||||
public int Station { get; init; } = -1;
|
||||
public int Depot { get; init; } = -1;
|
||||
public int[] Edges { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.Vec2(Position); w.F64(HeightM); w.I32(Owner); w.I32(Station); w.I32(Depot);
|
||||
w.Count(Edges.Length);
|
||||
foreach (int e in Edges) w.I32(e);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static NodeInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32();
|
||||
var pos = r.Vec2();
|
||||
double h = r.F64();
|
||||
int owner = r.I32(), station = r.I32(), depot = r.I32();
|
||||
var edges = new int[r.Count()];
|
||||
for (int i = 0; i < edges.Length; i++) edges[i] = r.I32();
|
||||
r.Flags();
|
||||
return new NodeInfo { Id = id, Position = pos, HeightM = h, Owner = owner, Station = station, Depot = depot, Edges = edges };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StationInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Node { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public int Platforms { get; init; }
|
||||
public int WaitingUnits { get; init; }
|
||||
public int[] Industries { get; init; } = [];
|
||||
public int[] Towns { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.I32(Platforms); w.I32(WaitingUnits);
|
||||
w.Count(Industries.Length);
|
||||
foreach (int i in Industries) w.I32(i);
|
||||
w.Count(Towns.Length);
|
||||
foreach (int t in Towns) w.I32(t);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static StationInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String();
|
||||
int node = r.I32();
|
||||
var pos = r.Vec2();
|
||||
int platforms = r.I32(), waiting = r.I32();
|
||||
var industries = new int[r.Count()];
|
||||
for (int i = 0; i < industries.Length; i++) industries[i] = r.I32();
|
||||
var towns = new int[r.Count()];
|
||||
for (int i = 0; i < towns.Length; i++) towns[i] = r.I32();
|
||||
r.Flags();
|
||||
return new StationInfo { Id = id, Owner = owner, Name = name, Node = node, Position = pos, Platforms = platforms, WaitingUnits = waiting, Industries = industries, Towns = towns };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DepotInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Node { get; init; }
|
||||
public Vec2 Position { get; init; }
|
||||
public string Size { get; init; } = "";
|
||||
|
||||
// blocco 1 (F4): officina
|
||||
public int Stalls { get; set; }
|
||||
public int JobsStaff { get; set; }
|
||||
/// <summary>Personale presente in percentuale (0–100).</summary>
|
||||
public byte StaffPercent { get; set; }
|
||||
public int SpareParts { get; set; }
|
||||
public int PartsTarget { get; set; }
|
||||
/// <summary>Lavori in officina (in coda e negli stalli).</summary>
|
||||
public int Jobs { get; set; }
|
||||
public bool WaitingParts { get; set; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Node); w.Vec2(Position); w.String(Size);
|
||||
w.Flags(1);
|
||||
w.I32(Stalls); w.I32(JobsStaff); w.U8(StaffPercent); w.I32(SpareParts); w.I32(PartsTarget); w.I32(Jobs); w.Bool(WaitingParts);
|
||||
}
|
||||
|
||||
public static DepotInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new DepotInfo { Id = r.I32(), Owner = r.I32(), Name = r.String(), Node = r.I32(), Position = r.Vec2(), Size = r.String() };
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
m.Stalls = r.I32(); m.JobsStaff = r.I32(); m.StaffPercent = r.U8(); m.SpareParts = r.I32(); m.PartsTarget = r.I32();
|
||||
m.Jobs = r.I32(); m.WaitingParts = r.Bool();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LineInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public string Color { get; init; } = "";
|
||||
public LineStop[] Stops { get; init; } = [];
|
||||
|
||||
// blocco 1 (F4): politica di manutenzione propria della linea (255 = quella della compagnia)
|
||||
public byte Policy { get; set; } = byte.MaxValue;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.String(Color);
|
||||
w.Count(Stops.Length);
|
||||
foreach (var s in Stops)
|
||||
{
|
||||
w.I32(s.Station.Value); w.Bool(s.Load); w.Bool(s.Unload); w.Bool(s.WaitFull); w.I32(s.MaxWaitDays);
|
||||
}
|
||||
w.Flags(1);
|
||||
w.U8(Policy);
|
||||
}
|
||||
|
||||
public static LineInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String(), color = r.String();
|
||||
var stops = new LineStop[r.Count()];
|
||||
for (int i = 0; i < stops.Length; i++) stops[i] = new LineStop(new StationId(r.I32()), r.Bool(), r.Bool(), r.Bool(), r.I32());
|
||||
var m = new LineInfo { Id = id, Owner = owner, Name = name, Color = color, Stops = stops };
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0) m.Policy = r.U8();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VehicleInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Type { get; init; } = "";
|
||||
public int Depot { get; init; } = -1;
|
||||
public int Train { get; init; } = -1;
|
||||
public float Condition { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Type); w.I32(Depot); w.I32(Train); w.F32(Condition);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static VehicleInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new VehicleInfo { Id = r.I32(), Owner = r.I32(), Type = r.String(), Depot = r.I32(), Train = r.I32(), Condition = r.F32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stato di manutenzione di un treno nel riepilogo (blocco F4 di TrainInfo).</summary>
|
||||
public enum TrainMaintenanceStatus : byte
|
||||
{
|
||||
InService = 0,
|
||||
/// <summary>Intervento prenotato: va in officina dopo la fermata corrente.</summary>
|
||||
Booked = 1,
|
||||
InWorkshop = 2,
|
||||
/// <summary>Guasto lieve: velocità dimezzata fino al deposito.</summary>
|
||||
MinorFailure = 3,
|
||||
/// <summary>Fermo in linea in attesa del soccorso (guasto grave o deragliamento).</summary>
|
||||
Stranded = 4,
|
||||
/// <summary>Rimorchiato verso l'officina.</summary>
|
||||
Towed = 5,
|
||||
}
|
||||
|
||||
public sealed class TrainInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public string Name { get; init; } = "";
|
||||
public int Line { get; init; } = -1;
|
||||
public byte State { get; init; }
|
||||
public int[] Vehicles { get; init; } = [];
|
||||
public float MaxSpeedKmh { get; init; }
|
||||
public float LengthM { get; init; }
|
||||
public int Load { get; init; }
|
||||
public int Capacity { get; init; }
|
||||
public int Depot { get; init; } = -1;
|
||||
|
||||
// blocco 1 (F4): riepilogo di manutenzione (la cartella completa si chiede con RequestDetail)
|
||||
/// <summary>Condizione del componente più debole (0–100).</summary>
|
||||
public byte Condition { get; set; } = 100;
|
||||
public TrainMaintenanceStatus Maintenance { get; set; }
|
||||
public bool Reserve { get; set; }
|
||||
/// <summary>Politica propria del treno (255 = quella della linea o della compagnia).</summary>
|
||||
public byte Policy { get; set; } = byte.MaxValue;
|
||||
/// <summary>Prossimo intervento (id di maintenance.json) e giorni alla scadenza (negativi se scaduto).</summary>
|
||||
public string NextIntervention { get; set; } = "";
|
||||
public int NextDueDays { get; set; }
|
||||
/// <summary>Officina prenotata o in cui si trova (−1 nessuna).</summary>
|
||||
public int MaintenanceDepot { get; set; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.String(Name); w.I32(Line); w.U8(State);
|
||||
w.Count(Vehicles.Length);
|
||||
foreach (int v in Vehicles) w.I32(v);
|
||||
w.F32(MaxSpeedKmh); w.F32(LengthM); w.I32(Load); w.I32(Capacity); w.I32(Depot);
|
||||
w.Flags(1);
|
||||
w.U8(Condition); w.U8((byte)Maintenance); w.Bool(Reserve); w.U8(Policy); w.String(NextIntervention); w.I32(NextDueDays);
|
||||
w.I32(MaintenanceDepot);
|
||||
}
|
||||
|
||||
public static TrainInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32();
|
||||
string name = r.String();
|
||||
int line = r.I32();
|
||||
byte state = r.U8();
|
||||
var vehicles = new int[r.Count()];
|
||||
for (int i = 0; i < vehicles.Length; i++) vehicles[i] = r.I32();
|
||||
var m = new TrainInfo
|
||||
{
|
||||
Id = id, Owner = owner, Name = name, Line = line, State = state, Vehicles = vehicles,
|
||||
MaxSpeedKmh = r.F32(), LengthM = r.F32(), Load = r.I32(), Capacity = r.I32(), Depot = r.I32(),
|
||||
};
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
m.Condition = r.U8(); m.Maintenance = (TrainMaintenanceStatus)r.U8(); m.Reserve = r.Bool(); m.Policy = r.U8();
|
||||
m.NextIntervention = r.String(); m.NextDueDays = r.I32(); m.MaintenanceDepot = r.I32();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Arco di binario con i campioni del profilo (TrackWorks): precisione piena per le anteprime del client.</summary>
|
||||
public sealed class EdgeInfo : IWritable
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Owner { get; init; }
|
||||
public int A { get; init; }
|
||||
public int B { get; init; }
|
||||
public byte TrackClass { get; init; }
|
||||
public bool DoubleTrack { get; init; }
|
||||
public bool Electrified { get; init; }
|
||||
public byte State { get; init; }
|
||||
public float Condition { get; init; }
|
||||
public float StructureCondition { get; init; }
|
||||
/// <summary>Avanzamento del cantiere 0–1 (1 = completato).</summary>
|
||||
public float Progress { get; init; } = 1;
|
||||
public double[] S { get; init; } = [];
|
||||
public double[] X { get; init; } = [];
|
||||
public double[] Y { get; init; } = [];
|
||||
public double[] H { get; init; } = [];
|
||||
public double[] Ground { get; init; } = [];
|
||||
public byte[] Kinds { get; init; } = [];
|
||||
public double[] SpeedLimitKmh { get; init; } = [];
|
||||
public double[] Signals { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Owner); w.I32(A); w.I32(B); w.U8(TrackClass); w.Bool(DoubleTrack); w.Bool(Electrified); w.U8(State);
|
||||
w.F32(Condition); w.F32(StructureCondition); w.F32(Progress);
|
||||
int n = S.Length;
|
||||
w.Count(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
w.F64(S[i]); w.F64(X[i]); w.F64(Y[i]); w.F64(H[i]); w.F64(Ground[i]); w.U8(Kinds[i]); w.F64(SpeedLimitKmh[i]);
|
||||
}
|
||||
w.Count(Signals.Length);
|
||||
foreach (double s in Signals) w.F64(s);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EdgeInfo Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32(), owner = r.I32(), a = r.I32(), b = r.I32();
|
||||
byte cls = r.U8();
|
||||
bool dbl = r.Bool(), el = r.Bool();
|
||||
byte state = r.U8();
|
||||
float cond = r.F32(), sc = r.F32(), progress = r.F32();
|
||||
int n = r.Count();
|
||||
var s = new double[n]; var x = new double[n]; var y = new double[n]; var h = new double[n]; var g = new double[n];
|
||||
var k = new byte[n]; var v = new double[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
s[i] = r.F64(); x[i] = r.F64(); y[i] = r.F64(); h[i] = r.F64(); g[i] = r.F64(); k[i] = r.U8(); v[i] = r.F64();
|
||||
}
|
||||
var signals = new double[r.Count()];
|
||||
for (int i = 0; i < signals.Length; i++) signals[i] = r.F64();
|
||||
r.Flags();
|
||||
return new EdgeInfo
|
||||
{
|
||||
Id = id, Owner = owner, A = a, B = b, TrackClass = cls, DoubleTrack = dbl, Electrified = el, State = state,
|
||||
Condition = cond, StructureCondition = sc, Progress = progress, S = s, X = x, Y = y, H = h, Ground = g, Kinds = k,
|
||||
SpeedLimitKmh = v, Signals = signals,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Evento della simulazione (notifiche, ticker, resoconto giornaliero).</summary>
|
||||
public sealed class EventInfo : IWritable
|
||||
{
|
||||
public long Day { get; init; }
|
||||
public byte Kind { get; init; }
|
||||
public byte Severity { get; init; }
|
||||
public string Text { get; init; } = "";
|
||||
public byte Entity { get; init; }
|
||||
public int EntityIndex { get; init; } = -1;
|
||||
public int Company { get; init; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day); w.U8(Kind); w.U8(Severity); w.String(Text); w.U8(Entity); w.I32(EntityIndex); w.I32(Company);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EventInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EventInfo { Day = r.I64(), Kind = r.U8(), Severity = r.U8(), Text = r.String(), Entity = r.U8(), EntityIndex = r.I32(), Company = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Tratto di moto di un treno: arco percorso nel verso indicato tra due progressive.</summary>
|
||||
public readonly record struct MotionSegment(int Edge, bool Forward, double FromS, double ToS)
|
||||
{
|
||||
public double Length => Math.Abs(ToS - FromS);
|
||||
}
|
||||
@@ -0,0 +1,958 @@
|
||||
using System.IO.Compression;
|
||||
using RailEnterprise.Core.Common;
|
||||
|
||||
namespace RailEnterprise.Protocol.Messages;
|
||||
|
||||
/// <summary>Codici di <see cref="Reject"/>.</summary>
|
||||
public enum RejectCode : ushort { ProtocolVersion = 1, BadToken = 2, DataMismatch = 3, ServerFull = 4, NoGame = 5 }
|
||||
|
||||
public sealed class Welcome : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Welcome;
|
||||
public ulong SessionId { get; init; }
|
||||
public required WorldSummary Summary { get; init; }
|
||||
public ulong DataHash { get; init; }
|
||||
public string Difficulty { get; init; } = "";
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U64(SessionId);
|
||||
Summary.Write(ref w);
|
||||
w.U64(DataHash);
|
||||
w.String(Difficulty);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Welcome Read(ref SpanReader r)
|
||||
{
|
||||
var m = new Welcome { SessionId = r.U64(), Summary = WorldSummary.Read(ref r), DataHash = r.U64(), Difficulty = r.String() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Reject : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Reject;
|
||||
public RejectCode Code { get; init; }
|
||||
public string Reason { get; init; } = "";
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U16((ushort)Code);
|
||||
w.String(Reason);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Reject Read(ref SpanReader r)
|
||||
{
|
||||
var m = new Reject { Code = (RejectCode)r.U16(), Reason = r.String() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Pong : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Pong;
|
||||
public ulong Nonce { get; init; }
|
||||
public ulong ServerTick { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U64(Nonce);
|
||||
w.U64(ServerTick);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Pong Read(ref SpanReader r)
|
||||
{
|
||||
var m = new Pong { Nonce = r.U64(), ServerTick = r.U64() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Un chunk del terreno: altezze (dm), tipi di cella e larghezza dei fiumi, compressi con Brotli.
|
||||
/// I chunk di bordo possono essere più piccoli di ChunkCells.
|
||||
/// </summary>
|
||||
public sealed class MapChunk : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.MapChunk;
|
||||
public int Cx { get; init; }
|
||||
public int Cy { get; init; }
|
||||
public int Width { get; init; }
|
||||
public int Height { get; init; }
|
||||
public ushort[] Heights { get; init; } = [];
|
||||
public byte[] Types { get; init; } = [];
|
||||
public byte[] RiverWidth { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Cx); w.I32(Cy); w.I32(Width); w.I32(Height);
|
||||
int n = Width * Height;
|
||||
if (Heights.Length != n || Types.Length != n || RiverWidth.Length != n) throw new ProtocolException("chunk incoerente");
|
||||
var raw = new byte[n * 4];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
raw[2 * i] = (byte)Heights[i];
|
||||
raw[2 * i + 1] = (byte)(Heights[i] >> 8);
|
||||
}
|
||||
Types.CopyTo(raw, 2 * n);
|
||||
RiverWidth.CopyTo(raw, 3 * n);
|
||||
var packed = new byte[BrotliEncoder.GetMaxCompressedLength(raw.Length)];
|
||||
if (!BrotliEncoder.TryCompress(raw, packed, out int written, quality: 4, window: 22)) throw new ProtocolException("compressione del chunk fallita");
|
||||
w.U8(1); // compressione: 1 = Brotli
|
||||
w.Bytes(packed.AsSpan(0, written));
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static MapChunk Read(ref SpanReader r)
|
||||
{
|
||||
int cx = r.I32(), cy = r.I32(), width = r.I32(), height = r.I32();
|
||||
if (width <= 0 || height <= 0 || width > 1024 || height > 1024) throw new ProtocolException("dimensioni del chunk non valide");
|
||||
int n = width * height;
|
||||
byte compression = r.U8();
|
||||
byte[] packed = r.Bytes();
|
||||
var raw = new byte[n * 4];
|
||||
if (compression == 1)
|
||||
{
|
||||
if (!BrotliDecoder.TryDecompress(packed, raw, out int written) || written != raw.Length) throw new ProtocolException("chunk corrotto");
|
||||
}
|
||||
else if (compression == 0 && packed.Length == raw.Length) packed.CopyTo(raw, 0);
|
||||
else throw new ProtocolException($"compressione sconosciuta: {compression}");
|
||||
var heights = new ushort[n];
|
||||
for (int i = 0; i < n; i++) heights[i] = (ushort)(raw[2 * i] | raw[2 * i + 1] << 8);
|
||||
r.Flags();
|
||||
return new MapChunk
|
||||
{
|
||||
Cx = cx, Cy = cy, Width = width, Height = height, Heights = heights,
|
||||
Types = raw.AsSpan(2 * n, n).ToArray(), RiverWidth = raw.AsSpan(3 * n, n).ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Liste di entità comuni a snapshot e delta.</summary>
|
||||
public sealed class EntityLists
|
||||
{
|
||||
public List<CompanyInfo> Companies { get; init; } = [];
|
||||
public List<TownInfo> Towns { get; init; } = [];
|
||||
public List<IndustryInfo> Industries { get; init; } = [];
|
||||
public List<HubInfo> Hubs { get; init; } = [];
|
||||
public List<NodeInfo> Nodes { get; init; } = [];
|
||||
public List<StationInfo> Stations { get; init; } = [];
|
||||
public List<DepotInfo> Depots { get; init; } = [];
|
||||
public List<LineInfo> Lines { get; init; } = [];
|
||||
public List<VehicleInfo> Vehicles { get; init; } = [];
|
||||
public List<TrainInfo> Trains { get; init; } = [];
|
||||
|
||||
public int TotalCount => Companies.Count + Towns.Count + Industries.Count + Hubs.Count + Nodes.Count + Stations.Count + Depots.Count + Lines.Count + Vehicles.Count + Trains.Count;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.Count(Companies.Count); foreach (var x in Companies) x.Write(ref w);
|
||||
w.Count(Towns.Count); foreach (var x in Towns) x.Write(ref w);
|
||||
w.Count(Industries.Count); foreach (var x in Industries) x.Write(ref w);
|
||||
w.Count(Hubs.Count); foreach (var x in Hubs) x.Write(ref w);
|
||||
w.Count(Nodes.Count); foreach (var x in Nodes) x.Write(ref w);
|
||||
w.Count(Stations.Count); foreach (var x in Stations) x.Write(ref w);
|
||||
w.Count(Depots.Count); foreach (var x in Depots) x.Write(ref w);
|
||||
w.Count(Lines.Count); foreach (var x in Lines) x.Write(ref w);
|
||||
w.Count(Vehicles.Count); foreach (var x in Vehicles) x.Write(ref w);
|
||||
w.Count(Trains.Count); foreach (var x in Trains) x.Write(ref w);
|
||||
}
|
||||
|
||||
public static EntityLists Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EntityLists();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Companies.Add(CompanyInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Towns.Add(TownInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Industries.Add(IndustryInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Hubs.Add(HubInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Nodes.Add(NodeInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Stations.Add(StationInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Depots.Add(DepotInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Lines.Add(LineInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Vehicles.Add(VehicleInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Trains.Add(TrainInfo.Read(ref r));
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stato completo delle entità (all'ingresso e dopo un caricamento).</summary>
|
||||
public sealed class EntitySnapshot : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.EntitySnapshot;
|
||||
public long Tick { get; init; }
|
||||
public required EntityLists Entities { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Tick);
|
||||
Entities.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EntitySnapshot Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EntitySnapshot { Tick = r.I64(), Entities = EntityLists.Read(ref r) };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Entità nuove o cambiate dall'ultimo invio e quelle rimosse.</summary>
|
||||
public sealed class EntityDelta : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.EntityDelta;
|
||||
public long Tick { get; init; }
|
||||
public required EntityLists Changed { get; init; }
|
||||
public List<(EntityType Type, int Id)> Removed { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Tick);
|
||||
Changed.Write(ref w);
|
||||
w.Count(Removed.Count);
|
||||
foreach (var (type, id) in Removed)
|
||||
{
|
||||
w.U8((byte)type);
|
||||
w.I32(id);
|
||||
}
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EntityDelta Read(ref SpanReader r)
|
||||
{
|
||||
long tick = r.I64();
|
||||
var changed = EntityLists.Read(ref r);
|
||||
int n = r.Count();
|
||||
var removed = new List<(EntityType, int)>(n);
|
||||
for (int i = 0; i < n; i++) removed.Add(((EntityType)r.U8(), r.I32()));
|
||||
r.Flags();
|
||||
return new EntityDelta { Tick = tick, Changed = changed, Removed = removed };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Archi di binario nuovi o cambiati (opere, cantiere, condizione, segnali) e archi demoliti.</summary>
|
||||
public sealed class TrackWorks : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.TrackWorks;
|
||||
public List<EdgeInfo> Edges { get; init; } = [];
|
||||
public List<int> Removed { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.Count(Edges.Count);
|
||||
foreach (var e in Edges) e.Write(ref w);
|
||||
w.Count(Removed.Count);
|
||||
foreach (int id in Removed) w.I32(id);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static TrackWorks Read(ref SpanReader r)
|
||||
{
|
||||
var m = new TrackWorks();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Edges.Add(EdgeInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Removed.Add(r.I32());
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moto di un treno: tratti dalla coda alla testa e oltre (percorso prenotato), la distanza della
|
||||
/// testa lungo i tratti e la velocità; il client interpola avanzando la testa a velocità costante.
|
||||
/// </summary>
|
||||
public sealed class TrainMotion : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.TrainMotion;
|
||||
public int TrainId { get; init; }
|
||||
public long Tick { get; init; }
|
||||
public byte State { get; init; }
|
||||
public float Condition { get; init; }
|
||||
/// <summary>Velocità in m/s di orologio dei treni.</summary>
|
||||
public float Speed { get; init; }
|
||||
public float LengthM { get; init; }
|
||||
public double HeadDistance { get; init; }
|
||||
public List<MotionSegment> Segments { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(TrainId); w.I64(Tick); w.U8(State); w.F32(Condition); w.F32(Speed); w.F32(LengthM); w.F64(HeadDistance);
|
||||
w.Count(Segments.Count);
|
||||
foreach (var s in Segments)
|
||||
{
|
||||
w.I32(s.Edge); w.Bool(s.Forward); w.F64(s.FromS); w.F64(s.ToS);
|
||||
}
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static TrainMotion Read(ref SpanReader r)
|
||||
{
|
||||
int id = r.I32();
|
||||
long tick = r.I64();
|
||||
byte state = r.U8();
|
||||
float cond = r.F32(), speed = r.F32(), length = r.F32();
|
||||
double head = r.F64();
|
||||
int n = r.Count();
|
||||
var segs = new List<MotionSegment>(n);
|
||||
for (int i = 0; i < n; i++) segs.Add(new MotionSegment(r.I32(), r.Bool(), r.F64(), r.F64()));
|
||||
r.Flags();
|
||||
return new TrainMotion { TrainId = id, Tick = tick, State = state, Condition = cond, Speed = speed, LengthM = length, HeadDistance = head, Segments = segs };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Resoconto giornaliero: data, conti della compagnia, prezzi, eventi del giorno, hash di stato.</summary>
|
||||
public sealed class DailyDigest : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.DailyDigest;
|
||||
public long Day { get; init; }
|
||||
public long Tick { get; init; }
|
||||
public string Date { get; init; } = "";
|
||||
public Money Cash { get; init; }
|
||||
public Money NetWorth { get; init; }
|
||||
public string Rating { get; init; } = "";
|
||||
public List<Money> Prices { get; init; } = [];
|
||||
public List<EventInfo> Events { get; init; } = [];
|
||||
public ulong StateHash { get; init; }
|
||||
public bool Slow { get; init; }
|
||||
public byte Speed { get; init; }
|
||||
public float TickMs { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day); w.I64(Tick); w.String(Date); w.Money(Cash); w.Money(NetWorth); w.String(Rating);
|
||||
w.Count(Prices.Count);
|
||||
foreach (var p in Prices) w.Money(p);
|
||||
w.Count(Events.Count);
|
||||
foreach (var e in Events) e.Write(ref w);
|
||||
w.U64(StateHash); w.Bool(Slow); w.U8(Speed); w.F32(TickMs);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static DailyDigest Read(ref SpanReader r)
|
||||
{
|
||||
long day = r.I64(), tick = r.I64();
|
||||
string date = r.String();
|
||||
var cash = r.Money();
|
||||
var worth = r.Money();
|
||||
string rating = r.String();
|
||||
var prices = new List<Money>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) prices.Add(r.Money());
|
||||
var events = new List<EventInfo>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) events.Add(EventInfo.Read(ref r));
|
||||
var m = new DailyDigest
|
||||
{
|
||||
Day = day, Tick = tick, Date = date, Cash = cash, NetWorth = worth, Rating = rating, Prices = prices, Events = events,
|
||||
StateHash = r.U64(), Slow = r.Bool(), Speed = r.U8(), TickMs = r.F32(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Avviso di manutenzione di un treno o di un deposito (F4).</summary>
|
||||
public sealed class MaintenanceAlert : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.MaintenanceAlert;
|
||||
public int Train { get; init; } = -1;
|
||||
public int Depot { get; init; } = -1;
|
||||
public string Component { get; init; } = "";
|
||||
public float Condition { get; init; }
|
||||
public int DueDays { get; init; }
|
||||
public string Text { get; init; } = "";
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Train); w.I32(Depot); w.String(Component); w.F32(Condition); w.I32(DueDays); w.String(Text);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static MaintenanceAlert Read(ref SpanReader r)
|
||||
{
|
||||
var m = new MaintenanceAlert { Train = r.I32(), Depot = r.I32(), Component = r.String(), Condition = r.F32(), DueDays = r.I32(), Text = r.String() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Provenienza dei lavoratori di un posto di lavoro (Registro degli impieghi).</summary>
|
||||
public sealed class EmploymentSource
|
||||
{
|
||||
public int Town { get; init; }
|
||||
public int HomeStation { get; init; } = -1;
|
||||
/// <summary>Prima linea del percorso (−1 = a piedi).</summary>
|
||||
public int Line { get; init; } = -1;
|
||||
public byte Category { get; init; }
|
||||
public int Headcount { get; init; }
|
||||
public float Coverage { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Town); w.I32(HomeStation); w.I32(Line); w.U8(Category); w.I32(Headcount); w.F32(Coverage);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static EmploymentSource Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EmploymentSource { Town = r.I32(), HomeStation = r.I32(), Line = r.I32(), Category = r.U8(), Headcount = r.I32(), Coverage = r.F32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Riga del Registro degli impieghi: un posto di lavoro (industria o deposito).</summary>
|
||||
public sealed class EmploymentRow
|
||||
{
|
||||
/// <summary>Indice del posto di lavoro (industria o deposito secondo <see cref="Kind"/>).</summary>
|
||||
public int Industry { get; init; }
|
||||
public int JobsWorkers { get; init; }
|
||||
public int JobsStaff { get; init; }
|
||||
public float WorkersPresent { get; init; }
|
||||
public float StaffPresent { get; init; }
|
||||
/// <summary>Motivo dei posti scoperti, in italiano (per i lettori senza il blocco F4).</summary>
|
||||
public string Reason { get; init; } = "";
|
||||
public List<EmploymentSource> Sources { get; init; } = [];
|
||||
|
||||
// blocco 1 (F4): tipo di posto e codici dei motivi (UncoveredReason di Core)
|
||||
/// <summary>0 = industria, 1 = deposito.</summary>
|
||||
public byte Kind { get; set; }
|
||||
public byte WorkersReason { get; set; }
|
||||
public byte StaffReason { get; set; }
|
||||
public int WorkersReasonLine { get; set; } = -1;
|
||||
public int StaffReasonLine { get; set; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Industry); w.I32(JobsWorkers); w.I32(JobsStaff); w.F32(WorkersPresent); w.F32(StaffPresent); w.String(Reason);
|
||||
w.Count(Sources.Count);
|
||||
foreach (var s in Sources) s.Write(ref w);
|
||||
w.Flags(1);
|
||||
w.U8(Kind); w.U8(WorkersReason); w.U8(StaffReason); w.I32(WorkersReasonLine); w.I32(StaffReasonLine);
|
||||
}
|
||||
|
||||
public static EmploymentRow Read(ref SpanReader r)
|
||||
{
|
||||
int ind = r.I32(), jw = r.I32(), js = r.I32();
|
||||
float wp = r.F32(), sp = r.F32();
|
||||
string reason = r.String();
|
||||
var sources = new List<EmploymentSource>();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) sources.Add(EmploymentSource.Read(ref r));
|
||||
var m = new EmploymentRow { Industry = ind, JobsWorkers = jw, JobsStaff = js, WorkersPresent = wp, StaffPresent = sp, Reason = reason, Sources = sources };
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
m.Kind = r.U8(); m.WorkersReason = r.U8(); m.StaffReason = r.U8(); m.WorkersReasonLine = r.I32(); m.StaffReasonLine = r.I32();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Convenzione di pendolarismo (sezione 9) nel Registro degli impieghi.</summary>
|
||||
public sealed class AgreementInfo
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public int Industry { get; init; }
|
||||
public int Carrier { get; init; }
|
||||
public int SeatsPerDay { get; init; }
|
||||
public Money MonthlyFee { get; init; }
|
||||
/// <summary>AgreementState di Core: 0 proposta, 1 attiva, 2 rifiutata, 3 scaduta, 4 saltata, 5 conclusa.</summary>
|
||||
public byte State { get; init; }
|
||||
public long ProposedDay { get; init; }
|
||||
public long StartDay { get; init; } = -1;
|
||||
public long EndDay { get; init; } = -1;
|
||||
public float Coverage { get; init; }
|
||||
public int ShortDays { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Id); w.I32(Industry); w.I32(Carrier); w.I32(SeatsPerDay); w.Money(MonthlyFee); w.U8(State);
|
||||
w.I64(ProposedDay); w.I64(StartDay); w.I64(EndDay); w.F32(Coverage); w.I32(ShortDays);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static AgreementInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new AgreementInfo
|
||||
{
|
||||
Id = r.I32(), Industry = r.I32(), Carrier = r.I32(), SeatsPerDay = r.I32(), MonthlyFee = r.Money(), State = r.U8(),
|
||||
ProposedDay = r.I64(), StartDay = r.I64(), EndDay = r.I64(), Coverage = r.F32(), ShortDays = r.I32(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Registro degli impieghi della compagnia: ogni mese, dopo le convenzioni e su richiesta.</summary>
|
||||
public sealed class EmploymentReport : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.EmploymentReport;
|
||||
public long Day { get; init; }
|
||||
public List<EmploymentRow> Rows { get; init; } = [];
|
||||
|
||||
// blocco 1 (F4): convenzioni proposte e attive
|
||||
public List<AgreementInfo> Agreements { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day);
|
||||
w.Count(Rows.Count);
|
||||
foreach (var row in Rows) row.Write(ref w);
|
||||
w.Flags(1);
|
||||
w.Count(Agreements.Count);
|
||||
foreach (var a in Agreements) a.Write(ref w);
|
||||
}
|
||||
|
||||
public static EmploymentReport Read(ref SpanReader r)
|
||||
{
|
||||
var m = new EmploymentReport { Day = r.I64() };
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Rows.Add(EmploymentRow.Read(ref r));
|
||||
byte flags = r.Flags();
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Agreements.Add(AgreementInfo.Read(ref r));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Componente di un veicolo nella cartella di manutenzione.</summary>
|
||||
public sealed class ComponentCard
|
||||
{
|
||||
public string Component { get; init; } = "";
|
||||
public float Condition { get; init; }
|
||||
/// <summary>Usura media giornaliera degli ultimi giorni (punti/giorno).</summary>
|
||||
public float WearPerDay { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.String(Component); w.F32(Condition); w.F32(WearPerDay);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static ComponentCard Read(ref SpanReader r)
|
||||
{
|
||||
var m = new ComponentCard { Component = r.String(), Condition = r.F32(), WearPerDay = r.F32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VehicleCard
|
||||
{
|
||||
public int Vehicle { get; init; }
|
||||
public string Type { get; init; } = "";
|
||||
public float OdometerKm { get; init; }
|
||||
public float AgeYears { get; init; }
|
||||
public float AgeFactor { get; init; }
|
||||
public List<ComponentCard> Components { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Vehicle); w.String(Type); w.F32(OdometerKm); w.F32(AgeYears); w.F32(AgeFactor);
|
||||
w.Count(Components.Count);
|
||||
foreach (var c in Components) c.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static VehicleCard Read(ref SpanReader r)
|
||||
{
|
||||
var m = new VehicleCard { Vehicle = r.I32(), Type = r.String(), OdometerKm = r.F32(), AgeYears = r.F32(), AgeFactor = r.F32() };
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Components.Add(ComponentCard.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Voce dello storico della cartella di manutenzione.</summary>
|
||||
public sealed class MaintenanceRecordInfo
|
||||
{
|
||||
public long Day { get; init; }
|
||||
public string Intervention { get; init; } = "";
|
||||
public Money Cost { get; init; }
|
||||
public int Depot { get; init; } = -1;
|
||||
public int Parts { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day); w.String(Intervention); w.Money(Cost); w.I32(Depot); w.I32(Parts);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static MaintenanceRecordInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new MaintenanceRecordInfo { Day = r.I64(), Intervention = r.String(), Cost = r.Money(), Depot = r.I32(), Parts = r.I32() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cartella di manutenzione di un treno (10.3): componenti per veicolo, guasto in corso, politica,
|
||||
/// prossimo intervento, costi dell'anno e storico. Si invia su richiesta (RequestDetail).
|
||||
/// </summary>
|
||||
public sealed class MaintenanceCard : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.MaintenanceCard;
|
||||
public int Train { get; init; }
|
||||
public long Day { get; init; }
|
||||
public float Condition { get; init; }
|
||||
public TrainMaintenanceStatus Status { get; init; }
|
||||
/// <summary>Politica effettiva e quella propria del treno (255 = ereditata).</summary>
|
||||
public byte Policy { get; init; }
|
||||
public byte PolicyOverride { get; init; } = byte.MaxValue;
|
||||
public bool Reserve { get; init; }
|
||||
/// <summary>FailureKind di Core e componente guasto.</summary>
|
||||
public byte Failure { get; init; }
|
||||
public int FailedVehicle { get; init; } = -1;
|
||||
public string FailedComponent { get; init; } = "";
|
||||
/// <summary>Ore (orologio dei treni) all'arrivo del soccorso, −1 se non serve.</summary>
|
||||
public float RescueHours { get; init; } = -1;
|
||||
public string NextIntervention { get; init; } = "";
|
||||
public float NextDueDays { get; init; }
|
||||
public string PlannedIntervention { get; init; } = "";
|
||||
public int MaintenanceDepot { get; init; } = -1;
|
||||
public float ServiceKmPerDay { get; init; }
|
||||
public Money CostThisYear { get; init; }
|
||||
public Money CostLastYear { get; init; }
|
||||
public int FailuresThisYear { get; init; }
|
||||
public int FailuresTotal { get; init; }
|
||||
public int CoveredBy { get; init; } = -1;
|
||||
public int Covering { get; init; } = -1;
|
||||
public List<VehicleCard> Vehicles { get; init; } = [];
|
||||
public List<MaintenanceRecordInfo> History { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Train); w.I64(Day); w.F32(Condition); w.U8((byte)Status); w.U8(Policy); w.U8(PolicyOverride); w.Bool(Reserve);
|
||||
w.U8(Failure); w.I32(FailedVehicle); w.String(FailedComponent); w.F32(RescueHours);
|
||||
w.String(NextIntervention); w.F32(NextDueDays); w.String(PlannedIntervention); w.I32(MaintenanceDepot);
|
||||
w.F32(ServiceKmPerDay); w.Money(CostThisYear); w.Money(CostLastYear); w.I32(FailuresThisYear); w.I32(FailuresTotal);
|
||||
w.I32(CoveredBy); w.I32(Covering);
|
||||
w.Count(Vehicles.Count);
|
||||
foreach (var v in Vehicles) v.Write(ref w);
|
||||
w.Count(History.Count);
|
||||
foreach (var h in History) h.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static MaintenanceCard Read(ref SpanReader r)
|
||||
{
|
||||
var m = new MaintenanceCard
|
||||
{
|
||||
Train = r.I32(), Day = r.I64(), Condition = r.F32(), Status = (TrainMaintenanceStatus)r.U8(), Policy = r.U8(), PolicyOverride = r.U8(),
|
||||
Reserve = r.Bool(), Failure = r.U8(), FailedVehicle = r.I32(), FailedComponent = r.String(), RescueHours = r.F32(),
|
||||
NextIntervention = r.String(), NextDueDays = r.F32(), PlannedIntervention = r.String(), MaintenanceDepot = r.I32(),
|
||||
ServiceKmPerDay = r.F32(), CostThisYear = r.Money(), CostLastYear = r.Money(), FailuresThisYear = r.I32(), FailuresTotal = r.I32(),
|
||||
CoveredBy = r.I32(), Covering = r.I32(),
|
||||
};
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Vehicles.Add(VehicleCard.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.History.Add(MaintenanceRecordInfo.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Lavoro nel Registro officina.</summary>
|
||||
public sealed class WorkshopJobInfo
|
||||
{
|
||||
public int Train { get; init; }
|
||||
public string Intervention { get; init; } = "";
|
||||
public bool InStall { get; init; }
|
||||
public bool Started { get; init; }
|
||||
public float Progress { get; init; }
|
||||
public float Days { get; init; }
|
||||
public int Parts { get; init; }
|
||||
public long QueuedDay { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Train); w.String(Intervention); w.Bool(InStall); w.Bool(Started); w.F32(Progress); w.F32(Days); w.I32(Parts); w.I64(QueuedDay);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static WorkshopJobInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new WorkshopJobInfo
|
||||
{
|
||||
Train = r.I32(), Intervention = r.String(), InStall = r.Bool(), Started = r.Bool(), Progress = r.F32(), Days = r.F32(),
|
||||
Parts = r.I32(), QueuedDay = r.I64(),
|
||||
};
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registro officina di un deposito (10.3): stalli, personale, ricambi, coda dei lavori e treni
|
||||
/// prenotati. Si invia su richiesta (RequestDetail).
|
||||
/// </summary>
|
||||
public sealed class WorkshopRegister : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.WorkshopRegister;
|
||||
public int Depot { get; init; }
|
||||
public long Day { get; init; }
|
||||
public int Stalls { get; init; }
|
||||
public int JobsStaff { get; init; }
|
||||
public float StaffPresent { get; init; }
|
||||
/// <summary>Velocità del lavoro (1 a personale completo).</summary>
|
||||
public float WorkRate { get; init; }
|
||||
public int SpareParts { get; init; }
|
||||
public int PartsTarget { get; init; }
|
||||
/// <summary>Ricambi già destinati a lavori in coda e treni prenotati.</summary>
|
||||
public int PartsReserved { get; init; }
|
||||
public List<WorkshopJobInfo> Jobs { get; init; } = [];
|
||||
/// <summary>Treni prenotati in arrivo e relativo intervento.</summary>
|
||||
public List<WorkshopJobInfo> Booked { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Depot); w.I64(Day); w.I32(Stalls); w.I32(JobsStaff); w.F32(StaffPresent); w.F32(WorkRate);
|
||||
w.I32(SpareParts); w.I32(PartsTarget); w.I32(PartsReserved);
|
||||
w.Count(Jobs.Count);
|
||||
foreach (var j in Jobs) j.Write(ref w);
|
||||
w.Count(Booked.Count);
|
||||
foreach (var j in Booked) j.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static WorkshopRegister Read(ref SpanReader r)
|
||||
{
|
||||
var m = new WorkshopRegister
|
||||
{
|
||||
Depot = r.I32(), Day = r.I64(), Stalls = r.I32(), JobsStaff = r.I32(), StaffPresent = r.F32(), WorkRate = r.F32(),
|
||||
SpareParts = r.I32(), PartsTarget = r.I32(), PartsReserved = r.I32(),
|
||||
};
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Jobs.Add(WorkshopJobInfo.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Booked.Add(WorkshopJobInfo.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SharePrice
|
||||
{
|
||||
public int Company { get; init; }
|
||||
public Money Price { get; init; }
|
||||
public float Change { get; init; }
|
||||
public bool Listed { get; init; }
|
||||
|
||||
// blocco 1 (F5): prezzo esatto, capitalizzazione, flottante libero, quota del fondatore
|
||||
public double PriceCents { get; set; }
|
||||
public long Outstanding { get; set; }
|
||||
public long FreeFloat { get; set; }
|
||||
public float FounderShare { get; set; }
|
||||
public int ControlledBy { get; set; } = -1;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Company); w.Money(Price); w.F32(Change); w.Bool(Listed);
|
||||
w.Flags(1);
|
||||
w.F64(PriceCents); w.I64(Outstanding); w.I64(FreeFloat); w.F32(FounderShare); w.I32(ControlledBy);
|
||||
}
|
||||
|
||||
public static SharePrice Read(ref SpanReader r)
|
||||
{
|
||||
var m = new SharePrice { Company = r.I32(), Price = r.Money(), Change = r.F32(), Listed = r.Bool() };
|
||||
if ((r.Flags() & 1) != 0)
|
||||
{
|
||||
m.PriceCents = r.F64(); m.Outstanding = r.I64(); m.FreeFloat = r.I64(); m.FounderShare = r.F32(); m.ControlledBy = r.I32();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class BidInfo
|
||||
{
|
||||
public int Bid { get; init; }
|
||||
public int Bidder { get; init; }
|
||||
public int Target { get; init; }
|
||||
public Money PricePerShare { get; init; }
|
||||
public double TargetShare { get; init; }
|
||||
public long ExpiresDay { get; init; }
|
||||
|
||||
// blocco 1 (F5): stato, quota già impegnata (azioni dell'offerente + adesioni), difese, cavaliere bianco
|
||||
public byte State { get; set; }
|
||||
public double CommittedShare { get; set; }
|
||||
public byte Defenses { get; set; }
|
||||
public int Knight { get; set; } = -1;
|
||||
/// <summary>Vero se la compagnia della sessione ha già aderito.</summary>
|
||||
public bool Accepted { get; set; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I32(Bid); w.I32(Bidder); w.I32(Target); w.Money(PricePerShare); w.F64(TargetShare); w.I64(ExpiresDay);
|
||||
w.Flags(1);
|
||||
w.U8(State); w.F64(CommittedShare); w.U8(Defenses); w.I32(Knight); w.Bool(Accepted);
|
||||
}
|
||||
|
||||
public static BidInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new BidInfo { Bid = r.I32(), Bidder = r.I32(), Target = r.I32(), PricePerShare = r.Money(), TargetShare = r.F64(), ExpiresDay = r.I64() };
|
||||
if ((r.Flags() & 1) != 0)
|
||||
{
|
||||
m.State = r.U8(); m.CommittedShare = r.F64(); m.Defenses = r.U8(); m.Knight = r.I32(); m.Accepted = r.Bool();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Listino giornaliero della borsa e offerte pubbliche in corso (F5).</summary>
|
||||
public sealed class MarketTape : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.MarketTape;
|
||||
public long Day { get; init; }
|
||||
public List<SharePrice> Shares { get; init; } = [];
|
||||
public List<BidInfo> Bids { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day);
|
||||
w.Count(Shares.Count);
|
||||
foreach (var s in Shares) s.Write(ref w);
|
||||
w.Count(Bids.Count);
|
||||
foreach (var b in Bids) b.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static MarketTape Read(ref SpanReader r)
|
||||
{
|
||||
var m = new MarketTape { Day = r.I64() };
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Shares.Add(SharePrice.Read(ref r));
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Bids.Add(BidInfo.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Esito di un comando, con il numero di sequenza del client e gli id creati.</summary>
|
||||
public sealed class CommandResultMessage : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.CommandResult;
|
||||
public uint Seq { get; init; }
|
||||
public ushort Type { get; init; }
|
||||
public ushort Error { get; init; }
|
||||
public string Message { get; init; } = "";
|
||||
public int[] Created { get; init; } = [];
|
||||
public long Tick { get; init; }
|
||||
|
||||
public bool Ok => Error == 0;
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.U32(Seq); w.U16(Type); w.U16(Error); w.String(Message);
|
||||
w.Count(Created.Length);
|
||||
foreach (int c in Created) w.I32(c);
|
||||
w.I64(Tick);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static CommandResultMessage Read(ref SpanReader r)
|
||||
{
|
||||
uint seq = r.U32();
|
||||
ushort type = r.U16(), error = r.U16();
|
||||
string message = r.String();
|
||||
var created = new int[r.Count()];
|
||||
for (int i = 0; i < created.Length; i++) created[i] = r.I32();
|
||||
var m = new CommandResultMessage { Seq = seq, Type = type, Error = error, Message = message, Created = created, Tick = r.I64() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Notifica immediata (evento rilevante per la compagnia della sessione).</summary>
|
||||
public sealed class Notification : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Notification;
|
||||
public required EventInfo Event { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
Event.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Notification Read(ref SpanReader r)
|
||||
{
|
||||
var m = new Notification { Event = EventInfo.Read(ref r) };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>La «Cronaca» mensile.</summary>
|
||||
public sealed class Newspaper : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.Newspaper;
|
||||
public long Day { get; init; }
|
||||
public string Title { get; init; } = "";
|
||||
public List<(string Headline, string Body)> Articles { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.I64(Day);
|
||||
w.String(Title);
|
||||
w.Count(Articles.Count);
|
||||
foreach (var (h, b) in Articles)
|
||||
{
|
||||
w.String(h);
|
||||
w.String(b);
|
||||
}
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static Newspaper Read(ref SpanReader r)
|
||||
{
|
||||
var m = new Newspaper { Day = r.I64(), Title = r.String() };
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Articles.Add((r.String(), r.String()));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SaveInfo
|
||||
{
|
||||
public string SaveId { get; init; } = "";
|
||||
public string Name { get; init; } = "";
|
||||
public string GameDate { get; init; } = "";
|
||||
public long SizeBytes { get; init; }
|
||||
public long SavedUnixMs { get; init; }
|
||||
public bool Autosave { get; init; }
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.String(SaveId); w.String(Name); w.String(GameDate); w.I64(SizeBytes); w.I64(SavedUnixMs); w.Bool(Autosave);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static SaveInfo Read(ref SpanReader r)
|
||||
{
|
||||
var m = new SaveInfo { SaveId = r.String(), Name = r.String(), GameDate = r.String(), SizeBytes = r.I64(), SavedUnixMs = r.I64(), Autosave = r.Bool() };
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Risposta a ListSaves e a Save (salvataggi disponibili sul server).</summary>
|
||||
public sealed class SaveList : IMessage
|
||||
{
|
||||
public MessageId Id => MessageId.SaveList;
|
||||
public List<SaveInfo> Saves { get; init; } = [];
|
||||
|
||||
public void Write(ref SpanWriter w)
|
||||
{
|
||||
w.Count(Saves.Count);
|
||||
foreach (var s in Saves) s.Write(ref w);
|
||||
w.Flags(0);
|
||||
}
|
||||
|
||||
public static SaveList Read(ref SpanReader r)
|
||||
{
|
||||
var m = new SaveList();
|
||||
for (int n = r.Count(), i = 0; i < n; i++) m.Saves.Add(SaveInfo.Read(ref r));
|
||||
r.Flags();
|
||||
return m;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user