using RailEnterprise.Core.Common; namespace RailEnterprise.Protocol.Messages; // Messaggi della fase F6: pannello «Finanze» definitivo. Il quadro arriva su richiesta (RequestDetail con // DetailKind.Finance) ed è calcolato dal server con FinanceOutlook (ADR 0019): il client lo mostra com'è, // senza calcolare utili, rating o rate. /// Voce del conto economico in ordine di presentazione: Kind come LedgerEntry del Core, Section 0 ricavi, 1 costi, 2 investimenti e finanziamenti. public sealed class FinanceLineInfo { public const byte SectionRevenue = 0, SectionCost = 1, SectionCapital = 2; public byte Kind { get; init; } public byte Section { get; init; } public void Write(ref SpanWriter w) { w.U8(Kind); w.U8(Section); w.Flags(0); } public static FinanceLineInfo Read(ref SpanReader r) { var m = new FinanceLineInfo { Kind = r.U8(), Section = r.U8() }; r.Flags(); return m; } } /// /// Colonna del conto economico: Kind 0 mese in corso, 1 mese chiuso, 2 anno in corso, 3 anno chiuso; /// Month 1–12 (0 per gli anni); Values con le sole voci diverse da zero (Kind = LedgerEntry); utile /// operativo e flusso di cassa del motore. /// public sealed class IncomeColumnInfo { public const byte CurrentMonth = 0, ClosedMonth = 1, CurrentYear = 2, ClosedYear = 3; public byte Kind { get; init; } public int Year { get; init; } public byte Month { get; init; } public List Values { get; init; } = []; public Money Profit { get; init; } public Money CashFlow { get; init; } /// Valore di una voce (zero se assente). public Money Value(byte entry) { foreach (var v in Values) { if (v.Kind == entry) return v.Value; } return Money.Zero; } public void Write(ref SpanWriter w) { w.U8(Kind); w.I32(Year); w.U8(Month); w.Count(Values.Count); foreach (var v in Values) v.Write(ref w); w.Money(Profit); w.Money(CashFlow); w.Flags(0); } public static IncomeColumnInfo Read(ref SpanReader r) { byte kind = r.U8(); int year = r.I32(); byte month = r.U8(); var values = new List(); for (int n = r.Count(), i = 0; i < n; i++) values.Add(AmountInfo.Read(ref r)); var m = new IncomeColumnInfo { Kind = kind, Year = year, Month = month, Values = values, Profit = r.Money(), CashFlow = r.Money() }; r.Flags(); return m; } } /// Soglia di un rating: punteggio minimo e tasso di un nuovo prestito con quel rating. public sealed class RatingThresholdInfo { public string Rating { get; init; } = ""; public float MinScore { get; init; } public float LoanRate { get; init; } public void Write(ref SpanWriter w) { w.String(Rating); w.F32(MinScore); w.F32(LoanRate); w.Flags(0); } public static RatingThresholdInfo Read(ref SpanReader r) { var m = new RatingThresholdInfo { Rating = r.String(), MinScore = r.F32(), LoanRate = r.F32() }; r.Flags(); return m; } } /// Prestito in corso: capitale, residuo, tasso fisso, mesi restanti, rata, interessi della prossima rata, scadenza. public sealed class FinanceLoanInfo { public int Loan { get; init; } public Money Principal { get; init; } public Money Remaining { get; init; } public float AnnualRate { get; init; } public int MonthsLeft { get; init; } public Money Payment { get; init; } public Money NextInterest { get; init; } public long MaturityDay { get; init; } public void Write(ref SpanWriter w) { w.I32(Loan); w.Money(Principal); w.Money(Remaining); w.F32(AnnualRate); w.I32(MonthsLeft); w.Money(Payment); w.Money(NextInterest); w.I64(MaturityDay); w.Flags(0); } public static FinanceLoanInfo Read(ref SpanReader r) { var m = new FinanceLoanInfo { Loan = r.I32(), Principal = r.Money(), Remaining = r.Money(), AnnualRate = r.F32(), MonthsLeft = r.I32(), Payment = r.Money(), NextInterest = r.Money(), MaturityDay = r.I64(), }; r.Flags(); return m; } } /// /// Quadro delle finanze della compagnia della sessione (F6, ADR 0019): conto economico per voce (mese in /// corso, mesi chiusi, anno in corso, anni chiusi), spiegazione del rating, prestiti, fido e conto alla /// rovescia della bancarotta. /// public sealed class FinanceReport : IMessage { public MessageId Id => MessageId.FinanceReport; public long Day { get; init; } public int Company { get; init; } public Money Cash { get; init; } public Money NetWorth { get; init; } public Money Loans { get; init; } public Money Overdraft { get; init; } /// Voci in ordine di presentazione e colonne del conto economico. public List Lines { get; init; } = []; public List Columns { get; init; } = []; /// Rating e sua spiegazione: misure dell'ultimo calcolo, componenti, punteggio. public string Rating { get; init; } = ""; /// Prima del primo calcolo mensile: rating iniziale dei dati, misure di oggi. public bool RatingInitial { get; init; } public byte RatingMonths { get; init; } /// /// Mesi della finestra con ricavi; con il margine vale 0 finché non ce ne /// sono (o la finestra non è piena). /// public byte RevenueMonths { get; init; } public bool MarginNeutral { get; init; } public byte MinRevenueMonths { get; init; } public float Margin { get; init; } /// Debiti / patrimonio netto; con il patrimonio non è positivo e la leva è infinita. public float Leverage { get; init; } public bool LeverageUnbounded { get; init; } public float MarginWeight { get; init; } public float LeverageWeight { get; init; } public float MarginComponent { get; init; } public float LeverageComponent { get; init; } public float Score { get; init; } public List Thresholds { get; init; } = []; /// /// Gradino sopra ("" ad AAA) con la soglia, il margine che basterebbe, la leva massima e il debito da /// rimborsare; con il punteggio basta già (rating /// al prossimo calcolo mensile) e gli obiettivi non si calcolano. /// public string NextRating { get; init; } = ""; public float NextThreshold { get; init; } public bool NextAlreadyMet { get; init; } public string ScoreRating { get; init; } = ""; public float MarginNeeded { get; init; } public bool MarginReachable { get; init; } public float LeverageMax { get; init; } public bool LeverageReachable { get; init; } public Money DebtToRepay { get; init; } /// Il rimborso indicato non supera la cassa né il residuo dei prestiti (lo scoperto non si rimborsa). public bool RepayAffordable { get; init; } /// Prestiti, fido e condizioni di un nuovo prestito (base della difficoltà + spread del rating). public List LoanList { get; init; } = []; public Money MaxLoan { get; init; } public Money LoanAvailable { get; init; } public float NewLoanRate { get; init; } public float BaseRate { get; init; } public float Spread { get; init; } public int LoanTermMonths { get; init; } public Money LoanStep { get; init; } public bool LoansAllowed { get; init; } = true; /// Giorni di cassa negativa e limite della difficoltà prima della bancarotta. public int NegativeCashDays { get; init; } public int BankruptcyDays { get; init; } public bool Bankrupt { get; init; } public bool Receivership { get; init; } public void Write(ref SpanWriter w) { w.I64(Day); w.I32(Company); w.Money(Cash); w.Money(NetWorth); w.Money(Loans); w.Money(Overdraft); w.Count(Lines.Count); foreach (var l in Lines) l.Write(ref w); w.Count(Columns.Count); foreach (var c in Columns) c.Write(ref w); w.String(Rating); w.Bool(RatingInitial); w.U8(RatingMonths); w.U8(RevenueMonths); w.Bool(MarginNeutral); w.U8(MinRevenueMonths); w.F32(Margin); w.F32(Leverage); w.Bool(LeverageUnbounded); w.F32(MarginWeight); w.F32(LeverageWeight); w.F32(MarginComponent); w.F32(LeverageComponent); w.F32(Score); w.Count(Thresholds.Count); foreach (var t in Thresholds) t.Write(ref w); w.String(NextRating); w.F32(NextThreshold); w.Bool(NextAlreadyMet); w.String(ScoreRating); w.F32(MarginNeeded); w.Bool(MarginReachable); w.F32(LeverageMax); w.Bool(LeverageReachable); w.Money(DebtToRepay); w.Bool(RepayAffordable); w.Count(LoanList.Count); foreach (var l in LoanList) l.Write(ref w); w.Money(MaxLoan); w.Money(LoanAvailable); w.F32(NewLoanRate); w.F32(BaseRate); w.F32(Spread); w.I32(LoanTermMonths); w.Money(LoanStep); w.Bool(LoansAllowed); w.I32(NegativeCashDays); w.I32(BankruptcyDays); w.Bool(Bankrupt); w.Bool(Receivership); w.Flags(0); } public static FinanceReport Read(ref SpanReader r) { long day = r.I64(); int company = r.I32(); var cash = r.Money(); var worth = r.Money(); var loans = r.Money(); var overdraft = r.Money(); var lines = new List(); for (int n = r.Count(), i = 0; i < n; i++) lines.Add(FinanceLineInfo.Read(ref r)); var columns = new List(); for (int n = r.Count(), i = 0; i < n; i++) columns.Add(IncomeColumnInfo.Read(ref r)); string rating = r.String(); bool initial = r.Bool(); byte months = r.U8(), revenueMonths = r.U8(); bool neutral = r.Bool(); byte minRevenueMonths = r.U8(); float margin = r.F32(), leverage = r.F32(); bool unbounded = r.Bool(); float marginWeight = r.F32(), leverageWeight = r.F32(), marginComponent = r.F32(), leverageComponent = r.F32(), score = r.F32(); var thresholds = new List(); for (int n = r.Count(), i = 0; i < n; i++) thresholds.Add(RatingThresholdInfo.Read(ref r)); string next = r.String(); float nextThreshold = r.F32(); bool alreadyMet = r.Bool(); string scoreRating = r.String(); float marginNeeded = r.F32(); bool marginReachable = r.Bool(); float leverageMax = r.F32(); bool leverageReachable = r.Bool(); var repay = r.Money(); bool affordable = r.Bool(); var loanList = new List(); for (int n = r.Count(), i = 0; i < n; i++) loanList.Add(FinanceLoanInfo.Read(ref r)); var maxLoan = r.Money(); var available = r.Money(); float newRate = r.F32(), baseRate = r.F32(), spread = r.F32(); int term = r.I32(); var step = r.Money(); bool allowed = r.Bool(); int negDays = r.I32(), limit = r.I32(); bool bankrupt = r.Bool(), receivership = r.Bool(); r.Flags(); return new FinanceReport { Day = day, Company = company, Cash = cash, NetWorth = worth, Loans = loans, Overdraft = overdraft, Lines = lines, Columns = columns, Rating = rating, RatingInitial = initial, RatingMonths = months, RevenueMonths = revenueMonths, MarginNeutral = neutral, MinRevenueMonths = minRevenueMonths, Margin = margin, Leverage = leverage, LeverageUnbounded = unbounded, MarginWeight = marginWeight, LeverageWeight = leverageWeight, MarginComponent = marginComponent, LeverageComponent = leverageComponent, Score = score, Thresholds = thresholds, NextRating = next, NextThreshold = nextThreshold, NextAlreadyMet = alreadyMet, ScoreRating = scoreRating, MarginNeeded = marginNeeded, MarginReachable = marginReachable, LeverageMax = leverageMax, LeverageReachable = leverageReachable, DebtToRepay = repay, RepayAffordable = affordable, LoanList = loanList, MaxLoan = maxLoan, LoanAvailable = available, NewLoanRate = newRate, BaseRate = baseRate, Spread = spread, LoanTermMonths = term, LoanStep = step, LoansAllowed = allowed, NegativeCashDays = negDays, BankruptcyDays = limit, Bankrupt = bankrupt, Receivership = receivership, }; } }