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>
353 lines
17 KiB
C#
353 lines
17 KiB
C#
using Godot;
|
|
using RailEnterprise.Client.App;
|
|
using RailEnterprise.Client.Net;
|
|
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Protocol.Messages;
|
|
using static RailEnterprise.Client.App.Localization;
|
|
|
|
namespace RailEnterprise.Client.Panels;
|
|
|
|
/// <summary>
|
|
/// Officine (sezione 10.3): politica della flotta, avvisi, flotta ordinata per condizione con la
|
|
/// mappa di calore, cartella di manutenzione del treno scelto, depositi con il Registro officina e
|
|
/// la scorta di ricambi. Le cartelle e i registri arrivano su richiesta dal server.
|
|
/// </summary>
|
|
public partial class MaintenancePanel : VBoxContainer
|
|
{
|
|
private static readonly string[] PolicyIds = ["economy", "standard", "thorough"];
|
|
|
|
private readonly GameSession _session;
|
|
private readonly OptionButton _fleetPolicy = new();
|
|
private readonly CheckButton _heat = new() { Text = T("Mappa di calore della condizione") };
|
|
private readonly VBoxContainer _alerts = PanelKit.Column(4);
|
|
private readonly ItemList _trains = new() { CustomMinimumSize = new Vector2(0, 150) };
|
|
private readonly VBoxContainer _card = PanelKit.Column(8);
|
|
private readonly VBoxContainer _cardControls = PanelKit.Column(8);
|
|
private readonly VBoxContainer _cardDetails = PanelKit.Column(6);
|
|
private readonly ItemList _depots = new() { CustomMinimumSize = new Vector2(0, 80) };
|
|
private readonly VBoxContainer _workshop = PanelKit.Column(8);
|
|
private readonly VBoxContainer _workshopDetails = PanelKit.Column(6);
|
|
private readonly HBoxContainer _orderRow = PanelKit.Row();
|
|
private readonly Label _status = UiTheme.Text("", 13, Palette.TextSecondary);
|
|
private readonly List<int> _trainIds = [];
|
|
private readonly List<int> _depotIds = [];
|
|
private int _train = -1, _depot = -1;
|
|
private int _entityVersion = -1, _detailVersion = -1;
|
|
private double _request;
|
|
private bool _updating;
|
|
|
|
public event Action<bool>? HeatMapToggled;
|
|
public event Action<int>? TrainSelected;
|
|
|
|
public MaintenancePanel(GameSession session, bool heatMap)
|
|
{
|
|
_session = session;
|
|
AddThemeConstantOverride("separation", 10);
|
|
AddChild(UiTheme.TitleLabel(T("Officine"), 34));
|
|
var policyRow = PanelKit.Row();
|
|
var caption = UiTheme.Text(T("Politica della flotta"), 14, Palette.TextSecondary);
|
|
caption.SizeFlagsHorizontal = SizeFlags.ExpandFill;
|
|
policyRow.AddChild(caption);
|
|
foreach (var p in session.World.Data.Maintenance.Policies) _fleetPolicy.AddItem(p.NameIt);
|
|
policyRow.AddChild(_fleetPolicy);
|
|
AddChild(policyRow);
|
|
_heat.ButtonPressed = heatMap;
|
|
_heat.Toggled += on => HeatMapToggled?.Invoke(on);
|
|
AddChild(_heat);
|
|
AddChild(_alerts);
|
|
|
|
AddChild(PanelKit.Separator());
|
|
AddChild(UiTheme.Text(T("Flotta, dal treno più usurato"), 14, bold: true));
|
|
AddChild(_trains);
|
|
AddChild(_card);
|
|
AddChild(_cardControls);
|
|
AddChild(_cardDetails);
|
|
AddChild(PanelKit.Separator());
|
|
AddChild(UiTheme.Text(T("Depositi e officine"), 14, bold: true));
|
|
AddChild(_depots);
|
|
AddChild(_workshop);
|
|
AddChild(_workshopDetails);
|
|
AddChild(_orderRow);
|
|
_status.AutowrapMode = TextServer.AutowrapMode.WordSmart;
|
|
AddChild(_status);
|
|
|
|
_fleetPolicy.ItemSelected += i =>
|
|
{
|
|
if (!_updating) _session.Command(new SetMaintenancePolicyCommand(MaintenanceScope.Company, 0, PolicyIds[i]), Result);
|
|
};
|
|
_trains.ItemSelected += i => SelectTrain(_trainIds[(int)i]);
|
|
_depots.ItemSelected += i => SelectDepot(_depotIds[(int)i]);
|
|
}
|
|
|
|
/// <summary>Allinea l'interruttore della mappa di calore senza rilanciare l'evento.</summary>
|
|
public void SetHeatMap(bool on) => _heat.SetPressedNoSignal(on);
|
|
|
|
public void SelectTrain(int id)
|
|
{
|
|
_train = id;
|
|
_detailVersion = -1;
|
|
_session.Send(new RequestDetail { Kind = DetailKind.TrainCard, Target = id });
|
|
TrainSelected?.Invoke(id);
|
|
BuildCardControls();
|
|
Refresh();
|
|
}
|
|
|
|
public void SelectDepot(int id)
|
|
{
|
|
_depot = id;
|
|
_detailVersion = -1;
|
|
_session.Send(new RequestDetail { Kind = DetailKind.Workshop, Target = id });
|
|
BuildOrderRow();
|
|
Refresh();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
// cartella e registro aperti si aggiornano una volta al secondo
|
|
_request += delta;
|
|
if (_request >= 1)
|
|
{
|
|
_request = 0;
|
|
if (_train >= 0) _session.Send(new RequestDetail { Kind = DetailKind.TrainCard, Target = _train });
|
|
if (_depot >= 0) _session.Send(new RequestDetail { Kind = DetailKind.Workshop, Target = _depot });
|
|
}
|
|
var w = _session.World;
|
|
if (w.EntityVersion != _entityVersion || w.DetailVersion != _detailVersion) Refresh();
|
|
}
|
|
|
|
private void Result(CommandResultMessage r) => _status.Text = r.Message;
|
|
|
|
private void Refresh()
|
|
{
|
|
var w = _session.World;
|
|
_entityVersion = w.EntityVersion;
|
|
_detailVersion = w.DetailVersion;
|
|
int company = w.Summary?.CompanyId ?? 0;
|
|
_updating = true;
|
|
if (w.Companies.TryGetValue(company, out var me)) _fleetPolicy.Selected = Math.Clamp(me.MaintenancePolicy, 0, PolicyIds.Length - 1);
|
|
_updating = false;
|
|
|
|
foreach (var c in _alerts.GetChildren()) c.QueueFree();
|
|
foreach (var a in w.Alerts.Take(4))
|
|
{
|
|
var l = UiTheme.Text("⚠ " + a.Text, 13, a.Condition < w.Data.Maintenance.Planner.CriticalCondition ? Palette.Danger : Palette.Amber);
|
|
l.AutowrapMode = TextServer.AutowrapMode.WordSmart;
|
|
_alerts.AddChild(l);
|
|
}
|
|
|
|
_trainIds.Clear();
|
|
_trains.Clear();
|
|
foreach (var t in w.Trains.Values.Where(t => t.Owner == company).OrderBy(t => t.Condition).ThenBy(t => t.Id))
|
|
{
|
|
_trainIds.Add(t.Id);
|
|
_trains.AddItem($"{t.Name} · {t.Condition}% · {StatusName(t.Maintenance)}{(t.Reserve ? " · " + T("riserva") : "")}{DueText(t)}");
|
|
_trains.SetItemCustomFgColor(_trains.ItemCount - 1, ConditionColor(t.Condition));
|
|
}
|
|
int ti = _trainIds.IndexOf(_train);
|
|
if (ti >= 0) _trains.Select(ti);
|
|
|
|
_depotIds.Clear();
|
|
_depots.Clear();
|
|
foreach (var d in w.Depots.Values.Where(d => d.Owner == company).OrderBy(d => d.Id))
|
|
{
|
|
_depotIds.Add(d.Id);
|
|
_depots.AddItem($"{d.Name} · {T("ricambi")} {d.SpareParts}/{d.PartsTarget} · {T("personale")} {d.StaffPercent}% · {d.Jobs} {T("lavori")}");
|
|
if (d.WaitingParts) _depots.SetItemCustomFgColor(_depots.ItemCount - 1, Palette.Danger);
|
|
}
|
|
int di = _depotIds.IndexOf(_depot);
|
|
if (di >= 0) _depots.Select(di);
|
|
|
|
BuildCard();
|
|
BuildWorkshop();
|
|
}
|
|
|
|
private string DueText(TrainInfo t)
|
|
{
|
|
if (t.NextIntervention.Length == 0 || t.Maintenance != TrainMaintenanceStatus.InService) return "";
|
|
return t.NextDueDays <= 0
|
|
? $" · {InterventionName(t.NextIntervention)} {T("scaduta")}"
|
|
: $" · {InterventionName(t.NextIntervention)} {T("tra")} {t.NextDueDays} {T("g")}";
|
|
}
|
|
|
|
// ------------------------------------------------------------------ cartella di manutenzione
|
|
|
|
private void BuildCard()
|
|
{
|
|
foreach (var c in _card.GetChildren()) c.QueueFree();
|
|
foreach (var c in _cardDetails.GetChildren()) c.QueueFree();
|
|
var w = _session.World;
|
|
if (_train < 0 || !w.Trains.TryGetValue(_train, out var t)) return;
|
|
_card.AddChild(UiTheme.TitleLabel(T("Cartella di manutenzione") + " · " + t.Name, 22));
|
|
var card = w.Cards.GetValueOrDefault(_train);
|
|
var stats = PanelKit.Row();
|
|
stats.AddChild(PanelKit.Stat(T("Condizione"), PanelKit.Value($"{Math.Floor(card?.Condition ?? t.Condition):0}%")));
|
|
stats.AddChild(PanelKit.Stat(T("Guasti nell'anno"), PanelKit.Value($"{card?.FailuresThisYear ?? 0}")));
|
|
stats.AddChild(PanelKit.Stat(T("Costi dell'anno"), PanelKit.Value((card?.CostThisYear ?? Money.Zero).ToString())));
|
|
_card.AddChild(stats);
|
|
_card.AddChild(PanelKit.Pair(T("Stato"), PanelKit.Value(StatusName(t.Maintenance))));
|
|
if (card is not null)
|
|
{
|
|
if (card.Failure != 0)
|
|
_card.AddChild(UiTheme.Notice($"{FailureName(card.Failure)}: {ComponentName(card.FailedComponent)}" +
|
|
(card.RescueHours >= 0 ? $" · {T("soccorso tra")} {card.RescueHours:0.0} {T("ore")}" : "")));
|
|
if (card.PlannedIntervention.Length > 0 && card.MaintenanceDepot >= 0 && w.Depots.TryGetValue(card.MaintenanceDepot, out var md))
|
|
_card.AddChild(PanelKit.Pair(T("Prenotato"), PanelKit.Value($"{InterventionName(card.PlannedIntervention)} · {md.Name}")));
|
|
if (card.NextIntervention.Length > 0)
|
|
_card.AddChild(PanelKit.Pair(T("Prossimo intervento"), PanelKit.Value(card.NextDueDays <= 0
|
|
? $"{InterventionName(card.NextIntervention)} · {T("scaduto")}"
|
|
: $"{InterventionName(card.NextIntervention)} · {T("tra")} {Math.Ceiling(card.NextDueDays):0} {T("giorni")}")));
|
|
_card.AddChild(PanelKit.Pair(T("Km di servizio al giorno"), PanelKit.Value($"{card.ServiceKmPerDay:0}")));
|
|
if (card.CoveredBy >= 0 && w.Trains.TryGetValue(card.CoveredBy, out var cover))
|
|
_card.AddChild(PanelKit.Pair(T("Coperto dalla riserva"), PanelKit.Value(cover.Name)));
|
|
}
|
|
|
|
if (card is null)
|
|
{
|
|
_cardDetails.AddChild(UiTheme.Text(T("Cartella in arrivo dal server…"), 13, Palette.TextSecondary));
|
|
return;
|
|
}
|
|
|
|
foreach (var v in card.Vehicles)
|
|
{
|
|
var def = w.Data.Vehicles.FirstOrDefault(d => d.Id == v.Type);
|
|
_cardDetails.AddChild(UiTheme.Text($"{def?.NameIt ?? v.Type} #{v.Vehicle} · {v.OdometerKm / 1000:0.#} {T("mila km")} · {v.AgeYears:0.#} {T("anni")}", 13, bold: true));
|
|
foreach (var c in v.Components)
|
|
{
|
|
var row = PanelKit.Row(6);
|
|
var name = UiTheme.Text(ComponentName(c.Component), 12, Palette.TextSecondary);
|
|
name.CustomMinimumSize = new Vector2(150, 0);
|
|
row.AddChild(name);
|
|
var bar = new ProgressBar { MaxValue = 100, Value = c.Condition, ShowPercentage = false, CustomMinimumSize = new Vector2(0, 10), SizeFlagsHorizontal = SizeFlags.ExpandFill };
|
|
bar.AddThemeStyleboxOverride("fill", UiTheme.Box(ConditionColor((int)c.Condition), ConditionColor((int)c.Condition), 3, 0));
|
|
row.AddChild(bar);
|
|
row.AddChild(UiTheme.Text($"{c.Condition:0}%", 12, bold: true));
|
|
_cardDetails.AddChild(row);
|
|
}
|
|
}
|
|
if (card.History.Count > 0)
|
|
{
|
|
_cardDetails.AddChild(UiTheme.Text(T("Storico"), 14, bold: true));
|
|
foreach (var h in Enumerable.Reverse(card.History).Take(6))
|
|
{
|
|
string depot = w.Depots.TryGetValue(h.Depot, out var dd) ? dd.Name : "";
|
|
_cardDetails.AddChild(UiTheme.Text($"{T("giorno")} {h.Day} · {InterventionName(h.Intervention)} · {h.Cost} · {depot}", 12, Palette.TextSecondary));
|
|
}
|
|
}
|
|
}
|
|
|
|
// controlli della cartella: costruiti alla selezione, non a ogni aggiornamento
|
|
private void BuildCardControls()
|
|
{
|
|
foreach (var c in _cardControls.GetChildren()) c.QueueFree();
|
|
var w = _session.World;
|
|
if (_train < 0 || !w.Trains.TryGetValue(_train, out var t)) return;
|
|
// politica propria del treno e riserva
|
|
var policy = new OptionButton();
|
|
policy.AddItem(T("Come la linea o la flotta"));
|
|
foreach (var p in w.Data.Maintenance.Policies) policy.AddItem(p.NameIt);
|
|
policy.Selected = t.Policy == byte.MaxValue ? 0 : t.Policy + 1;
|
|
policy.ItemSelected += i => _session.Command(new SetMaintenancePolicyCommand(MaintenanceScope.Train, _train, i == 0 ? "inherit" : PolicyIds[i - 1]), Result);
|
|
_cardControls.AddChild(PanelKit.Pair(T("Politica del treno"), new Label()));
|
|
_cardControls.AddChild(policy);
|
|
var reserve = new CheckBox { Text = T("Treno di riserva in deposito"), ButtonPressed = t.Reserve };
|
|
reserve.Toggled += on => _session.Command(new SetReserveCommand(new TrainId(_train), on), Result);
|
|
_cardControls.AddChild(reserve);
|
|
var actions = PanelKit.Row(6);
|
|
foreach (string id in new[] { "inspection", "service", "overhaul" })
|
|
{
|
|
var b = new Button { Text = InterventionName(id), SizeFlagsHorizontal = SizeFlags.ExpandFill };
|
|
b.Pressed += () => _session.Command(new ScheduleServiceCommand(new TrainId(_train), id), Result);
|
|
actions.AddChild(b);
|
|
}
|
|
_cardControls.AddChild(actions);
|
|
}
|
|
|
|
// ------------------------------------------------------------------ registro officina
|
|
|
|
private void BuildWorkshop()
|
|
{
|
|
foreach (var c in _workshop.GetChildren()) c.QueueFree();
|
|
foreach (var c in _workshopDetails.GetChildren()) c.QueueFree();
|
|
var w = _session.World;
|
|
if (_depot < 0 || !w.Depots.TryGetValue(_depot, out var d)) return;
|
|
_workshop.AddChild(UiTheme.TitleLabel(T("Registro officina") + " · " + d.Name, 22));
|
|
var reg = w.Workshops.GetValueOrDefault(_depot);
|
|
var stats = PanelKit.Row();
|
|
stats.AddChild(PanelKit.Stat(T("Stalli"), PanelKit.Value($"{d.Stalls}")));
|
|
stats.AddChild(PanelKit.Stat(T("Personale"), PanelKit.Value($"{d.StaffPercent}%")));
|
|
stats.AddChild(PanelKit.Stat(T("Ricambi"), PanelKit.Value($"{d.SpareParts}")));
|
|
_workshop.AddChild(stats);
|
|
if (d.WaitingParts) _workshop.AddChild(UiTheme.Notice(T("Ricambi esauriti: gli interventi aspettano. Porta i ricambi in treno da una fabbrica di macchinari o da un porto.")));
|
|
if (reg is not null)
|
|
{
|
|
if (reg.WorkRate < 0.999f)
|
|
_workshopDetails.AddChild(PanelKit.Pair(T("Ritmo di lavoro (manca personale)"), PanelKit.Value($"{reg.WorkRate * 100:0}%")));
|
|
_workshopDetails.AddChild(PanelKit.Pair(T("Ricambi già destinati"), PanelKit.Value($"{reg.PartsReserved}")));
|
|
int stall = 0;
|
|
foreach (var j in reg.Jobs)
|
|
{
|
|
string train = w.Trains.TryGetValue(j.Train, out var t) ? t.Name : $"#{j.Train}";
|
|
string where = j.InStall ? $"{T("stallo")} {++stall}" : T("in coda");
|
|
string what = j.Started ? $"{j.Progress:0.0}/{j.Days:0} {T("giorni")}" : j.InStall ? T("attende i ricambi") : T("in attesa");
|
|
var line = UiTheme.Text($"{where} · {train} · {InterventionName(j.Intervention)} · {what}", 13);
|
|
line.AutowrapMode = TextServer.AutowrapMode.WordSmart;
|
|
_workshopDetails.AddChild(line);
|
|
}
|
|
foreach (var j in reg.Booked)
|
|
{
|
|
string train = w.Trains.TryGetValue(j.Train, out var t) ? t.Name : $"#{j.Train}";
|
|
_workshopDetails.AddChild(UiTheme.Text($"{T("in arrivo")} · {train} · {InterventionName(j.Intervention)} · {j.Parts} {T("ricambi")}", 13, Palette.TextSecondary));
|
|
}
|
|
if (reg.Jobs.Count == 0 && reg.Booked.Count == 0) _workshopDetails.AddChild(UiTheme.Text(T("Nessun treno in officina"), 13, Palette.TextSecondary));
|
|
}
|
|
}
|
|
|
|
// scorta desiderata: costruita alla selezione del deposito
|
|
private void BuildOrderRow()
|
|
{
|
|
foreach (var c in _orderRow.GetChildren()) c.QueueFree();
|
|
var w = _session.World;
|
|
if (_depot < 0 || !w.Depots.TryGetValue(_depot, out var d)) return;
|
|
var target = new SpinBox { MinValue = 0, MaxValue = w.Data.Maintenance.Depots.MaxSpareParts, Value = d.PartsTarget, SizeFlagsHorizontal = SizeFlags.ExpandFill };
|
|
_orderRow.AddChild(UiTheme.Text(T("Scorta desiderata"), 13, Palette.TextSecondary));
|
|
_orderRow.AddChild(target);
|
|
var button = new Button { Text = T("Ordina") };
|
|
button.Pressed += () => _session.Command(new OrderPartsCommand(new DepotId(_depot), (int)target.Value), Result);
|
|
_orderRow.AddChild(button);
|
|
}
|
|
|
|
// ------------------------------------------------------------------ testi
|
|
|
|
public static string StatusName(TrainMaintenanceStatus s) => s switch
|
|
{
|
|
TrainMaintenanceStatus.Booked => T("intervento prenotato"),
|
|
TrainMaintenanceStatus.InWorkshop => T("in officina"),
|
|
TrainMaintenanceStatus.MinorFailure => T("guasto lieve"),
|
|
TrainMaintenanceStatus.Stranded => T("fermo in linea"),
|
|
TrainMaintenanceStatus.Towed => T("rimorchiato"),
|
|
_ => T("in servizio"),
|
|
};
|
|
|
|
private static string FailureName(byte f) => f switch
|
|
{
|
|
1 => T("Guasto lieve"),
|
|
2 => T("Guasto grave"),
|
|
3 => T("Deragliamento"),
|
|
_ => "",
|
|
};
|
|
|
|
public string InterventionName(string id) =>
|
|
_session.World.Data.Maintenance.Interventions.FirstOrDefault(i => i.Id == id)?.NameIt ?? id;
|
|
|
|
private string ComponentName(string id)
|
|
{
|
|
var m = _session.World.Data.Maintenance.Components;
|
|
return m.Locomotive.Concat(m.Car).FirstOrDefault(c => c.Id == id)?.NameIt ?? id;
|
|
}
|
|
|
|
/// <summary>Colore della condizione: verde sopra 70, ambra fino a 40, rosso sotto.</summary>
|
|
public static Color ConditionColor(int condition) =>
|
|
condition >= 70 ? Palette.Ok : condition >= 40 ? Palette.Amber : Palette.Danger;
|
|
}
|