using ViaFerrea.Core.Commands; using ViaFerrea.Core.Common; using ViaFerrea.Core.Economy; using ViaFerrea.Core.Rail; namespace ViaFerrea.Protocol; /// /// Codifica del payload dei comandi (Command.type = ). I campi seguono /// l'ordine dei parametri dei record di Core; i campi futuri si aggiungono in coda dopo il byte /// di estensione. Issuer non viaggia: lo assegna il server in base alla sessione. /// public static class CommandCodec { public static void Write(ref SpanWriter w, GameCommand command) { switch (command) { case BuildTrackCommand c: w.Count(c.Polyline.Count); foreach (var p in c.Polyline) w.Vec2(p); w.F64(c.HeightOffsetM); w.Bool(c.AllowBridges); w.Bool(c.AllowTunnels); w.String(c.TrackClass); w.Bool(c.DoubleTrack); w.Bool(c.Electrified); w.I64(c.ExpectedCostCents); break; case RenewTrackCommand c: w.I32(c.Edge.Value); break; case UpgradeTrackCommand c: w.I32(c.Edge.Value); w.String(c.TrackClass); w.Bool(c.DoubleTrack); w.Bool(c.Electrified); break; case DemolishCommand c: w.U8((byte)c.Target); w.I32(c.Id); break; case BuildStationCommand c: w.Vec2(c.Position); w.I32(c.Platforms); OptionalString(ref w, c.Name); break; case BuildDepotCommand c: w.Vec2(c.Position); w.String(c.Size); OptionalString(ref w, c.Name); break; case PlaceSignalCommand c: w.I32(c.Edge.Value); w.F64(c.PositionM); break; case BuyVehicleCommand c: w.String(c.VehicleType); w.I32(c.Depot.Value); w.I32(c.Count); break; case SellVehicleCommand c: w.I32(c.Vehicle.Value); break; case ComposeTrainCommand c: w.I32(c.Depot.Value); w.Count(c.Vehicles.Count); foreach (var v in c.Vehicles) w.I32(v.Value); OptionalString(ref w, c.Name); break; case CreateLineCommand c: w.String(c.Name); w.String(c.Color); Stops(ref w, c.Stops); break; case EditLineCommand c: w.I32(c.Line.Value); Stops(ref w, c.Stops); OptionalString(ref w, c.Name); break; case AssignTrainCommand c: w.I32(c.Train.Value); w.I32(c.Line.Value); break; case SendToDepotCommand c: w.I32(c.Train.Value); w.I32(c.Depot.Value); break; case SetReserveCommand c: w.I32(c.Train.Value); w.Bool(c.Reserve); break; case SetMaintenancePolicyCommand c: w.U8((byte)c.Scope); w.I32(c.Target); w.String(c.Policy); break; case ScheduleServiceCommand c: w.I32(c.Train.Value); w.String(c.Intervention); break; case OrderPartsCommand c: w.I32(c.Depot.Value); w.I32(c.Quantity); break; case TakeLoanCommand c: w.Money(c.Amount); break; case RepayLoanCommand c: w.I32(c.Loan.Value); w.Money(c.Amount); break; case AcceptAgreementCommand c: w.I32(c.Agreement.Value); break; case RejectAgreementCommand c: w.I32(c.Agreement.Value); break; case BuyGoodsCommand c: w.I32(c.Station.Value); w.String(c.Good); w.I32(c.Units); w.Money(c.MaxUnitPrice); break; case SellGoodsCommand c: w.I32(c.Station.Value); w.String(c.Good); w.I32(c.Units); w.Money(c.MinUnitPrice); break; case BuyIndustryCommand c: w.I32(c.Industry.Value); w.Money(c.Offer); break; case SetIndustryPolicyCommand c: w.I32(c.Industry.Value); w.U8((byte)c.Budget); break; case OverhaulIndustryCommand c: w.I32(c.Industry.Value); break; case UpgradeIndustryCommand c: w.I32(c.Industry.Value); break; case IpoCommand c: w.F64(c.FloatShare); break; case BuySharesCommand c: w.I32(c.Company.Value); w.I64(c.Shares); w.Money(c.MaxPrice); break; case SellSharesCommand c: w.I32(c.Company.Value); w.I64(c.Shares); w.Money(c.MinPrice); break; case PayDividendCommand c: w.Money(c.PerShare); break; case LaunchBidCommand c: w.I32(c.Target.Value); w.Money(c.PricePerShare); w.F64(c.TargetShare); break; case RespondBidCommand c: w.I32(c.Bid.Value); w.Bool(c.Accept); break; case BuyConcessionCommand c: w.I32(c.Region.Value); break; case AcceptContractCommand c: w.I32(c.Contract.Value); break; case FundResearchCommand c: w.String(c.Project); w.Money(c.Amount); break; case DelegateRegionCommand c: w.I32(c.Region.Value); w.String(c.Objective); w.Money(c.Budget); break; default: throw new ProtocolException($"comando senza codifica: {command.Type}"); } w.Flags(0); } public static GameCommand Read(CommandType type, ref SpanReader r) { GameCommand c = type switch { CommandType.BuildTrack => ReadBuildTrack(ref r), CommandType.RenewTrack => new RenewTrackCommand(new EdgeId(r.I32())), CommandType.UpgradeTrack => new UpgradeTrackCommand(new EdgeId(r.I32()), r.String(), r.Bool(), r.Bool()), CommandType.Demolish => new DemolishCommand(Enum(r.U8(), 3), r.I32()), CommandType.BuildStation => new BuildStationCommand(r.Vec2(), r.I32(), OptionalString(ref r)), CommandType.BuildDepot => new BuildDepotCommand(r.Vec2(), r.String(), OptionalString(ref r)), CommandType.PlaceSignal => new PlaceSignalCommand(new EdgeId(r.I32()), r.F64()), CommandType.BuyVehicle => new BuyVehicleCommand(r.String(), new DepotId(r.I32()), r.I32()), CommandType.SellVehicle => new SellVehicleCommand(new VehicleId(r.I32())), CommandType.ComposeTrain => ReadCompose(ref r), CommandType.CreateLine => new CreateLineCommand(r.String(), r.String(), Stops(ref r)), CommandType.EditLine => new EditLineCommand(new LineId(r.I32()), Stops(ref r), OptionalString(ref r)), CommandType.AssignTrain => new AssignTrainCommand(new TrainId(r.I32()), new LineId(r.I32())), CommandType.SendToDepot => new SendToDepotCommand(new TrainId(r.I32()), new DepotId(r.I32())), CommandType.SetReserve => new SetReserveCommand(new TrainId(r.I32()), r.Bool()), CommandType.SetMaintenancePolicy => new SetMaintenancePolicyCommand(Enum(r.U8(), 2), r.I32(), r.String()), CommandType.ScheduleService => new ScheduleServiceCommand(new TrainId(r.I32()), r.String()), CommandType.OrderParts => new OrderPartsCommand(new DepotId(r.I32()), r.I32()), CommandType.TakeLoan => new TakeLoanCommand(r.Money()), CommandType.RepayLoan => new RepayLoanCommand(new LoanId(r.I32()), r.Money()), CommandType.AcceptAgreement => new AcceptAgreementCommand(new AgreementId(r.I32())), CommandType.RejectAgreement => new RejectAgreementCommand(new AgreementId(r.I32())), CommandType.BuyGoods => new BuyGoodsCommand(new StationId(r.I32()), r.String(), r.I32(), r.Money()), CommandType.SellGoods => new SellGoodsCommand(new StationId(r.I32()), r.String(), r.I32(), r.Money()), CommandType.BuyIndustry => new BuyIndustryCommand(new IndustryId(r.I32()), r.Money()), CommandType.SetIndustryPolicy => new SetIndustryPolicyCommand(new IndustryId(r.I32()), Enum(r.U8(), 2)), CommandType.OverhaulIndustry => new OverhaulIndustryCommand(new IndustryId(r.I32())), CommandType.UpgradeIndustry => new UpgradeIndustryCommand(new IndustryId(r.I32())), CommandType.Ipo => new IpoCommand(r.F64()), CommandType.BuyShares => new BuySharesCommand(new CompanyId(r.I32()), r.I64(), r.Money()), CommandType.SellShares => new SellSharesCommand(new CompanyId(r.I32()), r.I64(), r.Money()), CommandType.PayDividend => new PayDividendCommand(r.Money()), CommandType.LaunchBid => new LaunchBidCommand(new CompanyId(r.I32()), r.Money(), r.F64()), CommandType.RespondBid => new RespondBidCommand(new BidId(r.I32()), r.Bool()), CommandType.BuyConcession => new BuyConcessionCommand(new RegionId(r.I32())), CommandType.AcceptContract => new AcceptContractCommand(new ContractId(r.I32())), CommandType.FundResearch => new FundResearchCommand(r.String(), r.Money()), CommandType.DelegateRegion => new DelegateRegionCommand(new RegionId(r.I32()), r.String(), r.Money()), _ => throw new ProtocolException($"tipo di comando sconosciuto: {(ushort)type}"), }; r.Flags(); return c; } private static BuildTrackCommand ReadBuildTrack(ref SpanReader r) { int n = r.Count(); var polyline = new Vec2[n]; for (int i = 0; i < n; i++) polyline[i] = r.Vec2(); return new BuildTrackCommand(polyline, r.F64(), r.Bool(), r.Bool(), r.String(), r.Bool(), r.Bool(), r.I64()); } private static ComposeTrainCommand ReadCompose(ref SpanReader r) { var depot = new DepotId(r.I32()); int n = r.Count(); var vehicles = new VehicleId[n]; for (int i = 0; i < n; i++) vehicles[i] = new VehicleId(r.I32()); return new ComposeTrainCommand(depot, vehicles, OptionalString(ref r)); } private static void Stops(ref SpanWriter w, IReadOnlyList stops) { w.Count(stops.Count); foreach (var s in stops) { w.I32(s.Station.Value); w.Bool(s.Load); w.Bool(s.Unload); w.Bool(s.WaitFull); w.I32(s.MaxWaitDays); } } private static LineStop[] Stops(ref SpanReader r) { int n = r.Count(); var stops = new LineStop[n]; for (int i = 0; i < n; i++) stops[i] = new LineStop(new StationId(r.I32()), r.Bool(), r.Bool(), r.Bool(), r.I32()); return stops; } private static void OptionalString(ref SpanWriter w, string? s) { w.Bool(s is not null); if (s is not null) w.String(s); } private static string? OptionalString(ref SpanReader r) => r.Bool() ? r.String() : null; private static T Enum(byte value, byte max) where T : struct, System.Enum { if (value > max) throw new ProtocolException($"{typeof(T).Name} non valido: {value}"); return (T)(object)value; } }