using System.Globalization; using RailEnterprise.Core.Common; using RailEnterprise.Protocol.Client; using static RailEnterprise.Client.App.Localization; namespace RailEnterprise.Client.Panels; /// /// Formati numerici dell'interfaccia (immagine 05): separatore delle migliaia con il punto, /// decimali con la virgola, prezzi delle azioni in lire con i centesimi. /// public static class Fmt { /// Numero con i decimali indicati e la virgola («12,4»). public static string Number(double value, int decimals = 1) { string text = value.ToString(decimals <= 0 ? "0" : "0." + new string('0', decimals), CultureInfo.InvariantCulture); int dot = text.IndexOf('.'); string integer = dot < 0 ? text : text[..dot]; bool negative = integer.StartsWith('-'); if (negative) integer = integer[1..]; string grouped = ulong.TryParse(integer, NumberStyles.None, CultureInfo.InvariantCulture, out ulong n) ? Money.GroupThousands(n) : integer; return (negative ? "−" : "") + grouped + (dot < 0 ? "" : "," + text[(dot + 1)..]); } /// Quota (0–1) in percentuale («12,4%»). public static string Pct(double fraction, int decimals = 1) => Number(fraction * 100, decimals) + "%"; /// Variazione con il segno («+1,2%», «−0,8%»). public static string Change(double fraction) => (fraction > 0 ? "+" : "") + Pct(fraction); /// Prezzo per azione dai centesimi frazionari («₤ 0,25»). public static string Price(double cents) => Money.FromCentsRounded(cents).ToString("it2"); /// Prezzo per azione con i decimali che servono a leggere le variazioni sotto il centesimo («₤ 0,2534»). public static string PricePrecise(double cents) => cents >= 10_000 ? Price(cents) : "₤ " + Number(cents / 100, cents >= 100 ? 2 : 4); /// Numero di azioni con il punto delle migliaia. public static string Shares(long shares) => shares < 0 ? "−" + Money.GroupThousands((ulong)(-shares)) : Money.GroupThousands((ulong)shares); /// Giorno corrente della partita (dal resoconto giornaliero o dal tick). public static long Today(ClientWorld w) => w.LastDigest?.Day ?? (w.Calendar is { } c ? w.Tick / c.TicksPerDay : 0); /// Data di calendario di un giorno della partita. public static string Date(ClientWorld w, long day) => w.Calendar is { } c ? c.DateOfDay(Math.Max(0, day)).ToString() : $"{T("giorno")} {day}"; /// Nome di una compagnia (o «—» se sconosciuta). public static string Company(ClientWorld w, int id) => w.Companies.TryGetValue(id, out var c) ? c.Name : id >= 0 ? $"#{id}" : "—"; /// Nome del livello della compagnia nella lingua dell'interfaccia. public static string Tier(ClientWorld w, int tier) { var tiers = w.Data.Tiers.Tiers; var def = tiers[Math.Clamp(tier, 1, tiers.Count) - 1]; return Godot.TranslationServer.GetLocale().StartsWith("en") ? def.NameEn : def.NameIt; } /// /// Livello che sblocca una funzione, se la compagnia del giocatore non ce l'ha ancora (solo per /// il suggerimento: è il server a decidere). /// public static int? UnlockTier(ClientWorld w, Func unlock) { int tier = w.Companies.TryGetValue(w.Summary?.CompanyId ?? 0, out var me) ? me.Tier : 1; var tiers = w.Data.Tiers.Tiers; if (unlock(tiers[Math.Clamp(tier, 1, tiers.Count) - 1].Unlocks)) return null; return tiers.FirstOrDefault(t => unlock(t.Unlocks))?.Level; } /// Carattere di una compagnia IA (id di ai.json); vuoto per il giocatore. public static string Personality(ClientWorld w, string id) => id switch { "" => "", "expansive" => T("Espansiva"), "prudent" => T("Prudente"), "predatory" => T("Predatoria"), _ => w.Data.Ai.Personalities.FirstOrDefault(p => p.Id == id)?.NameIt ?? id, }; /// Colore di una compagnia dalla stringa esadecimale, con l'ambra in mancanza. public static Godot.Color CompanyColor(ClientWorld w, int id) => w.Companies.TryGetValue(id, out var c) && Godot.Color.HtmlIsValid(c.Color) ? new Godot.Color(c.Color) : App.Palette.Amber; }