Files
Rail_Enterprise/src/RailEnterprise.Core/Simulation/MaintenanceCommandHandlers.cs
T
Alby96andClaude Opus 5.5 596ce2c376
CI / Build e test (pull_request) Successful in 1m19s
CI / Immagine Docker (pull_request) Successful in 29s
F6: compravendita in proprio, correzioni della revisione
La revisione ha trovato casi in cui la merce della compagnia, già pagata,
poteva perdersi o restare bloccata, e il magazzino di destinazione
restare prenotato per sempre. Le correzioni:

- Il percorso della merce propria rispetta i flag carico e scarico delle
  fermate: LineGraph.From ha l'opzione stopFlags. ShipGoods rifiuta con
  Blocked la spedizione che esiste solo ignorando i flag. I pacchetti dei
  clienti non cambiano: l'accettazione F4 dà ancora 35/20 e 75/2.
- La scadenza in stazione della merce propria riparte a ogni trasbordo.
- La merce propria in attesa ricalcola ogni giorno il tratto sulle linee
  di quel giorno; se non c'è più un percorso torna nel magazzino.
- La merce propria a bordo scende alla prima fermata quando il suo tratto
  non è più sulla linea del treno. Un treno che lascia la linea
  (riassegnato, al deposito, in riserva, fine copertura) la lascia alla
  stazione dove si trova o alla prossima fermata.
- Una stazione con merce propria in partenza o in arrivo non si demolisce.
- La merce propria, in magazzino e in viaggio, entra nel valore contabile
  al prezzo globale: un acquisto non abbassa più il patrimonio netto
  dell'intero importo.
- Solo l'acquisto richiede lo sblocco del livello: la merce ereditata con
  una fusione si vende e si spedisce, e il riquadro del pannello compare.
- Nel riquadro, la stazione di arrivo predefinita è diversa da quella di
  partenza.

ADR 0016 e STATE aggiornati; sei test nuovi in TradingTests.

Refs #31.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:05:29 +01:00

138 lines
7.9 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))
return reply(CommandError.InvalidArgument, "Intervento: inspection, service o overhaul");
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];
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;
}
}