Files
Rail_Enterprise/src/RailEnterprise.Core/Simulation/MaintenanceCommandHandlers.cs
T
Alby96andClaude Opus 5.5 b7737455c6 F6: ere e obsolescenza, correzioni della revisione
Due revisori hanno trovato 12 rilievi. I principali:
- Il rinnovo si ricontrolla all'avvio dei lavori: piano, spesa e cassa
  possono cambiare tra prenotazione e avvio. Se la cassa non basta il
  rinnovo si annulla senza pagare e con un avviso, così non va mai in
  scoperto.
- La cassa impegnata dai rinnovi prenotati e non ancora avviati
  (Obsolescence.Committed) conta per il giocatore e per le IA, così la
  stessa cassa non si spende due volte nello stesso mese.
- Le IA comprano il successore solo se non peggiora il treno, e rinnovano
  solo se le prestazioni restano almeno quelle di oggi.
- Dopo un deragliamento la ricostruzione non ringiovanisce nel patrimonio
  un modello fuori produzione.
- Quadro della flotta e pannelli mostrano un rinnovo già prenotato e la
  spesa netta al netto degli altri rinnovi.

Suite completa 210 su 210, client senza avvisi.

Refs #41.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:22:49 +01:00

151 lines
9.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using RailEnterprise.Core.Commands;
using RailEnterprise.Core.Common;
using RailEnterprise.Core.Data;
using RailEnterprise.Core.Economy;
using RailEnterprise.Core.Rail;
using RailEnterprise.Core.Simulation.Systems;
namespace RailEnterprise.Core.Simulation;
/// <summary>Comandi della manutenzione della flotta (10.2–10.3) e delle convenzioni di pendolarismo (9).</summary>
public static class MaintenanceCommandHandlers
{
public static CommandResult SetReserve(World w, Company company, SetReserveCommand c, RailCommandHandlers.Reply reply)
{
if (!TryTrain(w, c.Train, company, out var train)) return reply(CommandError.NotFound, "Treno inesistente");
if (!c.Reserve)
{
train.Reserve = false;
return reply(CommandError.None, $"{train.Name} non è più una riserva");
}
if (train.Covering.IsValid) return reply(CommandError.NotAllowed, $"{train.Name} sta già coprendo una linea");
if (!train.HomeDepot.IsValid || !w.Network.IsAlive(w.Network.Depots[train.HomeDepot.Value]))
return reply(CommandError.NotAllowed, "Il treno non ha un deposito di appartenenza");
train.Reserve = true;
if (train.Line.IsValid)
{
StationWork.DropOwnedCargo(w, train, train.Line);
train.Line = LineId.None;
w.LineGraphs.Touch();
}
var home = w.Network.Depots[train.HomeDepot.Value];
if (train.AtNode != home.Node && !train.InWorkshop && !train.MaintenanceDepot.IsValid)
{
train.GoingToDepot = home.Id;
if (train.State == TrainState.NoRoute) train.State = TrainState.Waiting;
}
return reply(CommandError.None, $"{train.Name} è una riserva del {home.Name}");
}
public static CommandResult SetPolicy(World w, Company company, SetMaintenancePolicyCommand c, RailCommandHandlers.Reply reply)
{
MaintenancePolicy? policy = c.Policy switch
{
"economy" => MaintenancePolicy.Economy,
"standard" => MaintenancePolicy.Standard,
"thorough" => MaintenancePolicy.Thorough,
"" or "inherit" => null,
_ => (MaintenancePolicy)255,
};
if (policy == (MaintenancePolicy)255) return reply(CommandError.InvalidArgument, "Politica: economy, standard, thorough o inherit");
string name = policy is { } p ? w.Data.Maintenance.Policy(p).NameIt : "quella generale";
switch (c.Scope)
{
case MaintenanceScope.Company:
if (policy is null) return reply(CommandError.InvalidArgument, "La flotta richiede una politica");
company.MaintenancePolicy = policy.Value;
return reply(CommandError.None, $"Politica di manutenzione della flotta: {name}");
case MaintenanceScope.Line:
if (c.Target < 0 || c.Target >= w.Lines.Count || w.Lines[c.Target].Owner != company.Id || w.Lines[c.Target].Removed)
return reply(CommandError.NotFound, "Linea inesistente");
w.Lines[c.Target].Policy = policy;
return reply(CommandError.None, $"{w.Lines[c.Target].Name}: politica {name}");
case MaintenanceScope.Train:
if (!TryTrain(w, new TrainId(c.Target), company, out var train)) return reply(CommandError.NotFound, "Treno inesistente");
train.Policy = policy;
return reply(CommandError.None, $"{train.Name}: politica {name}");
default:
return reply(CommandError.InvalidArgument, "Ambito sconosciuto");
}
}
public static CommandResult ScheduleService(World w, Company company, ScheduleServiceCommand c, WorldContext ctx, RailCommandHandlers.Reply reply)
{
if (!TryTrain(w, c.Train, company, out var train)) return reply(CommandError.NotFound, "Treno inesistente");
var m = w.Data.Maintenance;
int idx = m.InterventionIndex(c.Intervention);
if (idx < 0 || m.Interventions[idx].Effect is not (InterventionEffect.Inspection or InterventionEffect.Service or InterventionEffect.Overhaul or InterventionEffect.Renew))
return reply(CommandError.InvalidArgument, $"Intervento: inspection, service, overhaul o {m.Interventions.First(i => i.Effect == InterventionEffect.Renew).Id}");
if (train.Vehicles.Count == 0) return reply(CommandError.NotAllowed, "Treno senza veicoli");
if (train.InWorkshop) return reply(CommandError.NotAllowed, $"{train.Name} è già in officina");
if (train.State == TrainState.Broken) return reply(CommandError.NotAllowed, $"{train.Name} attende il soccorso");
if (train.Failure != FailureKind.None) return reply(CommandError.NotAllowed, $"{train.Name} ha un guasto: prima la riparazione");
var def = m.Interventions[idx];
if (def.Effect == InterventionEffect.Renew)
{
// rinnovo (ADR 0020): serve almeno un veicolo superato con un successore in produzione e la cassa
// per la spesa netta, al netto degli altri rinnovi già prenotati; all'avvio dei lavori si ricontrolla
var plan = Obsolescence.RenewalPlan(w, train);
if (plan.Count == 0) return reply(CommandError.NotAllowed, $"{train.Name}: nessun veicolo superato da rinnovare");
var net = Obsolescence.RenewalCost(w, train, plan);
var committed = Obsolescence.Committed(w, company.Id, train);
if (net.IsPositive && company.Cash - committed < net)
return reply(CommandError.InsufficientFunds, committed.IsPositive
? $"Il rinnovo di {train.Name} costa {net} al netto delle rivendite: cassa insufficiente (già impegnati {committed} in altri rinnovi)"
: $"Il rinnovo di {train.Name} costa {net} al netto delle rivendite: cassa insufficiente");
}
int parts = Fleet.PartsFor(w, train, def);
FleetMaintenanceSystem.CancelBooking(train);
if (!FleetMaintenanceSystem.Book(ctx, train, idx, parts, requireParts: true))
return reply(CommandError.Blocked, parts > 0 ? $"Nessun deposito raggiungibile con {parts} ricambi" : "Nessun deposito raggiungibile");
var depot = w.Network.Depots[train.MaintenanceDepot.Value];
return reply(CommandError.None, $"{train.Name}: {def.NameIt.ToLowerInvariant()} prenotato a {depot.Name}", depot.Id.Value);
}
public static CommandResult OrderParts(World w, Company company, OrderPartsCommand c, RailCommandHandlers.Reply reply)
{
if (c.Industry.IsValid) return IndustryCommandHandlers.OrderParts(w, company, c, reply);
if (!c.Depot.IsValid || c.Depot.Value >= w.Network.Depots.Count) return reply(CommandError.NotFound, "Deposito inesistente");
var d = w.Network.Depots[c.Depot.Value];
if (d.Owner != company.Id || !w.Network.IsAlive(d)) return reply(CommandError.NotFound, "Deposito inesistente");
int max = w.Data.Maintenance.Depots.MaxSpareParts;
if (c.Quantity < 0 || c.Quantity > max) return reply(CommandError.InvalidArgument, $"Scorta tra 0 e {max} casse");
d.PartsTarget = c.Quantity;
string unit = w.Data.Goods[w.Data.SparePartsGood].Unit;
return reply(CommandError.None, $"{d.Name}: scorta desiderata di ricambi {c.Quantity} {unit} (oggi {d.SpareParts})");
}
public static CommandResult AcceptAgreement(World w, Company company, AcceptAgreementCommand c, RailCommandHandlers.Reply reply)
{
var a = w.Agreements.FirstOrDefault(x => x.Id == c.Agreement);
if (a is null || a.Carrier != company.Id) return reply(CommandError.NotFound, "Convenzione inesistente");
if (a.State != AgreementState.Proposed) return reply(CommandError.NotAllowed, "La convenzione non è più in attesa di risposta");
var ab = w.Data.Balance.Employment.Agreements;
a.State = AgreementState.Active;
a.StartDay = w.Clock.Day;
a.EndDay = w.Clock.Day + (long)ab.DurationMonths * w.Calendar.DaysPerMonth;
a.Coverage = 1;
a.ShortDays = 0;
var ind = w.Industries[a.Industry.Value];
return reply(CommandError.None, $"Convenzione con {ind.Name}: {a.SeatsPerDay} posti al giorno per {a.MonthlyFee} al mese");
}
public static CommandResult RejectAgreement(World w, Company company, RejectAgreementCommand c, RailCommandHandlers.Reply reply)
{
var a = w.Agreements.FirstOrDefault(x => x.Id == c.Agreement);
if (a is null || a.Carrier != company.Id) return reply(CommandError.NotFound, "Convenzione inesistente");
if (a.State != AgreementState.Proposed) return reply(CommandError.NotAllowed, "La convenzione non è più in attesa di risposta");
a.State = AgreementState.Rejected;
a.EndDay = w.Clock.Day;
return reply(CommandError.None, $"Convenzione con {w.Industries[a.Industry.Value].Name} rifiutata");
}
private static bool TryTrain(World w, TrainId id, Company company, out Train train)
{
train = null!;
if (!id.IsValid || id.Value >= w.Trains.Count) return false;
train = w.Trains[id.Value];
return train.Owner == company.Id && !train.Removed;
}
}