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>
196 lines
8.2 KiB
C#
196 lines
8.2 KiB
C#
using Godot;
|
|
using RailEnterprise.Client.App;
|
|
using RailEnterprise.Client.Net;
|
|
using RailEnterprise.Core.Commands;
|
|
using RailEnterprise.Core.Common;
|
|
using RailEnterprise.Core.Data;
|
|
using RailEnterprise.Core.Rail;
|
|
using RailEnterprise.Protocol.Messages;
|
|
using static RailEnterprise.Client.App.Localization;
|
|
|
|
namespace RailEnterprise.Client.Panels;
|
|
|
|
/// <summary>
|
|
/// Treni: acquisto dei veicoli in un deposito, composizione, assegnazione alle linee, rientro.
|
|
/// Ogni azione è un comando; la lista si aggiorna con i delta del server.
|
|
/// </summary>
|
|
public partial class TrainsPanel : VBoxContainer
|
|
{
|
|
private readonly GameSession _session;
|
|
private readonly OptionButton _depot = new();
|
|
private readonly OptionButton _vehicle = new();
|
|
private readonly SpinBox _count = new() { MinValue = 1, MaxValue = 12, Value = 1 };
|
|
private readonly ItemList _inDepot = new() { SelectMode = ItemList.SelectModeEnum.Multi, CustomMinimumSize = new Vector2(0, 120) };
|
|
private readonly LineEdit _name = new() { PlaceholderText = T("Nome del treno") };
|
|
private readonly ItemList _trains = new() { CustomMinimumSize = new Vector2(0, 140) };
|
|
private readonly OptionButton _line = new();
|
|
private readonly Label _status = UiTheme.Text("", 13, Palette.TextSecondary);
|
|
private readonly List<int> _depotIds = [];
|
|
private readonly List<VehicleDef> _catalog = [];
|
|
private readonly List<int> _vehicleIds = [];
|
|
private readonly List<int> _trainIds = [];
|
|
private readonly List<int> _lineIds = [];
|
|
private int _version = -1;
|
|
|
|
public event Action<int>? TrainSelected;
|
|
|
|
public TrainsPanel(GameSession session)
|
|
{
|
|
_session = session;
|
|
AddThemeConstantOverride("separation", 8);
|
|
AddChild(UiTheme.TitleLabel(T("Treni"), 34));
|
|
AddChild(PanelKit.Pair(T("Deposito"), new Label()));
|
|
AddChild(_depot);
|
|
var buy = PanelKit.Row();
|
|
_vehicle.SizeFlagsHorizontal = SizeFlags.ExpandFill;
|
|
buy.AddChild(_vehicle);
|
|
buy.AddChild(_count);
|
|
AddChild(buy);
|
|
var buyButton = UiTheme.Primary(T("Compra veicoli"));
|
|
buyButton.Pressed += Buy;
|
|
AddChild(buyButton);
|
|
AddChild(UiTheme.Text(T("Veicoli liberi nel deposito (selezione multipla, in ordine)"), 13, Palette.TextSecondary));
|
|
AddChild(_inDepot);
|
|
var compose = PanelKit.Row();
|
|
_name.SizeFlagsHorizontal = SizeFlags.ExpandFill;
|
|
compose.AddChild(_name);
|
|
var composeButton = new Button { Text = T("Componi") };
|
|
composeButton.Pressed += Compose;
|
|
compose.AddChild(composeButton);
|
|
AddChild(compose);
|
|
AddChild(PanelKit.Separator());
|
|
AddChild(UiTheme.Text(T("Treni della compagnia"), 14, bold: true));
|
|
AddChild(_trains);
|
|
var assign = PanelKit.Row();
|
|
_line.SizeFlagsHorizontal = SizeFlags.ExpandFill;
|
|
assign.AddChild(_line);
|
|
var assignButton = new Button { Text = T("Assegna") };
|
|
assignButton.Pressed += Assign;
|
|
assign.AddChild(assignButton);
|
|
AddChild(assign);
|
|
var home = new Button { Text = T("Manda al deposito") };
|
|
home.Pressed += SendHome;
|
|
AddChild(home);
|
|
_status.AutowrapMode = TextServer.AutowrapMode.WordSmart;
|
|
AddChild(_status);
|
|
_depot.ItemSelected += _ => Refresh(force: true);
|
|
_trains.ItemSelected += i => TrainSelected?.Invoke(_trainIds[(int)i]);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_session.World.EntityVersion != _version) Refresh(force: false);
|
|
}
|
|
|
|
private int Company => _session.World.Summary?.CompanyId ?? 0;
|
|
|
|
private void Refresh(bool force)
|
|
{
|
|
var w = _session.World;
|
|
_version = w.EntityVersion;
|
|
int selectedDepot = _depot.Selected >= 0 && _depot.Selected < _depotIds.Count ? _depotIds[_depot.Selected] : -1;
|
|
_depotIds.Clear();
|
|
_depot.Clear();
|
|
foreach (var d in w.Depots.Values.Where(d => d.Owner == Company).OrderBy(d => d.Id))
|
|
{
|
|
_depotIds.Add(d.Id);
|
|
_depot.AddItem(d.Name);
|
|
}
|
|
int di = _depotIds.IndexOf(selectedDepot);
|
|
_depot.Selected = di >= 0 ? di : _depotIds.Count > 0 ? 0 : -1;
|
|
|
|
if (_catalog.Count == 0 || force)
|
|
{
|
|
int keep = _vehicle.Selected;
|
|
_catalog.Clear();
|
|
_vehicle.Clear();
|
|
foreach (var def in w.Data.Vehicles.Where(v => v.EraFrom <= w.Year && (v.EraTo is null || v.EraTo >= w.Year)))
|
|
{
|
|
_catalog.Add(def);
|
|
_vehicle.AddItem($"{def.NameIt} — {new Money(def.Price)}");
|
|
}
|
|
_vehicle.Selected = keep >= 0 && keep < _catalog.Count ? keep : 0;
|
|
}
|
|
|
|
int depot = _depot.Selected >= 0 ? _depotIds[_depot.Selected] : -1;
|
|
_vehicleIds.Clear();
|
|
_inDepot.Clear();
|
|
foreach (var v in w.Vehicles.Values.Where(v => v.Owner == Company && v.Depot == depot && v.Train < 0).OrderBy(v => v.Id))
|
|
{
|
|
_vehicleIds.Add(v.Id);
|
|
var def = w.Data.Vehicles.FirstOrDefault(d => d.Id == v.Type);
|
|
_inDepot.AddItem($"{def?.NameIt ?? v.Type} #{v.Id}");
|
|
}
|
|
|
|
var selectedTrain = _trains.GetSelectedItems() is { Length: > 0 } sel ? _trainIds[sel[0]] : -1;
|
|
_trainIds.Clear();
|
|
_trains.Clear();
|
|
foreach (var t in w.Trains.Values.Where(t => t.Owner == Company).OrderBy(t => t.Id))
|
|
{
|
|
_trainIds.Add(t.Id);
|
|
string line = t.Line >= 0 && w.Lines.TryGetValue(t.Line, out var l) ? l.Name : T("senza linea");
|
|
_trains.AddItem($"{t.Name} · {StateName((TrainState)t.State)} · {line}");
|
|
}
|
|
int ti = _trainIds.IndexOf(selectedTrain);
|
|
if (ti >= 0) _trains.Select(ti);
|
|
|
|
int selectedLine = _line.Selected >= 0 && _line.Selected < _lineIds.Count ? _lineIds[_line.Selected] : -1;
|
|
_lineIds.Clear();
|
|
_line.Clear();
|
|
_lineIds.Add(-1);
|
|
_line.AddItem(T("Nessuna linea"));
|
|
foreach (var l in w.Lines.Values.Where(l => l.Owner == Company).OrderBy(l => l.Id))
|
|
{
|
|
_lineIds.Add(l.Id);
|
|
_line.AddItem(l.Name);
|
|
}
|
|
int li = _lineIds.IndexOf(selectedLine);
|
|
_line.Selected = li >= 0 ? li : Math.Min(1, _lineIds.Count - 1);
|
|
}
|
|
|
|
public static string StateName(TrainState s) => s switch
|
|
{
|
|
TrainState.InDepot => T("in deposito"),
|
|
TrainState.Moving => T("in marcia"),
|
|
TrainState.Waiting => T("in attesa di via libera"),
|
|
TrainState.Loading => T("in stazione"),
|
|
TrainState.Stalled => T("fermo in salita"),
|
|
TrainState.Broken => T("guasto"),
|
|
TrainState.NoRoute => T("senza percorso"),
|
|
_ => s.ToString(),
|
|
};
|
|
|
|
private void Result(CommandResultMessage r) => _status.Text = r.Message;
|
|
|
|
private void Buy()
|
|
{
|
|
if (_depot.Selected < 0 || _vehicle.Selected < 0) { _status.Text = T("Serve un deposito"); return; }
|
|
_session.Command(new BuyVehicleCommand(_catalog[_vehicle.Selected].Id, new DepotId(_depotIds[_depot.Selected]), (int)_count.Value), Result);
|
|
}
|
|
|
|
private void Compose()
|
|
{
|
|
if (_depot.Selected < 0) return;
|
|
var selected = _inDepot.GetSelectedItems().OrderBy(i => i).Select(i => new VehicleId(_vehicleIds[i])).ToList();
|
|
if (selected.Count == 0) { _status.Text = T("Seleziona i veicoli da comporre"); return; }
|
|
string? name = string.IsNullOrWhiteSpace(_name.Text) ? null : _name.Text.Trim();
|
|
_session.Command(new ComposeTrainCommand(new DepotId(_depotIds[_depot.Selected]), selected, name), Result);
|
|
}
|
|
|
|
private int SelectedTrain => _trains.GetSelectedItems() is { Length: > 0 } sel ? _trainIds[sel[0]] : -1;
|
|
|
|
private void Assign()
|
|
{
|
|
int train = SelectedTrain;
|
|
if (train < 0 || _line.Selected < 0) { _status.Text = T("Seleziona un treno"); return; }
|
|
_session.Command(new AssignTrainCommand(new TrainId(train), new LineId(_lineIds[_line.Selected])), Result);
|
|
}
|
|
|
|
private void SendHome()
|
|
{
|
|
int train = SelectedTrain;
|
|
if (train < 0 || _depot.Selected < 0) { _status.Text = T("Seleziona un treno e un deposito"); return; }
|
|
_session.Command(new SendToDepotCommand(new TrainId(train), new DepotId(_depotIds[_depot.Selected])), Result);
|
|
}
|
|
}
|