I livelli esistevano dalla F5 ma il giocatore li vedeva solo come distintivo e come rifiuti con nomi scritti a mano. Ora il motore espone un quadro della scalata e il client lo mostra come l'immagine di riferimento 08. - ClimbOutlook (Core, sola lettura): livelli con gli sblocchi letti dai dati, giorno in cui sono stati raggiunti, requisiti del prossimo livello con valore, soglia, frazione e avanzamento (media delle tre frazioni, ora anche nel Mercato globale), stato patrimoniale, conto economico, prestiti, storico del patrimonio dalla Cronaca. - Stato patrimoniale con una sola fonte: FinanceSystem.BookValues scompone il valore contabile per voce e AssetBookValue ne e' la somma, cosi' il pannello somma al centesimo al patrimonio netto del motore. Le industrie possedute entrano nel patrimonio (impianti piu' cassa, senza avviamento): prima comprarle bruciava patrimonio. - Notizia di promozione con gli sblocchi ottenuti; rifiuti dei comandi bloccati con il livello che serve e il suo nome dai dati (compravendita, industrie, quotazione, scalate, concessioni, fido). - Salvataggio versione 6 (BalanceSheetSerializer) con voci e giorni dei livelli nell'hash; le versioni 1-5 si migrano con lo stesso patrimonio. - Protocollo: DetailKind.Climb (8) e ClimbReport (134), su richiesta. - Client: pannello ClimbPanel a tutto schermo, pulsante Scalata, livello e avanzamento nella barra superiore, schermata nel pilota automatico. - Test: Economy/ClimbTests.cs e quadro via WebSocket (170 test verdi). Decisioni in docs/adr/0018-scalata-e-livelli.md. Refs #38. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
6.5 KiB
C#
112 lines
6.5 KiB
C#
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Economy;
|
|
using RailEnterprise.Core.Simulation.Systems;
|
|
|
|
namespace RailEnterprise.Core.Simulation;
|
|
|
|
/// <summary>Comandi delle industrie possedute (sezione 11): acquisto, budget, revisione, ammodernamento, ricambi.</summary>
|
|
public static class IndustryCommandHandlers
|
|
{
|
|
public static CommandResult BuyIndustry(World w, Company company, BuyIndustryCommand c, RailCommandHandlers.Reply reply)
|
|
{
|
|
if (!Tiers.Unlocks(w, company).BuyIndustry)
|
|
return reply(CommandError.TierLocked, Tiers.LockedText(w, "L'acquisto di industrie", u => u.BuyIndustry));
|
|
if (!TryIndustry(w, c.Industry, out var ind) || ind.Closed) return reply(CommandError.NotFound, "Industria inesistente");
|
|
if (ind.Owner == company.Id) return reply(CommandError.NotAllowed, $"{ind.Name} è già della compagnia");
|
|
if (ind.Owner.IsValid) return reply(CommandError.NotAllowed, $"{ind.Name} appartiene a {w.Company(ind.Owner).Name}");
|
|
if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva e non è in vendita");
|
|
var price = IndustryMath.Valuation(w, ind);
|
|
if (c.Offer < price) return reply(CommandError.InvalidArgument, $"Offerta troppo bassa: i proprietari chiedono almeno {price}");
|
|
if (company.Cash < c.Offer) return reply(CommandError.InsufficientFunds, "Cassa insufficiente");
|
|
w.Money.Transfer(Account.Of(company.Id), Account.External, c.Offer, LedgerEntry.IndustryPurchase);
|
|
ind.Owner = company.Id;
|
|
ind.AcquiredDay = w.Clock.Day;
|
|
ind.MonthsHighMargin = 0;
|
|
w.Events.Add(new GameEvent(w.Clock.Day, EventKind.Industry, EventSeverity.Info,
|
|
$"{company.Name} acquista {ind.Name} per {c.Offer}", EntityKind.Industry, ind.Id.Value, company.Id));
|
|
return reply(CommandError.None, $"{ind.Name} acquistata per {c.Offer}", ind.Id.Value);
|
|
}
|
|
|
|
public static CommandResult SetPolicy(World w, Company company, SetIndustryPolicyCommand c, RailCommandHandlers.Reply reply)
|
|
{
|
|
if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message);
|
|
if (c.Budget > IndustryBudget.High) return reply(CommandError.InvalidArgument, "Budget non valido");
|
|
ind.Budget = c.Budget;
|
|
var b = w.Data.Balance.IndustryMaintenance.For(c.Budget);
|
|
return reply(CommandError.None,
|
|
$"{ind.Name}: budget {BudgetName(c.Budget)} (recupero {b.RecoveryPerDay:0.00} punti al giorno, {b.CostShareOfRevenue * 100:0} % dei ricavi, {b.SparePartsPerLevelPerMonth * ind.Level} ricambi al mese)");
|
|
}
|
|
|
|
public static CommandResult Overhaul(World w, Company company, OverhaulIndustryCommand c, RailCommandHandlers.Reply reply)
|
|
{
|
|
if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message);
|
|
if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva");
|
|
if (ind.IsStopped(w.Clock.Day)) return reply(CommandError.NotAllowed, $"{ind.Name} è già ferma");
|
|
if (ind.PendingWork != IndustryWork.None) return reply(CommandError.NotAllowed, $"{ind.Name} ha già un lavoro programmato");
|
|
var m = w.Data.Balance.IndustryMaintenance;
|
|
ind.PendingWork = IndustryWork.Overhaul;
|
|
int need = IndustryMath.MachineryRequired(ind, m);
|
|
return reply(CommandError.None,
|
|
$"{ind.Name}: revisione programmata ({m.OverhaulDays} giorni di fermo); servono {need} macchinari (in scorta {ind.Machinery}) e personale almeno all'{m.OverhaulMinStaffCoverage * 100:0} %");
|
|
}
|
|
|
|
public static CommandResult Upgrade(World w, Company company, UpgradeIndustryCommand c, RailCommandHandlers.Reply reply)
|
|
{
|
|
if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message);
|
|
if (ind.Level <= 0) return reply(CommandError.NotAllowed, $"{ind.Name} è inattiva");
|
|
if (ind.Level >= w.Data.Balance.Growth.MaxLevel) return reply(CommandError.NotAllowed, $"{ind.Name} è già al livello massimo");
|
|
if (ind.IsStopped(w.Clock.Day)) return reply(CommandError.NotAllowed, $"{ind.Name} è ferma");
|
|
if (ind.PendingWork != IndustryWork.None) return reply(CommandError.NotAllowed, $"{ind.Name} ha già un lavoro programmato");
|
|
var cost = IndustryMaintenanceSystem.UpgradeCost(ind);
|
|
if (company.Cash < cost) return reply(CommandError.InsufficientFunds, $"Servono {cost} per l'ammodernamento");
|
|
var m = w.Data.Balance.IndustryMaintenance;
|
|
ind.PendingWork = IndustryWork.Upgrade;
|
|
int need = IndustryMath.MachineryRequired(ind, m);
|
|
return reply(CommandError.None,
|
|
$"{ind.Name}: ammodernamento al livello {ind.Level + 1} programmato ({cost}, {m.UpgradeDays} giorni); servono {need} macchinari (in scorta {ind.Machinery})");
|
|
}
|
|
|
|
public static CommandResult OrderParts(World w, Company company, OrderPartsCommand c, RailCommandHandlers.Reply reply)
|
|
{
|
|
if (!TryOwned(w, company, c.Industry, out var ind, out var error)) return reply(error.Error, error.Message);
|
|
int max = w.Data.Balance.IndustryMaintenance.MaxOrderedParts;
|
|
if (c.Quantity < 0 || c.Quantity > max) return reply(CommandError.InvalidArgument, $"Ordine tra 0 e {max} unità");
|
|
ind.PartsOrdered = c.Quantity;
|
|
string unit = w.Data.Goods[w.Data.SparePartsGood].Unit;
|
|
return reply(CommandError.None,
|
|
$"{ind.Name}: ordinati {c.Quantity} {unit} di ricambi oltre la scorta della politica (scorta attuale {ind.SpareParts})");
|
|
}
|
|
|
|
public static string BudgetName(IndustryBudget b) => b switch
|
|
{
|
|
IndustryBudget.None => "Nessuno",
|
|
IndustryBudget.High => "Alto",
|
|
_ => "Base",
|
|
};
|
|
|
|
private static bool TryIndustry(World w, IndustryId id, out Industry ind)
|
|
{
|
|
ind = null!;
|
|
if (!id.IsValid || id.Value >= w.Industries.Count) return false;
|
|
ind = w.Industries[id.Value];
|
|
return true;
|
|
}
|
|
|
|
private static bool TryOwned(World w, Company company, IndustryId id, out Industry ind, out (CommandError Error, string Message) error)
|
|
{
|
|
error = default;
|
|
if (!TryIndustry(w, id, out ind) || ind.Closed)
|
|
{
|
|
error = (CommandError.NotFound, "Industria inesistente");
|
|
return false;
|
|
}
|
|
if (ind.Owner != company.Id)
|
|
{
|
|
error = (CommandError.NotAllowed, "L'industria non appartiene alla compagnia");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|